Appearance
Steps
Steps are the core execution unit in Loci generators. Every generator is expected to define at least one step, and the default BaseGenerator#call runs those steps in order.
Defining Steps
Each step has a name and a block. The block is evaluated in the generator instance, so all helpers and params are available.
ruby
class Users::ModelGenerator < Loci::BaseGenerator
arg :name, required: true
step "Create model file" do
path = "app/models/#{params.name}.rb"
create_file path, <<~RUBY
class #{params.name.to_class}
end
RUBY
end
endForward and Reverse Execution
Steps are executed in the order they are defined. When running with --reverse, Loci executes steps in reverse order and most helpers automatically perform the opposite action:
| Helper | Forward | Reverse |
|---|---|---|
create_file | creates the file | removes it |
create_directory | creates the directory | removes it |
copy_file | copies the file | removes the destination |
gem | adds to Gemfile | removes from Gemfile |
Some helpers have no safe inverse (e.g. system_command) and are skipped in reverse mode.
Inside a step, you can branch based on direction explicitly using up and down:
ruby
step "Create model file" do
path = "app/models/#{params.name}.rb"
up { create_file path, "class #{params.name}\nend\n" }
down { remove_file path }
endup runs only in forward mode. down runs only in reverse mode. Inside these blocks, automatic direction-switching is suppressed - the helper always performs its forward action. This means down { create_file path, content } creates the file on reverse rather than removing it.
Skipping Steps
Use skip to skip a step in forward mode, or skip_down to skip in reverse mode. The skip reason is recorded in the tracer output.
ruby
step "Create model file" do
path = "app/models/#{params.name}.rb"
# inline condition
skip "File already exists" if File.exist?(path)
# or with a block
skip("File already exists") { File.exist?(path) }
up { create_file path, "class #{params.name}\nend\n" }
down { remove_file path }
endskip_up is an alias for skip.