Class: Leg::Commands::BaseCommand

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

Direct Known Subclasses

Amend, Build, Commit, Diff, Help, Init, Reset, Resolve, Save, Status, Step

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, config) ⇒ BaseCommand

Returns a new instance of BaseCommand.



6
7
8
9
10
11
12
# File 'lib/leg/commands/base_command.rb', line 6

def initialize(args, config)
  @args = args
  @config = config
  @git = Leg::Representations::Git.new(@config)
  @litdiff = Leg::Representations::Litdiff.new(@config)
  parseopts!
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

Class Method Details

.inherited(subclass) ⇒ Object



19
20
21
# File 'lib/leg/commands/base_command.rb', line 19

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

.nameObject

Raises:

  • (NotImplementedError)


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

def self.name; raise NotImplementedError; end

.summaryObject

Raises:

  • (NotImplementedError)


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

def self.summary; raise NotImplementedError; end

Instance Method Details

#git_to_litdiff!Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/leg/commands/base_command.rb', line 68

def git_to_litdiff!
  tutorial = @git.load! do |step_num|
    output "\r\e[K[repo/ -> Tutorial] Step #{step_num}"
  end
  output "\n"

  num_steps = tutorial.num_steps
  @litdiff.save!(tutorial) do |step_num|
    output "\r\e[K[Tutorial -> doc/] Step #{step_num}/#{num_steps}"
  end
  output "\n"

  @config.synced!
end

#litdiff_to_git!Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/leg/commands/base_command.rb', line 83

def litdiff_to_git!
  tutorial = @litdiff.load! do |step_num|
    output "\r\e[K[doc/ -> Tutorial] Step #{step_num}"
  end
  output "\n"

  num_steps = tutorial.num_steps
  @git.save!(tutorial) do |step_num|
    output "\r\e[K[Tutorial -> repo/] Step #{step_num}/#{num_steps}"
  end
  output "\n"

  @config.synced!
end

#needs!(*whats) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/leg/commands/base_command.rb', line 45

def needs!(*whats)
  whats.each do |what|
    case what
    when :config
      if @config.nil?
        puts "Error: You are not in a leg working directory."
        exit 1
      end
    when :repo
      if @litdiff.modified? and @git.modified?
        puts "Error: doc/ and .leg/repo have diverged!"
        exit 1
      elsif @litdiff.modified? or !@git.exists?
        litdiff_to_git!
      end
    end
  end
end

#output(text) ⇒ Object



64
65
66
# File 'lib/leg/commands/base_command.rb', line 64

def output(text)
  print text unless @opts[:quiet]
end

#parseopts!Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/leg/commands/base_command.rb', line 23

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, OptionParser::InvalidArgument => e
  puts "#{e.message}"
  puts
  parser.parse("--help")
end

#runObject

Raises:

  • (NotImplementedError)


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

def run; raise NotImplementedError; end

#setopts!(o) ⇒ Object

Raises:

  • (NotImplementedError)


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

def setopts!(o); raise NotImplementedError; end