Class: Hisyo::Generator

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/hisyo/generator.rb,
lib/hisyo/generator/test.rb,
lib/hisyo/generator/travis.rb,
lib/hisyo/generator/sprockets.rb

Constant Summary

Constants included from Util

Util::COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#color, #command, #copy, #create, #merge, #skip

Constructor Details

#initialize(argv = []) ⇒ Generator

Returns a new instance of Generator.



11
12
13
14
15
# File 'lib/hisyo/generator.rb', line 11

def initialize(argv = [])
  @params = {}
  @argv = argv
  parse
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/hisyo/generator.rb', line 9

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/hisyo/generator.rb', line 9

def params
  @params
end

Instance Method Details

#gen_assistanceObject



81
82
83
84
85
86
# File 'lib/hisyo/generator.rb', line 81

def gen_assistance
  unless method = generators.find{|g| g.to_s == "generate_#{options[:kind]}"}
    raise "unknown"
  end
  __send__(method)
end

#gen_projectObject



67
68
69
70
71
72
73
74
75
# File 'lib/hisyo/generator.rb', line 67

def gen_project
  root = options[:root]
  src_dir = File.expand_path("project", DIR)
  copy(src_dir)

  puts "Complete."
  puts "  $ cd #{root}/"
  puts '  $ rackup (or `rspec spec/`, `vim app/helpers.rb`, etc)'
end

#generate_sprocketsObject



3
4
5
6
7
# File 'lib/hisyo/generator/sprockets.rb', line 3

def generate_sprockets
  src = File.expand_path("assistance/sprockets", DIR)
  root = options[:root]
  copy(src)
end

#generate_testObject



4
5
6
7
8
# File 'lib/hisyo/generator/test.rb', line 4

def generate_test
  src = File.expand_path("assistance/test", DIR)
  root = options[:root]
  copy(src)
end

#generate_travisObject



3
4
5
6
7
8
9
10
# File 'lib/hisyo/generator/travis.rb', line 3

def generate_travis
  src = File.expand_path("assistance/travis", DIR)
  root = options[:root]
  @params = {
    "email" => command("git config user.email") || "[email protected]"
  }.merge(@params)
  copy(src)
end

#generatorsObject



77
78
79
# File 'lib/hisyo/generator.rb', line 77

def generators
  methods.grep(/^generate_/)
end

#parseObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hisyo/generator.rb', line 17

def parse
  @options = begin
    options = {
      :dryrun => false,
      :color => false,
      :verbose => true,
      :root => Dir.pwd,
    }
    OptionParser.new do |opts|
      opts.on('-n', '--dryrun', 'Do not actually run'){|v| options[:dryrun] = true}
      opts.on('-v', '--verbose', 'Verbose mode'){|v| options[:verbose] = true}
      opts.on('-q', '--quite', 'Non-verbose mode'){|v| options[:verbose] = false}
      opts.on('-c', '--color', 'Colorise'){|v| options[:color] = true}
      opts.on('-r VAL', '--root=VAL', 'Application root directory'){|v| options[:root] = v}

      opts.on('-k VAL', '--kind=VAL', 'What to generate'){|v| options[:kind] = v}

      opts.on_tail("-h", "--help", "Show this message") do
        puts opts
        puts
        puts "Hisyo assistance help e.g. `hisyo -k foo bar=baz`"
        puts HELP.join("\n")
        exit
      end

      begin
        opts.parse!(@argv)
      rescue OptionParser::InvalidOption => e
        $stderr.puts e
        $stderr.puts opts.help
        exit 1
      end
    end
    options
  end

  @argv.each do |kv|
    k,v = kv.split("=")
    @params[k] = v
  end
end

#runObject



59
60
61
62
63
64
65
# File 'lib/hisyo/generator.rb', line 59

def run
  if options[:kind]
    gen_assistance
  else
    gen_project
  end
end