Class: Jsus::CLI

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = Jsus::CLI.cli_options) ⇒ CLI

Returns a new instance of CLI.



26
27
28
# File 'lib/jsus/cli.rb', line 26

def initialize(options = Jsus::CLI.cli_options)
  @options = options
end

Class Attribute Details

.cli_optionsObject

Returns the value of attribute cli_options.



5
6
7
# File 'lib/jsus/cli.rb', line 5

def cli_options
  @cli_options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



24
25
26
# File 'lib/jsus/cli.rb', line 24

def options
  @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!(options)
  self.cli_options = options
  new.launch

  if options[:watch]
    input_dirs = [ options[:input_dir], options[:deps_dir] ].compact
    output_dir = options[: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

Returns:

  • (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 = options.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)
  message = <<-EOF
Pool stats:
Main package:
#{display_package @package}

Supplementary packages:
#{pool.packages.map {|package| display_package package}.join }

EOF
  Jsus.logger.info message
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_docsObject



155
156
157
158
159
160
161
# File 'lib/jsus/cli.rb', line 155

def generate_docs
  documenter = Jsus::Util::Documenter.new(:highlight_source => !options[:no_syntax_highlight])
  @package.source_files.each {|source| documenter << source }
  @pool.sources.each {|source| documenter << source }
  documenter.only(options[:documented_classes]).generate(@output_dir + "/docs")
  checkpoint(:documentation)
end

#generate_includesObject



149
150
151
152
153
# File 'lib/jsus/cli.rb', line 149

def generate_includes
  includes_root = Pathname.new(options[: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_filesObject



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 options[:without_scripts_info]
    File.open(options[: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

#launchObject



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 options[:display_pool_stats]

  @resulting_sources = @resulting_sources_container = @pool.compile_package(@package)
  @resulting_sources = post_process(@resulting_sources, options[:postproc]) if options[:postproc]
  @package_content = compile_package(@resulting_sources)
  package_filename = File.join(@output_dir, @package.filename)

  if options[: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 options[:generate_includes]
  generate_docs if options[:documented_classes] && !options[:documented_classes].empty?
  output_benchmarks
rescue Exception => e
  $stderr.puts "Exception was raised: #{e.inspect}\n\nBacktrace: #{e.backtrace.join("\n")}"
end

#load_packageObject



76
77
78
79
# File 'lib/jsus/cli.rb', line 76

def load_package
  package = Jsus::Package.new(Pathname.new(options[:input_dir]))
  package
end

#output_benchmarksObject



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 options[:benchmark]
    message = "Benchmarking results:\n"
    message << "Total execution time:   #{formatted_time_for(:all)}\n"
    message << "\n"
    message << "Of them:\n"
    message << "Pool preloading time:   #{formatted_time_for(:pool)}\n" if checkpoint?(:pool)
    message << "Docs generation time:   #{formatted_time_for(:documentation)}\n" if checkpoint?(:documentation)
    message << "Total compilation time: #{formatted_time_for(:compilation)}\n" if checkpoint?(:compilation)
    message << "Post-processing time:   #{formatted_time_for(:postproc)}\n" if checkpoint?(:postproc)
    message << "Compression time:       #{formatted_time_for(:compress)}\n" if checkpoint?(:compress)
    message << "\n"
    message << "Compression ratio: #{sprintf("%.2f%%", @compression_ratio * 100)}\n" if Jsus.verbose? && @compression_ratio
    Jsus.logger.info message
  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_poolObject



66
67
68
69
70
71
72
73
74
# File 'lib/jsus/cli.rb', line 66

def preload_pool
  result = if options[:deps_dir]
    Jsus::Pool.new(options[:deps_dir])
  else
    Jsus::Pool.new
  end
  checkpoint(:pool)
  result
end

#setup_output_directoryObject



30
31
32
33
34
# File 'lib/jsus/cli.rb', line 30

def setup_output_directory
  output_dir = File.expand_path(options[: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_sourcesObject



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}
  (options[: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