summaryrefslogtreecommitdiff
path: root/xmake/actions/update/main.lua
blob: 6981d2231d1add5f3c7a0586c3e9c381f4251321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
--!A cross-platform build utility based on Lua
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements.  See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership.  The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License.  You may obtain a copy of the License at
--
--     http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-- 
-- Copyright (C) 2015 - 2018, TBOOX Open Source Group.
--
-- @author      ruki
-- @file        main.lua
--

-- imports
import("core.base.semver")
import("core.base.option")
import("core.base.task")
import("net.http")
import("devel.git")
import("net.fasturl")
import("core.base.privilege")
import("privilege.sudo")
import("actions.require.impl.environment", {rootdir = os.programdir()})

-- run cmd with privilege
function _sudo(cmd)

    -- attempt to install directly
    try
    {
        function ()
            os.vrun(cmd)
            return true
        end,

        catch
        {
            -- failed or not permission? request administrator permission and run it again
            function (errors)

                -- try get privilege
                if privilege.get() then
                    local ok = try
                    {
                        function ()
                            os.vrun(cmd)
                            return true
                        end
                    }

                    -- release privilege
                    privilege.store()

                    -- ok?
                    if ok then 
                        return true 
                    end
                end

                -- show tips
                cprint("\r${bright red}error: ${default red}run `%s` failed, may permission denied!", cmd)

                -- continue to install with administrator permission?
                if sudo.has() then

                    -- get confirm
                    local confirm = option.get("yes")
                    if confirm == nil then

                        -- show tips
                        cprint("\r${bright yellow}note: ${default yellow}try continue to run `%s` with administrator permission again?", cmd)
                        cprint("\rplease input: y (y/n)")

                        -- get answer
                        io.flush()
                        local answer = io.read()
                        if answer == 'y' or answer == '' then
                            confirm = true
                        end
                    end

                    -- confirm to install?
                    if confirm then
                        sudo.vrun(cmd)
                        return true
                    end
                end
            end
        }
    }
end

-- do uninstall
function _uninstall()
    if is_host("windows") then
        local uninstaller = path.join(os.programdir(), "uninstall.exe")
        if os.isfile(uninstaller) then
            -- UAC on win7
            if winos:version():gt("winxp") then
                local proc = process.openv("cscript", {path.join(os.programdir(), "scripts", "sudo.vbs"), uninstaller})
                if proc ~= nil then
                    process.close(proc)
                end
            else
                local proc = process.open(uninstaller)
                if proc ~= nil then
                    process.close(proc)
                end
            end
        else
            raise("the uninstaller(%s) not found!", uninstaller)
        end
    else
        if os.programdir():startswith("/usr/") then
            _sudo("rm -rf " .. os.programdir())
            if os.isfile("/usr/local/bin/xmake") then
                _sudo("rm -f /usr/local/bin/xmake")
            end
            if os.isfile("/usr/bin/xmake") then
                _sudo("rm -f /usr/bin/xmake")
            end
        else
            os.rm(os.programdir())
            os.rm(os.programfile())
            os.rm("~/.local/bin/xmake")
        end
    end
end

-- do install
function _install(sourcedir, version)

    -- the install task
    local install_task = function ()

        -- trace
        cprintf("\r${yellow}  => ${clear}installing to %s ..  ", os.programdir())
        local ok = try 
        {
            function ()

                -- install it 
                os.cd(sourcedir)
                if is_host("windows") then
                    local installer = "xmake-" .. version .. ".exe"
                    if os.isfile(installer) then
                        -- UAC on win7
                        if winos:version():gt("winxp") then
                            local proc = process.openv("cscript", {path.join(os.programdir(), "scripts", "sudo.vbs"), installer})
                            if proc ~= nil then
                                process.close(proc)
                            end
                        else
                            local proc = process.open(installer)
                            if proc ~= nil then
                                process.close(proc)
                            end
                        end
                    else
                        raise("the installer(%s) not found!", installer)
                    end
                else
                    if os.programdir():startswith("/usr/") then
                        os.vrun("make build")
                        _sudo("make install") 
                    else
                        os.vrun("./scripts/get.sh __local__")
                    end
                end
                return true
            end,
            catch 
            {
                function (errors)
                    vprint(errors)
                end
            }
        }
            
        -- trace
        if ok then
            cprint("\r${yellow}  => ${clear}install to %s .. ${green}ok", os.programdir())
        else
            raise("install failed!")
        end
    end

    -- do install 
    if option.get("verbose") then
        install_task()
    else
        process.asyncrun(install_task)
    end

    -- show version
    if not is_host("windows") then
        os.exec("xmake --version")
    end
end

-- main
function main()

    -- only uninstall it
    if option.get("uninstall") then

        -- do uninstall
        _uninstall()

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

    -- enter environment 
    environment.enter()

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

    -- get version
    local version = nil
    for _, url in ipairs(mainurls) do
        local 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

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

    -- cannot support to update dev/master on windows
    if is_host("windows") then
        if version:find('.', 1, true) then
            mainurls = {format("https://github.com/tboox/xmake/releases/download/%s/xmake-%s.exe", version, version)}
        else
            raise("not support to update %s on windows!", version)
        end
    end

    -- trace
    print("update version: %s ..", version)

    -- the download task
    local sourcedir = path.join(os.tmpdir(), "xmakesrc", version)
    local download_task = function ()
        for idx, url in ipairs(mainurls) do
            cprintf("\r${yellow}  => ${clear}clone %s ..  ", url)
            local ok = try
            {
                function ()
                    os.tryrm(sourcedir)
                    if not git.checkurl(url) then
                        os.mkdir(sourcedir)
                        http.download(url, path.join(sourcedir, path.filename(url)))
                    else
                        if version:find('.', 1, true) then
                            git.clone(url, {outputdir = sourcedir})
                            git.checkout(version, {repodir = sourcedir})
                        else
                            git.clone(url, {depth = 1, branch = version, outputdir = sourcedir})
                        end
                    end
                    return true
                end,
                catch 
                {
                    function (errors)
                        vprint(errors)
                    end
                }
            }
            if ok then
                cprint("\r${yellow}  => ${clear}download %s .. ${green}ok", url)
                break
            else
                cprint("\r${yellow}  => ${clear}download %s .. ${red}failed", url)
            end
            if not ok and idx == #mainurls then
                raise("download failed!")
            end
        end
    end

    -- do download 
    if option.get("verbose") then
        download_task()
    else
        process.asyncrun(download_task)
    end

    -- leave environment 
    environment.leave()

    -- do install
    _install(sourcedir, version)
end