Skip to content
Permalink
Browse files

support update script

  • Loading branch information...
OpportunityLiu committed Jun 3, 2019
1 parent 79f7b27 commit b0663b914f178333a3d14a6d6475250e593065bd
Showing with 107 additions and 30 deletions.
  1. +103 −27 xmake/actions/update/main.lua
  2. +4 −3 xmake/actions/update/xmake.lua
@@ -29,6 +29,12 @@ import("core.base.privilege")
import("privilege.sudo")
import("actions.require.impl.environment", {rootdir = os.programdir()})

local official_sources = {
"https://github.com/xmake-io/xmake.git",
"https://gitlab.com/tboox/xmake.git",
"https://gitee.com/tboox/xmake.git"
}

-- run cmd with privilege
function _sudo(cmd)

@@ -191,6 +197,84 @@ function _install(sourcedir, version)
end
end

-- do install script
function _install_script(sourcedir)
cprintf("\r${yellow} => ${clear}install script to %s .. ", os.programdir())
local dirname = path.filename(os.programdir())
if dirname ~= "xmake" then
os.mv(path.join(sourcedir, "xmake"), path.join(sourcedir, dirname))
end
local ok = try
{
function ()
if false and os.trycp(path.join(sourcedir, dirname), path.join(os.programdir(), "..")) then
return true
elseif is_host("windows") then
local proc = process.openv("cscript", {path.join(os.programdir(), "scripts", "sudo.vbs"), "XCOPY", path.join(sourcedir, dirname), os.programdir(), "/s", "/y" })
if proc ~= nil then
process.close(proc)
end
else
_sudo("cp -rf '" .. path.join(sourcedir, dirname) .. "' '" .. path.join(os.programdir(), "..") .. "'")
end
return true
end,
catch
{
function (errors)
vprint(errors)
end
}
}
-- trace
if ok then
cprint("${color.success}${text.success}")
os.exec("xmake --version")
else
cprint("${color.failure}${text.failure}")
end
end

function _get_version()
local ver_arg = option.get("xmakever") or "latest"

local commitish = nil
local urls = nil
if ver_arg:find(':', 1, true) then
if ver_arg:find('#', 1, true) then
urls = { ver_arg:sub(1, ver_arg:find('#', 1, true) - 1) }
commitish = ver_arg:sub(ver_arg:find('#', 1, true) + 1)
else
urls = { ver_arg }
end
else
if ver_arg:startswith('#') then
commitish = ver_arg:sub(2)
else
commitish = ver_arg
end
end
commitish = ifelse(commitish and #commitish > 0, commitish, "latest")
urls = urls or official_sources

-- sort urls
fasturl.add(urls)
urls = fasturl.sort(urls)

-- get version
local version = nil
local tags, branches
for _, url in ipairs(urls) do
tags, branches = git.refs(url)
if tags or branches then
version = semver.select(commitish, tags or {}, tags or {}, branches or {})
break
end
end

return urls == official_sources, urls, version or "master", tags, branches
end

-- main
function main()

@@ -202,42 +286,28 @@ function main()

-- trace
cprint("${bright}uninstall ok!")
return
return
end

-- enter environment
-- enter environment
environment.enter()

-- sort main urls
local mainurls = {"https://github.com/xmake-io/xmake.git",
"https://gitlab.com/tboox/xmake.git",
"https://gitee.com/tboox/xmake.git"}
fasturl.add(mainurls)
mainurls = fasturl.sort(mainurls)

-- get version
local tags = nil
local branches = nil
local version = nil
for _, url in ipairs(mainurls) do
tags, branches = git.refs(url)
if tags or branches then
version = semver.select(option.get("xmakever") or "lastest", tags or {}, tags or {}, branches or {})
break
end
end
if not version then
version = "master"
end
local is_official, mainurls, version, tags, branches = _get_version()

-- has been installed?
if os.xmakever():eq(version) then
if is_official and os.xmakever():eq(version) then
cprint("${bright}xmake %s has been installed!", version)
return
end

local script_only = option.get("scriptonly")

-- get urls on windows
if is_host("windows") then
if is_host("windows") and not script_only then
if not is_official then
raise("not support to update from unofficial source on windows, missing '--scriptonly' flag?")
end

if version:find('.', 1, true) then
mainurls = {format("https://github.com/xmake-io/xmake/releases/download/%s/xmake-%s.exe", version, version),
format("https://qcloud.coding.net/u/waruqi/p/xmake-releases/git/raw/master/xmake-%s.exe", version),
@@ -263,14 +333,16 @@ function main()

-- the download task
local sourcedir = path.join(os.tmpdir(), "xmakesrc", version)
print(sourcedir)
local download_task = function ()
for idx, url in ipairs(mainurls) do
cprintf("\r${yellow} => ${clear}downloading %s .. ", url)
local ok = try
{
function ()
os.tryrm(sourcedir)
if not git.checkurl(url) then
-- all user provides urls are considered as git url since check has been performed in _get_version
if is_official and not git.checkurl(url) then
os.mkdir(sourcedir)
http.download(url, path.join(sourcedir, path.filename(url)))
else
@@ -313,6 +385,10 @@ function main()
environment.leave()

-- do install
_install(sourcedir, version)
if script_only then
_install_script(sourcedir)
else
_install(sourcedir, version)
end
end

@@ -38,9 +38,10 @@ task("update")
-- options
, options =
{
{nil, "uninstall", "k", nil, "Uninstall the current xmake program." }
, { }
, {nil, "xmakever", "v", nil, "The given xmake version. (e.g. ~2.2.3, dev, master)" }
{nil, "uninstall", "k", nil, "Uninstall the current xmake program." }
, { }
, {nil, "xmakever", "v", nil, "The given xmake version. (e.g. ~2.2.3, dev, master)" }
, {"s", "scriptonly", "k", nil, "Update script only" }
}
}

0 comments on commit b0663b9

Please sign in to comment.
You can’t perform that action at this time.