Class: Sprocketize::CLI

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

Constant Summary collapse

SPROCKETS_FILE =
'.sprocksrc'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sprocketize/cli.rb', line 36

def initialize(*args)
  @assets = Set.new
  @local_paths = Set.new
  @global_options = load_global_options
  input = parse_input(args)
  @root = input.delete(:root)
  @save = input.delete(:save)
  @local_options = load_local_options(@root)
  @local_options = merge_options(load_local_options(@root), input)
  @local_options.freeze #to preserve exact user input
end

Instance Attribute Details

#global_optionsObject

Returns the value of attribute global_options.



12
13
14
# File 'lib/sprocketize/cli.rb', line 12

def global_options
  @global_options
end

#local_optionsObject

Returns the value of attribute local_options.



12
13
14
# File 'lib/sprocketize/cli.rb', line 12

def local_options
  @local_options
end

Instance Method Details

#compileObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sprocketize/cli.rb', line 14

def compile
  env = Sprockets::Environment.new(@root)
  env.js_compressor = expand_js_compressor(@local_options[:js_compressor])
  env.css_compressor = expand_css_compressor(:yui) if @local_options[:compress_css]
  (@paths + (@global_options[:paths] || Set.new)).each {|p| env.append_path(p)}

  assets = local_options[:assets].map {|a| realpath(a).to_s}
  filter = Proc.new do |asset|
    assets.any? {|a| asset.pathname.to_s.start_with?(a)}
  end

  options = {
      :manifest => local_options[:manifest] || global_options[:manifest],
      :manifest_path => local_options[:manifest_path],
      :digest => local_options[:digest] || global_options[:digest],
      :gzip => local_options[:gzip] || global_options[:gzip]
  }

  compiler = Sprocketize::Compiler.new(env, realpath(@local_options[:target], true), [filter], options)
  compiler.compile
end

#merge_options(target, source) ⇒ Object



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

def merge_options(target, source)
  {
      :target => source[:target] || target[:target],
      :paths => (target[:paths] || Set.new).merge((source[:paths] || Set.new)),
      :assets => (target[:assets] || Set.new).merge((source[:assets] || Set.new)),
      :digest => (source[:digest].nil? ? target[:digest] : source[:digest]),
      :manifest => (source[:manifest].nil? ? target[:manifest] : source[:manifest]),
      :manifest_path => (source[:manifest_path] || target[:manifest_path]),
      :gzip => (source[:gzip].nil? ? target[:gzip] : source[:gzip]),
      :js_compressor => source[:js_compressor] || target[:js_compressor],
      :compress_css => source[:compress_css].nil? ? target[:compress_css] : source[:compress_css]
  }
end

#parserObject



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
102
103
104
105
106
107
108
109
110
111
# File 'lib/sprocketize/cli.rb', line 62

def parser
  OptionParser.new do |opts|
    opts.banner = "Usage: sprocketize [options] output_directory filename [filename ...]"

    opts.on("-a DIRECTORY", "--asset-root=DIRECTORY", "Assets root path.") do |dir|
      exit_if_non_existent(dir)
      @input[:root] = realpath(dir)
    end

    opts.on("-I DIRECTORY", "--include-dir=DIRECTORY", "Adds the directory to the Sprockets load path.") do |dir|
      exit_if_non_existent(dir)
      @input[:paths] << dir
    end

    opts.on("-d", "--digest", "Incorporates a MD5 digest into all filenames.") do
      @input[:digest] = true
    end

    opts.on("-m [DIRECTORY]", "--manifest [=DIRECTORY]", "Writes a manifest for the assets. If no directory is specified the manifest will be written to the output directory.") do |dir|
      @input[:manifest] = true
      @input[:manifest_path] = dir
    end

    opts.on("-g", "--gzip", "Also create a compressed version of all Stylesheets and Javascripts.") do |dir|
      @input[:gzip] = true
    end

    opts.on("-s", "--save", "Add given parameters to #{SPROCKETS_FILE}") do |dir|
      @input[:save] = true
    end

    opts.on("-j [COMPRESSOR]", "--compress-javascripts [=COMPRESSOR]", "Compress all Javascript using either closure, yui or uglifier. If no compiler is specified closure will be used.") do |compressor|
      @input[:js_compressor] = (compressor || 'closure').to_sym
    end

    opts.on("-c", "--compress-stylesheets", "Compress all Stylesheets with the YUI CSS compressor.") do
      @input[:compress_css] = true
    end

    opts.on_tail("-h", "--help", "Show this help message.") do
      show_usage
      exit
    end

    opts.on_tail("-v", "--version", "Show version.") do
      show_version
      exit
    end
  end
end

#puts_error(message) ⇒ Object



113
114
115
# File 'lib/sprocketize/cli.rb', line 113

def puts_error(message)
  puts "\e[31msprocketize: #{message}\e[0m"
end

#runObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/sprocketize/cli.rb', line 117

def run
  if @local_options[:target].to_s.length == 0
    puts_error "no output directory provided"
    show_usage
  elsif @local_options[:assets].length == 0
    puts_error "no assets provided"
    show_usage
  elsif !([:closure, :uglifier, :yui, nil].include?(@local_options[:js_compressor]))
    puts_error "unsupported javascript processor #{@local_options[:js_compressor]}"
    show_usage
  else
    @paths = Set.new
    @local_options[:assets].each do |a|
      a = realpath(a)
      @paths << (a.file? ? a.dirname : a)
    end
    @local_options[:paths].each {|p| @paths << realpath(p)}

    compile
    save_local_options if @save
  end
rescue OptionParser::InvalidOption => e
  puts_error e.message
  show_usage
rescue Sprockets::FileNotFound => e
  puts_error e.message
end

#show_usageObject



145
146
147
# File 'lib/sprocketize/cli.rb', line 145

def show_usage
  puts parser
end

#show_versionObject



149
150
151
# File 'lib/sprocketize/cli.rb', line 149

def show_version
  puts Sprocketize::VERSION
end