diff options
| author | ruki <[email protected]> | 2023-11-12 00:05:21 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2023-11-12 00:05:21 +0800 |
| commit | fb648845e465bf0c376f6d0d4197c8243abdaecd (patch) | |
| tree | a177a51f56b57a3977f036b45c524943353a0315 /xmake/plugins | |
| parent | f23af7855d5d5d2b2126dd67db0de04e05836c12 (diff) | |
improve specvars
Diffstat (limited to 'xmake/plugins')
| -rw-r--r-- | xmake/plugins/pack/nsis/main.lua | 32 | ||||
| -rw-r--r-- | xmake/plugins/pack/xpack.lua | 65 |
2 files changed, 81 insertions, 16 deletions
diff --git a/xmake/plugins/pack/nsis/main.lua b/xmake/plugins/pack/nsis/main.lua index 920f447ae..5e03b1709 100644 --- a/xmake/plugins/pack/nsis/main.lua +++ b/xmake/plugins/pack/nsis/main.lua @@ -62,6 +62,21 @@ function _pack_nsis(makensis, package, opt) os.cp(specfile_template, specfile) end + -- replace variables in specfile + local specvars = package:specvars() + local pattern = package:extraconf("specfile", "pattern") or "%${([^\n]-)}" + io.gsub(specfile, "(" .. pattern .. ")", function(_, variable) + variable = variable:trim() + local value = specvars[variable] + if value ~= nil then + dprint(" > replace %s -> %s", variable, value) + end + if type(value) == "table" then + dprint("invalid variable value", value) + end + return value + end) + -- get version local version, version_build = package:version() assert(version, "xpack(%s): version not found!", package:name()) @@ -71,22 +86,7 @@ function _pack_nsis(makensis, package, opt) print("xpack(%s)", package:name()) print(" description: %s", package:description()) print(" installcmd: ", package:script("installcmd")) - - local argv = {} - if version:major() then - table.insert(argv, "/DMAJOR=" .. version:major()) - end - if version:minor() then - table.insert(argv, "/DMINOR=" .. version:minor()) - end - if version:patch() then - table.insert(argv, "/DPATCH=" .. version:patch()) - end - if version_build then - table.insert(argv, "/DBUILD=" .. version_build) - end - table.insert(argv, specfile) - os.vrunv(makensis, argv) + os.vrunv(makensis, {specfile}) end function main(package) diff --git a/xmake/plugins/pack/xpack.lua b/xmake/plugins/pack/xpack.lua index cbc2b9045..8c4537f2b 100644 --- a/xmake/plugins/pack/xpack.lua +++ b/xmake/plugins/pack/xpack.lua @@ -24,6 +24,7 @@ import("core.base.option") import("core.base.hashset") import("core.project.config") import("core.project.project") +import("lib.detect.find_tool") -- define module local xpack = xpack or object {_init = {"_name", "_info"}} @@ -209,6 +210,70 @@ function xpack:basename() return self:get("basename") or self:name() end +-- get the spec variables +function xpack:specvars() + local specvars = self._specvars + if specvars == nil then + specvars = { + ARCH = self:arch() + , PLAT = self:plat() + , HOST = os.host() + , MODE = config.get("mode") or "release" + } + + -- get version + local version, version_build = self:version() + if version then + specvars.VERSION = version + try {function () + local v = semver.new(version) + if v then + specvars.VERSION_MAJOR = v:major() + specvars.VERSION_MINOR = v:minor() + specvars.VERSION_ALTER = v:patch() + end + end} + if version_build then + specvars.VERSION_BUILD = version_build + end + end + + -- get git information + local cmds = + { + GIT_TAG = {"describe", "--tags"}, + GIT_TAG_LONG = {"describe", "--tags", "--long"}, + GIT_BRANCH = {"rev-parse", "--abbrev-ref", "HEAD"}, + GIT_COMMIT = {"rev-parse", "--short", "HEAD"}, + GIT_COMMIT_LONG = {"rev-parse", "HEAD"}, + GIT_COMMIT_DATE = {"log", "-1", "--date=format:%Y%m%d%H%M%S", "--format=%ad"} + } + for name, argv in pairs(cmds) do + specvars[name] = function () + local result + local git = find_tool("git") + if git then + result = try {function () + return os.iorunv(git.program, argv) + end} + end + if not result then + result = "none" + end + return result:trim() + end + end + + -- get user variables + local vars = self:get("specvar") + if vars then + table.join2(specvars, vars) + end + self._specvars = specvars + end + return specvars +end + -- get the specfile path function xpack:specfile() local extensions = { |
