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



135
136
137
# File 'lib/saga/runner.rb', line 135

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

#autofill(filename) ⇒ Object



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

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

#convert(filename) ⇒ Object



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

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

#convert_optionsObject



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

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

#copy_template(destination) ⇒ Object



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

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

#file_pathObject



139
140
141
# File 'lib/saga/runner.rb', line 139

def file_path
  File.expand_path(@argv[0])
end

#new_fileObject



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

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
40
# 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 '    verify <dir>        - checks internal consistency of the document'
    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



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

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

#runObject



125
126
127
128
129
130
131
132
133
# File 'lib/saga/runner.rb', line 125

def run
  return unless options.run

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

#run_command(command) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/saga/runner.rb', line 104

def run_command(command)
  case command
  when 'new'
    puts new_file
  when 'convert'
    puts convert(file_path)
  when 'inspect'
    write_parsed_document(file_path)
  when 'autofill'
    puts autofill(file_path)
  when 'planning'
    puts planning(file_path)
  when 'template'
    copy_template(file_path)
  when 'verify'
    verify(file_path)
  else
    puts convert(File.expand_path(command))
  end
end

#verify(filename) ⇒ Object



100
101
102
# File 'lib/saga/runner.rb', line 100

def verify(filename)
  Saga::Verifier.new(Saga::Parser.parse(File.read(filename))).run
end

#write_parsed_document(filename) ⇒ Object



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

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