Class: Saga::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/saga/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



6
7
8
# File 'lib/saga/runner.rb', line 6

def initialize(argv)
  @argv = argv
end

Instance Method Details

#authorObject



128
129
130
# File 'lib/saga/runner.rb', line 128

def author
  { name: `osascript -e "long user name of (system info)" &1> /dev/null`.strip }
end

#autofill(filename) ⇒ Object



78
79
80
81
82
# File 'lib/saga/runner.rb', line 78

def autofill(filename)
  document = Saga::Parser.parse(File.read(filename))
  document.autofill_ids
  Saga::Formatter.saga_format(document)
end

#convert(filename) ⇒ Object



64
65
66
# File 'lib/saga/runner.rb', line 64

def convert(filename)
  Saga::Formatter.format(Saga::Parser.parse(File.read(filename)), **convert_options)
end

#convert_optionsObject



58
59
60
61
62
# File 'lib/saga/runner.rb', line 58

def convert_options
  {
    template_path: options.template_path
  }.compact
end

#copy_template(destination) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/saga/runner.rb', line 88

def copy_template(destination)
  if File.exist?(destination)
    puts "The directory `#{destination}' already exists!"
  else
    require 'fileutils'
    FileUtils.mkdir_p(destination)
    FileUtils.cp(File.join(Saga::Formatter.template_path, 'default/helpers.rb'), destination)
    FileUtils.cp(File.join(Saga::Formatter.template_path, 'default/document.erb'), destination)
  end
end

#new_fileObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/saga/runner.rb', line 41

def new_file
  document = Saga::Document.new
  document.title = 'Title'
  document.authors << author
  document.stories[''] = [{
    description: 'As a writer I would like to write stories so developers can implement them.',
    id: 1,
    status: 'todo'
  }]
  document.definitions[''] = [{
    title: 'Writer',
    definition: 'Someone who is responsible for writing down requirements in the form of stories'
  }]

  Saga::Formatter.saga_format(document)
end

#optionsObject



10
11
12
13
14
15
16
# File 'lib/saga/runner.rb', line 10

def options
  unless defined?(@options)
    @options = OpenStruct.new(run: true)
    parser.parse!(@argv)
  end
  @options
end

#parserObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/saga/runner.rb', line 18

def parser
  @parser ||= OptionParser.new do |parser|
    parser.banner =  'Usage: saga [command]'
    parser.separator ''
    parser.separator 'Commands:'
    parser.separator '    new                 - prints a blank stub'
    parser.separator '    convert <filename>  - convert the stories to HTML'
    parser.separator '    inspect <filename>  - print the internals of the document'
    parser.separator '    autofill <filename> - adds an id to stories without one'
    parser.separator '    planning <filename> - shows the planning of stories in iterations'
    parser.separator '    template <dir>      - creates a template directory'
    parser.separator ''
    parser.separator 'Options:'
    parser.on('-t', '--template DIR', 'Use an external template for conversion to HTML') do |template_path|
      @options.template_path = File.expand_path(template_path)
    end
    parser.on('-h', '--help', 'Show help') do
      puts parser
      @options.run = false
    end
  end
end

#planning(filename) ⇒ Object



84
85
86
# File 'lib/saga/runner.rb', line 84

def planning(filename)
  Saga::Planning.new(Saga::Parser.parse(File.read(filename))).to_s
end

#runObject



118
119
120
121
122
123
124
125
126
# File 'lib/saga/runner.rb', line 118

def run
  return unless options.run

  if command = @argv.shift
    run_command(command)
  else
    puts parser.to_s
  end
end

#run_command(command) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/saga/runner.rb', line 99

def run_command(command)
  case command
  when 'new'
    puts new_file
  when 'convert'
    puts convert(File.expand_path(@argv[0]))
  when 'inspect'
    write_parsed_document(File.expand_path(@argv[0]))
  when 'autofill'
    puts autofill(File.expand_path(@argv[0]))
  when 'planning'
    puts planning(File.expand_path(@argv[0]))
  when 'template'
    copy_template(File.expand_path(@argv[0]))
  else
    puts convert(File.expand_path(command))
  end
end

#write_parsed_document(filename) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/saga/runner.rb', line 68

def write_parsed_document(filename)
  document = Saga::Parser.parse(File.read(filename))
  puts document.title
  document.authors.each { |author| p author }
  puts
  document.stories.each { |header, stories| puts header; stories.each { |story| p story } }
  puts
  document.definitions.each { |header, definitions| puts header; definitions.each { |definition| p definition } }
end