Blame SOURCES/macros.forge

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