Class: Snaptoken::Commands::BaseCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/snaptoken/commands/base_command.rb

Direct Known Subclasses

Deploy, Diff, Doc, Fancy, Help, Ref, Repo, Sync, Undiff, Unrepo

Constant Summary collapse

ERROR_MSG =
{
  config: {
    true: "You are not in a leg working directory.",
    false: "You are already in a leg working directory."
  },
  config_sync: {
    true: "The :sync option in leg.yml must be set to 'repo' or 'steps'."
  },
  steps_folder: {
    true: "There is no steps folder.",
    false: "There is already a steps folder."
  },
  steps: {
    true: "There are no steps in the steps folder."
  },
  repo: {
    true: "There is no repo folder.",
    false: "There is already a repo folder."
  },
  diff: {
    true: "There is no steps.diff file."
  },
  doc: {
    true: "There is no doc folder."
  },
  doc_out: {
    true: "There are no doc output files."
  },
  cached_diffs: {
    true: "There are no cached diffs."
  },
  ftp: {
    true: "There is no ftp.yml file."
  }
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, config) ⇒ BaseCommand

Returns a new instance of BaseCommand.



2
3
4
5
6
# File 'lib/snaptoken/commands/base_command.rb', line 2

def initialize(args, config)
  @args = args
  @config = config
  parseopts!
end

Class Method Details

.inherited(subclass) ⇒ Object



13
14
15
# File 'lib/snaptoken/commands/base_command.rb', line 13

def self.inherited(subclass)
  Snaptoken::Commands::LIST << subclass
end

.nameObject

Raises:

  • (NotImplementedError)


8
# File 'lib/snaptoken/commands/base_command.rb', line 8

def self.name; raise NotImplementedError; end

.summaryObject

Raises:

  • (NotImplementedError)


9
# File 'lib/snaptoken/commands/base_command.rb', line 9

def self.summary; raise NotImplementedError; end

Instance Method Details

#current_or_latest_stepObject



132
133
134
# File 'lib/snaptoken/commands/base_command.rb', line 132

def current_or_latest_step
  current_step || latest_step
end

#current_stepObject



122
123
124
125
126
# File 'lib/snaptoken/commands/base_command.rb', line 122

def current_step
  if @config[:step_path]
    Snaptoken::Step.from_folder_name(File.basename(@config[:step_path]))
  end
end

#latest_stepObject



128
129
130
# File 'lib/snaptoken/commands/base_command.rb', line 128

def latest_step
  steps.last
end

#needs!(*whats) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/snaptoken/commands/base_command.rb', line 75

def needs!(*whats)
  options = whats.pop if whats.last.is_a? Hash
  options ||= {}

  yes = Array(whats).flatten.map { |w| [w, true] }
  no = Array(options[:not]).map { |w| [w, false] }

  (yes + no).each do |what, v|
    valid = false
    case what
    when :config
      valid = true if @config
    when :config_sync
      valid = true if %w(repo steps).include?(@config[:sync])
    when :steps_folder
      valid = true if File.exist?(File.join(@config[:path], "steps"))
    when :steps
      valid = true if steps.length > 0
    when :repo
      valid = true if File.exist?(File.join(@config[:path], "repo"))
    when :diff
      valid = true if File.exist?(File.join(@config[:path], "steps.diff"))
    when :doc
      valid = true if File.exist?(File.join(@config[:path], "doc"))
    when :doc_out
      valid = true if File.exist?(File.join(@config[:path], "doc/html_out"))
    when :cached_diffs
      valid = true if File.exist?(File.join(@config[:path], ".cached-diffs"))
    when :ftp
      valid = true if File.exist?(File.join(@config[:path], "ftp.yml"))
    else
      raise NotImplementedError
    end

    if valid != v
      puts "Error: " + ERROR_MSG[what][v.to_s.to_sym]
      exit!
    end
  end
end

#parseopts!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/snaptoken/commands/base_command.rb', line 17

def parseopts!
  parser = OptionParser.new do |o|
    o.banner =  "Usage: leg #{self.class.name} #{self.class.usage}"
    self.class.summary.split("\n").each do |line|
      o.separator "    #{line}"
    end
    o.separator ""
    o.separator "Options:"
    setopts!(o)
    o.on_tail("-h", "--help", "Show this message") do
      puts o
      exit
    end
  end
  @opts = {}
  parser.parse!(@args)
rescue OptionParser::InvalidOption => e
  puts "#{e.message}"
  puts
  parser.parse("--help")
end

#runObject

Raises:

  • (NotImplementedError)


11
# File 'lib/snaptoken/commands/base_command.rb', line 11

def run; raise NotImplementedError; end

#select_step(step, &block) ⇒ Object



140
141
142
143
# File 'lib/snaptoken/commands/base_command.rb', line 140

def select_step(step, &block)
  puts "Selecting step: #{step.folder_name}"
  FileUtils.cd(step_path(step), &block)
end

#setopts!(o) ⇒ Object

Raises:

  • (NotImplementedError)


10
# File 'lib/snaptoken/commands/base_command.rb', line 10

def setopts!(o); raise NotImplementedError; end

#step_path(step) ⇒ Object



136
137
138
# File 'lib/snaptoken/commands/base_command.rb', line 136

def step_path(step)
  File.join(@config[:path], "steps", step.folder_name)
end

#stepsObject



116
117
118
119
120
# File 'lib/snaptoken/commands/base_command.rb', line 116

def steps
  @steps ||= Dir[File.join(@config[:path], "steps/*")].map do |f|
    Snaptoken::Step.from_folder_name(File.basename(f)) if File.directory?(f)
  end.compact.sort_by(&:number)
end