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