-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bladefile.example
172 lines (138 loc) · 4.55 KB
/
Bladefile.example
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
sh = require("sh")
-- The setup function is run before any other target
function blade.setup(target)
if target == "setup" then
print("setup: Executed before target")
end
end
-- The teardown function is run after the blade target
function blade.teardown(target)
if target ~= "setup" then return end
print("teardown: executed after target")
end
-- utilize setup and teardown functionality
function target.setup()
print("Example on using setup/teardown")
end
-- By defining a blade.default function we can run an action if the
-- no target was defined on the command line.
-- Without specifying a blade.default function a help message will be displayed
-- display HOME environment variable
function blade.default()
print("Example on overriding `blade.default`")
end
-- read the HOME environment variable
function target.environment()
print("========= ENV =========")
print("HOME: ", os.getenv("HOME"))
print("=======================")
end
-- Define a "watch" target and setup the watcher
-- callback can be a closure or a reference to a function
-- dir is a file or directory
-- recursivly watch files (default false)
-- filter is a regexp for files to include (default all files)
-- exclude is a table with directories to not include when recursivly watching files
--
-- excluding large directories can greatly improve speed
-- NOTE: The regexp in filter is a Go regexp and not Lua
-- watch txt files in .
function target.watch()
blade.plugin.watch{callback=notExported, dir=".", recursive=false, filter=".*\\.txt", exclude={"project"}}
end
-- To showcase how to use a function reference in the watcher, see target.watch
function notExported(file, op)
print("Handle watched call: " .. file .. " (" .. op .. ")")
end
-- Define a Hello world target with a input from the command line
function target.hello(use)
use = use or "lua"
if use == "shell" then
sh.echo("hello world"):print()
elseif use == "lua" then
print("hello world")
else
print("unknown option: " .. use)
end
end
-- Adding a bash completion lookup function for the target
blade.compgen(target.hello, function(compWords, compCWords)
return compCWords == 1 and "lua shell" or ""
end)
-- Adding a help message to the "hello" target, overriding the comment on the target function
blade.help(target.hello, "<lua|shell>")
-- showcase how to read on all command line parameters
function target.glob(...)
print("Number of inputs: ", arg.n)
for k, v in ipairs(arg) do
print(k, "=", v)
end
end
-- showcase the printStatus functionality
function target.printStatus()
-- output command status
blade.printStatus("true", true)
blade.printStatus("false", false)
blade.printStatus("0", 0)
blade.printStatus("1", 1)
blade.printStatus("nil")
blade.printStatus("true (shell)", sh("true"):success())
blade.printStatus("false (shell)", sh("false"):success())
end
-- showcase automatic command abort when shell command returns non-zero exit code
function target.fail()
sh.echo("Commands that execute successfully are ok"):print():ok()
sh.echo("However, the script will abort if we execute a command that fails")
sh("false"):ok()
sh.echo("So this is never printend"):print()
end
function target.foo()
-- capture exit status and command output
exitStatus, out = blade.sh("echo 'Blade runner'")
print("Exit: " .. exitStatus .. ", Output: ".. out)
-- no echo of running command
blade._sh("echo 'no cmd echo'")
-- do not break on command error
blade.exec("false")
-- automaticly break on errors
blade.sh("date -r")
-- this statement is unreachable due to error on previus statement
blade.sh([[echo "this should not run"]])
end
-- files lists all Go files in the directory
function target.files()
-- list with grep (globing files does not work)
for file in sh.ls():grep(".go"):lines() do
print("File:", file)
end
print("\n-------\n")
-- however we can evaluate it in the shell
for file in sh.sh("-c", "ls *.go"):lines() do
print("File:", file)
end
end
-- showcase how to split strings
function target.split()
out = "first\nsecond"
print("Split line with iterator:")
for line in out:split("\n") do
print("i", line)
end
print("\nSplit line with callback:")
out:split("\n", function(line)
print("cb", line)
end)
-- this can be done on commands directly
print("\nIterate lines on shell command:")
for line in sh.ls():lines() do
print("sh", line)
end
end
-- iterate columns, execute ps and returns CMD
function target.columns()
-- this can be done on commands directly
print("\nIterate lines and column on shell command:")
for line in sh.ps():lines() do
print(line:c(4))
end
end