summaryrefslogtreecommitdiff
path: root/xmake/core/base/deprecated/interpreter.lua
blob: d3935600196fb68842766add466d941db02e9d3b (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
--!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        deprecated_interpreter.lua
--

-- define module: deprecated_interpreter
local deprecated_interpreter = deprecated_interpreter or {}

-- load modules
local os            = require("base/os")
local path          = require("base/path")
local table         = require("base/table")
local utils         = require("base/utils")
local string        = require("base/string")
local deprecated    = require("base/deprecated")
local sandbox       = require("sandbox/sandbox")

-- register api for set_scope()
function deprecated_interpreter:api_register_set_scope(...)

    -- check
    assert(self)

    -- define implementation
    local implementation = function (self, scopes, scope_kind, scope_name)

        -- init scope for kind
        local scope_for_kind = scopes[scope_kind] or {}
        scopes[scope_kind] = scope_for_kind

        -- deprecated
        if not scope_name:startswith("__") then
            deprecated.add("%s(\"%s\")", "set_%s(\"%s\")", scope_kind, scope_name)
        end

        -- check 
        if not scope_for_kind[scope_name] then
            utils.error("set_%s(\"%s\") failed, %s not found!", scope_kind, scope_name, scope_name)
            os.raise("please uses add_%s(\"%s\") first!", scope_kind, scope_name)
        end

        -- init scope for name
        scope_for_kind[scope_name] = scope_for_kind[scope_name] or {}

        -- save the current scope
        scopes._CURRENT = scope_for_kind[scope_name]

        -- update the current scope kind
        scopes._CURRENT_KIND = scope_kind

    end

    -- register implementation
    self:_api_register_scope_api(nil, "set", implementation, ...)
end

-- register api for add_scope()
function deprecated_interpreter:api_register_add_scope(...)

    -- check
    assert(self)

    -- define implementation
    local implementation = function (self, scopes, scope_kind, scope_name)

        -- init scope for kind
        local scope_for_kind = scopes[scope_kind] or {}
        scopes[scope_kind] = scope_for_kind

        -- deprecated
        if not scope_name:startswith("__") then
            deprecated.add("%s(\"%s\")", "add_%s(\"%s\")", scope_kind, scope_name)
        end

        -- check 
        if scope_for_kind[scope_name] then
            utils.error("add_%s(\"%s\") failed, %s have been defined!", scope_kind, scope_name, scope_name)
            os.raise("please uses set_%s(\"%s\")!", scope_kind, scope_name)
        end

        -- init scope for name
        scope_for_kind[scope_name] = scope_for_kind[scope_name] or {}

        -- save the current scope
        scopes._CURRENT = scope_for_kind[scope_name]

        -- update the current scope kind
        scopes._CURRENT_KIND = scope_kind

    end

    -- register implementation
    self:_api_register_scope_api(nil, "add", implementation, ...)
end

-- register api for set_script
function deprecated_interpreter:api_register_set_script(scope_kind, ...)

    -- check
    assert(self)

    -- define implementation
    local implementation = function (self, scope, name, script)

        -- deprecated
        deprecated.add("on_%s()", "set_%s()", name)

        -- make sandbox instance with the given script
        local instance, errors = sandbox.new(script, self:filter(), self:rootdir())
        if not instance then
            os.raise("set_%s(): %s", name, errors)
        end

        -- update script?
        scope[name] = {}
        table.insert(scope[name], instance:script())

    end

    -- register implementation
    self:_api_register_xxx_values(scope_kind, "set", implementation, ...)
end

-- register api: set_xxx_xxx
function deprecated_interpreter:_api_register_set_xxx_xxx(scope_kind, apiname)

    -- the old api
    local oldapi = string.format("set_%s_%s", scope_kind, apiname)

    -- the new api
    local newapi = string.format("set_%s", apiname)

    -- get api function
    local apifunc = self:_api_within_scope(scope_kind, newapi)
    assert(apifunc)

    -- register api
    self:api_register_builtin(oldapi, function (value, ...) 

                                        -- deprecated
                                        deprecated.add(newapi .. "(\"%s\")", oldapi .. "(\"%s\")", tostring(value))
                                      
                                        -- dispatch it
                                        apifunc(value, ...)
                                    end)
end

-- register api: add_xxx_xxx
function deprecated_interpreter:_api_register_add_xxx_xxx(scope_kind, apiname)

    -- the old api
    local oldapi = string.format("add_%s_%s", scope_kind, apiname)

    -- the new api
    local newapi = string.format("add_%s", apiname)

    -- get api function
    local apifunc = self:_api_within_scope(scope_kind, newapi)
    assert(apifunc)

    -- register api
    self:api_register_builtin(oldapi, function (value, ...) 

                                        -- deprecated
                                        deprecated.add(newapi .. "(\"%s\")", oldapi .. "(\"%s\")", tostring(value))
                                      
                                        -- dispatch it
                                        apifunc(value, ...)
                                    end)
end

-- return module: deprecated_interpreter
return deprecated_interpreter