summaryrefslogtreecommitdiff
path: root/xmake/plugins/macro/main.lua
blob: cf0929792fce62168b8dcf43c3f9f3d82a5c8f27 (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
--!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-present, TBOOX Open Source Group.
--
-- @author      ruki
-- @file        main.lua
--

-- imports
import("core.base.option")
import("core.project.config")
import("core.cache.localcache")

-- the macro directories
function _directories()

    return {    path.join(config.directory(), "macros")
            ,   path.join(os.scriptdir(), "macros")}
end

-- the macro directory
function _directory(macroname)

    -- find macro directory
    local macrodir = nil
    for _, dir in ipairs(_directories()) do

        -- found?
        if os.isfile(path.join(dir, macroname .. ".lua")) then
            macrodir = dir
            break
        end
    end

    -- check
    assert(macrodir, "macro(%s) not found!", macroname)
    return macrodir
end

-- the readable macro file
function _rfile(macroname)
    if macroname == '.' then
        macroname = "anonymous"
    end
    return path.join(_directory(macroname), macroname .. ".lua")
end

-- the writable macro file
function _wfile(macroname)
    if macroname == '.' then
        macroname = "anonymous"
    end
    return path.join(path.join(config.directory(), "macros"), macroname .. ".lua")
end

-- get all macros
function macros(anonymousname)

    local results = {}
    -- find all macros
    for _, dir in ipairs(_directories()) do
        local macrofiles = os.match(path.join(dir, "*.lua"))
        for _, macrofile in ipairs(macrofiles) do

            -- get macro name
            local macroname = path.basename(macrofile)
            if macroname == "anonymous" and anonymousname then
                macroname = anonymousname
            end
            table.insert(results, macroname)
        end
    end
    return results
end

-- list macros
function _list()
    cprint("${bright}macros:")
    for _, macroname in ipairs(macros(".<anonymous>")) do
        print("    " .. macroname)
    end
end

-- show macro
function _show(macroname)
    local file = _rfile(macroname)
    if os.isfile(file) then
        io.cat(file)
    else
        raise("macro(%s) not found!", macroname)
    end
end

-- clear all macros
function _clear()
    os.rm(path.join(config.directory(), "macros"))
end

-- delete macro
function _delete(macroname)

    -- remove it
    if os.isfile(_wfile(macroname)) then
        os.rm(_wfile(macroname))
    elseif os.isfile(_rfile(macroname)) then
        raise("macro(%s) cannot be deleted!", macroname)
    else
        raise("macro(%s) not found!", macroname)
    end

    -- trace
    cprint("${color.success}delete macro(%s) ok!", macroname)
end

-- import macro
function _import(macrofile, macroname)

    -- import all macros
    if os.isdir(macrofile) then

        -- the macro directory
        local macrodir = macrofile
        local macrofiles = os.match(path.join(macrodir, "*.lua"))
        for _, macrofile in ipairs(macrofiles) do

            -- the macro name
            macroname = path.basename(macrofile)

            -- import it
            os.cp(macrofile, _wfile(macroname))

            -- trace
            cprint("${color.success}import macro(%s) ok!", macroname)
        end
    else

        -- import it
        os.cp(macrofile, _wfile(macroname))

        -- trace
        cprint("${color.success}import macro(%s) ok!", macroname)
    end
end

-- export macro
function _export(macrofile, macroname)

    -- export all macros
    if os.isdir(macrofile) then

        -- the output directory
        local outputdir = macrofile

        -- export all macros
        for _, dir in ipairs(_directories()) do
            local macrofiles = os.match(path.join(dir, "*.lua"))
            for _, macrofile in ipairs(macrofiles) do

                -- export it
                os.cp(macrofile, outputdir)

                -- trace
                cprint("${color.success}export macro(%s) ok!", path.basename(macrofile))
            end
        end
    else
        -- export it
        os.cp(_rfile(macroname), macrofile)

        -- trace
        cprint("${color.success}export macro(%s) ok!", macroname)
    end
end

-- begin to record macro
function _begin()
    localcache.set("history", "cmdlines", "__macro_begin__")
    localcache.save("history")
end

-- end to record macro
function _end(macroname)

    -- load the history: cmdlines
    local cmdlines = table.wrap(localcache.get("history", "cmdlines"))

    -- get the last macro block
    local begin = false
    local block = {}
    local total = #cmdlines
    local index = total
    while index ~= 0 do

        -- the command line
        local cmdline = cmdlines[index]

        -- found begin? break it
        if cmdline == "__macro_begin__" then
            begin = true
            break
        end

        -- found end? break it
        if cmdline == "__macro_end__" then
            break
        end

        -- ignore "xmake m .." and "xmake macro .."
        if not cmdline:find("xmake%s+macro%s*") and not cmdline:find("xmake%s+m%s*") then

            -- save this command line to block
            table.insert(block, 1, cmdline)
        end

        -- the previous line
        index = index - 1
    end

    -- the begin tag not found?
    if not begin then
        raise("please run: 'xmake macro --begin' first!")
    end

    -- patch end tag to the history: cmdlines
    table.insert(cmdlines, "__macro_end__")
    localcache.set("history", "cmdlines", cmdlines)
    localcache.save("history")

    -- open the macro file
    local file = io.open(_wfile(macroname), "w")

    -- save the macro begin
    file:print("function main(argv)")

    -- save the macro block
    for _, cmdline in ipairs(block) do
        file:print("    os.exec(\"%s\")", (cmdline:gsub("[\\\"]", function (w) return "\\" .. w end)))
    end

    -- save the macro end
    file:print("end")

    -- exit the macro file
    file:close()

    -- show this macro
    _show(macroname)

    -- trace
    cprint("${color.success}define macro(%s) ok!", macroname)
end

-- run macro
function _run(macroname)

    -- run last command?
    if macroname == ".." then

        -- load the history: cmdlines
        local cmdlines = localcache.get("history", "cmdlines")

        -- get the last command
        local lastcmd = nil
        if cmdlines then
            local total = #cmdlines
            local index = total
            while index ~= 0 do

                -- ignore "xmake m .." and "xmake macro .."
                local cmdline = cmdlines[index]
                if not cmdline:startswith("__macro_") and not cmdline:find("xmake%s+macro%s*") and not cmdline:find("xmake%s+m%s*") then
                    lastcmd = cmdline
                    break
                end

                -- the previous line
                index = index - 1
            end
        end

        -- run the last command
        if lastcmd then
            os.exec(lastcmd)
        end
        return
    end

    -- is anonymous?
    if macroname == '.' then
        macroname = "anonymous"
    end

    -- run macro
    import(macroname, {rootdir = _directory(macroname), anonymous = true})(option.get("arguments") or {})
end

-- main
function main()

    -- list macros
    if option.get("list") then

        _list()

    -- show macro
    elseif option.get("show") then

        _show(option.get("name"))

    -- clear macro
    elseif option.get("clear") then

        _clear()

    -- delete macro
    elseif option.get("delete") then

        _delete(option.get("name"))

    -- import macro
    elseif option.get("import") then

        _import(option.get("import"), option.get("name"))

    -- export macro
    elseif option.get("export") then

        _export(option.get("export"), option.get("name"))

    -- begin to record macro
    elseif option.get("begin") then

        _begin()

    -- end to record macro
    elseif option.get("end") then

        _end(option.get("name"))

    -- run macro
    else
        _run(option.get("name"))
    end
end