Skip to content

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
end

Forward 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:

HelperForwardReverse
create_filecreates the fileremoves it
create_directorycreates the directoryremoves it
copy_filecopies the fileremoves the destination
gemadds to Gemfileremoves 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 }
end

up 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 }
end

skip_up is an alias for skip.