asgard 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.loki +13 -26
- data/.rubocop.yml +157 -0
- data/CHANGELOG.md +89 -11
- data/CLAUDE.md +19 -9
- data/README.md +161 -54
- data/docs/api.md +91 -15
- data/docs/changelog.md +12 -0
- data/docs/dependencies.md +25 -25
- data/docs/environment.md +30 -14
- data/docs/examples.md +3 -3
- data/docs/getting-started.md +5 -6
- data/docs/helpers.md +117 -9
- data/docs/index.md +6 -6
- data/docs/options.md +42 -8
- data/docs/shell.md +11 -11
- data/docs/subcommands.md +9 -9
- data/docs/task-files.md +266 -113
- data/docs/tasks.md +17 -15
- data/docs/variables.md +267 -51
- data/examples/.env +4 -0
- data/examples/.loki +24 -2
- data/examples/concurrent.loki +5 -5
- data/examples/db_subcommands.loki +3 -3
- data/examples/env_usage.loki +27 -0
- data/examples/kitchen_sink.loki +48 -15
- data/examples/server_subcommands.loki +33 -24
- data/examples/subdir/.loki +12 -0
- data/examples/subdir/import_demo.loki +14 -0
- data/examples/subdir/import_up_demo.loki +18 -0
- data/gem_tasks.loki +35 -0
- data/lib/asgard/base.rb +158 -80
- data/lib/asgard/kernel_methods.rb +79 -0
- data/lib/asgard/shell.rb +8 -7
- data/lib/asgard/tasks.rb +12 -17
- data/lib/asgard/version.rb +1 -1
- data/lib/asgard.rb +6 -18
- data/quality.loki +75 -0
- metadata +15 -5
- data/Rakefile +0 -22
data/docs/tasks.md
CHANGED
|
@@ -10,12 +10,12 @@ A task with no parameters and no options:
|
|
|
10
10
|
|
|
11
11
|
```ruby
|
|
12
12
|
class Tasks
|
|
13
|
-
desc "
|
|
14
|
-
def hello =
|
|
13
|
+
desc "Say hello"
|
|
14
|
+
def hello = puts "Hello, World!"
|
|
15
15
|
end
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
`desc`
|
|
18
|
+
`desc` accepts either one or two strings. With one argument, the description is shown in `asgard help` and the usage string defaults to the method name. Pass two arguments when the usage string needs to document parameters — `desc "greet NAME", "Greet NAME by name"`.
|
|
19
19
|
|
|
20
20
|
```bash
|
|
21
21
|
asgard hello
|
|
@@ -31,7 +31,7 @@ Positional parameters are declared directly in the method signature. Document th
|
|
|
31
31
|
class Tasks
|
|
32
32
|
desc "greet NAME", "Greet NAME; omit NAME to greet the world"
|
|
33
33
|
def greet(name = "World")
|
|
34
|
-
|
|
34
|
+
puts "Hello, #{name}!"
|
|
35
35
|
end
|
|
36
36
|
end
|
|
37
37
|
```
|
|
@@ -51,7 +51,7 @@ Use `method_option` (alias: `option`) for named flags. Access them inside the me
|
|
|
51
51
|
|
|
52
52
|
```ruby
|
|
53
53
|
class Tasks
|
|
54
|
-
desc "
|
|
54
|
+
desc "Compile the project"
|
|
55
55
|
option :output, aliases: "-o", type: :string, default: "dist/", desc: "Output directory"
|
|
56
56
|
option :verbose, aliases: "-v", type: :boolean, default: false, desc: "Enable verbose output"
|
|
57
57
|
option :jobs, aliases: "-j", type: :numeric, default: 1, desc: "Number of parallel jobs"
|
|
@@ -117,7 +117,7 @@ asgard deploy production --strategy blue-green
|
|
|
117
117
|
|
|
118
118
|
```ruby
|
|
119
119
|
class Tasks
|
|
120
|
-
desc "
|
|
120
|
+
desc "Build the project"
|
|
121
121
|
option :env,
|
|
122
122
|
type: :string,
|
|
123
123
|
default: "development",
|
|
@@ -139,7 +139,7 @@ Thor validates the value against the enum and shows a helpful error if it doesn'
|
|
|
139
139
|
|
|
140
140
|
```ruby
|
|
141
141
|
class Tasks
|
|
142
|
-
desc "
|
|
142
|
+
desc "Wait for a service to become available"
|
|
143
143
|
option :timeout, type: :numeric, default: 30, banner: "SECONDS", desc: "Give up after SECONDS"
|
|
144
144
|
def wait
|
|
145
145
|
sh "wait-for-it --timeout #{options[:timeout]}"
|
|
@@ -169,7 +169,7 @@ class Tasks
|
|
|
169
169
|
asgard report --format json --output report.json\x5
|
|
170
170
|
asgard report --format text
|
|
171
171
|
DESC
|
|
172
|
-
desc "
|
|
172
|
+
desc "Generate a project report"
|
|
173
173
|
option :format, type: :string, default: "text", enum: %w[text html json], desc: "Output format"
|
|
174
174
|
option :since, type: :string, banner: "DATE", desc: "Limit to changes after DATE"
|
|
175
175
|
def report
|
|
@@ -191,7 +191,7 @@ end
|
|
|
191
191
|
class Tasks
|
|
192
192
|
default_task :greet
|
|
193
193
|
|
|
194
|
-
desc "
|
|
194
|
+
desc "Say hello (runs by default)"
|
|
195
195
|
def greet
|
|
196
196
|
puts "Hello from Asgard!"
|
|
197
197
|
end
|
|
@@ -202,6 +202,8 @@ end
|
|
|
202
202
|
asgard # same as: asgard greet
|
|
203
203
|
```
|
|
204
204
|
|
|
205
|
+
Without `default_task`, running `asgard` with no arguments displays the help message.
|
|
206
|
+
|
|
205
207
|
---
|
|
206
208
|
|
|
207
209
|
## Command Aliases
|
|
@@ -215,13 +217,13 @@ class Tasks
|
|
|
215
217
|
map "t" => "test"
|
|
216
218
|
map "b" => "build"
|
|
217
219
|
|
|
218
|
-
desc "
|
|
220
|
+
desc "Print the version"
|
|
219
221
|
def version = puts Asgard::VERSION
|
|
220
222
|
|
|
221
|
-
desc "
|
|
223
|
+
desc "Run tests"
|
|
222
224
|
def test = sh "bundle exec rake test"
|
|
223
225
|
|
|
224
|
-
desc "
|
|
226
|
+
desc "Build the gem"
|
|
225
227
|
def build = sh "bundle exec rake build"
|
|
226
228
|
end
|
|
227
229
|
```
|
|
@@ -249,14 +251,14 @@ class Tasks
|
|
|
249
251
|
desc: "Name to greet"
|
|
250
252
|
|
|
251
253
|
desc "hello NAME", "Say hello to NAME"
|
|
252
|
-
def hello =
|
|
254
|
+
def hello = puts "Hello, #{name}!"
|
|
253
255
|
end
|
|
254
256
|
```
|
|
255
257
|
|
|
256
258
|
For most multi-task `.loki` files, the simpler positional default pattern is safer:
|
|
257
259
|
|
|
258
260
|
```ruby
|
|
259
|
-
def hello(name = "World") =
|
|
261
|
+
def hello(name = "World") = puts "Hello, #{name}!"
|
|
260
262
|
```
|
|
261
263
|
|
|
262
264
|
---
|
|
@@ -267,7 +269,7 @@ def hello(name = "World") = sh "echo 'Hello, #{name}!'"
|
|
|
267
269
|
|
|
268
270
|
```ruby
|
|
269
271
|
class Tasks
|
|
270
|
-
desc "
|
|
272
|
+
desc "Compile the project"
|
|
271
273
|
def build
|
|
272
274
|
puts "Revision: #{current_sha}"
|
|
273
275
|
sh "rake build"
|
data/docs/variables.md
CHANGED
|
@@ -1,122 +1,338 @@
|
|
|
1
1
|
# Variables
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Asgard task files are plain Ruby. Shared configuration values are declared using Ruby class variables (`@@name`) at the top of the `Tasks` class body.
|
|
4
4
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Ruby Variable Types
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Ruby has four kinds of variables plus constants, each with a distinct prefix and scope. Understanding the differences matters because tasks are instance methods — the wrong variable type will simply not be visible where you expect it.
|
|
10
|
+
|
|
11
|
+
| Kind | Prefix | Example | Scope |
|
|
12
|
+
|------|--------|---------|-------|
|
|
13
|
+
| Local | none | `count = 0` | The method or block it is defined in only |
|
|
14
|
+
| Instance | `@` | `@name = "myapp"` | One specific object instance |
|
|
15
|
+
| Class | `@@` | `@@name = "myapp"` | The class and all its subclasses |
|
|
16
|
+
| Global | `$` | `$DEBUG = true` | Everywhere in the process |
|
|
17
|
+
| Constant | uppercase first letter | `APP = "myapp"` | Everywhere (namespaced to where defined) |
|
|
18
|
+
|
|
19
|
+
### Local variables
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
def build
|
|
23
|
+
output_dir = "dist" # only visible inside this method
|
|
24
|
+
sh "rake build OUTDIR=#{output_dir}"
|
|
25
|
+
end
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`output_dir` disappears when the method returns. It cannot be seen by any other task.
|
|
29
|
+
|
|
30
|
+
### Instance variables (`@`)
|
|
10
31
|
|
|
11
32
|
```ruby
|
|
12
33
|
class Tasks
|
|
13
|
-
|
|
14
|
-
var :env, "production"
|
|
15
|
-
var :port, 3000
|
|
34
|
+
@app = "myapp" # class instance variable — lives on the Tasks class object
|
|
16
35
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
puts "#{app} running on port #{port} in #{env}"
|
|
36
|
+
def build
|
|
37
|
+
puts @app # nil — this @app is on the instance, not the class
|
|
20
38
|
end
|
|
21
39
|
end
|
|
22
40
|
```
|
|
23
41
|
|
|
42
|
+
`@` in the class body sets a variable on the class object itself, not on the instances that run tasks. Inside a task method body, `@name` refers to the instance, which is a different object. They do not share state.
|
|
43
|
+
|
|
44
|
+
`@` inside a method is useful for memoization within a single task invocation:
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
def version
|
|
48
|
+
@version ||= `git describe --tags`.strip # computed once, cached for this run
|
|
49
|
+
end
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Class variables (`@@`)
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
class Tasks
|
|
56
|
+
@@app = "myapp" # visible in every task method and every subclass
|
|
57
|
+
|
|
58
|
+
def build
|
|
59
|
+
puts @@app # "myapp"
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`@@` is shared across the class body, all instance methods, and all subclasses (including Thor subcommand classes). This makes it the right choice for configuration values in Asgard task files.
|
|
65
|
+
|
|
66
|
+
### Global variables (`$`)
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
$DEBUG = true # visible everywhere in the Ruby process
|
|
70
|
+
$VERBOSE = true
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Asgard uses `$DEBUG` and `$VERBOSE` internally — they are set by the `--debug` and `--verbose` CLI flags. Avoid declaring your own `$` variables in `.loki` files; they affect the entire Ruby process including all loaded gems.
|
|
74
|
+
|
|
75
|
+
Several important Ruby globals you may encounter in task files:
|
|
76
|
+
|
|
77
|
+
| Variable | Purpose |
|
|
78
|
+
|----------|---------|
|
|
79
|
+
| `$stdout` / `$STDOUT` | Standard output stream — `puts` writes here |
|
|
80
|
+
| `$stderr` / `$STDERR` | Standard error stream — `warn` writes here |
|
|
81
|
+
| `$DEBUG` | Enables debug mode when `true` |
|
|
82
|
+
| `$VERBOSE` | Enables verbose warnings when `true` |
|
|
83
|
+
| `$PROGRAM_NAME` / `$0` | The name of the running script |
|
|
84
|
+
|
|
85
|
+
The uppercase versions (`$STDOUT`, `$STDERR`) are the original stream objects. The lowercase versions (`$stdout`, `$stderr`) are reassignable aliases — libraries sometimes redirect them temporarily to capture output. In task files, use `$stdout.puts` or `$stderr.puts` when you need explicit stream control; use plain `puts` and `warn` for normal output.
|
|
86
|
+
|
|
87
|
+
### Constants
|
|
88
|
+
|
|
89
|
+
Any name that begins with an uppercase letter is a constant in Ruby. Constants are available everywhere — inside methods, across files, and across classes — without any prefix:
|
|
90
|
+
|
|
91
|
+
```ruby
|
|
92
|
+
APP_NAME = "myapp".freeze
|
|
93
|
+
MAX_RETRIES = 3
|
|
94
|
+
BASE_URL = "https://example.com".freeze
|
|
95
|
+
|
|
96
|
+
class Tasks
|
|
97
|
+
desc "Deploy the app"
|
|
98
|
+
def deploy
|
|
99
|
+
puts "Deploying #{APP_NAME} to #{BASE_URL}"
|
|
100
|
+
sh "cap deploy"
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
The convention is `ALL_CAPS_WITH_UNDERSCORES` for values that are truly fixed. Class and module names are also constants — `Tasks`, `Asgard`, `String`, `Integer` all start with an uppercase letter.
|
|
106
|
+
|
|
107
|
+
Ruby will issue a warning if you reassign a constant but will not prevent it. Use `.freeze` to make the value itself immutable:
|
|
108
|
+
|
|
109
|
+
```ruby
|
|
110
|
+
APP_NAME = "myapp".freeze # value cannot be mutated; reassignment still warns
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
**Constants vs `@@` class variables in task files:**
|
|
114
|
+
|
|
115
|
+
| | Constant | `@@` class variable |
|
|
116
|
+
|---|---|---|
|
|
117
|
+
| Accessible in task methods | Yes | Yes |
|
|
118
|
+
| Accessible in subcommand subclasses | Yes | Yes |
|
|
119
|
+
| Visible outside the class | Yes — anywhere | Only within the class hierarchy |
|
|
120
|
+
| Reassignment warning | Yes | No |
|
|
121
|
+
| Convention | `ALL_CAPS` | `snake_case` |
|
|
122
|
+
|
|
123
|
+
For fixed values that will never change — app names, version strings, URLs, port numbers — constants are often the clearest choice. For values that might reasonably vary across environments or be overridden in a different `.loki` file, `@@` with `||=` is more flexible.
|
|
124
|
+
|
|
24
125
|
---
|
|
25
126
|
|
|
26
|
-
##
|
|
127
|
+
## Strings and Interpolation
|
|
128
|
+
|
|
129
|
+
### Always use double quotes
|
|
27
130
|
|
|
28
|
-
|
|
131
|
+
Ruby supports both single and double quoted strings. In Asgard task files, always use double quotes:
|
|
132
|
+
|
|
133
|
+
```ruby
|
|
134
|
+
@@app ||= "myapp".freeze # correct
|
|
135
|
+
sh "bundle exec rake test" # correct
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Single-quoted strings look similar but behave differently — they do not support interpolation or escape sequences. Mixing the two styles adds confusion for no benefit. Double quotes work everywhere single quotes do, and more.
|
|
139
|
+
|
|
140
|
+
### String interpolation
|
|
141
|
+
|
|
142
|
+
Embedding a variable's value inside a string uses the `#{}` syntax. Everything inside the braces is Ruby code — the result is converted to a string and inserted in place:
|
|
29
143
|
|
|
30
144
|
```ruby
|
|
31
145
|
class Tasks
|
|
32
|
-
|
|
33
|
-
|
|
146
|
+
@@app ||= "myapp".freeze
|
|
147
|
+
@@env ||= "production".freeze
|
|
34
148
|
|
|
35
|
-
desc "
|
|
36
|
-
def
|
|
149
|
+
desc "Deploy the app"
|
|
150
|
+
def deploy
|
|
151
|
+
sh "cap #{@@env} deploy APP=#{@@app}"
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
```
|
|
37
155
|
|
|
38
|
-
|
|
39
|
-
|
|
156
|
+
Any Ruby expression works inside `#{}`:
|
|
157
|
+
|
|
158
|
+
```ruby
|
|
159
|
+
puts "build started at #{Time.now}"
|
|
160
|
+
sh "puma -p #{env(:port, '3000').to_i + 1}"
|
|
161
|
+
sh "git tag #{@@app}-#{`git describe --tags`.strip}"
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Interpolation only works inside double-quoted strings. This is the primary reason Asgard tasks use double quotes exclusively — shell commands almost always need to embed variable values.
|
|
165
|
+
|
|
166
|
+
### Multi-line strings
|
|
167
|
+
|
|
168
|
+
For shell scripts with multiple lines, use a heredoc. The `~` modifier strips leading indentation so the script aligns with your code:
|
|
169
|
+
|
|
170
|
+
```ruby
|
|
171
|
+
desc "Bootstrap the project"
|
|
172
|
+
def bootstrap
|
|
173
|
+
sh <<~SHELL
|
|
174
|
+
bundle install
|
|
175
|
+
rails db:create db:migrate
|
|
176
|
+
echo "#{@@app} ready on #{env(:port, '3000')}"
|
|
177
|
+
SHELL
|
|
40
178
|
end
|
|
41
179
|
```
|
|
42
180
|
|
|
43
|
-
|
|
44
|
-
Lazy lambdas are ideal for values that require a shell call or file read — they only pay the cost if the variable is actually used in the task being run.
|
|
181
|
+
Interpolation works inside heredocs the same as in double-quoted strings.
|
|
45
182
|
|
|
46
183
|
---
|
|
47
184
|
|
|
48
|
-
##
|
|
185
|
+
## System Environment Variables
|
|
49
186
|
|
|
50
|
-
|
|
187
|
+
System environment variables are set outside Ruby — in the shell, a CI environment, or a `.env` file — and are accessed inside tasks via the `env` Kernel method:
|
|
51
188
|
|
|
52
189
|
```ruby
|
|
53
190
|
class Tasks
|
|
54
|
-
|
|
55
|
-
|
|
191
|
+
desc "Start the server"
|
|
192
|
+
def start
|
|
193
|
+
sh "puma -p #{env(:port, '3000')} -e #{env(:rack_env, 'development')}"
|
|
194
|
+
end
|
|
56
195
|
|
|
57
|
-
desc "
|
|
58
|
-
def
|
|
196
|
+
desc "Deploy the app"
|
|
197
|
+
def deploy
|
|
198
|
+
sh "cap #{env(:deploy_target)} deploy" # raises KeyError if DEPLOY_TARGET is not set
|
|
199
|
+
end
|
|
59
200
|
end
|
|
60
201
|
```
|
|
61
202
|
|
|
203
|
+
`env` accepts a symbol or string and converts it to an uppercase `ENV` key automatically:
|
|
204
|
+
|
|
205
|
+
| Call | Equivalent | Behaviour |
|
|
206
|
+
|------|-----------|-----------|
|
|
207
|
+
| `env(:port, "3000")` | `ENV.fetch("PORT", "3000")` | Returns `"3000"` if `PORT` is unset |
|
|
208
|
+
| `env(:api_key)` | `ENV.fetch("API_KEY")` | Raises `KeyError` if `API_KEY` is unset |
|
|
209
|
+
| `env("DATABASE_URL")` | `ENV.fetch("DATABASE_URL")` | Raises `KeyError` if unset |
|
|
210
|
+
| `env("database_url")` | `ENV.fetch("DATABASE_URL")` | Same — name is always upcased |
|
|
211
|
+
|
|
62
212
|
!!! note
|
|
63
|
-
|
|
213
|
+
All environment variable values are strings. Convert to other types explicitly: `env(:port, "3000").to_i`, `env(:debug, "false") == "true"`.
|
|
214
|
+
|
|
215
|
+
Use `dotenv` to load a `.env` file before tasks run — see [Environment](environment.md).
|
|
64
216
|
|
|
65
217
|
---
|
|
66
218
|
|
|
67
|
-
##
|
|
219
|
+
## The Pattern
|
|
68
220
|
|
|
69
|
-
|
|
221
|
+
```ruby
|
|
222
|
+
class Tasks
|
|
223
|
+
@@app ||= "myapp".freeze
|
|
224
|
+
@@port ||= 3000
|
|
225
|
+
@@env ||= "production".freeze
|
|
226
|
+
|
|
227
|
+
desc "Print app info"
|
|
228
|
+
def info
|
|
229
|
+
puts "#{@@app} running on port #{@@port} in #{@@env}"
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
**Why `||=` instead of `=`?**
|
|
235
|
+
Because multiple `.loki` files reopen the same `class Tasks`. Using `||=` means the first file to declare a value wins, and subsequent files that reopen `Tasks` won't accidentally overwrite it.
|
|
236
|
+
|
|
237
|
+
**Why `.freeze`?**
|
|
238
|
+
It prevents mutation of the value (e.g. `@@app << "-extra"` raises a `FrozenError`). Numbers and symbols are already frozen. Use `.freeze` on strings, arrays, and hashes.
|
|
239
|
+
|
|
240
|
+
**Why `@@` instead of `@`?**
|
|
241
|
+
A single `@` in the class body sets a class instance variable — it lives on the `Tasks` class object and is **not** accessible inside task method bodies. `@@` is a class variable and is visible everywhere: in all instance methods, and in any subclass (including Thor subcommand classes).
|
|
242
|
+
|
|
243
|
+
---
|
|
244
|
+
|
|
245
|
+
## Sharing Values Across Subcommands
|
|
246
|
+
|
|
247
|
+
Class variables are visible in subclasses, which makes them the right choice when you have Thor subcommands defined in separate classes:
|
|
70
248
|
|
|
71
249
|
```ruby
|
|
250
|
+
# config.loki
|
|
72
251
|
class Tasks
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
var :pkg, -> { "pkg/#{app}-#{version}.gem" }
|
|
252
|
+
@@app ||= "myapp".freeze
|
|
253
|
+
end
|
|
76
254
|
|
|
77
|
-
|
|
78
|
-
|
|
255
|
+
# deploy.loki
|
|
256
|
+
class DeployCommands < Tasks
|
|
257
|
+
desc "Deploy to production"
|
|
258
|
+
def production
|
|
259
|
+
sh "cap production deploy APP=#{@@app}" # @@app is visible here
|
|
260
|
+
end
|
|
261
|
+
end
|
|
79
262
|
|
|
80
|
-
|
|
81
|
-
|
|
263
|
+
class Tasks
|
|
264
|
+
desc "deploy SUBCOMMAND", "Deployment tasks"
|
|
265
|
+
subcommand "deploy", DeployCommands
|
|
82
266
|
end
|
|
83
267
|
```
|
|
84
268
|
|
|
85
|
-
|
|
269
|
+
---
|
|
270
|
+
|
|
271
|
+
## Computed Values
|
|
272
|
+
|
|
273
|
+
For values that require a shell call, file read, or any runtime computation, define a method instead:
|
|
274
|
+
|
|
275
|
+
```ruby
|
|
276
|
+
class Tasks
|
|
277
|
+
def version = `git describe --tags`.strip
|
|
278
|
+
def sha = `git rev-parse --short HEAD`.strip
|
|
279
|
+
|
|
280
|
+
desc "Show version info"
|
|
281
|
+
def info = puts "#{version} (#{sha})"
|
|
282
|
+
end
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
Methods defined without `desc` do not appear in `--help` output or as CLI commands. If you need memoization (the computation is expensive and called multiple times), use `||=` on an instance variable inside the method:
|
|
286
|
+
|
|
287
|
+
```ruby
|
|
288
|
+
class Tasks
|
|
289
|
+
def version
|
|
290
|
+
@version ||= `git describe --tags`.strip
|
|
291
|
+
end
|
|
292
|
+
end
|
|
293
|
+
```
|
|
86
294
|
|
|
87
295
|
---
|
|
88
296
|
|
|
89
|
-
## Sharing
|
|
297
|
+
## Sharing Values Across Files
|
|
90
298
|
|
|
91
|
-
Because all `.loki` files reopen the same `class Tasks`,
|
|
299
|
+
Because all `.loki` files reopen the same `class Tasks`, a `@@` variable declared in one file is available in all other files loaded in the same session:
|
|
92
300
|
|
|
93
301
|
```ruby
|
|
94
302
|
# config.loki
|
|
95
303
|
class Tasks
|
|
96
|
-
|
|
97
|
-
|
|
304
|
+
@@app ||= "myapp".freeze
|
|
305
|
+
@@port ||= 8080
|
|
98
306
|
end
|
|
99
307
|
|
|
100
308
|
# deploy.loki
|
|
101
309
|
class Tasks
|
|
102
|
-
desc "
|
|
103
|
-
def deploy = sh "cap deploy APP=#{app} PORT=#{port}"
|
|
310
|
+
desc "Deploy the app"
|
|
311
|
+
def deploy = sh "cap deploy APP=#{@@app} PORT=#{@@port}"
|
|
104
312
|
end
|
|
105
313
|
```
|
|
106
314
|
|
|
107
315
|
---
|
|
108
316
|
|
|
109
|
-
## Naming
|
|
317
|
+
## Naming Conventions
|
|
110
318
|
|
|
111
|
-
|
|
112
|
-
Do not use `var` names that conflict with built-in Ruby method names, Thor DSL method names, or Asgard's own built-in methods. In particular, avoid naming a variable `version` — `Tasks` already defines `_version` (the `--version` flag handler), and a `var :version` would collide with that namespace and produce confusing behavior. Use a more specific name like `app_version` or `gem_version` instead.
|
|
319
|
+
Class variable names use `snake_case` — the standard Ruby convention for variables and methods:
|
|
113
320
|
|
|
114
321
|
```ruby
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
322
|
+
class Tasks
|
|
323
|
+
@@app_name ||= "myapp".freeze
|
|
324
|
+
@@deploy_host ||= "production.example.com".freeze
|
|
325
|
+
@@max_workers ||= 4
|
|
326
|
+
end
|
|
120
327
|
```
|
|
121
328
|
|
|
122
|
-
|
|
329
|
+
Multi-word names are separated by underscores, not camelCase or hyphens.
|
|
330
|
+
|
|
331
|
+
---
|
|
332
|
+
|
|
333
|
+
## Naming Caution
|
|
334
|
+
|
|
335
|
+
!!! warning
|
|
336
|
+
Avoid `@@` names that conflict with built-in Ruby or Thor internals. Safe practice: use descriptive names that are unlikely to clash.
|
|
337
|
+
|
|
338
|
+
Names to avoid: `options`, `shell`, `invoke`, `command`, `args`.
|
data/examples/.env
ADDED
data/examples/.loki
CHANGED
|
@@ -1,2 +1,24 @@
|
|
|
1
|
-
#
|
|
2
|
-
#
|
|
1
|
+
# examples/.loki — root task file for the examples directory.
|
|
2
|
+
#
|
|
3
|
+
# Asgard loads only this file by default. Everything else must be
|
|
4
|
+
# explicitly pulled in with import.
|
|
5
|
+
#
|
|
6
|
+
# import(path)
|
|
7
|
+
# Load a specific file or a glob of files, idempotently. Absolute
|
|
8
|
+
# paths, relative paths, and globs are all accepted.
|
|
9
|
+
#
|
|
10
|
+
# import_up(name)
|
|
11
|
+
# Walk CWD and every ancestor directory looking for `name`, then
|
|
12
|
+
# import it. Useful inside a nested task file that needs to pull in
|
|
13
|
+
# a shared config from the project root without knowing how deep it is.
|
|
14
|
+
#
|
|
15
|
+
# loki_up(name)
|
|
16
|
+
# Same walk as import_up but just returns the absolute path (or nil)
|
|
17
|
+
# without loading it — handy when you need the path for another purpose.
|
|
18
|
+
|
|
19
|
+
# Load all *.loki files in this directory (kitchen_sink, concurrent, etc.)
|
|
20
|
+
import "*.loki"
|
|
21
|
+
|
|
22
|
+
# Load a task file from a subdirectory.
|
|
23
|
+
# import also accepts a direct path, not just a glob.
|
|
24
|
+
import "subdir/import_demo.loki"
|
data/examples/concurrent.loki
CHANGED
|
@@ -21,12 +21,12 @@ $stdout.sync = true # flush every print immediately across all threads
|
|
|
21
21
|
CONCURRENT_REPS = 10 # how many times each worker prints its character
|
|
22
22
|
|
|
23
23
|
class Tasks
|
|
24
|
-
desc "
|
|
24
|
+
desc "Print start marker"
|
|
25
25
|
def start
|
|
26
26
|
puts "starting demo of concurrent task execution ..."
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
desc "
|
|
29
|
+
desc "Print 'A' repeatedly with random delays"
|
|
30
30
|
def worker_a
|
|
31
31
|
CONCURRENT_REPS.times do
|
|
32
32
|
print "A"
|
|
@@ -34,7 +34,7 @@ class Tasks
|
|
|
34
34
|
end
|
|
35
35
|
end
|
|
36
36
|
|
|
37
|
-
desc "
|
|
37
|
+
desc "Print 'B' repeatedly with random delays"
|
|
38
38
|
def worker_b
|
|
39
39
|
CONCURRENT_REPS.times do
|
|
40
40
|
print "B"
|
|
@@ -42,7 +42,7 @@ class Tasks
|
|
|
42
42
|
end
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
-
desc "
|
|
45
|
+
desc "Print 'C' repeatedly with random delays"
|
|
46
46
|
def worker_c
|
|
47
47
|
CONCURRENT_REPS.times do
|
|
48
48
|
print "C"
|
|
@@ -51,7 +51,7 @@ class Tasks
|
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
depends_on :start, [:worker_a, :worker_b, :worker_c]
|
|
54
|
-
desc "
|
|
54
|
+
desc "Print end marker after all workers complete"
|
|
55
55
|
def finish
|
|
56
56
|
puts "\nfini - the end of concurrent task demo"
|
|
57
57
|
end
|
|
@@ -39,7 +39,7 @@ class DBCommands < Tasks
|
|
|
39
39
|
# depends_on chains within the subcommand group:
|
|
40
40
|
# rollback → migrate → seed → reset
|
|
41
41
|
depends_on :rollback, :migrate, :seed
|
|
42
|
-
desc "
|
|
42
|
+
desc "Rollback all migrations, re-migrate, and reseed"
|
|
43
43
|
def reset
|
|
44
44
|
puts "Database reset complete."
|
|
45
45
|
end
|
|
@@ -54,7 +54,7 @@ class DBCommands < Tasks
|
|
|
54
54
|
asgard db console\x5
|
|
55
55
|
asgard db console --env staging
|
|
56
56
|
DESC
|
|
57
|
-
desc "
|
|
57
|
+
desc "Open an interactive database console"
|
|
58
58
|
option :env, type: :string, default: "development",
|
|
59
59
|
enum: %w[development staging production],
|
|
60
60
|
desc: "Environment to connect to"
|
|
@@ -62,7 +62,7 @@ class DBCommands < Tasks
|
|
|
62
62
|
puts "Opening #{options[:env]} database console..."
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
-
desc "
|
|
65
|
+
desc "Show applied and pending migrations"
|
|
66
66
|
def status
|
|
67
67
|
puts "Checking migration status..."
|
|
68
68
|
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# Demonstrates the env() Kernel helper for reading system environment variables.
|
|
3
|
+
#
|
|
4
|
+
# env() is cleaner than ENV[] — it accepts symbols or strings, upcases
|
|
5
|
+
# automatically, and raises KeyError when a required variable is missing
|
|
6
|
+
# rather than silently returning nil.
|
|
7
|
+
#
|
|
8
|
+
# Run from examples/ or any subdirectory — loki_up locates .env automatically:
|
|
9
|
+
# asgard env_demo
|
|
10
|
+
|
|
11
|
+
class Tasks
|
|
12
|
+
# loki_up walks CWD and every ancestor until it finds .env, returning
|
|
13
|
+
# its absolute path. This works regardless of which directory asgard
|
|
14
|
+
# is run from — no hardcoded relative path needed.
|
|
15
|
+
dotenv loki_up(".env") || ".env"
|
|
16
|
+
|
|
17
|
+
desc "Print environment variable values resolved via env()"
|
|
18
|
+
def env_demo
|
|
19
|
+
puts <<~OUT
|
|
20
|
+
APP_NAME : #{env(:app_name)}
|
|
21
|
+
PORT : #{env(:port)}
|
|
22
|
+
API_KEY : #{env(:api_key)}
|
|
23
|
+
DATABASE_URL : #{env(:database_url)}
|
|
24
|
+
LOG_LEVEL : #{env(:log_level, "info")}
|
|
25
|
+
OUT
|
|
26
|
+
end
|
|
27
|
+
end
|