summaryrefslogtreecommitdiff
path: root/xmake/actions/require/impl/action/patch_sources.lua
blob: 55c21abf3ab44382d3bbe5e565b4016517585d40 (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
--!A cross-platform build utility based on Lua
--
-- Licensed 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-2020, TBOOX Open Source Group.
--
-- @author      ruki
-- @file        patch_sources.lua
--

-- imports
import("core.base.option")
import("net.http")
import("devel.git")

-- do patch
function _patch(package, patch_url, patch_hash)

    -- trace
    vprint("patching %s to %s-%s ..", patch_url, package:name(), package:version_str())
 
    -- get the patch file
    local patch_file = path.join(os.tmpdir(), "patches", package:name(), package:version_str(), (path.filename(patch_url):gsub("%?.+$", "")))

    -- the package file have been downloaded?
    local cached = true
    if option.get("force") or not os.isfile(patch_file) or patch_hash ~= hash.sha256(patch_file) then

        -- no cached
        cached = false

        -- attempt to remove the previous file first
        os.tryrm(patch_file)

        -- download the patch file
        if patch_url:find(string.ipattern("https-://")) or patch_url:find(string.ipattern("ftps-://")) then
            http.download(patch_url, patch_file)
        else
            -- copy the patch file
            if os.isfile(patch_url) then
                os.cp(patch_url, patch_file)
            else
                local scriptdir = package:scriptdir()
                if scriptdir and os.isfile(path.join(scriptdir, patch_url)) then
                    os.cp(path.join(scriptdir, patch_url), patch_file)
                else
                    raise("patch(%s): not found!", patch_url)
                end
            end
        end

        -- check hash
        if patch_hash and patch_hash ~= hash.sha256(patch_file) then
            raise("patch(%s): unmatched checksum!", patch_url)
        end
    end

    -- apply the patch file
    git.apply(patch_file)
end

-- patch the given package
function main(package)

    -- no patches?
    local patches = package:patches()
    if not patches then
        return
    end

    -- do all patches
    for _, patchinfo in ipairs(patches) do
        _patch(package, patchinfo.url, patchinfo.sha256)
    end
end