Class: AssetPacker::CLI

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

Defined Under Namespace

Classes: ExitEarly

Constant Summary collapse

EXIT_SUCCESS =
0
EXIT_FAILURE =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(infile, outfile) ⇒ CLI

Returns a new instance of CLI.



21
22
23
24
# File 'lib/asset_packer/cli.rb', line 21

def initialize(infile, outfile)
  @infile  = infile
  @outfile = Pathname(outfile).expand_path
end

Instance Attribute Details

#infileObject (readonly)

Returns the value of attribute infile.



19
20
21
# File 'lib/asset_packer/cli.rb', line 19

def infile
  @infile
end

#outfileObject (readonly)

Returns the value of attribute outfile.



19
20
21
# File 'lib/asset_packer/cli.rb', line 19

def outfile
  @outfile
end

Class Method Details

.create_from_args(argv) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/asset_packer/cli.rb', line 37

def self.create_from_args(argv)
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: asset_packer [options] input_file.html output_file.html"
    opts.on('--version', 'Print asset_packers version') do |name|
      raise ExitEarly, "asset_packer-#{AssetPacker::VERSION}"
    end.on('--help', 'Display help on command line usage') do
      raise ExitEarly, opts
    end
  end

  opts.parse!(argv)

  if argv.length != 2
    raise ExitEarly.new(opts, EXIT_FAILURE)
  end

  new(*argv)
end

.run(argv) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/asset_packer/cli.rb', line 26

def self.run(argv)
  create_from_args(argv).run
  EXIT_SUCCESS
rescue ExitEarly => exception
  $stderr.puts(exception)
  exception.exit_code
rescue => exception
  $stderr.puts(exception)
  EXIT_FAILURE
end

Instance Method Details

#runObject



56
57
58
59
60
61
# File 'lib/asset_packer/cli.rb', line 56

def run
  asset_dir = outfile.dirname.join("#{outfile.basename(outfile.extname)}_assets")
  local = Processor::Local.new(infile, asset_dir, outfile)
  doc = Hexp.parse(local.retrieve_asset(infile))
  File.write(outfile, local.(doc).to_html)
end