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.
@@ -0,0 +1,221 @@
1
+ # Task Dependencies
2
+
3
+ `depends_on` declares what must run before a task. Asgard resolves the dependency graph at startup, validates it for cycles, and executes prerequisites automatically when a task is invoked.
4
+
5
+ !!! note
6
+ `desc` and `depends_on` are independent — either can come first. Both must appear before the `def`.
7
+
8
+ ---
9
+
10
+ ## How It Works
11
+
12
+ When you run `asgard <task>`, Asgard:
13
+
14
+ 1. Validates the full dependency graph for circular references (fails fast with a clear error).
15
+ 2. Resolves the dependency stages for the requested task in order.
16
+ 3. Executes each stage — running parallel groups in native Ruby threads.
17
+ 4. Runs the task itself after all prerequisites complete.
18
+
19
+ **Deduplication:** each task runs at most once per `asgard` invocation, regardless of how many other tasks declare it as a dependency. This is enforced thread-safely via a class-level `Set` and `Mutex`.
20
+
21
+ ---
22
+
23
+ ## Sequential Dependencies
24
+
25
+ Bare symbols run one after another in the order declared:
26
+
27
+ ```ruby
28
+ class Tasks
29
+ desc "build", "Compile the project"
30
+ def build = sh "rake build"
31
+
32
+ depends_on :build
33
+ desc "test", "Run the test suite"
34
+ def test = sh "rake test"
35
+
36
+ depends_on :test
37
+ desc "release", "Publish the gem"
38
+ def release = sh "bundle exec rake release"
39
+ end
40
+ ```
41
+
42
+ ```bash
43
+ asgard release # build → test → release
44
+ ```
45
+
46
+ Multiple sequential dependencies in a single `depends_on` call run left to right:
47
+
48
+ ```ruby
49
+ depends_on :clean, :build, :test
50
+ desc "package", "Clean, build, and test"
51
+ def package = sh "rake package"
52
+ ```
53
+
54
+ ---
55
+
56
+ ## Parallel Dependencies
57
+
58
+ Wrap symbols in an array to declare they can run concurrently. Asgard waits for all tasks in a parallel group to finish before moving to the next stage:
59
+
60
+ ```ruby
61
+ class Tasks
62
+ desc "lint", "Check code style"
63
+ def lint = sh "bundle exec rubocop"
64
+
65
+ desc "typecheck", "Run type checks"
66
+ def typecheck = sh "bundle exec srb tc"
67
+
68
+ depends_on [:lint, :typecheck]
69
+ desc "test", "Run tests (after lint and typecheck)"
70
+ def test = sh "bundle exec rake test"
71
+ end
72
+ ```
73
+
74
+ ```bash
75
+ asgard test # lint ∥ typecheck → test
76
+ ```
77
+
78
+ Parallel groups run in native Ruby threads. For CPU-bound work, keep in mind the GVL; for I/O-bound work (shell commands, network), true concurrency is achieved.
79
+
80
+ ---
81
+
82
+ ## Mixed Sequential and Parallel
83
+
84
+ Mix bare symbols and arrays in a single `depends_on` call. Execution proceeds stage by stage — each stage completes before the next begins:
85
+
86
+ ```ruby
87
+ class Tasks
88
+ desc "setup", "Install dependencies"; def setup = sh "bundle install"
89
+ desc "lint", "Check code style"; def lint = sh "bundle exec rubocop"
90
+ desc "build", "Compile assets"; def build = sh "rake assets:precompile"
91
+ desc "test", "Run tests"; def test = sh "bundle exec rake test"
92
+ desc "notify", "Post to Slack"; def notify = sh "curl $SLACK_WEBHOOK -d '{\"text\":\"done\"}'"
93
+
94
+ # setup first, then lint+build in parallel, then test, then notify
95
+ depends_on :setup, [:lint, :build], :test, :notify
96
+ desc "ci", "Full CI pipeline"
97
+ def ci = sh "echo 'CI complete'"
98
+ end
99
+ ```
100
+
101
+ ```bash
102
+ asgard ci
103
+ ```
104
+
105
+ Execution order:
106
+
107
+ ```
108
+ setup
109
+
110
+ lint ∥ build (concurrent)
111
+
112
+ test
113
+
114
+ notify
115
+
116
+ ci
117
+ ```
118
+
119
+ ---
120
+
121
+ ## Deduplication
122
+
123
+ Each task runs at most once per `asgard` invocation. If multiple tasks declare the same dependency, it executes only on its first encounter:
124
+
125
+ ```ruby
126
+ class Tasks
127
+ desc "setup", "Install gems"
128
+ def setup = sh "bundle install"
129
+
130
+ depends_on :setup
131
+ desc "test", "Run tests"
132
+ def test = sh "rake test"
133
+
134
+ depends_on :setup
135
+ desc "lint", "Check style"
136
+ def lint = sh "rubocop"
137
+
138
+ depends_on [:test, :lint]
139
+ desc "ci", "Test and lint (setup runs once)"
140
+ def ci = puts "done"
141
+ end
142
+ ```
143
+
144
+ When `asgard ci` runs, `setup` executes once even though both `test` and `lint` declare it as a dependency. The deduplication set is managed with a `Mutex` so parallel groups are also safe.
145
+
146
+ ---
147
+
148
+ ## Circular Dependency Detection
149
+
150
+ Asgard validates the full dependency graph using [Dagwood](https://rubygems.org/gems/dagwood) before any task runs. A circular dependency produces a clean error and exits:
151
+
152
+ ```ruby
153
+ class Tasks
154
+ depends_on :b
155
+ desc "a", "Task A"; def a = puts "a"
156
+
157
+ depends_on :a
158
+ desc "b", "Task B"; def b = puts "b"
159
+ end
160
+ ```
161
+
162
+ ```bash
163
+ asgard a
164
+ # asgard: circular dependency — TSort::Cyclic: ...
165
+ ```
166
+
167
+ No backtrace is shown — just a single diagnostic line.
168
+
169
+ ---
170
+
171
+ ## depends_on Across Multiple Files
172
+
173
+ `depends_on` works across `.loki` files because all files reopen the same `class Tasks`. The dependency is recorded when the `def` is encountered, so load order matters:
174
+
175
+ ```ruby
176
+ # build.loki
177
+ class Tasks
178
+ desc "build", "Compile"
179
+ def build = sh "rake build"
180
+ end
181
+
182
+ # test.loki
183
+ class Tasks
184
+ depends_on :build # build.loki must be loaded first
185
+ desc "test", "Test"
186
+ def test = sh "rake test"
187
+ end
188
+ ```
189
+
190
+ When `--auto-load` is used, `*.loki` files are loaded alphabetically, so `build.loki` loads before `test.loki`. If you need to control load order, use explicit `require_relative` from `.loki`.
191
+
192
+ ---
193
+
194
+ ## depends_on Inside Subcommands
195
+
196
+ `depends_on` works within subcommand classes exactly as it does at the top level. Dependency scope is per-class:
197
+
198
+ ```ruby
199
+ class DBCommands < Tasks
200
+ desc "migrate", "Run migrations"
201
+ def migrate = sh "rails db:migrate"
202
+
203
+ desc "seed", "Load seed data"
204
+ def seed = sh "rails db:seed"
205
+
206
+ depends_on :migrate, :seed
207
+ desc "reset", "Migrate then seed"
208
+ def reset = puts "Done."
209
+ end
210
+
211
+ class Tasks
212
+ desc "db SUBCOMMAND", "Manage the database"
213
+ subcommand "db", DBCommands
214
+ end
215
+ ```
216
+
217
+ ```bash
218
+ asgard db reset # migrate → seed → reset
219
+ ```
220
+
221
+ See [Subcommands](subcommands.md) for the full guide.
@@ -0,0 +1,113 @@
1
+ # Environment Variables
2
+
3
+ Asgard provides the `dotenv` class method to load `.env` files into the process environment before tasks run. It is a thin wrapper around the [dotenv gem](https://github.com/bkeepers/dotenv).
4
+
5
+ ---
6
+
7
+ ## Basic Usage
8
+
9
+ Call `dotenv` inside the class body (not inside a task method) to load the default `.env` file:
10
+
11
+ ```ruby
12
+ class Tasks
13
+ dotenv # loads .env from the current working directory
14
+
15
+ desc "check", "Print the app name from .env"
16
+ def check = sh "echo $APP_NAME"
17
+ end
18
+ ```
19
+
20
+ ### Load a Named File
21
+
22
+ Pass a file path string to load a specific file:
23
+
24
+ ```ruby
25
+ class Tasks
26
+ dotenv ".env.local" # load a local override
27
+ dotenv ".env.staging" # load staging-specific vars
28
+ end
29
+ ```
30
+
31
+ ### Multiple Calls
32
+
33
+ Call `dotenv` multiple times to load several files. Each call merges the loaded variables into `ENV`. Later calls do not overwrite variables already set by earlier calls (standard dotenv behavior):
34
+
35
+ ```ruby
36
+ class Tasks
37
+ dotenv # loads .env (base config)
38
+ dotenv ".env.local" # loads .env.local (local overrides)
39
+ end
40
+ ```
41
+
42
+ ---
43
+
44
+ ## When `dotenv` Runs
45
+
46
+ `dotenv` is a **class-level** call — it executes at Ruby class-load time, not when a task is invoked. This means:
47
+
48
+ 1. Variables are available in `ENV` before any task method runs.
49
+ 2. They are also available to lambda variables declared with `var` that reference `ENV`.
50
+ 3. They are available during `depends_on` dependency resolution.
51
+
52
+ ```ruby
53
+ class Tasks
54
+ dotenv
55
+
56
+ var :database_url, -> { ENV.fetch("DATABASE_URL") }
57
+
58
+ desc "migrate", "Run migrations"
59
+ def migrate = sh "DATABASE_URL=#{database_url} rails db:migrate"
60
+ end
61
+ ```
62
+
63
+ !!! warning
64
+ If `.env` does not exist, `dotenv` silently does nothing — it checks `File.exist?` before loading. There is no error for a missing file.
65
+
66
+ ---
67
+
68
+ ## File Not Found
69
+
70
+ Asgard calls `Dotenv.load(path)` only when `File.exist?(path)` is true. If the file is absent, the call is a no-op:
71
+
72
+ ```ruby
73
+ class Tasks
74
+ dotenv ".env.local" # silently skipped if .env.local does not exist
75
+ end
76
+ ```
77
+
78
+ This makes it safe to commit a `.env.local` line to your `.loki` without requiring every developer to create the file.
79
+
80
+ ---
81
+
82
+ ## Environment Variables vs. `var`
83
+
84
+ Use `dotenv` to bring external configuration into `ENV`, and `var` to define task-internal computed values:
85
+
86
+ ```ruby
87
+ class Tasks
88
+ dotenv
89
+
90
+ # Read from ENV (set by dotenv or the shell)
91
+ var :app_name, -> { ENV.fetch("APP_NAME", "myapp") }
92
+ var :port, -> { ENV.fetch("PORT", "3000").to_i }
93
+
94
+ desc "start", "Start the server"
95
+ def start = sh "puma -p #{port}"
96
+ end
97
+ ```
98
+
99
+ ---
100
+
101
+ ## Dotenv File Format
102
+
103
+ Standard dotenv file syntax applies:
104
+
105
+ ```bash
106
+ # .env
107
+ APP_NAME=myapp
108
+ DATABASE_URL=postgres://localhost/myapp_development
109
+ REDIS_URL=redis://localhost:6379/0
110
+ PORT=3000
111
+ ```
112
+
113
+ Multi-line values and quotes are supported per the dotenv gem's own documentation.
data/docs/examples.md ADDED
@@ -0,0 +1,140 @@
1
+ # Examples
2
+
3
+ The `examples/` directory in the Asgard repository contains complete, working `.loki` files that demonstrate every feature of the gem. You can use them as a standalone Asgard project or as copy-paste references.
4
+
5
+ ---
6
+
7
+ ## Using the Examples Directory
8
+
9
+ The `examples/` directory contains its own `.loki` root marker (the gem's project-level `.loki`), which means you can run `asgard` from inside the `examples/` directory and all example files will be loaded:
10
+
11
+ ```bash
12
+ git clone https://github.com/MadBomber/asgard.git
13
+ cd asgard/examples
14
+ asgard help
15
+ ```
16
+
17
+ Alternatively, copy individual example files into your own project's directory.
18
+
19
+ !!! note
20
+ Some examples (notably `concurrent.loki`) produce visible interleaved output to demonstrate real thread concurrency. They are designed to be run, not just read.
21
+
22
+ ---
23
+
24
+ ## `kitchen_sink.loki`
25
+
26
+ **Path:** `examples/kitchen_sink.loki`
27
+
28
+ The most comprehensive example — demonstrates every Thor DSL feature available in Asgard:
29
+
30
+ - `var` with a static value and a lazy lambda
31
+ - `dotenv` (commented out, ready to activate)
32
+ - `class_option` with `:boolean` and `:string` types, including `enum`
33
+ - `default_task` — sets the default command when `asgard` is run with no arguments
34
+ - `map` — short aliases for multiple tasks
35
+ - A basic task with no parameters
36
+ - A task with a positional parameter and default
37
+ - A task with `option` (the `method_option` alias)
38
+ - All five `method_option` types: `:string`, `:boolean`, `:numeric`, `:array`, `:hash`
39
+ - `required` option, `enum` validation, and `banner` customization
40
+ - `long_desc` with `\x5` line-break trick for formatted examples in help text
41
+ - Sequential `depends_on` (`:analyze` before `:spec`)
42
+ - Parallel `depends_on` (`[:analyze, :typecheck]` run concurrently)
43
+ - Mixed sequential + parallel `depends_on` (`:check`, `[:compile, :spec]`, `:pack`)
44
+ - `no_commands` block for a public helper excluded from CLI
45
+ - `private` methods for internal helpers
46
+
47
+ ```bash
48
+ asgard help # see all tasks
49
+ asgard greet # default task
50
+ asgard hello Alice
51
+ asgard compile --jobs 4 --tags debug release --defines VERSION:2 MODE:fast
52
+ asgard deploy --strategy rolling
53
+ asgard report --format html --since 2024-01-01
54
+ asgard pipeline
55
+ ```
56
+
57
+ ---
58
+
59
+ ## `server_subcommands.loki`
60
+
61
+ **Path:** `examples/server_subcommands.loki`
62
+
63
+ Demonstrates Thor subcommands with a server management group. Covers:
64
+
65
+ - Defining a subcommand class (`ServerCommands < Tasks`)
66
+ - Registering it with `subcommand "server", ServerCommands`
67
+ - Per-command options (`--daemon`, `--workers`, `--log`, `--force`, `--wait`)
68
+ - `depends_on` inside a subcommand group (`:stop` and `:start` before `:restart`)
69
+
70
+ ```bash
71
+ asgard server help
72
+ asgard server start
73
+ asgard server start 4000 --workers 4 --daemon
74
+ asgard server stop --force
75
+ asgard server status
76
+ asgard server restart 4000
77
+ ```
78
+
79
+ The `ServerCommands` class inherits from `Tasks`, giving it access to `sh`, `depends_on`, and the built-in `--debug`/`--verbose` flags.
80
+
81
+ ---
82
+
83
+ ## `db_subcommands.loki`
84
+
85
+ **Path:** `examples/db_subcommands.loki`
86
+
87
+ Demonstrates subcommands with more complex `depends_on` chaining within the group. Covers:
88
+
89
+ - `DBCommands < Tasks` with migrate, rollback, seed, reset, console, and status commands
90
+ - Multi-step `depends_on` chain: `rollback → migrate → seed → reset`
91
+ - `long_desc` with formatted examples inside a subcommand class
92
+ - `enum` validation on subcommand options
93
+ - Optional positional parameters (`migrate [VERSION]`, `rollback [STEPS]`, `seed [FILE]`)
94
+
95
+ ```bash
96
+ asgard db help
97
+ asgard db migrate
98
+ asgard db migrate 20240101120000 --dry-run
99
+ asgard db rollback
100
+ asgard db rollback 3
101
+ asgard db seed --env staging
102
+ asgard db reset # rollback → migrate → seed → reset
103
+ asgard db console --env staging
104
+ asgard db status
105
+ ```
106
+
107
+ ---
108
+
109
+ ## `concurrent.loki`
110
+
111
+ **Path:** `examples/concurrent.loki`
112
+
113
+ A focused demonstration of true concurrent execution via parallel `depends_on` groups:
114
+
115
+ - Three worker tasks (`worker_a`, `worker_b`, `worker_c`) each print a character repeatedly with random sleep delays
116
+ - All three run in parallel threads when `asgard finish` is invoked
117
+ - The interleaved output proves that real concurrency is occurring (not sequential batching)
118
+ - Uses `$stdout.sync = true` to ensure thread-safe immediate output flushing
119
+
120
+ ```bash
121
+ asgard finish
122
+ # starting demo of concurrent task execution ...
123
+ # ABCBACBACBABCBACBACB (order varies every run)
124
+ # fini - the end of concurrent task demo
125
+ ```
126
+
127
+ Execution order: `start` → `worker_a ∥ worker_b ∥ worker_c` → `finish`
128
+
129
+ This example is also useful as a test harness for verifying that parallel execution is working correctly on a given system.
130
+
131
+ ---
132
+
133
+ ## Summary
134
+
135
+ | File | Primary Focus |
136
+ |---|---|
137
+ | `kitchen_sink.loki` | Comprehensive Thor DSL reference — options, aliases, long_desc, depends_on |
138
+ | `server_subcommands.loki` | Subcommand groups, per-command options, depends_on in subcommands |
139
+ | `db_subcommands.loki` | Multi-step depends_on chains, enum validation, long_desc in subcommands |
140
+ | `concurrent.loki` | Parallel task execution, thread concurrency demonstration |
@@ -0,0 +1,180 @@
1
+ # Getting Started
2
+
3
+ This guide walks you through installing Asgard, creating your first `.loki` task file, and running tasks from the command line.
4
+
5
+ ---
6
+
7
+ ## Installation
8
+
9
+ === "RubyGems"
10
+
11
+ ```bash
12
+ gem install asgard
13
+ ```
14
+
15
+ === "Bundler"
16
+
17
+ ```bash
18
+ bundle add asgard
19
+ ```
20
+
21
+ Or add it manually to your `Gemfile`:
22
+
23
+ ```ruby
24
+ gem "asgard", "~> 0.1"
25
+ ```
26
+
27
+ then run `bundle install`.
28
+
29
+ ---
30
+
31
+ ## Verify the Installation
32
+
33
+ ```bash
34
+ asgard --version
35
+ # 0.1.2
36
+ ```
37
+
38
+ ---
39
+
40
+ ## Create Your First Task File
41
+
42
+ Every Asgard project needs a `.loki` file at its root. This hidden file is both the project root marker (Asgard searches upward from CWD to find it) and the entry point for your tasks.
43
+
44
+ ```bash
45
+ # Create the root marker in your project directory
46
+ touch .loki
47
+ ```
48
+
49
+ Open `.loki` in your editor and add a task:
50
+
51
+ ```ruby
52
+ class Tasks
53
+ desc "hello", "Say hello to the world"
54
+ def hello = sh 'echo "Hello, World!"'
55
+ end
56
+ ```
57
+
58
+ !!! note
59
+ The `Tasks` class is pre-defined by the gem as `class Tasks < Asgard::Base`. You just reopen it — no `require` or superclass declaration needed.
60
+
61
+ ---
62
+
63
+ ## Run Your Task
64
+
65
+ ```bash
66
+ asgard hello
67
+ # echo "Hello, World!"
68
+ # Hello, World!
69
+ ```
70
+
71
+ See all available tasks:
72
+
73
+ ```bash
74
+ asgard help
75
+ ```
76
+
77
+ See help for a specific task:
78
+
79
+ ```bash
80
+ asgard help hello
81
+ ```
82
+
83
+ ---
84
+
85
+ ## Add a Parameter
86
+
87
+ Positional parameters are declared directly in the method signature. Document them in the `desc` usage string:
88
+
89
+ ```ruby
90
+ class Tasks
91
+ desc "greet NAME", "Greet someone by name"
92
+ def greet(name = "World")
93
+ sh "echo 'Hello, #{name}!'"
94
+ end
95
+ end
96
+ ```
97
+
98
+ ```bash
99
+ asgard greet
100
+ # Hello, World!
101
+
102
+ asgard greet Alice
103
+ # Hello, Alice!
104
+ ```
105
+
106
+ ---
107
+
108
+ ## Add an Option
109
+
110
+ Use `method_option` (alias: `option`) to declare named flags:
111
+
112
+ ```ruby
113
+ class Tasks
114
+ desc "greet NAME", "Greet someone by name"
115
+ option :shout, aliases: "-s", type: :boolean, desc: "Uppercase the greeting"
116
+ def greet(name = "World")
117
+ msg = options[:shout] ? "HELLO, #{name.upcase}!" : "Hello, #{name}!"
118
+ sh "echo '#{msg}'"
119
+ end
120
+ end
121
+ ```
122
+
123
+ ```bash
124
+ asgard greet Alice --shout
125
+ # HELLO, ALICE!
126
+ ```
127
+
128
+ ---
129
+
130
+ ## Multi-Loki Structure
131
+
132
+ A large Asgard project might look like this:
133
+
134
+ ```
135
+ myproject/
136
+ .loki ← root marker and entry point (may be empty or contain tasks)
137
+ build.loki ← build-related and library dependency-related tasks
138
+ deploy.loki ← deployment tasks
139
+ qa.loki ← test and lint tasks
140
+ ```
141
+
142
+ Each `*.loki` file reopens `class Tasks`. To load them, pass `--auto-load` to the `asgard` command — they are loaded alphabetically before `.loki`. See [Task Files](task-files.md) for full details.
143
+
144
+ ---
145
+
146
+ ## Built-in Flags
147
+
148
+ Every task automatically has three flags available, defined as `class_option` on `Tasks`:
149
+
150
+ | Flag | Description |
151
+ |---|---|
152
+ | `--version` | Print the Asgard version and exit |
153
+ | `--debug` | Set `$DEBUG = true` before the task runs |
154
+ | `--verbose` | Set `$VERBOSE = true` before the task runs |
155
+
156
+ ```bash
157
+ asgard --version
158
+ asgard hello --debug
159
+ asgard hello --verbose
160
+ ```
161
+
162
+ Inside a task body, use the `debug?` and `verbose?` predicates:
163
+
164
+ ```ruby
165
+ def hello
166
+ sh "echo 'building...'"
167
+ sh "make --debug" if debug?
168
+ end
169
+ ```
170
+
171
+ ---
172
+
173
+ ## Next Steps
174
+
175
+ - [Defining Tasks](tasks.md) — parameters, options, aliases, long_desc
176
+ - [Dependencies](dependencies.md) — sequential, parallel, and mixed dependency graphs
177
+ - [Variables](variables.md) — share values across tasks
178
+ - [Shell Helpers](shell.md) — `sh`, `shebang`, and polyglot scripts
179
+ - [Subcommands](subcommands.md) — group related tasks under a namespace
180
+ - [Examples](examples.md) — working `.loki` files for every feature