Class: Leg::Commands::BaseCommand

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

Direct Known Subclasses

Diff, Help, Pieces, Ref, Repo, 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."
  },
  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."
  }
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, config) ⇒ BaseCommand

Returns a new instance of BaseCommand.



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

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

Class Method Details

.inherited(subclass) ⇒ Object



11
12
13
# File 'lib/leg/commands/base_command.rb', line 11

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

.nameObject

Raises:

  • (NotImplementedError)


7
# File 'lib/leg/commands/base_command.rb', line 7

def self.name; raise NotImplementedError; end

.summaryObject

Raises:

  • (NotImplementedError)


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

def self.summary; raise NotImplementedError; end

Instance Method Details

#current_or_latest_stepObject



84
85
86
# File 'lib/leg/commands/base_command.rb', line 84

def current_or_latest_step
  current_step || latest_step
end

#current_stepObject



74
75
76
77
78
# File 'lib/leg/commands/base_command.rb', line 74

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

#latest_stepObject



80
81
82
# File 'lib/leg/commands/base_command.rb', line 80

def latest_step
  steps.last
end

#needs!(*whats) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/leg/commands/base_command.rb', line 36

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 :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"))
    else
      raise NotImplementedError
    end

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

#runObject

Raises:

  • (NotImplementedError)


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

def run; raise NotImplementedError; end

#select_step(step, &block) ⇒ Object



99
100
101
102
# File 'lib/leg/commands/base_command.rb', line 99

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

#step_name(step) ⇒ Object



88
89
90
91
92
93
# File 'lib/leg/commands/base_command.rb', line 88

def step_name(step)
  parts = step.split('-')
  if parts.length > 1
    parts[1..-1].join('-')
  end
end

#step_path(step) ⇒ Object



95
96
97
# File 'lib/leg/commands/base_command.rb', line 95

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

#stepsObject



67
68
69
70
71
72
# File 'lib/leg/commands/base_command.rb', line 67

def steps
  @steps ||= Dir[File.join(@config[:path], "steps/*")].map do |f|
    name = File.basename(f)
    name if File.directory?(f) && name =~ /\A\d+(\.\d+)*(-\w+)*\z/
  end.compact.sort_by { |s| s.split(".").map(&:to_i) }.reject { |s| s.to_i.zero? }
end