jscat 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/README.rdoc +24 -0
  2. data/lib/jscat.rb +69 -0
  3. data/spec/jscat_spec.rb +23 -0
  4. metadata +49 -0
data/README.rdoc ADDED
@@ -0,0 +1,24 @@
1
+ = jscat
2
+
3
+ Grabs all the javascript files in a directory and smooshes them into a single file for caching & transfer. Optional YUI compression, file order prioritisation and ignore list (so you don't recursively add the output to itself)
4
+
5
+ Do this:
6
+
7
+ javascript = JsCat.new({
8
+ :js_dir => 'scripts', # read javascript files from this directory
9
+ :compress => 'true', # uses/requires yui/compressor
10
+ :prioritize => ['framework.js'], # puts framework.js at the top of the file
11
+ :ignore => ["script_c.js"], # files to ignore
12
+ :output => "output/javascript.js" # write the compressed javsascript here
13
+ })
14
+
15
+ Get this:
16
+
17
+ var Framework={};function script_a(){alert("I am script a")}function script_b(){alert("I am script b")};
18
+
19
+
20
+ == Copyright
21
+
22
+ Copyright (c) 2011 Daniel Sim. See LICENSE.txt for
23
+ further details.
24
+
data/lib/jscat.rb ADDED
@@ -0,0 +1,69 @@
1
+ class JsCat
2
+ attr_reader :js
3
+ def initialize(params = {})
4
+ @js_dir = params[:js_dir] || "public"
5
+ @compress = params[:compress] || false
6
+ @output = params[:output] || false
7
+ @priority_files = params[:prioritize] || false
8
+ @ignore_files = params[:ignore] || false
9
+ require "yui/compressor" if @compress
10
+ render_js
11
+ write_js if @output
12
+ end
13
+ def list_scripts
14
+ @scripts = Dir.glob("#{@js_dir}/*.js")
15
+ if @ignore_files then
16
+ @ignore_files.uniq.each {|file|
17
+ @scripts.delete(@js_dir+'/'+file)
18
+ }
19
+ end
20
+ if @priority_files then
21
+ @priority_files_full = []
22
+ @priority_files.uniq.each {|file|
23
+ @priority_files_full << @js_dir+'/'+file
24
+ }
25
+ @priority_files_full.each {|file|
26
+ if @scripts.include? file then
27
+ @scripts.delete(file)
28
+ else
29
+ @priority_files_full.delete file
30
+ end
31
+ }
32
+ @scripts = @priority_files_full + @scripts
33
+ end
34
+ return @scripts
35
+ end
36
+ def set_scripts(scripts_array)
37
+ @scripts = scripts_array
38
+ end
39
+ def render_js
40
+ @js = ""
41
+ list_scripts.each { |script|
42
+ File.open(script, 'r') do |f1|
43
+ while line = f1.gets
44
+ @js = @js + line
45
+ end
46
+ @js = @js + "\n"
47
+ end
48
+ }
49
+ compress_js if @compress
50
+ return @js
51
+ end
52
+ def compress_js
53
+ begin
54
+ compressor = YUI::JavaScriptCompressor.new
55
+ compressed_js = compressor.compress(@js)
56
+ compress_success = true
57
+ puts "compressed js"
58
+ rescue Exception=>e
59
+ puts 'Warning: Could not write javascript file, syntax error in javascript'
60
+ end
61
+ @js = compressed_js if compress_success
62
+ return @js
63
+ end
64
+ def write_js
65
+ File.open(@output,'w') { |f|
66
+ f.write(@js)
67
+ }
68
+ end
69
+ end
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'yui/compressor'
3
+
4
+ require_relative '../lib/jscat'
5
+
6
+ describe JsCat do
7
+
8
+ before :all do
9
+
10
+ end
11
+
12
+ it 'should concatenate javascript without catching fire' do
13
+ javascript = JsCat.new({
14
+ :js_dir => 'scripts', #read javascript files from this directory
15
+ :compress => 'true', #uses/requires yui/compressor
16
+ :prioritize => ['framework.js'], #puts framework.js at the top of the file
17
+ :ignore => ["script_c.js"], #dont include these files
18
+ :output => "output/javascript.js" #write the file here
19
+ })
20
+ end
21
+
22
+
23
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jscat
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Sim
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-28 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Joins and compresses a directory of javascript files to a single file
15
+ email: dan@explodingbox.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.rdoc
21
+ - lib/jscat.rb
22
+ - spec/jscat_spec.rb
23
+ homepage: http://www.daniel-sim.com
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.6
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Grabs all the javascript files in a directory and smooshes them into a single
47
+ file for caching & transfer. Optional YUI compression, file order prioritisation
48
+ and ignore list (so you don't recursively add the output to itself)
49
+ test_files: []