Class: Sprocketize::CLI
- Inherits:
-
Object
- Object
- Sprocketize::CLI
- Defined in:
- lib/sprocketize/cli.rb
Constant Summary collapse
- SPROCKETS_FILE =
'.sprocksrc'
Instance Attribute Summary collapse
-
#global_options ⇒ Object
Returns the value of attribute global_options.
-
#local_options ⇒ Object
Returns the value of attribute local_options.
Instance Method Summary collapse
- #compile ⇒ Object
-
#initialize(*args) ⇒ CLI
constructor
A new instance of CLI.
- #merge_options(target, source) ⇒ Object
- #parser ⇒ Object
- #puts_error(message) ⇒ Object
- #run ⇒ Object
- #show_usage ⇒ Object
- #show_version ⇒ Object
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 = input = parse_input(args) @root = input.delete(:root) @save = input.delete(:save) @local_options = (@root) @local_options = ((@root), input) @local_options.freeze #to preserve exact user input end |
Instance Attribute Details
#global_options ⇒ Object
Returns the value of attribute global_options.
12 13 14 |
# File 'lib/sprocketize/cli.rb', line 12 def @global_options end |
#local_options ⇒ Object
Returns the value of attribute local_options.
12 13 14 |
# File 'lib/sprocketize/cli.rb', line 12 def @local_options end |
Instance Method Details
#compile ⇒ Object
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 = (@local_options[:js_compressor]) env.css_compressor = (:yui) if @local_options[:compress_css] (@paths + (@global_options[:paths] || Set.new)).each {|p| env.append_path(p)} assets = [:assets].map {|a| realpath(a).to_s} filter = Proc.new do |asset| assets.any? {|a| asset.pathname.to_s.start_with?(a)} end = { :manifest => [:manifest] || [:manifest], :manifest_path => [:manifest_path], :digest => [:digest] || [:digest], :gzip => [:gzip] || [:gzip] } compiler = Sprocketize::Compiler.new(env, realpath(@local_options[:target], true), [filter], ) 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 (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 |
#parser ⇒ Object
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. = "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() puts "\e[31msprocketize: #{}\e[0m" end |
#run ⇒ Object
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 if @save end rescue OptionParser::InvalidOption => e puts_error e. show_usage rescue Sprockets::FileNotFound => e puts_error e. end |
#show_usage ⇒ Object
145 146 147 |
# File 'lib/sprocketize/cli.rb', line 145 def show_usage puts parser end |
#show_version ⇒ Object
149 150 151 |
# File 'lib/sprocketize/cli.rb', line 149 def show_version puts Sprocketize::VERSION end |