summaryrefslogtreecommitdiff
path: root/xmake/plugins/macro/main.lua
blob: 3de5ca5986ac7a4ae35fc0d2757aade19a751770 (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
--!The Automatic Cross-platform Build Tool
-- 
-- XMake is free software; you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
-- 
-- XMake is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Lesser General Public License for more details.
-- 
-- You should have received a copy of the GNU Lesser General Public License
-- along with XMake; 
-- If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
-- 
-- Copyright (C) 2015 - 2016, ruki All rights reserved.
--
-- @author      ruki
-- @file        main.lua
--

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

-- the macro directory
function _directory()

    -- get it
    return path.join(config.directory(), "macros")
end

-- the macro file 
function _file(macroname)

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

    -- get it
    return path.join(_directory(), macroname .. ".lua")
end

-- list macros
function _list()

    -- trace
    print("macros:")

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

        -- get macro name
        local macroname = path.basename(macrofile)
        if macroname == "anonymous" then
            macroname = ".<anonymous>"
        end

        -- show it
        print("    " .. macroname)
    end
end

-- show macro
function _show(macroname)

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

-- delete macro
function _delete(macroname)

    -- remove it
    os.rm(_file(macroname))

    -- trace
    print("delete macro(%s) ok!", macroname)
end

-- import macro
function _import(macrofile, macroname)

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

    -- trace
    print("import macro(%s) ok!", macroname)
end

-- export macro
function _export(macrofile, macroname)

    -- export it
    os.cp(_file(macroname), macrofile)

    -- trace
    print("export macro(%s) ok!", macroname)
end

-- begin to record macro
function _begin()

    -- patch begin tag to the history: cmdlines
    history.save("cmdlines", "__macro_begin__")
end

-- end to record macro
function _end(macroname)

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

    -- get the last macro block
    local begin = false
    local block = {}
    if cmdlines then
        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
    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
    history.save("cmdlines", "__macro_end__")

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

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

    -- save the macro block
    for _, cmdline in ipairs(block) do

        -- save command line
        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
    print("define macro(%s) ok!", macroname)
end

-- run macro
function _run(macroname)

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

    -- import macro
    macro = import(macroname, {rootdir = _directory()})

    -- run macro
    macro.main(option.get("arguments"))

    -- trace
    print("run macro(%s) ok!", macroname)
end

-- main
function main()

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

        _list()

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

        _show(option.get("name"))

    -- 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