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 | |
| parent | f23af7855d5d5d2b2126dd67db0de04e05836c12 (diff) | |
improve specvars
| -rw-r--r-- | xmake/includes/xpack/xmake.lua | 6 | ||||
| -rw-r--r-- | xmake/plugins/pack/nsis/main.lua | 32 | ||||
| -rw-r--r-- | xmake/plugins/pack/xpack.lua | 65 | ||||
| -rw-r--r-- | xmake/scripts/xpack/nsis/makensis.nsi | 24 |
4 files changed, 88 insertions, 39 deletions
diff --git a/xmake/includes/xpack/xmake.lua b/xmake/includes/xpack/xmake.lua index 6d6cb99ec..691645116 100644 --- a/xmake/includes/xpack/xmake.lua +++ b/xmake/includes/xpack/xmake.lua @@ -40,7 +40,7 @@ local apis = { "xpack.set_formats", -- set the base name of the output file "xpack.set_basename", - -- set the spec file path + -- set the spec file path, support the custom variable pattern, e.g. set_specfile("", {pattern = "%${([^\n]-)}"}) "xpack.set_specfile", -- add targets to be packaged "xpack.add_targets", @@ -54,6 +54,10 @@ local apis = { "xpack.on_installcmd", -- add custom uninstall commands script, we will also get it from target/rules "xpack.on_uninstallcmd" + }, + keyvalues = { + -- set the spec variable + "xpack.set_specvar" } } interp_add_scopeapis(apis) 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 = { diff --git a/xmake/scripts/xpack/nsis/makensis.nsi b/xmake/scripts/xpack/nsis/makensis.nsi index a8c7b207b..120f94f46 100644 --- a/xmake/scripts/xpack/nsis/makensis.nsi +++ b/xmake/scripts/xpack/nsis/makensis.nsi @@ -11,27 +11,7 @@ !include "UAC.nsh" ; the version information -!ifndef MAJOR - !define MAJOR 1 -!endif -!ifndef MINOR - !define MINOR 0 -!endif -!ifndef PATCH - !define PATCH 0 -!endif -!ifndef BUILD - !define BUILD 0 -!endif - -!define VERSION ${MAJOR}.${MINOR}.${PATCH} -!define VERSION_FULL ${VERSION}+${BUILD} - -!ifdef x64 - !define ARCH x64 -!else - !define ARCH x86 -!endif +!define VERSION_FULL ${VERSION}+${VERSION_BUILD} ;-------------------------------- @@ -101,7 +81,7 @@ ManifestDPIAware true ; Install Pages !insertmacro MUI_PAGE_WELCOME -!insertmacro MUI_PAGE_LICENSE "..\LICENSE.md" +;!insertmacro MUI_PAGE_LICENSE "..\LICENSE.md" !insertmacro MUI_PAGE_COMPONENTS !insertmacro MUI_PAGE_DIRECTORY !insertmacro MUI_PAGE_INSTFILES |
