asgard 0.1.1 → 0.2.0

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.
data/docs/tasks.md ADDED
@@ -0,0 +1,284 @@
1
+ # Defining Tasks
2
+
3
+ Every task is a public method inside `class Tasks`. Asgard pre-defines `Tasks` as a subclass of `Asgard::Base` (which is itself a Thor subclass), so your `.loki` files just reopen the class and add methods. The full Thor DSL is available everywhere.
4
+
5
+ ---
6
+
7
+ ## Basic Task
8
+
9
+ A task with no parameters and no options:
10
+
11
+ ```ruby
12
+ class Tasks
13
+ desc "hello", "Say hello"
14
+ def hello = sh 'echo "Hello, World!"'
15
+ end
16
+ ```
17
+
18
+ `desc` takes two arguments: the usage string and the one-line description shown in `asgard help`.
19
+
20
+ ```bash
21
+ asgard hello
22
+ ```
23
+
24
+ ---
25
+
26
+ ## Positional Parameter with Default
27
+
28
+ Positional parameters are declared directly in the method signature. Document them in the `desc` usage string (uppercase by convention):
29
+
30
+ ```ruby
31
+ class Tasks
32
+ desc "greet NAME", "Greet NAME; omit NAME to greet the world"
33
+ def greet(name = "World")
34
+ sh "echo 'Hello, #{name}!'"
35
+ end
36
+ end
37
+ ```
38
+
39
+ ```bash
40
+ asgard greet # Hello, World!
41
+ asgard greet Alice # Hello, Alice!
42
+ ```
43
+
44
+ ---
45
+
46
+ ## Named Options
47
+
48
+ Use `method_option` (alias: `option`) for named flags. Access them inside the method via `options[:name]`.
49
+
50
+ ### All Five Option Types
51
+
52
+ ```ruby
53
+ class Tasks
54
+ desc "compile", "Compile the project"
55
+ option :output, aliases: "-o", type: :string, default: "dist/", desc: "Output directory"
56
+ option :verbose, aliases: "-v", type: :boolean, default: false, desc: "Enable verbose output"
57
+ option :jobs, aliases: "-j", type: :numeric, default: 1, desc: "Number of parallel jobs"
58
+ option :tags, type: :array, desc: "Build tags to apply"
59
+ option :defines, type: :hash, desc: "Preprocessor defines (KEY:VALUE)"
60
+ def compile
61
+ puts "Compiling → #{options[:output]} with #{options[:jobs]} job(s)"
62
+ puts "Tags: #{options[:tags].join(', ')}" if options[:tags]
63
+ puts "Defines: #{options[:defines]}" if options[:defines]
64
+ end
65
+ end
66
+ ```
67
+
68
+ ### Option Types Reference
69
+
70
+ | Type | CLI Example | Ruby Value |
71
+ |---|---|---|
72
+ | `:string` | `--output dist/` | `"dist/"` |
73
+ | `:boolean` | `--verbose` / `--no-verbose` | `true` / `false` |
74
+ | `:numeric` | `--jobs 4` | `4` |
75
+ | `:array` | `--tags foo bar baz` | `["foo", "bar", "baz"]` |
76
+ | `:hash` | `--defines KEY:val FOO:bar` | `{"KEY"=>"val", "FOO"=>"bar"}` |
77
+
78
+ ### Common Option Keys
79
+
80
+ | Key | Description |
81
+ |---|---|
82
+ | `aliases` | Short-form flag, e.g. `"-o"` |
83
+ | `type` | `:string`, `:boolean`, `:numeric`, `:array`, or `:hash` |
84
+ | `default` | Value used when the flag is omitted |
85
+ | `required` | If `true`, Thor raises an error when the flag is missing |
86
+ | `desc` | One-line description shown in help |
87
+ | `enum` | Array of allowed values; Thor validates automatically |
88
+ | `banner` | Placeholder shown in help for the value slot, e.g. `"SECONDS"` |
89
+
90
+ ---
91
+
92
+ ## Required Option
93
+
94
+ ```ruby
95
+ class Tasks
96
+ desc "deploy ENV", "Deploy to ENV"
97
+ option :strategy,
98
+ type: :string,
99
+ required: true,
100
+ enum: %w[blue-green rolling canary],
101
+ desc: "Deployment strategy"
102
+ def deploy(env = "staging")
103
+ sh "cap #{env} deploy --strategy #{options[:strategy]}"
104
+ end
105
+ end
106
+ ```
107
+
108
+ ```bash
109
+ asgard deploy # Error: required option '--strategy' is missing
110
+ asgard deploy --strategy rolling
111
+ asgard deploy production --strategy blue-green
112
+ ```
113
+
114
+ ---
115
+
116
+ ## Enum Validation
117
+
118
+ ```ruby
119
+ class Tasks
120
+ desc "build", "Build the project"
121
+ option :env,
122
+ type: :string,
123
+ default: "development",
124
+ enum: %w[development staging production],
125
+ desc: "Target environment"
126
+ def build
127
+ sh "rake build ENV=#{options[:env]}"
128
+ end
129
+ end
130
+ ```
131
+
132
+ Thor validates the value against the enum and shows a helpful error if it doesn't match.
133
+
134
+ ---
135
+
136
+ ## Banner
137
+
138
+ `banner` replaces the default `VALUE` placeholder in help output with a more descriptive name:
139
+
140
+ ```ruby
141
+ class Tasks
142
+ desc "wait", "Wait for a service to become available"
143
+ option :timeout, type: :numeric, default: 30, banner: "SECONDS", desc: "Give up after SECONDS"
144
+ def wait
145
+ sh "wait-for-it --timeout #{options[:timeout]}"
146
+ end
147
+ end
148
+ ```
149
+
150
+ Help output shows: `[--timeout=SECONDS]` instead of `[--timeout=VALUE]`.
151
+
152
+ ---
153
+
154
+ ## Extended Description
155
+
156
+ `long_desc` provides detailed help shown by `asgard help <task>`. Use `\x5` at the start of a line to force a line break within the wrapped text (a Thor convention):
157
+
158
+ ```ruby
159
+ class Tasks
160
+ long_desc <<~DESC
161
+ Generates a project report covering test coverage, lint results,
162
+ and a dependency audit.
163
+
164
+ Pass --format to control output style. Use --since to scope the
165
+ report to changes after a given date.
166
+
167
+ Examples:\x5
168
+ asgard report --format html --since 2024-01-01\x5
169
+ asgard report --format json --output report.json\x5
170
+ asgard report --format text
171
+ DESC
172
+ desc "report", "Generate a project report"
173
+ option :format, type: :string, default: "text", enum: %w[text html json], desc: "Output format"
174
+ option :since, type: :string, banner: "DATE", desc: "Limit to changes after DATE"
175
+ def report
176
+ sh "generate-report --format #{options[:format]}"
177
+ end
178
+ end
179
+ ```
180
+
181
+ !!! tip
182
+ `desc` and `depends_on` are independent of each other — either can come first, but both must appear before the `def`.
183
+
184
+ ---
185
+
186
+ ## Default Task
187
+
188
+ `default_task` declares which command runs when `asgard` is invoked with no arguments:
189
+
190
+ ```ruby
191
+ class Tasks
192
+ default_task :greet
193
+
194
+ desc "greet", "Say hello (runs by default)"
195
+ def greet
196
+ puts "Hello from Asgard!"
197
+ end
198
+ end
199
+ ```
200
+
201
+ ```bash
202
+ asgard # same as: asgard greet
203
+ ```
204
+
205
+ ---
206
+
207
+ ## Command Aliases
208
+
209
+ `map` creates short aliases for existing tasks:
210
+
211
+ ```ruby
212
+ class Tasks
213
+ map "-v" => "version"
214
+ map "--v" => "version"
215
+ map "t" => "test"
216
+ map "b" => "build"
217
+
218
+ desc "version", "Print the version"
219
+ def version = puts Asgard::VERSION
220
+
221
+ desc "test", "Run tests"
222
+ def test = sh "bundle exec rake test"
223
+
224
+ desc "build", "Build the gem"
225
+ def build = sh "bundle exec rake build"
226
+ end
227
+ ```
228
+
229
+ ```bash
230
+ asgard t # same as: asgard test
231
+ asgard b # same as: asgard build
232
+ asgard -v # same as: asgard version (note: --version is the built-in flag)
233
+ ```
234
+
235
+ ---
236
+
237
+ ## Formal Argument Declaration
238
+
239
+ `argument` provides rich positional-parameter metadata including type checking, enums, and help text.
240
+
241
+ !!! warning "Class-level scope"
242
+ `argument` is a **class-level declaration** that applies to **every task in the class**, not just the one that follows it. It is best suited for single-command CLIs or when every task in the file genuinely shares the same positional input. In multi-task files, prefer method signature parameters instead.
243
+
244
+ ```ruby
245
+ class Tasks
246
+ argument :name,
247
+ type: :string,
248
+ default: "World",
249
+ desc: "Name to greet"
250
+
251
+ desc "hello NAME", "Say hello to NAME"
252
+ def hello = sh "echo 'Hello, #{name}!'"
253
+ end
254
+ ```
255
+
256
+ For most multi-task `.loki` files, the simpler positional default pattern is safer:
257
+
258
+ ```ruby
259
+ def hello(name = "World") = sh "echo 'Hello, #{name}!'"
260
+ ```
261
+
262
+ ---
263
+
264
+ ## No Commands Block
265
+
266
+ `no_commands` marks a block of methods as public helpers that are excluded from the CLI and `--help` output. They are callable from any task in the same class:
267
+
268
+ ```ruby
269
+ class Tasks
270
+ desc "build", "Compile the project"
271
+ def build
272
+ puts "Revision: #{current_sha}"
273
+ sh "rake build"
274
+ end
275
+
276
+ no_commands do
277
+ def current_sha
278
+ `git rev-parse --short HEAD`.strip
279
+ end
280
+ end
281
+ end
282
+ ```
283
+
284
+ See [Helper Methods](helpers.md) for the full guide on helpers, `private`, and cross-file sharing.
data/docs/variables.md ADDED
@@ -0,0 +1,122 @@
1
+ # Variables
2
+
3
+ `var` declares a named value that is available to all tasks in the class as a method call. Values can be static or lazily evaluated.
4
+
5
+ ---
6
+
7
+ ## Static Value
8
+
9
+ Pass the value directly as the second argument:
10
+
11
+ ```ruby
12
+ class Tasks
13
+ var :app, "myapp"
14
+ var :env, "production"
15
+ var :port, 3000
16
+
17
+ desc "info", "Print app info"
18
+ def info
19
+ puts "#{app} running on port #{port} in #{env}"
20
+ end
21
+ end
22
+ ```
23
+
24
+ ---
25
+
26
+ ## Lazy Lambda
27
+
28
+ Pass a lambda (or proc) to defer evaluation until the variable is first accessed. The lambda is called once and its return value is used for all subsequent accesses:
29
+
30
+ ```ruby
31
+ class Tasks
32
+ var :version, -> { `git describe --tags`.strip }
33
+ var :sha, -> { `git rev-parse --short HEAD`.strip }
34
+
35
+ desc "tag", "Create a release tag"
36
+ def tag = sh "git tag v#{version}"
37
+
38
+ desc "info", "Show version info"
39
+ def info = puts "#{version} (#{sha})"
40
+ end
41
+ ```
42
+
43
+ !!! tip
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.
45
+
46
+ ---
47
+
48
+ ## Block Syntax
49
+
50
+ You can also use a block instead of a lambda:
51
+
52
+ ```ruby
53
+ class Tasks
54
+ var(:build_dir) { "builds/#{app}" }
55
+ var(:app) { "myapp" }
56
+
57
+ desc "build", "Compile into build_dir"
58
+ def build = sh "rake build OUTDIR=#{build_dir}"
59
+ end
60
+ ```
61
+
62
+ !!! note
63
+ The block form and the lambda form behave identically — both are stored as callables and invoked on first access.
64
+
65
+ ---
66
+
67
+ ## Accessing Variables from Tasks
68
+
69
+ Variables are available as method calls from within any task body (or other method) in the same class. They are defined using `no_commands`, so they appear neither in `--help` output nor as CLI commands:
70
+
71
+ ```ruby
72
+ class Tasks
73
+ var :app, "myapp"
74
+ var :version, -> { `git describe --tags`.strip }
75
+ var :pkg, -> { "pkg/#{app}-#{version}.gem" }
76
+
77
+ desc "build", "Build the gem"
78
+ def build = sh "gem build #{app}.gemspec"
79
+
80
+ desc "push", "Push the gem to RubyGems"
81
+ def push = sh "gem push #{pkg}"
82
+ end
83
+ ```
84
+
85
+ Variables can reference other variables in their lambdas as long as the referenced variable is also defined with `var` on the same class.
86
+
87
+ ---
88
+
89
+ ## Sharing Variables Across Files
90
+
91
+ Because all `.loki` files reopen the same `class Tasks`, variables declared in one file are available in all other files loaded in the same session:
92
+
93
+ ```ruby
94
+ # config.loki
95
+ class Tasks
96
+ var :app, "myapp"
97
+ var :port, 8080
98
+ end
99
+
100
+ # deploy.loki
101
+ class Tasks
102
+ desc "deploy", "Deploy the app"
103
+ def deploy = sh "cap deploy APP=#{app} PORT=#{port}"
104
+ end
105
+ ```
106
+
107
+ ---
108
+
109
+ ## Naming Caution
110
+
111
+ !!! warning
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.
113
+
114
+ ```ruby
115
+ # Avoid this — conflicts with the built-in version infrastructure:
116
+ # var :version, -> { "1.0.0" }
117
+
118
+ # Use this instead:
119
+ var :app_version, -> { `git describe --tags`.strip }
120
+ ```
121
+
122
+ Other names to avoid: `options`, `class_options`, `shell`, `invoke`, `invoke_command`.
data/examples/.loki ADDED
@@ -0,0 +1,2 @@
1
+ # The .loki file can be empty if there are *.loki files define the tasks
2
+ # they will be auto loaded.
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+ # Demonstrates concurrent task execution via parallel depends_on groups.
3
+ #
4
+ # Each worker prints its letter repeatedly with random delays. Because
5
+ # worker_a, worker_b, and worker_c run in separate threads their output
6
+ # interleaves on stdout, proving real concurrency.
7
+ #
8
+ # Execution order:
9
+ # start → worker_a + worker_b + worker_c (all three concurrent) → finish
10
+ #
11
+ # Run with:
12
+ # asgard finish
13
+ #
14
+ # Sample output (character order varies every run):
15
+ # start
16
+ # ABCBACBACBABCBACBACB
17
+ # end
18
+
19
+ $stdout.sync = true # flush every print immediately across all threads
20
+
21
+ CONCURRENT_REPS = 10 # how many times each worker prints its character
22
+
23
+ class Tasks
24
+ desc "start", "Print start marker"
25
+ def start
26
+ puts "starting demo of concurrent task execution ..."
27
+ end
28
+
29
+ desc "worker_a", "Print 'A' repeatedly with random delays"
30
+ def worker_a
31
+ CONCURRENT_REPS.times do
32
+ print "A"
33
+ sleep rand(0.05..0.3)
34
+ end
35
+ end
36
+
37
+ desc "worker_b", "Print 'B' repeatedly with random delays"
38
+ def worker_b
39
+ CONCURRENT_REPS.times do
40
+ print "B"
41
+ sleep rand(0.05..0.3)
42
+ end
43
+ end
44
+
45
+ desc "worker_c", "Print 'C' repeatedly with random delays"
46
+ def worker_c
47
+ CONCURRENT_REPS.times do
48
+ print "C"
49
+ sleep rand(0.05..0.3)
50
+ end
51
+ end
52
+
53
+ depends_on :start, [:worker_a, :worker_b, :worker_c]
54
+ desc "finish", "Print end marker after all workers complete"
55
+ def finish
56
+ puts "\nfini - the end of concurrent task demo"
57
+ end
58
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+ # Demonstrates Thor subcommands with Asgard's depends_on chaining within
3
+ # the subcommand group. Inherits from Tasks for full Asgard DSL access.
4
+ #
5
+ # Usage:
6
+ # asgard db # shows subcommand help
7
+ # asgard db migrate
8
+ # asgard db migrate 20240101120000 --dry-run
9
+ # asgard db rollback
10
+ # asgard db rollback 3
11
+ # asgard db seed --file db/seeds/staging.rb
12
+ # asgard db reset # runs: rollback → migrate → seed
13
+ # asgard db console --env staging
14
+ # asgard db status
15
+
16
+ class DBCommands < Tasks
17
+ desc "migrate [VERSION]", "Run pending migrations up to VERSION"
18
+ option :dry_run, aliases: "-n", type: :boolean, default: false, desc: "Print SQL without executing"
19
+ option :verbose, aliases: "-v", type: :boolean, default: false, desc: "Show each migration as it runs"
20
+ def migrate(version = nil)
21
+ target = version ? "to version #{version}" : "to latest"
22
+ puts "Migrating #{target}#{options[:dry_run] ? " (dry run)" : ""}..."
23
+ end
24
+
25
+ desc "rollback [STEPS]", "Roll back the last STEPS migrations (default: 1)"
26
+ option :dry_run, aliases: "-n", type: :boolean, default: false, desc: "Print SQL without executing"
27
+ def rollback(steps = "1")
28
+ puts "Rolling back #{steps} migration(s)#{options[:dry_run] ? " (dry run)" : ""}..."
29
+ end
30
+
31
+ desc "seed [FILE]", "Load seed data into the database"
32
+ option :env, type: :string, default: "development",
33
+ enum: %w[development staging production],
34
+ desc: "Environment to seed"
35
+ def seed(file = "db/seeds.rb")
36
+ puts "Seeding #{options[:env]} from #{file}..."
37
+ end
38
+
39
+ # depends_on chains within the subcommand group:
40
+ # rollback → migrate → seed → reset
41
+ depends_on :rollback, :migrate, :seed
42
+ desc "reset", "Rollback all migrations, re-migrate, and reseed"
43
+ def reset
44
+ puts "Database reset complete."
45
+ end
46
+
47
+ long_desc <<~DESC
48
+ Opens an interactive SQL console connected to the configured database.
49
+
50
+ The console inherits credentials from the current environment's
51
+ database.yml (Rails) or DATABASE_URL.
52
+
53
+ Examples:\x5
54
+ asgard db console\x5
55
+ asgard db console --env staging
56
+ DESC
57
+ desc "console", "Open an interactive database console"
58
+ option :env, type: :string, default: "development",
59
+ enum: %w[development staging production],
60
+ desc: "Environment to connect to"
61
+ def console
62
+ puts "Opening #{options[:env]} database console..."
63
+ end
64
+
65
+ desc "status", "Show applied and pending migrations"
66
+ def status
67
+ puts "Checking migration status..."
68
+ end
69
+ end
70
+
71
+ class Tasks
72
+ desc "db SUBCOMMAND", "Manage the database"
73
+ subcommand "db", DBCommands
74
+ end