Class: NibJS::Main
- Inherits:
-
Object
- Object
- NibJS::Main
- Defined in:
- lib/nibjs/main.rb
Overview
nibjs - Package and embed node.js/coffeescript application in your browser
SYNOPSIS
#{program_name} [--help] [--version] [FOLDER]
#summarized_options
DESCRIPTION
This command packages a complete javascript/coffeescript as a single .js file
to be embedded in the browser. Basically, it defines Node.js's exports and
require to work nicely. For this, we expect a project structure that respect
Node.js's package conventions (exports, require, index):
See typical use cases at http://blambeau.github.com/nib.js
Instance Attribute Summary collapse
-
#autorequire ⇒ Object
Add ‘libname = NibJS.require(libname)’ at end of script.
-
#coffee ⇒ Object
Look for .coffee files.
-
#coffee_compile ⇒ Object
Compile .coffee files.
-
#folder ⇒ Object
Source folder that we compile.
-
#footer ⇒ Object
Path to a file to add as footer.
-
#header ⇒ Object
Path to a licencing file to add as header.
-
#join ⇒ Object
Join the sources instead of treating them separately.
-
#libname ⇒ Object
Name of the library which is packaged.
-
#output ⇒ Object
IO or filename where to output result.
-
#standalone ⇒ Object
Embed NibJS in output.
-
#uglify ⇒ Object
Invoke ugligyjs on result.
Instance Method Summary collapse
- #__main_compile ⇒ Object
- #coffee_output? ⇒ Boolean
- #collect_on_files(folder) ⇒ Object
- #compile ⇒ Object
- #compile_coffee_source(code) ⇒ Object
- #execute(args) ⇒ Object
- #file2require(root_folder, file) ⇒ Object
-
#initialize ⇒ Main
constructor
A new instance of Main.
- #normalize_libname(folder, libname) ⇒ Object
-
#safe_run(cmd) ⇒ Object
Runs a command, returns result on STDOUT.
- #with_autorequire ⇒ Object
- #with_coffee_define(package) ⇒ Object
-
#with_coffee_registration(file) ⇒ Object
In coffee.
- #with_header_and_footer ⇒ Object
- #with_js_define(package) ⇒ Object
-
#with_js_registration(file) ⇒ Object
In javascript.
- #with_output ⇒ Object
- #with_standalone ⇒ Object
- #with_temp_file(content) ⇒ Object
- #with_uglify ⇒ Object
Constructor Details
#initialize ⇒ Main
Returns a new instance of Main.
54 55 56 |
# File 'lib/nibjs/main.rb', line 54 def initialize @output = STDOUT end |
Instance Attribute Details
#autorequire ⇒ Object
Add ‘libname = NibJS.require(libname)’ at end of script
40 41 42 |
# File 'lib/nibjs/main.rb', line 40 def autorequire @autorequire end |
#coffee ⇒ Object
Look for .coffee files
28 29 30 |
# File 'lib/nibjs/main.rb', line 28 def coffee @coffee end |
#coffee_compile ⇒ Object
Compile .coffee files
31 32 33 |
# File 'lib/nibjs/main.rb', line 31 def coffee_compile @coffee_compile end |
#folder ⇒ Object
Source folder that we compile
22 23 24 |
# File 'lib/nibjs/main.rb', line 22 def folder @folder end |
#footer ⇒ Object
Path to a file to add as footer
49 50 51 |
# File 'lib/nibjs/main.rb', line 49 def @footer end |
#header ⇒ Object
Path to a licencing file to add as header
46 47 48 |
# File 'lib/nibjs/main.rb', line 46 def header @header end |
#join ⇒ Object
Join the sources instead of treating them separately
34 35 36 |
# File 'lib/nibjs/main.rb', line 34 def join @join end |
#libname ⇒ Object
Name of the library which is packaged
25 26 27 |
# File 'lib/nibjs/main.rb', line 25 def libname @libname end |
#output ⇒ Object
IO or filename where to output result
52 53 54 |
# File 'lib/nibjs/main.rb', line 52 def output @output end |
#standalone ⇒ Object
Embed NibJS in output
43 44 45 |
# File 'lib/nibjs/main.rb', line 43 def standalone @standalone end |
#uglify ⇒ Object
Invoke ugligyjs on result
37 38 39 |
# File 'lib/nibjs/main.rb', line 37 def uglify @uglify end |
Instance Method Details
#__main_compile ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/nibjs/main.rb', line 212 def __main_compile if coffee code = with_coffee_define(libname){ collect_on_files(folder){|filepath, reqname| with_coffee_registration(reqname){ File.read(filepath) } }.join } if coffee_compile compile_coffee_source(code) else code end else with_js_define(libname){ collect_on_files(folder){|filepath, reqname| with_js_registration(reqname){ File.read(filepath) } }.join } end end |
#coffee_output? ⇒ Boolean
178 179 180 |
# File 'lib/nibjs/main.rb', line 178 def coffee_output? coffee && !coffee_compile end |
#collect_on_files(folder) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/nibjs/main.rb', line 191 def collect_on_files(folder) files = if coffee Dir["#{folder}/**/*.coffee"] else Dir["#{folder}/**/*.js"] end if join files.sort! content = files.collect{|f| File.read(f)}.join("\n") [ with_temp_file(content){|f| yield(f.path, './index')} ] else files.collect{|f| yield(f, file2require(folder, f))} end end |
#compile ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/nibjs/main.rb', line 290 def compile with_output{ with_uglify{ { with_standalone{ with_autorequire{ __main_compile } } } } } end |
#compile_coffee_source(code) ⇒ Object
206 207 208 209 210 |
# File 'lib/nibjs/main.rb', line 206 def compile_coffee_source(code) with_temp_file(code){|f| safe_run("cat #{f.path} | coffee --compile --stdio --bare") } end |
#execute(args) ⇒ Object
302 303 304 305 306 307 308 309 310 |
# File 'lib/nibjs/main.rb', line 302 def execute(args) if args.size == 1 @folder = File.(args[0]) @libname = normalize_libname(@folder, @libname) compile else raise Quickl::Help end end |
#file2require(root_folder, file) ⇒ Object
182 183 184 185 186 187 |
# File 'lib/nibjs/main.rb', line 182 def file2require(root_folder, file) root_folder, file = File.(root_folder), File.(file) stripped = file[(1+root_folder.size)..-1] stripped =~ /(.*)\.(js|coffee)$/ "./#{$1}" end |
#normalize_libname(folder, libname) ⇒ Object
170 171 172 173 174 175 176 |
# File 'lib/nibjs/main.rb', line 170 def normalize_libname(folder, libname) libname ||= File.basename(folder) if libname =~ /(.*)\.\w/ libname = $1 end libname end |
#safe_run(cmd) ⇒ Object
Runs a command, returns result on STDOUT. If the exit status was no 0, a RuntimeError is raised.
150 151 152 153 154 155 156 |
# File 'lib/nibjs/main.rb', line 150 def safe_run(cmd) res = `#{cmd}` unless $?.exitstatus == 0 raise RuntimeError, "Error while executing #{cmd}" end res end |
#with_autorequire ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/nibjs/main.rb', line 235 def with_autorequire code = yield if autorequire if coffee_output? code += "#{libname} = NibJS.require '#{libname}'\n" else code += "var #{libname} = NibJS.require('#{libname}');\n" end else code end end |
#with_coffee_define(package) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/nibjs/main.rb', line 121 def with_coffee_define(package) code = "" code += "NibJS.define '#{package}', (nibjs)->\n" code += yield.strip.gsub(/^/m, ' ') + "\n" code += "\n" code += " nibjs.require './index'\n" end |
#with_coffee_registration(file) ⇒ Object
In coffee
114 115 116 117 118 119 |
# File 'lib/nibjs/main.rb', line 114 def with_coffee_registration(file) code = "" code += "nibjs.register '#{file}', (exports, require)->\n" code += yield.strip.gsub(/^/m, ' ') + "\n" code += "\n" end |
#with_header_and_footer ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/nibjs/main.rb', line 262 def code = "" if header code += File.read(header) + "\n" end code += yield if code += "\n" + File.read() end code end |
#with_js_define(package) ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/nibjs/main.rb', line 138 def with_js_define(package) code = "" code += "NibJS.define('#{package}', function(nibjs) {\n" code += yield.strip.gsub(/^/m, ' ') + "\n" code += " return nibjs.require('./index');\n" code += "});\n" end |
#with_js_registration(file) ⇒ Object
In javascript
131 132 133 134 135 136 |
# File 'lib/nibjs/main.rb', line 131 def with_js_registration(file) code = "" code += "nibjs.register('#{file}', function(exports, require) {\n" code += yield.strip.gsub(/^/m, ' ') + "\n" code += "});\n" end |
#with_output ⇒ Object
282 283 284 285 286 287 288 |
# File 'lib/nibjs/main.rb', line 282 def with_output if String === output File.open(output, 'w'){|io| io << yield } else output << yield end end |
#with_standalone ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/nibjs/main.rb', line 248 def with_standalone if standalone code = if coffee_output? coffeesrc = File.read(File.('../../../src/nibjs.coffee', __FILE__)) "NibJSBuild = (exports)->\n" + coffeesrc.gsub(/^/m, ' ') + "\nNibJSBuild(this)\n" else File.read(File.("../../../dist/nibjs-#{NibJS::VERSION}.js", __FILE__)) end code += "\n\n" + yield else yield end end |
#with_temp_file(content) ⇒ Object
158 159 160 161 162 163 164 165 166 |
# File 'lib/nibjs/main.rb', line 158 def with_temp_file(content) require "tempfile" file = Tempfile.new('nibjs') file << content file.close res = yield(file) file.unlink res end |
#with_uglify ⇒ Object
274 275 276 277 278 279 280 |
# File 'lib/nibjs/main.rb', line 274 def with_uglify if uglify with_temp_file(yield){|f| safe_run("uglifyjs #{f.path}") } else yield end end |