asgard 0.1.2 → 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/helpers.md ADDED
@@ -0,0 +1,154 @@
1
+ # Helper Methods
2
+
3
+ Not every method needs to be a CLI command. Asgard (via Thor) provides two mechanisms to define callable helper methods that are excluded from `asgard help` and cannot be invoked directly from the command line.
4
+
5
+ ---
6
+
7
+ ## Private Methods
8
+
9
+ Methods declared after `private` are callable from any task in the same class but are invisible to Thor's command dispatcher. They will not appear in `--help` output and cannot be called from the CLI:
10
+
11
+ ```ruby
12
+ class Tasks
13
+ desc "build", "Compile and package"
14
+ def build
15
+ compile("src")
16
+ package(app_version)
17
+ end
18
+
19
+ desc "release", "Build and publish to RubyGems"
20
+ def release
21
+ build
22
+ sh "gem push pkg/myapp-#{app_version}.gem"
23
+ end
24
+
25
+ private
26
+
27
+ def compile(dir)
28
+ sh "gcc -O2 -o bin/myapp #{dir}/*.c"
29
+ end
30
+
31
+ def package(ver)
32
+ sh "tar czf pkg/myapp-#{ver}.tar.gz bin/"
33
+ end
34
+
35
+ def app_version
36
+ `git describe --tags`.strip
37
+ end
38
+ end
39
+ ```
40
+
41
+ !!! note
42
+ In Ruby, `private` applies to all methods defined after it in the same class body. You can group all helpers at the bottom of the class after a single `private` declaration.
43
+
44
+ ---
45
+
46
+ ## The `no_commands` Block
47
+
48
+ Thor's `no_commands` block marks public methods as excluded from CLI discovery. Unlike `private`, these methods are still publicly accessible from Ruby code (e.g., from a subclass or a module). They are useful for methods that must be public for technical reasons but should not appear as commands:
49
+
50
+ ```ruby
51
+ class Tasks
52
+ desc "build", "Compile the project"
53
+ def build
54
+ puts "Revision: #{current_sha}"
55
+ sh "rake build"
56
+ end
57
+
58
+ desc "deploy", "Deploy to production"
59
+ def deploy
60
+ puts "Deploying revision #{current_sha}..."
61
+ sh "cap production deploy"
62
+ end
63
+
64
+ no_commands do
65
+ def current_sha
66
+ `git rev-parse --short HEAD`.strip
67
+ end
68
+
69
+ def timestamp
70
+ Time.now.strftime("%Y%m%d-%H%M%S")
71
+ end
72
+ end
73
+ end
74
+ ```
75
+
76
+ `var`-declared variables are also implemented using `no_commands` internally, which is why they appear as callable methods but not as CLI commands.
77
+
78
+ ---
79
+
80
+ ## Choosing Between `private` and `no_commands`
81
+
82
+ | | `private` | `no_commands` |
83
+ |---|---|---|
84
+ | Hidden from `--help` | Yes | Yes |
85
+ | Blocked from CLI | Yes | Yes |
86
+ | Accessible from subclass | No | Yes |
87
+ | Accessible from module include | No | Yes |
88
+ | Ruby idiom | Familiar | Thor-specific |
89
+
90
+ For most helpers, `private` is the right choice. Use `no_commands` when the helper must remain technically public (e.g., it will be inherited by a subcommand class).
91
+
92
+ ---
93
+
94
+ ## Sharing Helpers Across Files
95
+
96
+ Extract shared helpers into a plain Ruby module and load it from `.loki` using `require_relative`:
97
+
98
+ ```ruby
99
+ # shared/helpers.rb
100
+ module BuildHelpers
101
+ private
102
+
103
+ def compile(dir)
104
+ sh "gcc -O2 -o bin/myapp #{dir}/*.c"
105
+ end
106
+
107
+ def dist_path(ver)
108
+ "pkg/myapp-#{ver}.tar.gz"
109
+ end
110
+ end
111
+ ```
112
+
113
+ ```ruby
114
+ # .loki
115
+ require_relative "shared/helpers"
116
+
117
+ class Tasks
118
+ include BuildHelpers
119
+
120
+ desc "build", "Compile the project"
121
+ def build = compile("src")
122
+
123
+ desc "package", "Create distribution archive"
124
+ def package = sh "tar czf #{dist_path(app_version)} bin/"
125
+ end
126
+ ```
127
+
128
+ Because `include` in the class body makes the module methods available as instance methods, and they are declared `private` inside the module, they remain invisible to Thor.
129
+
130
+ !!! tip
131
+ Helpers in a shared module can call `sh`, `shebang`, and other Asgard DSL methods because those are included in `Tasks` (via `Asgard::Base` and `Asgard::Shell`) and are available in `self` when the module method is invoked.
132
+
133
+ ---
134
+
135
+ ## Helper Methods in Subcommands
136
+
137
+ Subcommand classes that inherit from `Tasks` also inherit all private helpers and `no_commands` methods defined on `Tasks`. You can also define helpers local to the subcommand class:
138
+
139
+ ```ruby
140
+ class DeployCommands < Tasks
141
+ desc "staging", "Deploy to staging"
142
+ def staging = deploy_to("staging")
143
+
144
+ desc "production", "Deploy to production"
145
+ def production = deploy_to("production")
146
+
147
+ private
148
+
149
+ def deploy_to(env)
150
+ sh "cap #{env} deploy REV=#{current_sha}"
151
+ end
152
+ # current_sha is inherited from Tasks if defined there
153
+ end
154
+ ```
data/docs/index.md ADDED
@@ -0,0 +1,85 @@
1
+ # Asgard
2
+
3
+ <table>
4
+ <tr>
5
+ <td width="40%" align="center" valign="top">
6
+ <img src="assets/images/asgard.jpg" alt="Asgard" width="300"><br>
7
+ <em>"Loki collects the tricks.<br>Thor of Asgard runs them."</em>
8
+ </td>
9
+ <td width="60%" valign="top">
10
+ <strong>Key Features</strong>
11
+ <ul>
12
+ <li><strong>Thor-Powered CLI</strong> — every Thor DSL feature available inside <code>.loki</code> task files</li>
13
+ <li><strong>Task Dependencies</strong> — sequential, parallel, and mixed dependency graphs via <code>depends_on</code></li>
14
+ <li><strong>Concurrent Execution</strong> — parallel task groups run in native Ruby threads</li>
15
+ <li><strong>Subcommands</strong> — group related tasks under a named namespace</li>
16
+ <li><strong>Variables</strong> — static values and lazy-evaluated lambdas via <code>var</code></li>
17
+ <li><strong>Shell Helpers</strong> — <code>sh</code> for any shell command or heredoc; <code>shebang</code> for polyglot scripts</li>
18
+ <li><strong>Dotenv Support</strong> — load <code>.env</code> files into the environment with <code>dotenv</code></li>
19
+ <li><strong>Auto-Discovery</strong> — <code>.loki</code> root marker searched from CWD upward through parent directories</li>
20
+ <li><strong>Multi-File Tasks</strong> — split tasks across <code>*.loki</code> files, loaded on demand with <code>--auto-load</code></li>
21
+ <li><strong>Built-in Flags</strong> — <code>--version</code>, <code>--debug</code>, and <code>--verbose</code> available on every task</li>
22
+ </ul>
23
+ </td>
24
+ </tr>
25
+ </table>
26
+
27
+ Asgard is a [Thor](https://github.com/rails/thor)-based task runner for Ruby projects. Define tasks in `.loki` files, declare dependencies between them, and let Asgard handle ordering and concurrent execution. Anything Thor can do — subcommands, typed options, argument validation — is available inside a `.loki` file.
28
+
29
+ ---
30
+
31
+ ## Quick Start
32
+
33
+ ```bash
34
+ # Install
35
+ gem install asgard
36
+
37
+ # Create your project root marker
38
+ touch .loki
39
+
40
+ # Add your first task
41
+ cat >> .loki << 'EOF'
42
+ class Tasks
43
+ desc "hello", "Say hello"
44
+ def hello = sh 'echo "Hello from Asgard!"'
45
+ end
46
+ EOF
47
+
48
+ # Run it
49
+ asgard hello
50
+ ```
51
+
52
+ ---
53
+
54
+ ## How It Works
55
+
56
+ Asgard searches upward from your current directory for a `.loki` file. That file marks the project root. Additional `*.loki` files in the same directory can be loaded by passing `--auto-load` to the `asgard` command. All task files reopen `class Tasks`, which is pre-defined by the gem as a subclass of `Asgard::Base` (itself a Thor subclass).
57
+
58
+ The full Thor DSL is available: `desc`, `method_option`, `class_option`, `long_desc`, `argument`, `default_task`, `map`, and `subcommand` all work exactly as documented in Thor — with Asgard's own `depends_on`, `var`, `sh`, `shebang`, and `dotenv` layered on top.
59
+
60
+ ---
61
+
62
+ ## Documentation
63
+
64
+ | Section | Description |
65
+ |---|---|
66
+ | [Getting Started](getting-started.md) | Install, create your first `.loki`, run your first task |
67
+ | [Defining Tasks](tasks.md) | Parameters, options, long_desc, aliases, default_task |
68
+ | [Dependencies](dependencies.md) | Sequential, parallel, and mixed dependency graphs |
69
+ | [Variables](variables.md) | Static and lazy-evaluated task variables |
70
+ | [Helper Methods](helpers.md) | Private helpers and the `no_commands` block |
71
+ | [Options & Flags](options.md) | class_option, built-in flags, debug? and verbose? |
72
+ | [Subcommands](subcommands.md) | Grouping tasks under a namespace |
73
+ | [Shell Helpers](shell.md) | `sh`, `shebang`, and supported interpreters |
74
+ | [Environment](environment.md) | Loading `.env` files with `dotenv` |
75
+ | [Task Files](task-files.md) | `.loki` root marker, `--auto-load`, multi-file layout |
76
+ | [API Reference](api.md) | Module methods, DSL methods, error classes |
77
+ | [Examples](examples.md) | Working `.loki` files for every feature |
78
+ | [Changelog](changelog.md) | Release history |
79
+
80
+ ---
81
+
82
+ ## Requirements
83
+
84
+ - Ruby >= 3.2.0
85
+ - Dependencies: [thor](https://github.com/rails/thor) `~> 1.0`, [dagwood](https://rubygems.org/gems/dagwood) `~> 1.0`, [dotenv](https://github.com/bkeepers/dotenv) `~> 3.0`
data/docs/options.md ADDED
@@ -0,0 +1,180 @@
1
+ # Options & Flags
2
+
3
+ Asgard tasks use the full Thor option system. Options declared with `method_option` (alias: `option`) apply to a single task. Options declared with `class_option` apply to every task in the class. Asgard ships with three built-in `class_option` declarations on `Tasks`: `--debug`, `--verbose`, and `--version`.
4
+
5
+ ---
6
+
7
+ ## Per-Task Options
8
+
9
+ `method_option` (or its alias `option`) declares an option for the immediately following task:
10
+
11
+ ```ruby
12
+ class Tasks
13
+ desc "deploy ENV", "Deploy to ENV"
14
+ method_option :branch,
15
+ aliases: "-b",
16
+ type: :string,
17
+ default: "main",
18
+ desc: "Git branch to deploy"
19
+ method_option :dry_run,
20
+ aliases: "-n",
21
+ type: :boolean,
22
+ default: false,
23
+ desc: "Print commands without running"
24
+ def deploy(env = "staging")
25
+ if options[:dry_run]
26
+ puts "Would deploy #{options[:branch]} to #{env}"
27
+ else
28
+ sh "cap #{env} deploy BRANCH=#{options[:branch]}"
29
+ end
30
+ end
31
+ end
32
+ ```
33
+
34
+ Access option values inside the task body via `options[:name]` (a hash keyed by symbol).
35
+
36
+ ---
37
+
38
+ ## Class Options (Shared Across All Tasks)
39
+
40
+ `class_option` defines an option available on every task in the class. Add your own to complement the built-in ones:
41
+
42
+ ```ruby
43
+ class Tasks
44
+ class_option :dry_run,
45
+ aliases: "-n",
46
+ type: :boolean,
47
+ default: false,
48
+ desc: "Print commands without running"
49
+
50
+ class_option :env,
51
+ type: :string,
52
+ default: "development",
53
+ enum: %w[development staging production],
54
+ desc: "Target environment"
55
+
56
+ desc "deploy", "Deploy the application"
57
+ def deploy
58
+ if options[:dry_run]
59
+ puts "Would deploy to #{options[:env]}"
60
+ else
61
+ sh "cap #{options[:env]} deploy"
62
+ end
63
+ end
64
+
65
+ desc "migrate", "Run database migrations"
66
+ def migrate
67
+ sh "rails db:migrate RAILS_ENV=#{options[:env]}"
68
+ end
69
+ end
70
+ ```
71
+
72
+ Both `deploy` and `migrate` automatically accept `--dry-run` and `--env`.
73
+
74
+ ---
75
+
76
+ ## Built-in Flags
77
+
78
+ `Tasks` ships with three built-in class options and a version flag:
79
+
80
+ ### `--version`
81
+
82
+ Prints `Asgard::VERSION` and exits. Implemented as the `_version` method with the `_` prefix convention (gem-owned, blocked from direct CLI invocation):
83
+
84
+ ```bash
85
+ asgard --version
86
+ # 0.1.2
87
+ ```
88
+
89
+ ### `--debug`
90
+
91
+ A `class_option :debug` of type `:boolean`. When passed, sets `$DEBUG = true` before the task body runs (via the `invoke_command` hook in `Asgard::Base`):
92
+
93
+ ```bash
94
+ asgard build --debug
95
+ ```
96
+
97
+ Inside the task, use the `debug?` predicate:
98
+
99
+ ```ruby
100
+ def build
101
+ sh "rake build"
102
+ sh "rake build --trace" if debug?
103
+ end
104
+ ```
105
+
106
+ ### `--verbose`
107
+
108
+ A `class_option :verbose` of type `:boolean`. When passed, sets `$VERBOSE = true` before the task body runs:
109
+
110
+ ```bash
111
+ asgard test --verbose
112
+ ```
113
+
114
+ Inside the task, use the `verbose?` predicate:
115
+
116
+ ```ruby
117
+ def test
118
+ flags = verbose? ? "--verbose" : ""
119
+ sh "bundle exec rake test #{flags}"
120
+ end
121
+ ```
122
+
123
+ ---
124
+
125
+ ## `debug?` and `verbose?` Predicates
126
+
127
+ Both are private methods on `Tasks`, thin wrappers around the global variables:
128
+
129
+ ```ruby
130
+ private
131
+
132
+ def debug? = $DEBUG
133
+ def verbose? = $VERBOSE
134
+ ```
135
+
136
+ They are available in every task body and in subcommand classes that inherit from `Tasks`. Because `--debug` and `--verbose` are `class_option` declarations (not standalone commands), they work as modifiers alongside any task:
137
+
138
+ ```bash
139
+ asgard build --debug --verbose
140
+ asgard deploy production --verbose
141
+ ```
142
+
143
+ ---
144
+
145
+ ## Option Types Reference
146
+
147
+ | Type | CLI Example | Ruby Value |
148
+ |---|---|---|
149
+ | `:string` | `--branch main` | `"main"` |
150
+ | `:boolean` | `--force` / `--no-force` | `true` / `false` |
151
+ | `:numeric` | `--count 3` | `3` |
152
+ | `:array` | `--tags foo bar baz` | `["foo", "bar", "baz"]` |
153
+ | `:hash` | `--vars KEY:val FOO:bar` | `{"KEY"=>"val", "FOO"=>"bar"}` |
154
+
155
+ ---
156
+
157
+ ## Option Keys Reference
158
+
159
+ | Key | Applies to | Description |
160
+ |---|---|---|
161
+ | `aliases` | `method_option`, `class_option` | Short-form flag string, e.g. `"-b"` |
162
+ | `type` | `method_option`, `class_option` | One of the five types above |
163
+ | `default` | `method_option`, `class_option` | Value used when the flag is omitted |
164
+ | `required` | `method_option` | Raises an error if the flag is missing |
165
+ | `desc` | `method_option`, `class_option` | One-line description shown in help |
166
+ | `enum` | `method_option`, `class_option` | Allowed values; validated by Thor |
167
+ | `banner` | `method_option` | Placeholder shown in help for the value slot |
168
+
169
+ ---
170
+
171
+ ## `_` Prefix Convention
172
+
173
+ Methods whose names start with `_` are considered gem-owned in Asgard's naming convention. `run!` guards against invoking them directly from the CLI:
174
+
175
+ ```bash
176
+ asgard _version
177
+ # asgard: unknown command '_version'
178
+ ```
179
+
180
+ If you define your own methods on `Tasks`, avoid the `_` prefix to prevent them from being silently blocked.
data/docs/shell.md ADDED
@@ -0,0 +1,208 @@
1
+ # Shell Helpers
2
+
3
+ Asgard provides two methods for running shell commands and scripts from within task bodies: `sh` for shell commands and heredocs, and `shebang` for polyglot scripts. Both are provided by `Asgard::Shell` and mixed into every `Tasks` instance.
4
+
5
+ Both methods exit with the command's status code on failure — they do not raise Ruby exceptions.
6
+
7
+ ---
8
+
9
+ ## `sh` — Run Shell Commands
10
+
11
+ ### Single-Line Command
12
+
13
+ Pass a single-line string to run it via `system`:
14
+
15
+ ```ruby
16
+ class Tasks
17
+ desc "build", "Compile the project"
18
+ def build = sh "rake build"
19
+
20
+ desc "clean", "Remove build artifacts"
21
+ def clean = sh "rm -rf dist/ tmp/"
22
+ end
23
+ ```
24
+
25
+ By default, `sh` prints the command before running it:
26
+
27
+ ```
28
+ rake build
29
+ ```
30
+
31
+ ### Multi-Line Heredoc
32
+
33
+ Pass a multiline string (e.g., a heredoc) to run it as a single `bash -c` script. All lines execute in the same shell session, so variable assignments carry across lines:
34
+
35
+ ```ruby
36
+ class Tasks
37
+ desc "setup", "Bootstrap the development environment"
38
+ def setup
39
+ sh <<~SHELL
40
+ brew install redis postgresql
41
+ brew services start redis
42
+ bundle install
43
+ rails db:setup
44
+ SHELL
45
+ end
46
+ end
47
+ ```
48
+
49
+ Asgard detects the newline and automatically routes multiline scripts through `bash -c`.
50
+
51
+ ### Silent Mode
52
+
53
+ Pass `silent: true` to suppress the command echo. The command still runs and still exits on failure; it just doesn't print the command text first:
54
+
55
+ ```ruby
56
+ class Tasks
57
+ desc "build", "Compile (quiet)"
58
+ def build = sh "rake build", silent: true
59
+
60
+ desc "info", "Print environment info without noise"
61
+ def info
62
+ sh "printenv | grep APP_", silent: true
63
+ end
64
+ end
65
+ ```
66
+
67
+ ### Exit on Failure
68
+
69
+ `sh` always calls `exit($?.exitstatus)` if the command fails. There is no rescue path — a failing command terminates the `asgard` process. This is intentional: failed steps should stop the pipeline rather than silently continue.
70
+
71
+ ```ruby
72
+ class Tasks
73
+ depends_on :test
74
+ desc "release", "Test then release"
75
+ def release
76
+ sh "bundle exec rake release"
77
+ # Never reached if rake release fails
78
+ puts "Released!"
79
+ end
80
+ end
81
+ ```
82
+
83
+ ---
84
+
85
+ ## `shebang` — Polyglot Scripts
86
+
87
+ `shebang` writes the script body to a tempfile with the appropriate extension and executes it with the specified interpreter. Use it to embed Python, Node.js, Ruby, Perl, or any other interpreter directly in a task:
88
+
89
+ ### Python
90
+
91
+ ```ruby
92
+ class Tasks
93
+ desc "analyze", "Run Python data analysis"
94
+ def analyze
95
+ shebang :python3, <<~PYTHON
96
+ import json
97
+ data = json.load(open("results.json"))
98
+ print(f"Total: {sum(data.values())}")
99
+ PYTHON
100
+ end
101
+ end
102
+ ```
103
+
104
+ ### Node.js
105
+
106
+ ```ruby
107
+ class Tasks
108
+ desc "bundle_assets", "Build frontend assets with esbuild"
109
+ def bundle_assets
110
+ shebang :node, <<~JS
111
+ const esbuild = require("esbuild")
112
+ esbuild.buildSync({
113
+ entryPoints: ["src/app.js"],
114
+ bundle: true,
115
+ outfile: "dist/app.js"
116
+ })
117
+ JS
118
+ end
119
+ end
120
+ ```
121
+
122
+ ### Ruby
123
+
124
+ ```ruby
125
+ class Tasks
126
+ desc "transform", "Transform data with Ruby"
127
+ def transform
128
+ shebang :ruby, <<~RUBY
129
+ require "json"
130
+ data = JSON.parse(File.read("input.json"))
131
+ File.write("output.json", JSON.pretty_generate(data.transform_values(&:upcase)))
132
+ RUBY
133
+ end
134
+ end
135
+ ```
136
+
137
+ ### Bash
138
+
139
+ ```ruby
140
+ class Tasks
141
+ desc "provision", "Run a bash provisioning script"
142
+ def provision
143
+ shebang :bash, <<~BASH
144
+ set -euo pipefail
145
+ apt-get update
146
+ apt-get install -y curl wget git
147
+ echo "Provisioned at $(date)"
148
+ BASH
149
+ end
150
+ end
151
+ ```
152
+
153
+ ---
154
+
155
+ ## Supported Interpreters
156
+
157
+ | Symbol | File Extension | Interpreter |
158
+ |---|---|---|
159
+ | `:python3` | `.py` | `python3` |
160
+ | `:python` | `.py` | `python` |
161
+ | `:node` | `.js` | `node` |
162
+ | `:ruby` | `.rb` | `ruby` |
163
+ | `:perl` | `.pl` | `perl` |
164
+ | `:bash` | `.sh` | `bash` |
165
+ | `:sh` | `.sh` | `sh` |
166
+ | Any other symbol | `.tmp` | Passed directly to `system` |
167
+
168
+ !!! note
169
+ Unknown interpreter symbols get a `.tmp` extension and are passed to `system` directly. This makes it easy to use interpreters not in the table above:
170
+
171
+ ```ruby
172
+ shebang :lua, <<~LUA
173
+ print("Hello from Lua!")
174
+ LUA
175
+ ```
176
+
177
+ ### Silent Mode
178
+
179
+ `shebang` also accepts `silent: true`, though in practice the interpreter itself controls what is printed:
180
+
181
+ ```ruby
182
+ def analyze
183
+ shebang :python3, script_body, silent: true
184
+ end
185
+ ```
186
+
187
+ ---
188
+
189
+ ## Combining `sh` and `shebang`
190
+
191
+ You can mix both in the same task:
192
+
193
+ ```ruby
194
+ class Tasks
195
+ desc "pipeline", "Run a mixed shell + Python pipeline"
196
+ def pipeline
197
+ sh "bundle exec rake build"
198
+
199
+ shebang :python3, <<~PYTHON
200
+ import subprocess
201
+ result = subprocess.run(["./bin/validate"], capture_output=True, text=True)
202
+ print(result.stdout)
203
+ PYTHON
204
+
205
+ sh "rake deploy"
206
+ end
207
+ end
208
+ ```