Class: Bunch::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/bunch/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output, opts) ⇒ CLI

Returns a new instance of CLI.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/bunch/cli.rb', line 3

def initialize(input, output, opts)
  @input  = Pathname.new(input)
  @output = output ? Pathname.new(output) : nil
  @opts   = opts

  if @opts[:server]
    run_server
  else
    generate_files
  end
end

Class Method Details

.parse_optsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bunch/cli.rb', line 59

def parse_opts
  options = {:individual => true}

  opts = OptionParser.new do |opts|
    opts.banner = 'Usage: bunch [options] INPUT_PATH [OUTPUT_PATH]'

    opts.on '-s', '--server', 'Instead of creating files, use WEBrick to serve files from INPUT_PATH.' do
      options[:server] = true
    end

    opts.on '-i', '--[no-]individual', 'Create one output file for each file or directory in the input path (default).' do |i|
      options[:individual] = i
    end

    opts.on '-a', '--all', 'Create an all.[extension] file combining all inputs.' do
      options[:all] = true
    end

    opts.on '-r', '--recurse', 'Recursively generate one output file for every input file and directory.' do
      options[:recurse] = true
    end

    opts.on_tail '-h', '--help', 'Show this message.' do
      puts opts
      exit
    end
  end

  opts.parse!

  if ARGV.count < 1
    raise "Must give an input path.\n\n#{opts}"
  end

  if ARGV.count < 2 && options[:individual] && !options[:server]
    raise "Must give an output path unless --no-individual or --server is provided."
  end

  input  = ARGV.shift
  output = ARGV.shift

  [input, output, options]
end

.process!Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/bunch/cli.rb', line 48

def process!
  CLI.new(*parse_opts)
rescue => e
  if ENV['BUNCH_DEBUG']
    raise
  else
    $stderr.puts "ERROR: #{e.message}"
    exit 1
  end
end

Instance Method Details

#generate_filesObject



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

def generate_files
  Bunch.load_ignores(@input)
  tree = Bunch.tree_for(@input.to_s, @opts.merge(:root => @input.to_s))

  if @output
    FileUtils.mkdir_p(@output.to_s)
  end

  if @opts[:all]
    if @output
      tree.write_to_file(@output.join('all'))
    else
      puts tree.content
    end
  end

  if @opts[:individual]
    tree.children.each do |child|
      child.write_to_dir(@output)
    end
  end
end

#run_serverObject



15
16
17
18
19
20
21
# File 'lib/bunch/cli.rb', line 15

def run_server
  require 'rack'
  ::Rack::Handler::WEBrick.run(Bunch::Rack.new(@input), :Port => 3001)
rescue LoadError
  $stderr.puts "ERROR: 'gem install rack' to run Bunch in server mode."
  exit 1
end