Class: Stairs::Step

Inherits:
Object
  • Object
show all
Defined in:
lib/stairs/step.rb

Defined Under Namespace

Classes: Choice

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Step

Returns a new instance of Step.



5
6
7
8
9
10
# File 'lib/stairs/step.rb', line 5

def initialize(*args)
  options = args.extract_options!
  @options = options.reverse_merge required: true

  @groups = args.first
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



3
4
5
# File 'lib/stairs/step.rb', line 3

def groups
  @groups
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/stairs/step.rb', line 3

def options
  @options
end

#step_descriptionObject



34
35
36
# File 'lib/stairs/step.rb', line 34

def step_description
  @step_description || self.class.step_description
end

#step_titleObject



30
31
32
# File 'lib/stairs/step.rb', line 30

def step_title
  @step_title || self.class.step_title
end

Class Method Details

.description(description) ⇒ Object



26
27
28
# File 'lib/stairs/step.rb', line 26

def self.description(description)
  self.step_description = description
end

.title(title) ⇒ Object



22
23
24
# File 'lib/stairs/step.rb', line 22

def self.title(title)
  self.step_title = title
end

Instance Method Details

#choice(*args, &block) ⇒ Object

Prompt user to make a choice



55
56
57
# File 'lib/stairs/step.rb', line 55

def choice(*args, &block)
  Choice.new(*args, &block).run
end

#env(name, value) ⇒ Object

Set or update env var



66
67
68
69
70
71
72
73
74
# File 'lib/stairs/step.rb', line 66

def env(name, value)
  ENV[name] = value

  if value
    Stairs.configuration.env_adapter.set name, value
  else
    Stairs.configuration.env_adapter.unset name
  end
end

#finish(message) ⇒ Object



105
106
107
108
# File 'lib/stairs/step.rb', line 105

def finish(message)
  puts "== All done!".green
  puts message.green
end

#group(*names) ⇒ Object



101
102
103
# File 'lib/stairs/step.rb', line 101

def group(*names)
  yield if !groups || (names & groups).any?
end

#provide(prompt, options = {}) ⇒ Object

Prompt user to provide input



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stairs/step.rb', line 39

def provide(prompt, options={})
  options.reverse_merge! required: true, default: nil
  required = options[:required] && !options[:default]

  prompt << " (leave blank for #{options[:default]})" if options[:default]
  prompt << ": "

  if Stairs.configuration.use_defaults && options[:default]
    options[:default]
  else
    Stairs::Util::CLI.collect(prompt.blue, required: required) ||
      options[:default]
  end
end

#rake(task) ⇒ Object



59
60
61
62
63
# File 'lib/stairs/step.rb', line 59

def rake(task)
  stairs_info "== Running #{task}"
  system "rake #{task}"
  stairs_info "== Completed #{task}"
end

#run!Object



12
13
14
15
16
# File 'lib/stairs/step.rb', line 12

def run!
  stairs_info "== Running #{step_title}"
  run if run_step?
  stairs_info "== Completed #{step_title}"
end

#setup(step_name, options = {}, &block) ⇒ Object

Embed a step where step_name is a symbol that can be resolved to a class in Stairs::Steps or a block is provided to be executed in an instance of Step



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/stairs/step.rb', line 89

def setup(step_name, options={}, &block)
  if block_given?
    Step.new(groups, options).tap do |step|
      step.define_singleton_method :run, &block
      step.step_title = step_name.to_s.titleize
    end.run!
  else
    klass = "Stairs::Steps::#{step_name.to_s.camelize}".constantize
    klass.new(groups, options).run!
  end
end

#stairs_info(message) ⇒ Object



110
111
112
# File 'lib/stairs/step.rb', line 110

def stairs_info(message)
  puts message.light_black
end

#write(string, filename) ⇒ Object

Replace contents of file



77
78
79
# File 'lib/stairs/step.rb', line 77

def write(string, filename)
  Util::FileMutation.write(string, filename)
end

#write_line(string, filename) ⇒ Object

Append line to file



82
83
84
# File 'lib/stairs/step.rb', line 82

def write_line(string, filename)
  Util::FileMutation.write_line(string, filename)
end