Blame SOURCES/macros.forge

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