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(options = {}) ⇒ Step

Returns a new instance of Step.



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

def initialize(options={})
  @options = options.reverse_merge required: true
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#step_descriptionObject



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

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

#step_titleObject



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

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

Class Method Details

.description(description) ⇒ Object



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

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

.title(title) ⇒ Object



19
20
21
# File 'lib/stairs/step.rb', line 19

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

Instance Method Details

#bundleObject



52
53
54
55
56
# File 'lib/stairs/step.rb', line 52

def bundle
  stairs_info "== Running bundle"
  system "bundle"
  stairs_info "== Completed bundle"
end

#choice(*args, &block) ⇒ Object

Prompt user to make a choice



48
49
50
# File 'lib/stairs/step.rb', line 48

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

#env(name, value) ⇒ Object

Set or update env var



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

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



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

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

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

Prompt user to provide input



36
37
38
39
40
41
42
43
44
45
# File 'lib/stairs/step.rb', line 36

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

  Stairs::Util::CLI.collect(prompt.blue, required: required) ||
    options[:default]
end

#rake(task) ⇒ Object



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

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

#run!Object



9
10
11
12
13
# File 'lib/stairs/step.rb', line 9

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



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

def setup(step_name, options={}, &block)
  if block_given?
    Step.new(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(options).run!
  end
end

#stairs_info(message) ⇒ Object



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

def stairs_info(message)
  puts message.light_black
end

#write(string, filename) ⇒ Object

Replace contents of file



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

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

#write_line(string, filename) ⇒ Object

Append line to file



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

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