Class: Jammit::CommandLine

Inherits:
Object
Defined in:
lib/jammit/command_line.rb

Overview

The CommandLine is able to compress, pre-package, and pre-gzip all the assets specified in the configuration file, in order to avoid an initial round of slow requests after a fresh deployment.

Constant Summary

"\nUsage: jammit OPTIONS\n\nRun jammit inside a Rails application to compresses all JS, CSS,\nand JST according to config/assets.yml, saving the packaged\nfiles and corresponding gzipped versions.\n\nIf you're using \"embed_assets\", and you wish to precompile the\nMHTML stylesheet variants, you must specify the \"base-url\".\n\nOptions:\n"

Method Summary

Constructor Details

- (CommandLine) initialize

The Jammit::CommandLine runs from the contents of ARGV.

Returns:



27
28
29
30
31
32
33
# File 'lib/jammit/command_line.rb', line 27

def initialize
  parse_options
  ensure_configuration_file
  Jammit.load_configuration(@options[:config_path])
  Jammit.packager.force = @options[:force]
  Jammit.packager.precache_all(@options[:output_folder], @options[:base_url])
end

Method Details

- (Object) ensure_configuration_file (private)

Make sure that we have a readable configuration file. The jammit command can’t run without one.



40
41
42
43
44
45
# File 'lib/jammit/command_line.rb', line 40

def ensure_configuration_file
  config = @options[:config_path]
  return true if File.exists?(config) && File.readable?(config)
  puts "Could not find the asset configuration file \"#{config}\""
  exit(1)
end

- (Object) parse_options (private)

Uses OptionParser to grab the options: —output, —config, and —base-url



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/jammit/command_line.rb', line 49

def parse_options
  @options = {
    :config_path    => Jammit::DEFAULT_CONFIG_PATH,
    :output_folder  => nil,
    :base_url       => nil,
    :force          => false
  }
  @option_parser = OptionParser.new do |opts|
    opts.on('-o', '--output PATH', 'output folder for packages (default: "public/assets")') do |output_folder|
      @options[:output_folder] = output_folder
    end
    opts.on('-c', '--config PATH', 'path to assets.yml (default: "config/assets.yml")') do |config_path|
      @options[:config_path] = config_path
    end
    opts.on('-u', '--base-url URL', 'base URL for MHTML (ex: "http://example.com")') do |base_url|
      @options[:base_url] = base_url
    end
    opts.on('-f', '--force', 'force a rebuild of all assets') do |force|
      @options[:force] = force
    end
    opts.on_tail('-v', '--version', 'display Jammit version') do
      puts "Jammit version #{Jammit::VERSION}"
      exit
    end
  end
  @option_parser. = 
  @option_parser.parse!(ARGV)
end