7c4cd33
# Map forge information to rpm metadata. This macro will compute default spec
7c4cd33
# variable values.
7c4cd33
#
7c4cd33
# The following spec variables SHOULD be set before calling the macro:
7c4cd33
#
7c4cd33
#   forgeurl  the project url on the forge, strongly recommended;
7c4cd33
#             alternatively, use -u <url>
7c4cd33
#   Version   if applicable, set it with Version: <version>
7c4cd33
#   tag       if applicable
7c4cd33
#   commit    if applicable
7c4cd33
#
7c4cd33
# The macro will attempt to compute and set the following variables if they are
7c4cd33
# not already set by the packager:
7c4cd33
#
7c4cd33
#   forgesource    an URL that can be used as SourceX: value
7c4cd33
#   forgesetupargs the correct arguments to pass to %setup for this source
7c4cd33
#                  used by %forgesetup and %forgeautosetup
7c4cd33
#   archivename    the source archive filename, without extentions
7c4cd33
#   archiveext     the source archive filename extensions, without leading dot
7c4cd33
#   archiveurl     the url that can be used to download the source archive,
7c4cd33
#                  without renaming
7c4cd33
#   scm            the scm type, when packaging code snapshots: commits or tags
7c4cd33
#
7c4cd33
# If the macro is unable to parse your forgeurl value set at least archivename
7c4cd33
# and archiveurl before calling it.
7c4cd33
#
7c4cd33
# Most of the computed variables are both overridable and optional. However,
7c4cd33
# the macro WILL REDEFINE %{dist} when packaging a snapshot (commit or tag).
7c4cd33
# The previous %{dist} value will be lost. Don’t call the macro if you don’t
7c4cd33
# wish %{dist} to be changed.
7c4cd33
#
7c4cd33
# Optional parameters:
7c4cd33
#   -u <url>  Ignore forgeurl even if it exists and use <url> instead. Note
7c4cd33
#             that the macro will still end up setting <url> as the forgeurl
7c4cd33
#             spec variable if it manages to parse it.
7c4cd33
#   -s  Silently ignore problems in forgeurl, use it if it can be parsed,
7c4cd33
#       ignore it otherwise.
7c4cd33
#   -v  Be verbose and print every spec variable the macro sets.
7c4cd33
#   -i  Print some info about the state of spec variables the macro may use or
7c4cd33
#       set at the end of the processing.
7c4cd33
%forgemeta(u:svi) %{lua:
7c4cd33
local forgeurl    = rpm.expand("%{?-u*}")
7c4cd33
if (forgeurl == "") then
7c4cd33
  forgeurl        = rpm.expand("%{?forgeurl}")
7c4cd33
end
7c4cd33
local silent      = false
7c4cd33
local verbose     = false
7c4cd33
local informative = false
7c4cd33
if (rpm.expand("%{?-s}") ~= "") then
7c4cd33
  silent          = true
7c4cd33
end
7c4cd33
if (rpm.expand("%{?-v}") ~= "") then
7c4cd33
  verbose         = true
7c4cd33
end
7c4cd33
if (rpm.expand("%{?-i}") ~= "") then
7c4cd33
  informative     = true
7c4cd33
end
7c4cd33
local tag         = rpm.expand("%{?tag}")
7c4cd33
local commit      = rpm.expand("%{?commit}")
7c4cd33
-- Be explicit about the spec variables we’re setting
7c4cd33
local function explicitset(rpmvariable,value)
7c4cd33
  rpm.define(rpmvariable .. " " .. value)
7c4cd33
  if verbose then
7c4cd33
    rpm.expand("%{echo:Setting %%{" .. rpmvariable .. "} = " .. value .. "\\n}")
7c4cd33
  end
7c4cd33
end
7c4cd33
-- Never ever stomp on a spec variable the packager already set
7c4cd33
local function safeset(rpmvariable,value)
7c4cd33
  if (rpm.expand("%{?" .. rpmvariable .. "}") == "") then
7c4cd33
    explicitset(rpmvariable,value)
7c4cd33
  end
7c4cd33
end
7c4cd33
-- Set spec variable values for each known software publishing service
7c4cd33
if (forgeurl ~= "") then
7c4cd33
  local forge          = string.match(forgeurl, "^[^:]+://([^/]+)/")
7c4cd33
  if (string.match(forge, "^gitlab[%.-]") or string.match(forge, "[%.-]gitlab[%.]")) then
7c4cd33
    forgeurl = string.match(forgeurl, "https://[^/]+/[^/]+/[^/#?]+")
7c4cd33
    if (forgeurl == "") then
7c4cd33
      if not silent then
7c4cd33
        rpm.expand("%{error:Gitlab URLs must match https://(…[-.])gitlab[-.]…/owner/repo !\\n}")
7c4cd33
      end
7c4cd33
    else
7c4cd33
      explicitset("forgeurl",   forgeurl)
7c4cd33
      if (commit == "") then
7c4cd33
        rpm.expand("%{error:All Gitlab URLs require commit value knowledge: you need to define %{commit}!\\nPlease vote on https://gitlab.com/gitlab-org/gitlab-ce/issues/38830\\n}")
7c4cd33
      end
7c4cd33
      safeset("archiveext",     "tar.bz2")
7c4cd33
      safeset("forgesetupargs", "-n %{archivename}")
7c4cd33
      if (commit ~= "") or (tag ~= "") then
7c4cd33
        safeset("scm", "git")
7c4cd33
      end
7c4cd33
      local owner   = string.match(forgeurl, "^[^:]+://[^/]+/([^/]+)")
7c4cd33
      local repo    = string.match(forgeurl, "^[^:]+://[^/]+/[^/]+/([^/]+)")
7c4cd33
      local version = rpm.expand("%{?version}")
7c4cd33
      if (version ~= "") and (version ~= "0") and (tag == "") then
7c4cd33
        -- GitLab does not have strong versionning semantics
7c4cd33
        -- Some projects use "version" as release tag, others "v" + "version"
7c4cd33
        -- Tag value needs to be explicitly declared before calling the macro
7c4cd33
        -- in the second case
7c4cd33
        tag = version
7c4cd33
        safeset("tag", tag)
7c4cd33
      end
7c4cd33
      if (tag ~= "") then
7c4cd33
        safeset("archivename", repo .. "-%{tag}-%{commit}")
7c4cd33
        safeset("archiveurl",  "%{forgeurl}/repository/%{tag}/archive.%{archiveext}")
7c4cd33
      else
7c4cd33
        safeset("archivename", repo .. "-%{commit}")
7c4cd33
        safeset("archiveurl",  "%{forgeurl}/repository/%{commit}/archive.%{archiveext}")
7c4cd33
      end
7c4cd33
    end
7c4cd33
  end
7c4cd33
  if (string.match(forge, "^github[%.-]") or string.match(forge, "[%.-]github[%.]")) then
7c4cd33
    forgeurl = string.match(forgeurl, "https://[^/]+/[^/]+/[^/#?]+")
7c4cd33
    if (forgeurl == "") then
7c4cd33
      if not silent then
7c4cd33
        rpm.expand("%{error:GitHub URLs must match https://(…[-.])github[-.]…/owner/repo !\\n}")
7c4cd33
      end
7c4cd33
    else
7c4cd33
      explicitset("forgeurl",   forgeurl)
7c4cd33
      safeset("archiveext",     "tar.gz")
7c4cd33
      safeset("forgesetupargs", "-n %{archivename}")
7c4cd33
      if (commit ~= "") or (tag ~= "") then
7c4cd33
        safeset("scm", "git")
7c4cd33
      end
7c4cd33
      local owner = string.match(forgeurl, "^[^:]+://[^/]+/([^/]+)")
7c4cd33
      local repo  = string.match(forgeurl, "^[^:]+://[^/]+/[^/]+/([^/]+)")
7c4cd33
      if (tag ~= "") then
7c4cd33
        safeset("archivename",   repo .. "-%{tag}")
7c4cd33
        safeset("archiveurl",    "%{forgeurl}/archive/%{tag}.%{archiveext}")
7c4cd33
      else
7c4cd33
        if (commit ~= "") then
7c4cd33
          safeset("archivename", repo .. "-%{commit}")
7c4cd33
          safeset("archiveurl",  "%{forgeurl}/archive/%{commit}/" .. repo .. "-%{commit}.%{archiveext}")
7c4cd33
        else
7c4cd33
          safeset("archivename", repo .. "-%{version}")
7c4cd33
          safeset("archiveurl",   "%{forgeurl}/archive/v%{version}.%{archiveext}")
7c4cd33
        end
7c4cd33
      end
7c4cd33
    end
7c4cd33
  end
7c4cd33
  if (forge == "code.googlesource.com") then
7c4cd33
    forgeurl = string.match(forgeurl, "https://code.googlesource.com/[^#?]*[^/#?]+")
7c4cd33
    if (forgeurl == "") then
7c4cd33
      if not silent then
7c4cd33
        rpm.expand("%{error:Googlesource URLs must match https://code.googlesource.com/…/repo !\\n}")
7c4cd33
      end
7c4cd33
    else
7c4cd33
      explicitset("forgeurl",   forgeurl)
7c4cd33
      safeset("archiveext",     "tar.gz")
7c4cd33
      safeset("forgesetupargs", "-c")
7c4cd33
      if (commit ~= "") or (tag ~= "") then
7c4cd33
        safeset("scm", "git")
7c4cd33
      end
7c4cd33
      local repo = string.match(forgeurl, "^[^:]+://.+/([^/?#]+)")
7c4cd33
      if (tag ~= "") then
7c4cd33
        safeset("archivename",   repo .. "-%{tag}")
7c4cd33
        safeset("archiveurl",    "%{forgeurl}/+archive/%{tag}.%{archiveext}")
7c4cd33
      else
7c4cd33
        if (commit ~= "") then
7c4cd33
          safeset("archivename", repo .. "-%{commit}")
7c4cd33
          safeset("archiveurl",  "%{forgeurl}/+archive/%{commit}.%{archiveext}")
7c4cd33
        else
7c4cd33
          safeset("archivename", repo .. "-v%{version}")
7c4cd33
          safeset("archiveurl",  "%{forgeurl}/+archive/v%{version}.%{archiveext}")
7c4cd33
        end
7c4cd33
      end
7c4cd33
    end
7c4cd33
  end
7c4cd33
  -- Final tests to check forgeurl was successfuly parsed
7c4cd33
  if not silent then
7c4cd33
    if (forge == "pagure.io") then
7c4cd33
      rpm.expand("%{warn:https://pagure.io/pagure/issue/861 needs to be resolved before the “pagure.io”\\nsoftware publishing service can be supported.\\n}")
7c4cd33
    end
7c4cd33
    if (rpm.expand("%{?archivename}") == "") or (rpm.expand("%{?archiveurl}") == "") then
7c4cd33
      rpm.expand("%{error:Automation for the “" .. forge .. "”\\nsoftware publishing service is not implemented yet.\\nPlease extend the %%forgemeta macro!\\n}")
7c4cd33
    end
7c4cd33
  end
7c4cd33
end
7c4cd33
-- Set defaults if forgeurl is missing or does not parse
7c4cd33
local archivename = rpm.expand("%{?archivename}")
7c4cd33
safeset("archiveext",       "tar.gz")
7c4cd33
if (archivename ~= "") then
7c4cd33
  safeset("forgesetupargs", "-n %{archivename}")
7c4cd33
end
7c4cd33
if (commit ~= "") or (tag ~= "") then
7c4cd33
  safeset("scm",            "git")
7c4cd33
end
7c4cd33
-- Source URL processing (computing the forgesource spec variable)
7c4cd33
local archiveurl  = rpm.expand("%{?archiveurl}")
7c4cd33
local archiveext  = rpm.expand("%{?archiveext}")
7c4cd33
if (archivename ~= "") and (archiveurl ~= "") then
7c4cd33
  if (string.match(archiveurl, "/([^/]+)$") == archivename .. "." .. archiveext) then
7c4cd33
    safeset("forgesource", "%{archiveurl}")
7c4cd33
  else
7c4cd33
    safeset("forgesource", "%{?archiveurl}#/%{?archivename}.%{archiveext}")
7c4cd33
  end
7c4cd33
end
7c4cd33
-- dist processing (computing the correct pefix for snapshots)
7c4cd33
local distprefix = rpm.expand("%{?tag}")
7c4cd33
local version    = rpm.expand("%{?version}")
7c4cd33
if (distprefix == version) or (distprefix == "v" .. version) then
7c4cd33
  distprefix = ""
7c4cd33
end
7c4cd33
if (distprefix == "") then
7c4cd33
  distprefix = string.sub(rpm.expand("%{?commit}"), 1, 7)
7c4cd33
end
7c4cd33
if (distprefix ~= "") then
7c4cd33
  local dist = ".%([ -r %{_sourcedir}/%{archivename}.%{archiveext} ] && date +%Y%m%d -u -r %{_sourcedir}/%{archivename}.%{archiveext})%{scm}" .. string.gsub(distprefix, "-",".") .. rpm.expand("%{?dist}")
7c4cd33
  explicitset("dist", dist)
7c4cd33
end
7c4cd33
-- Final spec variable summary if the macro was called with -i
7c4cd33
if informative then
7c4cd33
  rpm.expand("%{echo:Forge-specific packaging variables\\n}")
7c4cd33
  rpm.expand("%{echo:  forgeurl:        %{?forgeurl}\\n}")
7c4cd33
  rpm.expand("%{echo:  forgesource:     %{?forgesource}\\n}")
7c4cd33
  rpm.expand("%{echo:  forgesetupargs:  %{?forgesetupargs}\\n}")
7c4cd33
  rpm.expand("%{echo:Generic variables\\n}")
7c4cd33
  rpm.expand("%{echo:  archivename:     %{?archivename}\\n}")
7c4cd33
  rpm.expand("%{echo:  archiveext:      %{?archiveext}\\n}")
7c4cd33
  rpm.expand("%{echo:  archiveurl:      %{?archiveurl}\\n}")
7c4cd33
  rpm.expand("%{echo:  scm:             %{?scm}\\n}")
7c4cd33
  rpm.expand("%{echo:  tag:             %{?tag}\\n}")
7c4cd33
  rpm.expand("%{echo:  commit:          %{?commit}\\n}")
7c4cd33
  rpm.expand("%{echo:  dist:            %{?dist} (snapshot date is computed once %%{_sourcedir}/%%{archivename}.%%{archiveext} is available)\\n}")
7c4cd33
end
7c4cd33
}
7c4cd33
7c4cd33
# Convenience macro to relay computed arguments to %setup
7c4cd33
%forgesetup(a:b:cDn:Tq) %setup %{?forgesetupargs} %{-a} %{-b} %{-c} %{-D} %{-n} %{-T} %{-q}
7c4cd33
7c4cd33
# Convenience macro to relay computed arguments to %autosetup
7c4cd33
%forgeautosetup(a:b:cDn:TvNS:p) %autosetup %{?forgesetupargs} %{-a} %{-b} %{-c} %{-D} %{-n} %{-T} %{-v} %{-N} %{-S} %{-p}