diff options
| author | ruki <[email protected]> | 2023-11-17 21:53:04 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-11-17 21:53:04 +0800 |
| commit | 4ec6682dea22ec5a2e4b818700288e625f761d52 (patch) | |
| tree | 40e3e618ca22ae4e838319d458285698ce7528ee | |
| parent | 14f1ed5587ebe19a644f4832c059ff4ec830f6df (diff) | |
| parent | 151b4ba88313652627da6f6747ed11bca6edc86c (diff) | |
Merge pull request #4396 from xmake-io/xpack
improve installdir for xpack
| -rw-r--r-- | core/src/demo/xmake.lua | 27 | ||||
| -rw-r--r-- | core/xpack.lua | 6 | ||||
| -rw-r--r-- | tests/plugins/pack/xmake.lua | 9 | ||||
| -rw-r--r-- | xmake/plugins/pack/archive.lua | 51 | ||||
| -rw-r--r-- | xmake/plugins/pack/batchcmds.lua | 13 | ||||
| -rw-r--r-- | xmake/plugins/pack/main.lua | 21 | ||||
| -rw-r--r-- | xmake/plugins/pack/nsis/main.lua | 38 | ||||
| -rw-r--r-- | xmake/plugins/pack/xpack.lua | 27 | ||||
| -rw-r--r-- | xmake/scripts/xpack/nsis/makensis.nsi | 30 |
9 files changed, 98 insertions, 124 deletions
diff --git a/core/src/demo/xmake.lua b/core/src/demo/xmake.lua index a45c82693..f519dd88a 100644 --- a/core/src/demo/xmake.lua +++ b/core/src/demo/xmake.lua @@ -70,20 +70,21 @@ target("demo") before_installcmd(function (target, batchcmds, opt) -- we need to avoid some old files interfering with xmake's module import. + local package = opt.package if target:is_plat("windows") then - batchcmds:rmdir("actions") - batchcmds:rmdir("core") - batchcmds:rmdir("includes") - batchcmds:rmdir("languages") - batchcmds:rmdir("modules") - batchcmds:rmdir("platforms") - batchcmds:rmdir("plugins") - batchcmds:rmdir("repository") - batchcmds:rmdir("rules") - batchcmds:rmdir("templates") - batchcmds:rmdir("scripts") - batchcmds:rmdir("themes") - batchcmds:rmdir("toolchains") + batchcmds:rmdir(package:installdir("actions")) + batchcmds:rmdir(package:installdir("core")) + batchcmds:rmdir(package:installdir("includes")) + batchcmds:rmdir(package:installdir("languages")) + batchcmds:rmdir(package:installdir("modules")) + batchcmds:rmdir(package:installdir("platforms")) + batchcmds:rmdir(package:installdir("plugins")) + batchcmds:rmdir(package:installdir("repository")) + batchcmds:rmdir(package:installdir("rules")) + batchcmds:rmdir(package:installdir("templates")) + batchcmds:rmdir(package:installdir("scripts")) + batchcmds:rmdir(package:installdir("themes")) + batchcmds:rmdir(package:installdir("toolchains")) end end) diff --git a/core/xpack.lua b/core/xpack.lua index eb61f49e4..86c87fc83 100644 --- a/core/xpack.lua +++ b/core/xpack.lua @@ -1,7 +1,9 @@ xpack("xmake") - set_formats("nsis", "zip") - set_description("A cross-platform build utility based on Lua. https://xmake.io") + set_homepage("https://xmake.io") + set_description("A cross-platform build utility based on Lua.") + set_copyright("Copyright (C) 2015-present, TBOOX Open Source Group") set_licensefile("../LICENSE.md") + set_formats("nsis", "zip") add_targets("demo") set_bindir(".") set_iconfile("src/demo/xmake.ico") diff --git a/tests/plugins/pack/xmake.lua b/tests/plugins/pack/xmake.lua index 2828524f2..d10af4d54 100644 --- a/tests/plugins/pack/xmake.lua +++ b/tests/plugins/pack/xmake.lua @@ -28,13 +28,14 @@ xpack("test") set_iconfile("src/assets/xmake.ico") after_installcmd(function (package, batchcmds) - batchcmds:cp("src/assets/*.txt", "resources/", {rootdir = "src"}) - batchcmds:mkdir("stub") + batchcmds:mkdir(package:installdir("resources")) + batchcmds:cp("src/assets/*.txt", package:installdir("resources"), {rootdir = "src"}) + batchcmds:mkdir(package:installdir("stub")) end) after_uninstallcmd(function (package, batchcmds) - batchcmds:rmdir("resources") - batchcmds:rmdir("stub") + batchcmds:rmdir(package:installdir("resources")) + batchcmds:rmdir(package:installdir("stub")) end) add_nsis_installcmds("Enable Long Path", [[ diff --git a/xmake/plugins/pack/archive.lua b/xmake/plugins/pack/archive.lua index 2db7a1e3c..5a890ca85 100644 --- a/xmake/plugins/pack/archive.lua +++ b/xmake/plugins/pack/archive.lua @@ -23,63 +23,22 @@ import("core.base.option") import("utils.archive") import("batchcmds") --- get archive directory -function _get_archivedir(package) - return path.join(package:buildir(), "archive", package:format()) -end - --- run command -function _run_command(package, cmd) - local opt = cmd.opt or {} - local kind = cmd.kind - local archivedir = _get_archivedir(package) - if kind == "cp" then - local srcpath = cmd.srcpath - local dstpath = path.join(archivedir, cmd.dstpath) - os.vcp(srcpath, dstpath, opt) - elseif kind == "rm" then - local filepath = path.join(archivedir, cmd.filepath) - os.tryrm(filepath, opt) - elseif kind == "rmdir" then - local dir = path.join(archivedir, cmd.dir) - if os.isdir(dir) then - os.tryrm(dir, opt) - end - elseif kind == "mv" then - local srcpath = cmd.srcpath - local dstpath = path.join(archivedir, cmd.dstpath) - os.vmv(srcpath, dstpath, opt) - elseif kind == "cd" then - local dir = path.join(archivedir, cmd.dir) - os.cd(dir) - elseif kind == "mkdir" then - local dir = path.join(archivedir, cmd.dir) - os.mkdir(dir) - end -end - --- run commands -function _run_commands(package, cmds) - for _, cmd in ipairs(cmds) do - _run_command(package, cmd) - end -end - -- pack archive package function _pack_archive(package) -- do install - _run_commands(package, batchcmds.get_installcmds(package):cmds()) + batchcmds.get_installcmds(package):runcmds() -- archive install files - local archivedir = _get_archivedir(package) - local oldir = os.cd(archivedir) + local installdir = package:installdir() + local oldir = os.cd(installdir) local archivefiles = os.files("**") os.cd(oldir) - archive.archive(path.absolute(package:outputfile()), archivefiles, {curdir = archivedir}) + archive.archive(path.absolute(package:outputfile()), archivefiles, {curdir = installdir}) end function main(package) cprint("packing %s .. ", package:outputfile()) + _pack_archive(package) end diff --git a/xmake/plugins/pack/batchcmds.lua b/xmake/plugins/pack/batchcmds.lua index 2bd45b86e..1b3023283 100644 --- a/xmake/plugins/pack/batchcmds.lua +++ b/xmake/plugins/pack/batchcmds.lua @@ -178,10 +178,14 @@ end function _on_target_installcmd_headeronly(target, batchcmds_, opt) local package = opt.package local includedir = package:includedir() + + -- install headers + _install_headers(target, batchcmds_, includedir) end -- on install target command function _on_target_installcmd(target, batchcmds_, opt) + local package = opt.package -- install target binaries local scripts = { @@ -196,7 +200,7 @@ function _on_target_installcmd(target, batchcmds_, opt) end -- install target files - local srcfiles, dstfiles = target:installfiles(".") + local srcfiles, dstfiles = target:installfiles(package:installdir()) for idx, srcfile in ipairs(srcfiles) do batchcmds_:cp(srcfile, dstfiles[idx]) end @@ -270,6 +274,7 @@ end -- on uninstall target command function _on_target_uninstallcmd(target, batchcmds_, opt) + local package = opt.package -- uninstall target binaries local scripts = { @@ -284,7 +289,7 @@ function _on_target_uninstallcmd(target, batchcmds_, opt) end -- uninstall target files - local _, dstfiles = target:installfiles(".") + local _, dstfiles = target:installfiles(package:installdir()) for _, dstfile in ipairs(dstfiles) do batchcmds_:rm(dstfile, {emptydirs = true}) end @@ -358,7 +363,7 @@ end -- on install command function _on_installcmd(package, batchcmds_) - local srcfiles, dstfiles = package:installfiles(".") + local srcfiles, dstfiles = package:installfiles() for idx, srcfile in ipairs(srcfiles) do batchcmds_:cp(srcfile, dstfiles[idx]) end @@ -369,7 +374,7 @@ end -- on uninstall command function _on_uninstallcmd(package, batchcmds_) - local _, dstfiles = package:installfiles(".") + local _, dstfiles = package:installfiles() for _, dstfile in ipairs(dstfiles) do batchcmds_:rm(dstfile, {emptydirs = true}) end diff --git a/xmake/plugins/pack/main.lua b/xmake/plugins/pack/main.lua index 7a4ab0fbf..e404aba9b 100644 --- a/xmake/plugins/pack/main.lua +++ b/xmake/plugins/pack/main.lua @@ -44,6 +44,15 @@ function _pack_package(package) os.tryrm(package:buildir()) os.mkdir(package:outputdir()) + -- get need formats + local formats_need = option.get("formats") + if formats_need then + formats_need = formats_need:split(",") + if formats_need[1] == "all" then + formats_need = nil + end + end + -- do pack assert(package:formats(), "xpack(%s): formats not found, please use `set_formats()` to set it.", package:name()) local scripts = { @@ -52,11 +61,13 @@ function _pack_package(package) package:script("package_after") } for _, format in package:formats():keys() do - _load_package(package, format) - for i = 1, 3 do - local script = scripts[i] - if script ~= nil then - script(package) + if not formats_need or table.contains(formats_need, format) then + _load_package(package, format) + for i = 1, 3 do + local script = scripts[i] + if script ~= nil then + script(package) + end end end end diff --git a/xmake/plugins/pack/nsis/main.lua b/xmake/plugins/pack/nsis/main.lua index 6b9d2961f..660192b02 100644 --- a/xmake/plugins/pack/nsis/main.lua +++ b/xmake/plugins/pack/nsis/main.lua @@ -59,6 +59,11 @@ function _get_unique_tag(content) return hash.uuid(content):split("-", {plain = true})[1]:lower() end +-- translate the file path +function _translate_filepath(package, filepath) + return filepath:replace(package:installdir(), "$InstDir", {plain = true}) +end + -- get command string function _get_command_strings(package, cmd, opt) opt = table.join(cmd.opt or {}, opt) @@ -69,8 +74,8 @@ function _get_command_strings(package, cmd, opt) local srcfiles = os.files(cmd.srcpath) for _, srcfile in ipairs(srcfiles) do -- the destination is directory? append the filename - local dstfile = cmd.dstpath - if path.islastsep(dstfile) then + local dstfile = _translate_filepath(package, cmd.dstpath) + if #srcfiles > 1 or path.islastsep(dstfile) then if opt.rootdir then dstfile = path.join(dstfile, path.relative(srcfile, opt.rootdir)) else @@ -79,31 +84,31 @@ function _get_command_strings(package, cmd, opt) end srcfile = path.normalize(srcfile) local dstname = path.filename(dstfile) - local dstdir = path.normalize(path.directory(path.join("$InstDir", dstfile))) + local dstdir = path.normalize(path.directory(dstfile)) table.insert(result, string.format("SetOutPath \"%s\"", dstdir)) table.insert(result, string.format("File /oname=%s \"%s\"", dstname, srcfile)) end elseif kind == "rm" then - local filepath = path.normalize(path.join("$InstDir", cmd.filepath)) + local filepath = _translate_filepath(package, cmd.filepath) table.insert(result, string.format("${%s} \"%s\"", opt.install and "RMFileIfExists" or "unRMFileIfExists", filepath)) if opt.emptydirs then table.insert(result, string.format("${%s} \"%s\"", opt.install and "RMEmptyParentDirs" or "unRMEmptyParentDirs", filepath)) end elseif kind == "rmdir" then - local dir = path.normalize(path.join("$InstDir", cmd.dir)) + local dir = _translate_filepath(package, cmd.dir) table.insert(result, string.format("${%s} \"%s\"", opt.install and "RMDirIfExists" or "unRMDirIfExists", dir)) if opt.emptydirs then table.insert(result, string.format("${%s} \"%s\"", opt.install and "RMEmptyParentDirs" or "unRMEmptyParentDirs", dir)) end elseif kind == "mv" then - local srcpath = path.normalize(path.join("$InstDir", cmd.srcpath)) - local dstpath = path.normalize(path.join("$InstDir", cmd.dstpath)) + local srcpath = _translate_filepath(package, cmd.srcpath) + local dstpath = _translate_filepath(package, cmd.dstpath) table.insert(result, string.format("Rename \"%s\" \"%s\"", srcpath, dstpath)) elseif kind == "cd" then - local dir = path.normalize(path.join("$InstDir", cmd.dir)) + local dir = _translate_filepath(package, cmd.dir) table.insert(result, string.format("SetOutPath \"%s\"", dir)) elseif kind == "mkdir" then - local dir = path.normalize(path.join("$InstDir", cmd.dir)) + local dir = _translate_filepath(package, cmd.dir) table.insert(result, string.format("CreateDirectory \"%s\"", dir)) end return result @@ -147,7 +152,7 @@ function _get_target_filepath(package) end end if targetfile then - return path.normalize(path.join(package:bindir(), path.filename(targetfile))) + return _translate_filepath(package, path.join(package:bindir(), path.filename(targetfile))) end end @@ -155,7 +160,7 @@ end function _get_specvars(package) local specvars = table.clone(package:specvars()) specvars.PACKAGE_WORKDIR = path.absolute(os.projectdir()) - specvars.PACKAGE_BINDIR = package:bindir() + specvars.PACKAGE_BINDIR = _translate_filepath(package, package:bindir()) specvars.PACKAGE_OUTPUTFILE = path.absolute(package:outputfile()) specvars.PACKAGE_INSTALLCMDS = function () return _get_installcmds(package) @@ -164,7 +169,16 @@ function _get_specvars(package) return _get_uninstallcmds(package) end specvars.PACKAGE_NSIS_DISPLAY_NAME = _get_filter_value(package, "nsis_displayname") or package:name() - specvars.PACKAGE_NSIS_DISPLAY_ICON = _get_filter_value(package, "nsis_displayicon") or _get_target_filepath(package) or "" + specvars.PACKAGE_NSIS_DISPLAY_ICON = function () + local iconpath = _get_filter_value(package, "nsis_displayicon") + if iconpath then + iconpath = path.join(package:installdir(), iconpath) + end + if not iconpath then + iconpath = _get_target_filepath(package) or "" + end + return _translate_filepath(package, iconpath) + end specvars.PACKAGE_NSIS_INSTALL_SECTIONS = function () local result = {} local cmds = package:get("nsis_installcmds") diff --git a/xmake/plugins/pack/xpack.lua b/xmake/plugins/pack/xpack.lua index 0c255c6b2..71fd3ac08 100644 --- a/xmake/plugins/pack/xpack.lua +++ b/xmake/plugins/pack/xpack.lua @@ -442,8 +442,14 @@ function xpack:_copiedfiles(filetype, outputdir) end -- get the install files -function xpack:installfiles(outputdir) - return self:_copiedfiles("installfiles", outputdir) +function xpack:installfiles() + return self:_copiedfiles("installfiles", self:installdir()) +end + +-- get the install directory, this is just a temporary sandbox installation path, +-- we may replace it with the actual installation path in the specfile +function xpack:installdir(...) + return path.normalize(path.join(self:buildir(), "installed", self:format(), ...)) end -- get the binary directory @@ -452,7 +458,7 @@ function xpack:bindir() if bindir == nil then bindir = "bin" end - return bindir + return self:installdir(bindir) end -- get the library directory @@ -461,7 +467,7 @@ function xpack:libdir() if libdir == nil then libdir = "lib" end - return libdir + return self:installdir(libdir) end -- get the include directory @@ -470,7 +476,7 @@ function xpack:includedir() if includedir == nil then includedir = "include" end - return includedir + return self:installdir(includedir) end -- new a xpack @@ -487,13 +493,6 @@ function packages() if packages_need then packages_need = hashset.from(packages_need) end - local formats_need = option.get("formats") - if formats_need then - formats_need = formats_need:split(",") - if formats_need[1] == "all" then - formats_need = nil - end - end local xpack_scope = project.scope("xpack") for name, scope in pairs(xpack_scope) do local need = false @@ -506,9 +505,7 @@ function packages() end if need then local instance = _new(name, scope) - if not formats_need or instance:format_has(table.unpack(formats_need)) then - packages[name] = instance - end + packages[name] = instance end end _g.packages = packages diff --git a/xmake/scripts/xpack/nsis/makensis.nsi b/xmake/scripts/xpack/nsis/makensis.nsi index a1827f348..7be50dd0b 100644 --- a/xmake/scripts/xpack/nsis/makensis.nsi +++ b/xmake/scripts/xpack/nsis/makensis.nsi @@ -243,7 +243,6 @@ FunctionEnd ; setup installer -Var BinDir Var NoAdmin Function .onInit ${GetOptions} $CMDLINE "/NOADMIN" $NoAdmin @@ -271,14 +270,6 @@ Function .onInit ${If} $InstDir == "" StrCpy $InstDir "${PROGRAMFILES}\${PACKAGE_NAME}" ${EndIf} - - ; get binary directory -!if "${PACKAGE_BINDIR}" == "." - StrCpy $BinDir "$InstDir" -!else - StrCpy $BinDir "$InstDir\${PACKAGE_BINDIR}" -!endif - FunctionEnd Section "${PACKAGE_NAME} (required)" InstallExeutable @@ -298,7 +289,7 @@ Section "${PACKAGE_NAME} (required)" InstallExeutable WriteRegStr ${RootKey} ${RegUninstall} "NoAdmin" "$NoAdmin" WriteRegStr ${RootKey} ${RegUninstall} "DisplayName" "${PACKAGE_NSIS_DISPLAY_NAME}" !if "${PACKAGE_NSIS_DISPLAY_ICON}" != "" - WriteRegStr ${RootKey} ${RegUninstall} "DisplayIcon" '"$InstDir\${PACKAGE_NSIS_DISPLAY_ICON}"' + WriteRegStr ${RootKey} ${RegUninstall} "DisplayIcon" '"${PACKAGE_NSIS_DISPLAY_ICON}"' !endif WriteRegStr ${RootKey} ${RegUninstall} "Comments" "${PACKAGE_DESCRIPTION}" WriteRegStr ${RootKey} ${RegUninstall} "Publisher" "${PACKAGE_COPYRIGHT}" @@ -331,12 +322,12 @@ SectionEnd Section "Add to PATH" InstallPath ${If} $NoAdmin == "false" ReadRegStr $R0 ${HKLM} "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" - ${WordReplace} $R0 ";$BinDir" "" "+" $R1 - WriteRegExpandStr ${HKLM} "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" "$R1;$BinDir" + ${WordReplace} $R0 ";${PACKAGE_BINDIR}" "" "+" $R1 + WriteRegExpandStr ${HKLM} "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" "$R1;${PACKAGE_BINDIR}" ${Else} ReadRegStr $R0 ${HKCU} "Environment" "Path" - ${WordReplace} $R0 ";$BinDir" "" "+" $R1 - WriteRegExpandStr ${HKCU} "Environment" "Path" "$R1;$BinDir" + ${WordReplace} $R0 ";${PACKAGE_BINDIR}" "" "+" $R1 + WriteRegExpandStr ${HKCU} "Environment" "Path" "$R1;${PACKAGE_BINDIR}" ${EndIf} SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000 SectionEnd @@ -365,13 +356,6 @@ Function un.onInit ${IfNot} $NoAdmin == "true" !insertmacro Init "uninstaller" ${EndIf} - - ; get binary directory -!if "${PACKAGE_BINDIR}" == "." - StrCpy $BinDir "$InstDir" -!else - StrCpy $BinDir "$InstDir\${PACKAGE_BINDIR}" -!endif FunctionEnd Section "Uninstall" @@ -383,12 +367,12 @@ Section "Uninstall" ${If} $NoAdmin == "false" DeleteRegKey ${HKLM} ${RegUninstall} ReadRegStr $R0 ${HKLM} "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" - ${WordReplace} $R0 ";$BinDir" "" "+" $R1 + ${WordReplace} $R0 ";${PACKAGE_BINDIR}" "" "+" $R1 WriteRegExpandStr ${HKLM} "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" "$R1" ${Else} DeleteRegKey ${HKCU} ${RegUninstall} ReadRegStr $R0 ${HKCU} "Environment" "Path" - ${WordReplace} $R0 ";$BinDir" "" "+" $R1 + ${WordReplace} $R0 ";${PACKAGE_BINDIR}" "" "+" $R1 WriteRegExpandStr ${HKCU} "Environment" "Path" "$R1" ${EndIf} |
