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(sources) ⇒ 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(source_files, processors) ⇒ 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.
24 25 26 |
# File 'lib/jsus/cli.rb', line 24 def @options end |
Class Method Details
.run!(options) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/jsus/cli.rb', line 7 def run!() self. = new.launch if [:watch] input_dirs = [ [:input_dir], [:deps_dir] ].compact output_dir = [:output_dir] Jsus.logger.info "Jsus enters watch mode, it will watch your files for changes and relaunch itself" Jsus::Util::Watcher.watch(input_dirs, output_dir) do |filename| Jsus.logger.info "#{filename} changed, recompiling..." new.launch Jsus.logger.info "... done" end end end |
Instance Method Details
#checkpoint(checkpoint_name) ⇒ Object
196 197 198 199 200 201 202 203 204 |
# File 'lib/jsus/cli.rb', line 196 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
206 207 208 |
# File 'lib/jsus/cli.rb', line 206 def checkpoint?(checkpoint_name) @checkpoints[checkpoint_name] end |
#compile_package(sources) ⇒ Object
105 106 107 108 109 |
# File 'lib/jsus/cli.rb', line 105 def compile_package(sources) result = Packager.new(sources).pack(nil) checkpoint(:compilation) result end |
#compress_package(content) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/jsus/cli.rb', line 118 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
95 96 97 98 99 100 101 102 103 |
# File 'lib/jsus/cli.rb', line 95 def display_package(package) result = "Package: #{package.name}\n" package.source_files.to_a.sort_by {|sf| sf.filename}.each do |sf| result << " [#{sf.filename}]\n" result << " Provides: [#{sf.provides.map {|tag| tag.full_name }.join(", ")}]\n" result << " Requires: [#{sf.requires.map {|tag| tag.full_name }.join(", ")}]\n" end result << "\n" end |
#display_pool_stats(pool) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/jsus/cli.rb', line 81 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
218 219 220 |
# File 'lib/jsus/cli.rb', line 218 def formatted_time_for(checkpoint_name) "#{format("%.3f", time_for(checkpoint_name))}s" end |
#generate_docs ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/jsus/cli.rb', line 155 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
149 150 151 152 153 |
# File 'lib/jsus/cli.rb', line 149 def generate_includes includes_root = Pathname.new([:includes_root] || @output_dir).to_s File.open(File.join(@output_dir, "includes.js"), "w+") {|f| f.puts Util::CodeGenerator.generate_includes(@resulting_sources.required_files(includes_root)) } checkpoint(:includes) end |
#generate_supplemental_files ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/jsus/cli.rb', line 133 def generate_supplemental_files unless [:without_scripts_info] File.open([:output_dir] + "/scripts.json", "w+") do |f| scripts_hash = { @package.name => { :desc => @package.description, :provides => @resulting_sources_container.provides.map {|tag| tag.to_s}, :requires => @resulting_sources_container.requires.map {|tag| tag.to_s} } } f.puts JSON.pretty_generate(scripts_hash) end end checkpoint(:supplemental_files) end |
#launch ⇒ Object
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 62 63 64 |
# File 'lib/jsus/cli.rb', line 36 def launch checkpoint(:start) @output_dir = setup_output_directory @pool = preload_pool @package = load_package @pool << @package display_pool_stats(@pool) if [:display_pool_stats] @resulting_sources = @resulting_sources_container = @pool.compile_package(@package) @resulting_sources = post_process(@resulting_sources, [:postproc]) if [:postproc] @package_content = compile_package(@resulting_sources) package_filename = File.join(@output_dir, @package.filename) if [:compress] File.open(package_filename.chomp(".js") + ".min.js", 'w') do |f| f.write compress_package(@package_content) end end File.open(package_filename, '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 $stderr.puts "Exception was raised: #{e.inspect}\n\nBacktrace: #{e.backtrace.join("\n")}" end |
#load_package ⇒ Object
76 77 78 79 |
# File 'lib/jsus/cli.rb', line 76 def load_package package = Jsus::Package.new(Pathname.new([:input_dir])) package end |
#output_benchmarks ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/jsus/cli.rb', line 179 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(source_files, processors) ⇒ Object
Modificate content string
112 113 114 115 116 |
# File 'lib/jsus/cli.rb', line 112 def post_process(source_files, processors) result = Util::PostProcessor.process(source_files, processors) checkpoint(:postproc) result end |
#preload_pool ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/jsus/cli.rb', line 66 def preload_pool result = if [:deps_dir] Jsus::Pool.new([:deps_dir]) else Jsus::Pool.new end checkpoint(:pool) result end |
#setup_output_directory ⇒ Object
30 31 32 33 34 |
# File 'lib/jsus/cli.rb', line 30 def setup_output_directory output_dir = File.([:output_dir]) FileUtils.mkdir_p(output_dir) output_dir end |
#time_for(checkpoint_name) ⇒ Object
210 211 212 213 214 215 216 |
# File 'lib/jsus/cli.rb', line 210 def time_for(checkpoint_name) if checkpoint_name == :all @last_checkpoint - @checkpoints[:start] else @time_for[checkpoint_name] end end |
#validate_sources ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/jsus/cli.rb', line 163 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 |