Class: Jsus::CLI
- Inherits:
-
Object
- Object
- Jsus::CLI
- Defined in:
- lib/jsus/cli.rb
Class Attribute Summary collapse
-
.cli_options ⇒ Object
Returns the value of attribute cli_options.
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #checkpoint(checkpoint_name) ⇒ Object
- #checkpoint?(checkpoint_name) ⇒ Boolean
- #compile_package(package) ⇒ Object
- #compress_package(content) ⇒ Object
- #display_package(package) ⇒ Object
- #display_pool_stats(pool) ⇒ Object
- #formatted_time_for(checkpoint_name) ⇒ Object
- #generate_docs ⇒ Object
- #generate_includes ⇒ Object
- #generate_supplemental_files ⇒ Object
-
#initialize(options = Jsus::CLI.cli_options) ⇒ CLI
constructor
A new instance of CLI.
- #launch ⇒ Object
- #load_package ⇒ Object
- #output_benchmarks ⇒ Object
-
#post_process!(content, postproc) ⇒ Object
Modificate content string.
- #preload_pool ⇒ Object
- #setup_output_directory ⇒ Object
- #time_for(checkpoint_name) ⇒ Object
- #validate_sources ⇒ Object
Constructor Details
Class Attribute Details
.cli_options ⇒ Object
Returns the value of attribute cli_options.
5 6 7 |
# File 'lib/jsus/cli.rb', line 5 def @cli_options end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
23 24 25 |
# File 'lib/jsus/cli.rb', line 23 def @options end |
Class Method Details
.run!(options) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/jsus/cli.rb', line 7 def run!() self. = new.launch if [:watch] input_dirs = [ [:input_dir], [:deps_dir] ].compact Jsus.logger.info "Jsus enters watch mode, it will watch your files for changes and relaunch itself" Jsus::Util::Watcher.watch(input_dirs) do |filename| Jsus.logger.info "#{filename} changed, recompiling..." new.launch Jsus.logger.info "... done" end end end |
Instance Method Details
#checkpoint(checkpoint_name) ⇒ Object
180 181 182 183 184 185 186 187 188 |
# File 'lib/jsus/cli.rb', line 180 def checkpoint(checkpoint_name) @checkpoints ||= {} @time_for ||= {} @checkpoints[checkpoint_name] = Time.now if @last_checkpoint @time_for[checkpoint_name] = @checkpoints[checkpoint_name] - @last_checkpoint end @last_checkpoint = Time.now end |
#checkpoint?(checkpoint_name) ⇒ Boolean
190 191 192 |
# File 'lib/jsus/cli.rb', line 190 def checkpoint?(checkpoint_name) @checkpoints[checkpoint_name] end |
#compile_package(package) ⇒ Object
102 103 104 |
# File 'lib/jsus/cli.rb', line 102 def compile_package(package) package.compile(nil).tap { checkpoint(:compilation) } end |
#compress_package(content) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/jsus/cli.rb', line 112 def compress_package(content) compression_method = .fetch(:compression_method, :yui) compressed_content = Jsus::Util::Compressor.compress(content, :method => compression_method) if compressed_content != "" @compression_ratio = compressed_content.size.to_f / content.size.to_f else @compression_ratio = 1.00 Jsus.logger.error "YUI compressor could not parse input. \n" << "Compressor command used: #{compressor.command.join(' ')}" end checkpoint(:compress) compressed_content end |
#display_package(package) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/jsus/cli.rb', line 92 def display_package(package) result = "Package: #{package.name}\n" package.source_files.to_a.sort_by {|sf| sf.filename}.each do |sf| result << " [#{sf.relative_filename}]\n" result << " Provides: [#{sf.provides_names.join(", ")}]\n" result << " Requires: [#{sf.requires_names.join(", ")}]\n" end result << "\n" end |
#display_pool_stats(pool) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/jsus/cli.rb', line 78 def display_pool_stats(pool) checkpoint(:pool_stats) = <<-EOF Pool stats: Main package: #{display_package @package} Supplementary packages: #{pool.packages.map {|package| display_package package}.join } EOF Jsus.logger.info end |
#formatted_time_for(checkpoint_name) ⇒ Object
202 203 204 |
# File 'lib/jsus/cli.rb', line 202 def formatted_time_for(checkpoint_name) "#{format("%.3f", time_for(checkpoint_name))}s" end |
#generate_docs ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/jsus/cli.rb', line 139 def generate_docs documenter = Jsus::Util::Documenter.new(:highlight_source => ![:no_syntax_highlight]) @package.source_files.each {|source| documenter << source } @pool.sources.each {|source| documenter << source } documenter.only([:documented_classes]).generate(@output_dir + "/docs") checkpoint(:documentation) end |
#generate_includes ⇒ Object
133 134 135 136 137 |
# File 'lib/jsus/cli.rb', line 133 def generate_includes includes_root = Pathname.new([:includes_root] || @output_dir) Compiler.generate_includes(@package, includes_root, @output_dir + "includes.js") checkpoint(:includes) end |
#generate_supplemental_files ⇒ Object
127 128 129 130 131 |
# File 'lib/jsus/cli.rb', line 127 def generate_supplemental_files @package.generate_scripts_info(@output_dir) unless [:without_scripts_info] @package.generate_tree(@output_dir) unless [:without_tree_info] checkpoint(:supplemental_files) end |
#launch ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/jsus/cli.rb', line 35 def launch checkpoint(:start) @output_dir = setup_output_directory @pool = preload_pool @package = load_package display_pool_stats(@pool) if [:display_pool_stats] @package_content = compile_package(@package) post_process!(@package_content, [:postproc]) if [:postproc] package_filename = @output_dir + @package.filename if [:compress] File.open(package_filename.to_s.chomp(".js") + ".min.js", 'w') do |f| f.write compress_package(@package_content) end end package_filename.open('w') {|f| f << @package_content } generate_supplemental_files validate_sources generate_includes if [:generate_includes] generate_docs if [:documented_classes] && ![:documented_classes].empty? output_benchmarks rescue Exception => e output_benchmarks end |
#load_package ⇒ Object
71 72 73 74 75 76 |
# File 'lib/jsus/cli.rb', line 71 def load_package package = Jsus::Package.new(Pathname.new([:input_dir]), :pool => @pool) package.include_dependencies! checkpoint(:dependencies) package end |
#output_benchmarks ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/jsus/cli.rb', line 163 def output_benchmarks if [:benchmark] = "Benchmarking results:\n" << "Total execution time: #{formatted_time_for(:all)}\n" << "\n" << "Of them:\n" << "Pool preloading time: #{formatted_time_for(:pool)}\n" if checkpoint?(:pool) << "Docs generation time: #{formatted_time_for(:documentation)}\n" if checkpoint?(:documentation) << "Total compilation time: #{formatted_time_for(:compilation)}\n" if checkpoint?(:compilation) << "Post-processing time: #{formatted_time_for(:postproc)}\n" if checkpoint?(:postproc) << "Compression time: #{formatted_time_for(:compress)}\n" if checkpoint?(:compress) << "\n" << "Compression ratio: #{sprintf("%.2f%%", @compression_ratio * 100)}\n" if Jsus.verbose? && @compression_ratio Jsus.logger.info end end |
#post_process!(content, postproc) ⇒ Object
Modificate content string
107 108 109 110 |
# File 'lib/jsus/cli.rb', line 107 def post_process!(content, postproc) Compiler.post_process!(content, postproc) checkpoint(:postproc) end |
#preload_pool ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/jsus/cli.rb', line 63 def preload_pool if [:deps_dir] Jsus::Pool.new([:deps_dir]) else Jsus::Pool.new end.tap { checkpoint(:pool) } end |
#setup_output_directory ⇒ Object
29 30 31 32 33 |
# File 'lib/jsus/cli.rb', line 29 def setup_output_directory output_dir = Pathname.new([:output_dir]) output_dir.mkpath output_dir end |
#time_for(checkpoint_name) ⇒ Object
194 195 196 197 198 199 200 |
# File 'lib/jsus/cli.rb', line 194 def time_for(checkpoint_name) if checkpoint_name == :all @last_checkpoint - @checkpoints[:start] else @time_for[checkpoint_name] end end |
#validate_sources ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/jsus/cli.rb', line 147 def validate_sources validators_map = {"mooforge" => Jsus::Util::Validator::Mooforge} ([:validators] || []).each do |validator_name| if validator = validators_map[validator_name] errors = validator.new(@pool.sources.to_a & @package.source_files.to_a).validation_errors unless errors.empty? Jsus.logger.info "Validator #{validator_name} found errors: " << errors.map {|e| Jsus.logger.info " * #{e}"}.join("\n") end else Jsus.logger.info "No such validator: #{validator_name}" end end checkpoint(:validators) end |