From 0384acec887c4132c4bead81ed6dc05d3c0397ad Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 17 Nov 2023 00:52:20 +0800 Subject: remove empty dirs --- xmake/plugins/pack/nsis/main.lua | 33 ++++++++++------ xmake/scripts/xpack/nsis/makensis.nsi | 72 +++++++++++++++++++++++------------ 2 files changed, 69 insertions(+), 36 deletions(-) diff --git a/xmake/plugins/pack/nsis/main.lua b/xmake/plugins/pack/nsis/main.lua index 02d31ac48..1dd5221b3 100644 --- a/xmake/plugins/pack/nsis/main.lua +++ b/xmake/plugins/pack/nsis/main.lua @@ -85,6 +85,9 @@ function _get_command_strings(package, cmd) elseif kind == "rm" then local filepath = path.normalize(path.join("$InstDir", cmd.filepath)) table.insert(result, string.format("Delete \"%s\"", filepath)) + if opt.emptydirs then + table.insert(result, string.format("${RMDirUP} \"%s\"", filepath)) + end elseif kind == "tryrm" then --[[ IfFileExists "$InstDir\file" file_found file_not_found_or_end @@ -100,9 +103,15 @@ function _get_command_strings(package, cmd) table.insert(result, string.format(" Delete \"%s\"", filepath)) table.insert(result, string.format(" goto file_not_found_or_end_%s", tag)) table.insert(result, string.format("file_not_found_or_end_%s:", tag)) + if opt.emptydirs then + table.insert(result, string.format("${RMDirUP} \"%s\"", filepath)) + end elseif kind == "rmdir" then local dir = path.normalize(path.join("$InstDir", cmd.dir)) table.insert(result, string.format("RMDir /r \"%s\"", dir)) + if opt.emptydirs then + table.insert(result, string.format("${RMDirUP} \"%s\"", dir)) + end elseif kind == "mv" then local srcpath = path.normalize(path.join("$InstDir", cmd.srcpath)) local dstpath = path.normalize(path.join("$InstDir", cmd.dstpath)) @@ -173,7 +182,7 @@ end function _uninstall_headers(target, batchcmds_, includedir) local _, dstheaders = target:headerfiles(includedir, {installonly = true}) for _, dstheader in ipairs(dstheaders) do - batchcmds_:rm(dstheader) + batchcmds_:rm(dstheader, {emptydirs = true}) end end @@ -182,7 +191,7 @@ function _uninstall_shared_for_package(target, pkg, batchcmds_, outputdir) for _, dllpath in ipairs(table.wrap(pkg:get("libfiles"))) do if dllpath:endswith(".dll") then local dllname = path.filename(dllpath) - batchcmds_:rm(path.join(outputdir, dllname)) + batchcmds_:rm(path.join(outputdir, dllname), {emptydirs = true}) end end end @@ -311,14 +320,14 @@ function _on_target_uninstallcmd_binary(target, batchcmds_, opt) local bindir = package:bindir() -- uninstall target file - batchcmds_:rm(path.join(bindir, target:filename())) - batchcmds_:tryrm(path.join(bindir, path.filename(target:symbolfile()))) + batchcmds_:rm(path.join(bindir, target:filename()), {emptydirs = true}) + batchcmds_:tryrm(path.join(bindir, path.filename(target:symbolfile())), {emptydirs = true}) -- remove the dependent shared/windows (*.dll) target -- @see https://github.com/xmake-io/xmake/issues/961 for _, dep in ipairs(target:orderdeps()) do if dep:is_shared() then - batchcmds_:rm(path.join(bindir, path.filename(dep:targetfile()))) + batchcmds_:rm(path.join(bindir, path.filename(dep:targetfile())), {emptydirs = true}) end _uninstall_shared_for_packages(dep, batchcmds_, bindir) end @@ -335,13 +344,13 @@ function _on_target_uninstallcmd_shared(target, batchcmds_, opt) local includedir = package:includedir() -- uninstall target file - batchcmds_:rm(path.join(bindir, target:filename())) - batchcmds_:tryrm(path.join(bindir, path.filename(target:symbolfile()))) + batchcmds_:rm(path.join(bindir, target:filename()), {emptydirs = true}) + batchcmds_:tryrm(path.join(bindir, path.filename(target:symbolfile())), {emptydirs = true}) -- remove *.lib for shared/windows (*.dll) target -- @see https://github.com/xmake-io/xmake/issues/714 local targetfile = target:targetfile() - batchcmds_:rm(path.join(libdir, path.basename(targetfile) .. (target:is_plat("mingw") and ".dll.a" or ".lib"))) + batchcmds_:rm(path.join(libdir, path.basename(targetfile) .. (target:is_plat("mingw") and ".dll.a" or ".lib")), {emptydirs = true}) -- remove headers from the include directory _uninstall_headers(target, batchcmds_, includedir) @@ -357,8 +366,8 @@ function _on_target_uninstallcmd_static(target, batchcmds_, opt) local includedir = package:includedir() -- uninstall target file - batchcmds_:rm(path.join(libdir, target:filename())) - batchcmds_:tryrm(path.join(libdir, path.filename(target:symbolfile()))) + batchcmds_:rm(path.join(libdir, target:filename()), {emptydirs = true}) + batchcmds_:tryrm(path.join(libdir, path.filename(target:symbolfile())), {emptydirs = true}) -- remove headers from the include directory _uninstall_headers(target, batchcmds_, includedir) @@ -389,7 +398,7 @@ function _on_target_uninstallcmd(target, batchcmds_, opt) -- uninstall target files local _, dstfiles = target:installfiles(".") for _, dstfile in ipairs(dstfiles) do - batchcmds_:rm(dstfile) + batchcmds_:rm(dstfile, {emptydirs = true}) end end @@ -474,7 +483,7 @@ end function _on_uninstallcmd(package, batchcmds_) local _, dstfiles = package:installfiles(".") for _, dstfile in ipairs(dstfiles) do - batchcmds_:rm(dstfile) + batchcmds_:rm(dstfile, {emptydirs = true}) end for _, target in ipairs(package:targets()) do _get_target_uninstallcmds(target, batchcmds_, {package = package}) diff --git a/xmake/scripts/xpack/nsis/makensis.nsi b/xmake/scripts/xpack/nsis/makensis.nsi index 5d1a99dc0..2a7260519 100644 --- a/xmake/scripts/xpack/nsis/makensis.nsi +++ b/xmake/scripts/xpack/nsis/makensis.nsi @@ -120,38 +120,38 @@ VIAddVersionKey /LANG=0 ProductVersion "${VERSION_FULL}" ; helper functions Function TrimQuote - Exch $R1 ; Original string - Push $R2 + Exch $R1 ; Original string + Push $R2 Loop: - StrCpy $R2 "$R1" 1 - StrCmp "$R2" "'" TrimLeft - StrCmp "$R2" "$\"" TrimLeft - StrCmp "$R2" "$\r" TrimLeft - StrCmp "$R2" "$\n" TrimLeft - StrCmp "$R2" "$\t" TrimLeft - StrCmp "$R2" " " TrimLeft - GoTo Loop2 + StrCpy $R2 "$R1" 1 + StrCmp "$R2" "'" TrimLeft + StrCmp "$R2" "$\"" TrimLeft + StrCmp "$R2" "$\r" TrimLeft + StrCmp "$R2" "$\n" TrimLeft + StrCmp "$R2" "$\t" TrimLeft + StrCmp "$R2" " " TrimLeft + GoTo Loop2 TrimLeft: - StrCpy $R1 "$R1" "" 1 - Goto Loop + StrCpy $R1 "$R1" "" 1 + Goto Loop Loop2: - StrCpy $R2 "$R1" 1 -1 - StrCmp "$R2" "'" TrimRight - StrCmp "$R2" "$\"" TrimRight - StrCmp "$R2" "$\r" TrimRight - StrCmp "$R2" "$\n" TrimRight - StrCmp "$R2" "$\t" TrimRight - StrCmp "$R2" " " TrimRight - GoTo Done + StrCpy $R2 "$R1" 1 -1 + StrCmp "$R2" "'" TrimRight + StrCmp "$R2" "$\"" TrimRight + StrCmp "$R2" "$\r" TrimRight + StrCmp "$R2" "$\n" TrimRight + StrCmp "$R2" "$\t" TrimRight + StrCmp "$R2" " " TrimRight + GoTo Done TrimRight: - StrCpy $R1 "$R1" -1 - Goto Loop2 + StrCpy $R1 "$R1" -1 + Goto Loop2 Done: - Pop $R2 - Exch $R1 + Pop $R2 + Exch $R1 FunctionEnd ; setup installer @@ -282,7 +282,29 @@ Function un.onInit !else StrCpy $BinDir "$InstDir\${PACKAGE_BINDIR}" !endif +FunctionEnd + +; remove it's parent directories if they are empty +; ${RMDirUP} "filepath" +Function un.RMDirUP + !define RMDirUP '!insertmacro RMDirUPCall' + !macro RMDirUPCall _PATH + push '${_PATH}' + Call un.RMDirUP + !macroend + + ; $0 - current folder + ClearErrors + Exch $0 + ;DetailPrint "ASDF - $0\.." + RMDir "$0\.." + + IfErrors Skip + ${RMDirUP} "$0\.." + Skip: + + Pop $0 FunctionEnd Section "Uninstall" @@ -305,5 +327,7 @@ Section "Uninstall" ; remove uninstall.exe Delete "$InstDir\uninstall.exe" + ${RMDirUP} "$InstDir\uninstall.exe" + SectionEnd -- cgit v1.3.1 From bd74082ad5740e0b97ffe237216dd533eb3d0383 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 17 Nov 2023 22:29:54 +0800 Subject: fix batchcmds --- xmake/modules/private/utils/batchcmds.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/xmake/modules/private/utils/batchcmds.lua b/xmake/modules/private/utils/batchcmds.lua index fe518cbec..cbe924f53 100644 --- a/xmake/modules/private/utils/batchcmds.lua +++ b/xmake/modules/private/utils/batchcmds.lua @@ -352,18 +352,18 @@ function batchcmds:mkdir(dir) end -- add command: os.rmdir -function batchcmds:rmdir(dir) - table.insert(self:cmds(), {kind = "rmdir", dir = dir}) +function batchcmds:rmdir(dir, opt) + table.insert(self:cmds(), {kind = "rmdir", dir = dir, opt = opt}) end -- add command: os.rm -function batchcmds:rm(filepath) - table.insert(self:cmds(), {kind = "rm", filepath = filepath}) +function batchcmds:rm(filepath, opt) + table.insert(self:cmds(), {kind = "rm", filepath = filepath, opt = opt}) end -- add command: os.tryrm -function batchcmds:tryrm(filepath) - table.insert(self:cmds(), {kind = "tryrm", filepath = filepath}) +function batchcmds:tryrm(filepath, opt) + table.insert(self:cmds(), {kind = "tryrm", filepath = filepath, opt = opt}) end -- add command: os.cp -- cgit v1.3.1 From 02c004d42d70979f3ca983dd960291761800e2a6 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 17 Nov 2023 22:31:31 +0800 Subject: improve os.rm to support remove emptydirs --- xmake/core/base/os.lua | 30 ++++++++++++++++++++-- xmake/core/sandbox/modules/os.lua | 8 +++--- .../modules/private/action/clean/remove_files.lua | 10 +------- xmake/modules/private/utils/batchcmds.lua | 6 ++--- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 7ae34ccc2..ed5490b7d 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -159,6 +159,19 @@ function os._rm(filedir) return true end +-- remove empty parent directories of this file path +function os._rm_empty_parentdirs(filepath) + local parentdir = path.directory(filepath) + while parentdir and os.isdir(parentdir) and os.emptydir(parentdir) do + local ok, errors = os._rm(parentdir) + if not ok then + return false, errors + end + parentdir = path.directory(parentdir) + end + return true +end + -- get the ramdisk root directory -- https://github.com/xmake-io/xmake/issues/3408 function os._ramdir() @@ -464,7 +477,7 @@ function os.mv(srcpath, dstpath, opt) end -- remove files or directories -function os.rm(filepath) +function os.rm(filepath, opt) -- check arguments if not filepath then @@ -472,16 +485,29 @@ function os.rm(filepath) end -- remove file or directories + opt = opt or {} filepath = tostring(filepath) local filepathes = os._match_wildcard_pathes(filepath) if type(filepathes) == "string" then - return os._rm(filepathes) + local ok, errors = os._rm(filepathes) + if not ok then + return false, errors + end + if opt.emptydirs then + return os._rm_empty_parentdirs(filepathes) + end else for _, _filepath in ipairs(filepathes) do local ok, errors = os._rm(_filepath) if not ok then return false, errors end + if opt.emptydirs then + ok, errors = os._rm_empty_parentdirs(filepath) + if not ok then + return false, errors + end + end end end return true diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index c23ffb261..9004f66ee 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -152,12 +152,12 @@ function sandbox_os.vmv(srcpath, dstpath, opt) end -- remove file or directory with the verbose info -function sandbox_os.vrm(filepath) +function sandbox_os.vrm(filepath, opt) assert(filepath) if option.get("verbose") then utils.cprint("${dim}> remove %s", filepath) end - return sandbox_os.rm(filepath) + return sandbox_os.rm(filepath, opt) end -- link file or directory with the verbose info @@ -182,9 +182,9 @@ function sandbox_os.trymv(srcpath, dstpath) end -- try to remove files or directories -function sandbox_os.tryrm(filepath) +function sandbox_os.tryrm(filepath, opt) assert(filepath) - return os.rm(vformat(filepath)) + return os.rm(vformat(filepath), opt) end -- change to directory diff --git a/xmake/modules/private/action/clean/remove_files.lua b/xmake/modules/private/action/clean/remove_files.lua index 606bb405d..3d62c6b78 100644 --- a/xmake/modules/private/action/clean/remove_files.lua +++ b/xmake/modules/private/action/clean/remove_files.lua @@ -25,14 +25,6 @@ import("core.base.option") function main(filedirs, opt) opt = opt or {} for _, filedir in ipairs(filedirs) do - os.tryrm(filedir) - if option.get("all") or opt.emptydir then - -- remove it if the parent directory is empty - local parentdir = path.directory(filedir) - while parentdir and os.isdir(parentdir) and os.emptydir(parentdir) do - os.tryrm(parentdir) - parentdir = path.directory(parentdir) - end - end + os.tryrm(filedir, {emptydirs = option.get("all") or opt.emptydir}) end end diff --git a/xmake/modules/private/utils/batchcmds.lua b/xmake/modules/private/utils/batchcmds.lua index cbe924f53..0000b7111 100644 --- a/xmake/modules/private/utils/batchcmds.lua +++ b/xmake/modules/private/utils/batchcmds.lua @@ -140,7 +140,7 @@ end function _runcmd_rm(cmd, opt) local filepath = cmd.filepath if not opt.dryrun then - os.tryrm(filepath) + os.tryrm(filepath, opt) end end @@ -148,7 +148,7 @@ end function _runcmd_tryrm(cmd, opt) local filepath = cmd.filepath if not opt.dryrun then - os.tryrm(filepath) + os.tryrm(filepath, opt) end end @@ -156,7 +156,7 @@ end function _runcmd_rmdir(cmd, opt) local dir = cmd.dir if not opt.dryrun and os.isdir(dir) then - os.tryrm(dir) + os.tryrm(dir, opt) end end -- cgit v1.3.1 From 03671d0941bf93a5a66eff081d8b50c8f43a532a Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 17 Nov 2023 22:40:22 +0800 Subject: improve to remove dirs --- core/src/demo/xmake.lua | 18 +++++ xmake/modules/private/utils/batchcmds.lua | 14 ---- xmake/plugins/pack/nsis/main.lua | 44 ++++-------- xmake/scripts/xpack/nsis/makensis.nsi | 115 +++++++++++++++++++++++------- 4 files changed, 121 insertions(+), 70 deletions(-) diff --git a/core/src/demo/xmake.lua b/core/src/demo/xmake.lua index 02466b31f..72892693d 100644 --- a/core/src/demo/xmake.lua +++ b/core/src/demo/xmake.lua @@ -68,3 +68,21 @@ target("demo") add_installfiles("$(projectdir)/../scripts/xrepo.sh", {prefixdir = "bin", filename = "xrepo"}) end + before_installcmd(function (target, batchcmds, opt) + 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") + end + end) + diff --git a/xmake/modules/private/utils/batchcmds.lua b/xmake/modules/private/utils/batchcmds.lua index 0000b7111..eda3887c8 100644 --- a/xmake/modules/private/utils/batchcmds.lua +++ b/xmake/modules/private/utils/batchcmds.lua @@ -144,14 +144,6 @@ function _runcmd_rm(cmd, opt) end end --- run command: os.tryrm -function _runcmd_tryrm(cmd, opt) - local filepath = cmd.filepath - if not opt.dryrun then - os.tryrm(filepath, opt) - end -end - -- run command: os.rmdir function _runcmd_rmdir(cmd, opt) local dir = cmd.dir @@ -197,7 +189,6 @@ function _runcmd(cmd, opt) rmdir = _runcmd_rmdir, cd = _runcmd_cd, rm = _runcmd_rm, - tryrm = _runcmd_tryrm, cp = _runcmd_cp, mv = _runcmd_mv, ln = _runcmd_ln @@ -361,11 +352,6 @@ function batchcmds:rm(filepath, opt) table.insert(self:cmds(), {kind = "rm", filepath = filepath, opt = opt}) end --- add command: os.tryrm -function batchcmds:tryrm(filepath, opt) - table.insert(self:cmds(), {kind = "tryrm", filepath = filepath, opt = opt}) -end - -- add command: os.cp function batchcmds:cp(srcpath, dstpath, opt) table.insert(self:cmds(), {kind = "cp", srcpath = srcpath, dstpath = dstpath, opt = opt}) diff --git a/xmake/plugins/pack/nsis/main.lua b/xmake/plugins/pack/nsis/main.lua index 1dd5221b3..a99370a01 100644 --- a/xmake/plugins/pack/nsis/main.lua +++ b/xmake/plugins/pack/nsis/main.lua @@ -59,9 +59,9 @@ function _get_unique_tag(content) end -- get command string -function _get_command_strings(package, cmd) +function _get_command_strings(package, cmd, opt) + opt = table.join(cmd.opt or {}, opt) local result = {} - local opt = cmd.opt or {} local kind = cmd.kind if kind == "cp" then -- https://nsis.sourceforge.io/Reference/File @@ -84,33 +84,15 @@ function _get_command_strings(package, cmd) end elseif kind == "rm" then local filepath = path.normalize(path.join("$InstDir", cmd.filepath)) - table.insert(result, string.format("Delete \"%s\"", filepath)) + table.insert(result, string.format("${%s} \"%s\"", opt.install and "RMFileIfExists" or "unRMFileIfExists", filepath)) if opt.emptydirs then - table.insert(result, string.format("${RMDirUP} \"%s\"", filepath)) - end - elseif kind == "tryrm" then - --[[ - IfFileExists "$InstDir\file" file_found file_not_found_or_end - file_found: - Delete "$InstDir\file" - goto file_not_found_or_end - file_not_found_or_end: - --]] - local filepath = path.normalize(path.join("$InstDir", cmd.filepath)) - local tag = _get_unique_tag(filepath) - table.insert(result, string.format("IfFileExists \"%s\" file_found_%s file_not_found_or_end_%s", filepath, tag, tag)) - table.insert(result, string.format("file_found_%s:", tag)) - table.insert(result, string.format(" Delete \"%s\"", filepath)) - table.insert(result, string.format(" goto file_not_found_or_end_%s", tag)) - table.insert(result, string.format("file_not_found_or_end_%s:", tag)) - if opt.emptydirs then - table.insert(result, string.format("${RMDirUP} \"%s\"", filepath)) + 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)) - table.insert(result, string.format("RMDir /r \"%s\"", dir)) + table.insert(result, string.format("${%s} \"%s\"", opt.install and "RMDirIfExists" or "unRMDirIfExists", dir)) if opt.emptydirs then - table.insert(result, string.format("${RMDirUP} \"%s\"", dir)) + 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)) @@ -127,10 +109,10 @@ function _get_command_strings(package, cmd) end -- get commands string -function _get_commands_string(package, cmds) +function _get_commands_string(package, cmds, opt) local cmdstrs = {} for _, cmd in ipairs(cmds) do - table.join2(cmdstrs, _get_command_strings(package, cmd)) + table.join2(cmdstrs, _get_command_strings(package, cmd, opt)) end return table.concat(cmdstrs, "\n ") end @@ -321,7 +303,7 @@ function _on_target_uninstallcmd_binary(target, batchcmds_, opt) -- uninstall target file batchcmds_:rm(path.join(bindir, target:filename()), {emptydirs = true}) - batchcmds_:tryrm(path.join(bindir, path.filename(target:symbolfile())), {emptydirs = true}) + batchcmds_:rm(path.join(bindir, path.filename(target:symbolfile())), {emptydirs = true}) -- remove the dependent shared/windows (*.dll) target -- @see https://github.com/xmake-io/xmake/issues/961 @@ -345,7 +327,7 @@ function _on_target_uninstallcmd_shared(target, batchcmds_, opt) -- uninstall target file batchcmds_:rm(path.join(bindir, target:filename()), {emptydirs = true}) - batchcmds_:tryrm(path.join(bindir, path.filename(target:symbolfile())), {emptydirs = true}) + batchcmds_:rm(path.join(bindir, path.filename(target:symbolfile())), {emptydirs = true}) -- remove *.lib for shared/windows (*.dll) target -- @see https://github.com/xmake-io/xmake/issues/714 @@ -367,7 +349,7 @@ function _on_target_uninstallcmd_static(target, batchcmds_, opt) -- uninstall target file batchcmds_:rm(path.join(libdir, target:filename()), {emptydirs = true}) - batchcmds_:tryrm(path.join(libdir, path.filename(target:symbolfile())), {emptydirs = true}) + batchcmds_:rm(path.join(libdir, path.filename(target:symbolfile())), {emptydirs = true}) -- remove headers from the include directory _uninstall_headers(target, batchcmds_, includedir) @@ -508,7 +490,7 @@ function _get_installcmds(package) end -- generate command string - return _get_commands_string(package, batchcmds_:cmds()) + return _get_commands_string(package, batchcmds_:cmds(), {install = true}) end -- get uninstall commands @@ -529,7 +511,7 @@ function _get_uninstallcmds(package) end -- generate command string - return _get_commands_string(package, batchcmds_:cmds()) + return _get_commands_string(package, batchcmds_:cmds(), {install = false}) end -- get specvars diff --git a/xmake/scripts/xpack/nsis/makensis.nsi b/xmake/scripts/xpack/nsis/makensis.nsi index 2a7260519..63625bc9a 100644 --- a/xmake/scripts/xpack/nsis/makensis.nsi +++ b/xmake/scripts/xpack/nsis/makensis.nsi @@ -154,6 +154,94 @@ Done: Exch $R1 FunctionEnd +; remove directory if it exists +Function RMDirIfExists +!define RMDirIfExists '!insertmacro RMDirIfExistsCall' +!macro RMDirIfExistsCall _PATH + push '${_PATH}' + Call RMDirIfExists +!macroend + Exch $0 + IfFileExists "$0" 0 fileDoesNotExist + RMDir /r "$0" + fileDoesNotExist: +FunctionEnd + +Function unRMDirIfExists +!define unRMDirIfExists '!insertmacro un.RMDirIfExistsCall' +!macro unRMDirIfExistsCall _PATH + push '${_PATH}' + Call un.RMDirIfExists +!macroend + Exch $0 + IfFileExists "$0" 0 fileDoesNotExist + RMDir /r "$0" + fileDoesNotExist: +FunctionEnd + +; remove file if it exists +Function RMFileIfExists +!define RMFileIfExists '!insertmacro RMFileIfExistsCall' +!macro RMFileIfExistsCall _PATH + push '${_PATH}' + Call RMFileIfExists +!macroend + Exch $0 + IfFileExists "$0" 0 fileDoesNotExist + Delete "$0" + fileDoesNotExist: +FunctionEnd + +Function un.RMFileIfExists +!define unRMFileIfExists '!insertmacro unRMFileIfExistsCall' +!macro unRMFileIfExistsCall _PATH + push '${_PATH}' + Call un.RMFileIfExists +!macroend + Exch $0 + IfFileExists "$0" 0 fileDoesNotExist + Delete "$0" + fileDoesNotExist: +FunctionEnd + +; remove it's parent directories if they are empty +Function RMEmptyParentDirs +!define RMEmptyParentDirs '!insertmacro RMEmptyParentDirsCall' +!macro RMEmptyParentDirsCall _PATH + push '${_PATH}' + Call RMEmptyParentDirs +!macroend + ClearErrors + + Exch $0 + RMDir "$0\.." + + IfErrors Skip + ${RMEmptyParentDirs} "$0\.." + Skip: + + Pop $0 +FunctionEnd + +Function un.RMEmptyParentDirs +!define unRMEmptyParentDirs '!insertmacro unRMEmptyParentDirsCall' +!macro unRMEmptyParentDirsCall _PATH + push '${_PATH}' + Call un.RMEmptyParentDirs +!macroend + ClearErrors + + Exch $0 + RMDir "$0\.." + + IfErrors Skip + ${unRMEmptyParentDirs} "$0\.." + Skip: + + Pop $0 +FunctionEnd + + ; setup installer Var BinDir Var NoAdmin @@ -284,29 +372,6 @@ Function un.onInit !endif FunctionEnd -; remove it's parent directories if they are empty -; ${RMDirUP} "filepath" -Function un.RMDirUP - !define RMDirUP '!insertmacro RMDirUPCall' - !macro RMDirUPCall _PATH - push '${_PATH}' - Call un.RMDirUP - !macroend - - ; $0 - current folder - ClearErrors - - Exch $0 - ;DetailPrint "ASDF - $0\.." - RMDir "$0\.." - - IfErrors Skip - ${RMDirUP} "$0\.." - Skip: - - Pop $0 -FunctionEnd - Section "Uninstall" ; add uninstall commands @@ -326,8 +391,8 @@ Section "Uninstall" ${EndIf} ; remove uninstall.exe - Delete "$InstDir\uninstall.exe" - ${RMDirUP} "$InstDir\uninstall.exe" + ${unRMFileIfExists} "$InstDir\uninstall.exe" + ${unRMEmptyParentDirs} "$InstDir\uninstall.exe" SectionEnd -- cgit v1.3.1 From 6a4c1a917ef785489af8cdbed042fdf369f75d75 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 17 Nov 2023 22:41:02 +0800 Subject: update comments --- core/src/demo/xmake.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/demo/xmake.lua b/core/src/demo/xmake.lua index 72892693d..a45c82693 100644 --- a/core/src/demo/xmake.lua +++ b/core/src/demo/xmake.lua @@ -69,6 +69,7 @@ target("demo") end before_installcmd(function (target, batchcmds, opt) + -- we need to avoid some old files interfering with xmake's module import. if target:is_plat("windows") then batchcmds:rmdir("actions") batchcmds:rmdir("core") -- cgit v1.3.1 From e0f074c79be1f712d09df9f7b4b32245ba95f1c9 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 17 Nov 2023 22:50:34 +0800 Subject: add nsis display name --- core/xpack.lua | 1 + xmake/includes/xpack/xmake.lua | 4 +++- xmake/plugins/pack/nsis/main.lua | 11 +++++++++++ xmake/scripts/xpack/nsis/makensis.nsi | 2 +- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/core/xpack.lua b/core/xpack.lua index 5cbff58e8..280b1365a 100644 --- a/core/xpack.lua +++ b/core/xpack.lua @@ -5,6 +5,7 @@ xpack("xmake") add_targets("demo") set_bindir(".") set_iconfile("src/demo/xmake.ico") + set_nsis_displayname("Xmake build utility ($(arch))") on_load(function (package) local arch = package:arch() diff --git a/xmake/includes/xpack/xmake.lua b/xmake/includes/xpack/xmake.lua index f7198600d..9021404f7 100644 --- a/xmake/includes/xpack/xmake.lua +++ b/xmake/includes/xpack/xmake.lua @@ -49,7 +49,9 @@ local apis = { -- set installed library directory, e.g. lib "xpack.set_libdir", -- set installed include directory, e.g. include - "xpack.set_includedir" + "xpack.set_includedir", + -- set nsis display name + "xpack.set_nsis_displayname" }, paths = { -- set the spec file path, support the custom variable pattern, e.g. set_specfile("", {pattern = "%${([^\n]-)}"}) diff --git a/xmake/plugins/pack/nsis/main.lua b/xmake/plugins/pack/nsis/main.lua index a99370a01..3c4fa3bbd 100644 --- a/xmake/plugins/pack/nsis/main.lua +++ b/xmake/plugins/pack/nsis/main.lua @@ -25,6 +25,7 @@ import("lib.detect.find_tool") import("private.utils.batchcmds") import("private.action.require.impl.packagenv") import("private.action.require.impl.install_packages") +import(".filter") -- get the makensis function _get_makensis() @@ -514,6 +515,15 @@ function _get_uninstallcmds(package) return _get_commands_string(package, batchcmds_:cmds(), {install = false}) end +-- get value and filter it +function _get_filter_value(package, name) + local value = package:get(name) + if type(value) == "string" then + value = filter.handle(value, package) + end + return value +end + -- get specvars function _get_specvars(package) local specvars = table.clone(package:specvars()) @@ -526,6 +536,7 @@ function _get_specvars(package) specvars.PACKAGE_UNINSTALLCMDS = function () return _get_uninstallcmds(package) end + specvars.PACKAGE_NSIS_DISPLAY_NAME = _get_filter_value(package, "nsis_displayname") or package:name() specvars.PACKAGE_NSIS_INSTALL_SECTIONS = function () local result = {} local cmds = package:get("nsis_installcmds") diff --git a/xmake/scripts/xpack/nsis/makensis.nsi b/xmake/scripts/xpack/nsis/makensis.nsi index 63625bc9a..ffcb39c7c 100644 --- a/xmake/scripts/xpack/nsis/makensis.nsi +++ b/xmake/scripts/xpack/nsis/makensis.nsi @@ -296,7 +296,7 @@ Section "${PACKAGE_NAME} (required)" InstallExeutable ; Write the uninstall keys for Windows !macro AddReg RootKey WriteRegStr ${RootKey} ${RegUninstall} "NoAdmin" "$NoAdmin" - WriteRegStr ${RootKey} ${RegUninstall} "DisplayName" "${PACKAGE_NAME} (${PACKAGE_ARCH})" + WriteRegStr ${RootKey} ${RegUninstall} "DisplayName" "${PACKAGE_NSIS_DISPLAY_NAME}" WriteRegStr ${RootKey} ${RegUninstall} "DisplayIcon" '"$InstDir\${PACKAGE_BINDIR}\${PACKAGE_FILENAME}"' ; TODO WriteRegStr ${RootKey} ${RegUninstall} "Comments" "${PACKAGE_DESCRIPTION}" WriteRegStr ${RootKey} ${RegUninstall} "Publisher" "${PACKAGE_COPYRIGHT}" -- cgit v1.3.1