join 0.0.3 → 0.0.5

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.
@@ -8,13 +8,9 @@ require 'join'
8
8
 
9
9
  #Get command line args
10
10
  args = ARGV.dup.unshift()
11
- dir = Dir.pwd
12
11
 
13
- if ARGV.empty?
14
- STDOUT.puts "You must supply a filename or directory"
15
- else
16
- join = Join::Concatenation.new(dir)
17
- modules = join.find_required_modules(args[0])
18
- join.concat(modules, args[1], args[0])
19
- end
12
+ compiler = Join::Compiler.new({:dest => "compiled.js", :compressed => true})
13
+ compiler.join(args)
14
+
15
+
20
16
 
@@ -1,20 +1,30 @@
1
- # Copyright (c) 20011 Owain Lewis. owain@owainlewis.com
1
+ # Copyright (c) Owain Lewis. owain@owainlewis.com
2
2
  # All files in this distribution are subject to the terms of the Ruby license.
3
3
 
4
+ # Join is an asset packer that joins together multiple files into one, typically for deployment
5
+ # If required, Join will compress JavaScript files with the Google Closure compiler
6
+
4
7
  $:.unshift File.dirname(__FILE__)
5
8
 
6
- require "fileutils"
7
- require "join/version"
8
- require 'join/process'
9
- require 'join/concatenate'
9
+ require 'join/version'
10
10
 
11
11
  module Join
12
-
13
- class << self
14
- def message(m)
15
- STDOUT.puts m
16
- end
17
- end
18
12
 
13
+ ROOT = File.expand_path(File.dirname(__FILE__) + '/')
14
+
15
+ COMPRESS = false
16
+
17
+ autoload :Base, 'join/base'
18
+ autoload :Compiler, 'join/compiler'
19
+ autoload :Assets, 'join/asset_processor'
20
+ autoload :Compressor, 'join/compressor'
21
+
19
22
  end
20
23
 
24
+ compiler = Join::Compiler.new({:dest => "compiled", :compressed => true})
25
+
26
+ compiler.join([
27
+ 'src/1.css',
28
+ 'src/2.css'
29
+ ])
30
+
@@ -0,0 +1,74 @@
1
+ module Join
2
+
3
+ # Takes multiple files and compiles them into a single file
4
+ class Compiler
5
+
6
+ attr_reader :options
7
+
8
+ def initialize(opts = {})
9
+ @dest = opts[:dest] || 'compressed.js'
10
+ @compressed = opts[:compressed] || false
11
+ @compressor = Join::Compressor.new
12
+ end
13
+
14
+ F_TYPES = {
15
+ :javascript => '.js',
16
+ :css => '.css'
17
+ }
18
+
19
+ # Delete or flush the contents of a given file.
20
+ def flush!(file)
21
+ File.open(file, 'w').truncate(0)
22
+ end
23
+
24
+ # We need this to determine what type of file we are dealing with
25
+ # before sending the data to the file compressor
26
+ def get_file_type(ext)
27
+ F_TYPES.key(ext)
28
+ end
29
+
30
+ # Open a file and append data to the file.
31
+ def append_to(filename, data)
32
+ begin
33
+ File.open(filename, 'a') do |f|
34
+ if @compressed == true
35
+ ext = get_ext(filename)
36
+ type = get_file_type(ext)
37
+ data = @compressor.compress(type, data)
38
+ end
39
+ f.puts data
40
+ end
41
+ show_success_message
42
+ rescue
43
+ STDOUT.puts "Cannot append to file"
44
+ exit #Terminate if file can't be found
45
+ end
46
+ end
47
+
48
+ def get_ext(path)
49
+ File.extname(path)
50
+ end
51
+
52
+ def show_success_message
53
+ STDOUT.puts "Successfully joined file for production"
54
+ end
55
+
56
+ def read_file(path)
57
+ raise "File does not exist" unless File.exists?(path)
58
+ File.open(path, 'r') do |f|
59
+ f.read
60
+ end
61
+ end
62
+
63
+ # When given an array of files, (e.g [file1.js, file2.js])
64
+ # join them together and output the result
65
+ # into the destination set in @options
66
+ def join(args)
67
+ flush!(@dest) if File.exists? @dest #Empty file if it already exists
68
+ result = args.map { |p| read_file(p) }.join("\n")
69
+ append_to(@dest, result)
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,38 @@
1
+ module Join
2
+
3
+ # Files get sent through the asset compressor
4
+ # to be minified before deployment
5
+
6
+ class Compressor
7
+
8
+ require 'closure-compiler'
9
+
10
+ class UnknownTypeError < StandardError; end
11
+
12
+ def compress(type, data)
13
+ if type == :javascript
14
+ compress_javascript(data)
15
+ elsif type == :css
16
+ compress_css(data)
17
+ else
18
+ throw UnknownTypeError
19
+ end
20
+ end
21
+
22
+ def compress_javascript(data)
23
+ Closure::Compiler.new.compile(data)
24
+ end
25
+
26
+ def compress_css(data)
27
+ data.gsub!(/\s+/, " ") #remove space
28
+ data.gsub(/\s/, '')
29
+ data.gsub!(/\/\*(.*?)\*\//, "") # remove comments
30
+ data.gsub!(/\n$/, "") # remove last break
31
+ data.gsub!(/ \{ /, " {") # trim inside brackets
32
+ data.gsub!(/; \}/, "}") # trim inside brackets
33
+ data
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -1,5 +1,5 @@
1
1
  module Join
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
4
4
 
5
5
 
@@ -0,0 +1,40 @@
1
+ hr {
2
+ display: block;
3
+ height: 1px;
4
+ border: 0;
5
+ border-top: 1px solid #cccccc;
6
+ margin: 20px 0 19px 0;
7
+ padding: 0; }
8
+
9
+ input, select {
10
+ vertical-align: middle; }
11
+
12
+ textarea {
13
+ overflow: auto; }
14
+
15
+ .ie6 legend, .ie7 legend {
16
+ margin-left: -7px; }
17
+
18
+ input[wtype="radio"] {
19
+ vertical-align: text-bottom; }
20
+
21
+ input[type="checkbox"] {
22
+ vertical-align: bottom; }
23
+
24
+ .ie7 input[type="checkbox"] {
25
+ vertical-align: baseline; }
26
+
27
+ .ie6 input {
28
+ vertical-align: text-bottom; }
29
+
30
+ label, input[type="button"], input[type="submit"], input[type="image"], button {
31
+ cursor: pointer; }
32
+
33
+ button, input, select, textarea {
34
+ margin: 0; }
35
+
36
+ body, h1, h2, h3, h4, h5, h6, p, ul, ol, dl, input, textarea {
37
+ line-height: 20px;
38
+ font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
39
+ color: gray;
40
+ font-size: 13px; }
@@ -0,0 +1,3 @@
1
+ var x = 56;
2
+
3
+ Object.prototype.join = function(){}
@@ -0,0 +1,23 @@
1
+ /* --------------------------------------------------------------
2
+ * Gravity CSS
3
+ * Version 0.1
4
+ * Developed by: Owain Lewis
5
+ * www.owainlewis.com
6
+ * License: MIT
7
+ * http://www.opensource.org/licenses/mit-license.php
8
+ *-------------------------------------------------------------- */
9
+ html, body, div, span, object, iframe,
10
+ h1, h2, h3, h4, h5, h6, p, blockquote, pre,
11
+ abbr, address, cite, code, del, dfn, em, img, ins, kbd, q, samp,
12
+ small, strong, sub, sup, var, b, i, dl, dt, dd, ol, ul, li,
13
+ fieldset, form, label, legend,
14
+ table, caption, tbody, tfoot, thead, tr, th, td,
15
+ article, aside, canvas, details, figcaption, figure,
16
+ footer, header, hgroup, menu, nav, section, summary,
17
+ time, mark, audio, video {
18
+ margin: 0;
19
+ padding: 0;
20
+ border: 0;
21
+ font-size: 100%;
22
+ font: inherit;
23
+ vertical-align: baseline; }
@@ -0,0 +1,6 @@
1
+ (function(){
2
+
3
+ //File number 1
4
+
5
+ });
6
+
data/readme.md CHANGED
@@ -1,68 +1,25 @@
1
1
  #Join
2
2
 
3
- Join is a Ruby gem for concatenating together multiple JavaScript files and libraries ready for deployment. The primary goals are
4
-
5
- + To provide a pseudo common.js module system for the browser. //= require myfile.js
6
- + To reduce the number of script tag calls on websites
7
- + To make combining multiple js files easier.
8
-
9
- Each JavaScript module is created with a time stamp and a path to the original source file.
3
+ Join is an asset manager for concatenating together files for deployment.
10
4
 
11
5
  ##Installation
12
6
 
13
- gem install join
7
+ gem install join
14
8
 
15
9
  ##Usage
16
-
17
- The basic idea is to have one main.js file that calls in other dependencies via a simple join statement:
18
10
 
19
- //= require path_to/somefile.js
11
+ join file1.js file2.js file3.js
12
+
13
+ ##Calling directly
20
14
 
21
- You can require other files using the Join require statement (//= require filename.js) like this
15
+ You can call the join compiler directly by using the compiler
22
16
 
23
- //= require cufon.js
24
- //= require lightbox.js
25
- //= require plugins/jquery.js
26
-
27
- (function(){
28
- //Application code here
29
- })();
30
-
31
- Then run the join-files command in the terminal to join together all your files
32
-
33
- join-compile source_file.js output_file.js
34
-
35
- Sample output should be something like this:
17
+ compiler = Join::Compiler.new({:dest => "compiled.js", :compressed => true})
36
18
 
37
- /*
38
- Compiled with Join at 2011-08-28 23:11:02 +0100
39
- from /Users/owainlewis/Projects/Ruby/jointest/app/other.js
40
- */
41
-
42
- Object.keys = function( obj ) {
43
- var array = new Array();
44
- for ( var prop in obj ) {
45
- if ( obj.hasOwnProperty( prop ) ) {
46
- array.push( prop );
47
- }
48
- }
49
- return array;
50
- };
51
-
52
- /*
53
- Compiled with Join at 2011-08-28 23:11:02 +0100
54
- from /Users/owainlewis/Projects/Ruby/jointest/app/main.js
55
- */
56
-
57
- //Main File
58
- //= other.js
59
-
60
- Object.defineProperties = function( obj, props ) {
61
- for ( var prop in props ) {
62
- Object.defineProperty( obj, prop, props[prop] );
63
- }
64
- };
65
-
66
- ## Known issues
19
+ #Join the following files into compiled.js
67
20
 
68
- There are a few path issues that I still need to address. Also, relative paths are currently required in the require statements ( //= require /app/main.js etc )
21
+ compiler.join([
22
+ 'src/1.js',
23
+ 'src/2.js'
24
+ ])
25
+
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: join
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,31 +9,32 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-11 00:00:00.000000000Z
12
+ date: 2011-09-18 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: A Ruby library for joining files together. Principally aimed at joining
15
15
  JavaScript modules
16
16
  email:
17
17
  - owain@owainlewis.com
18
18
  executables:
19
- - join-compile
19
+ - join
20
20
  extensions: []
21
21
  extra_rdoc_files: []
22
22
  files:
23
23
  - .gitignore
24
24
  - Gemfile
25
25
  - Rakefile
26
- - bin/join-compile
27
- - index.js
26
+ - bin/join
28
27
  - join.gemspec
29
28
  - lib/join.rb
30
- - lib/join/concatenate.rb
31
- - lib/join/process.rb
29
+ - lib/join/compiler.rb
30
+ - lib/join/compressor.rb
32
31
  - lib/join/version.rb
32
+ - lib/src/1.css
33
+ - lib/src/1.js
34
+ - lib/src/2.css
35
+ - lib/src/2.js
33
36
  - readme.md
34
- - test/compiled.js
35
- - test/include.js
36
- - test/index.js
37
+ - test/tc_join.rb
37
38
  homepage: ''
38
39
  licenses: []
39
40
  post_install_message:
@@ -60,6 +61,4 @@ specification_version: 3
60
61
  summary: A Ruby library for joining files together. Principally aimed at joining JavaScript
61
62
  modules
62
63
  test_files:
63
- - test/compiled.js
64
- - test/include.js
65
- - test/index.js
64
+ - test/tc_join.rb
data/index.js DELETED
File without changes
@@ -1,67 +0,0 @@
1
- module Join
2
- class Concatenation
3
-
4
- attr_reader :files
5
-
6
- def initialize(dir)
7
- @files = []
8
- @dir = dir
9
- @process = ::Join::Process.new()
10
- end
11
-
12
- #Return all js files in a directory
13
- def find_required_modules(dir)
14
- @process.required(read(@dir + handlePrefix(dir)))
15
- @process.required_modules()
16
- end
17
-
18
- #If the user omits a / from the path to the js folder we correct it for them
19
- def handlePrefix(s)
20
- if s.chars.first == '/'
21
- s = s
22
- else
23
- s = '/' + s
24
- end
25
- s
26
- end
27
-
28
- def exists?(filename)
29
- File.exists?(filename)
30
- end
31
-
32
- #Takes an array of files with absolute paths
33
- def concat(files, dest, original)
34
- File.new(dest, "w+") if !File.exists?(dest)
35
- files.each do |f|
36
- file = @dir + handlePrefix(f.fetch(0))
37
- puts file
38
- processFile(file) if exists?(file)
39
- end
40
- processFile(@dir + handlePrefix(original))
41
-
42
- write(@files, dest)
43
- ::Join.message("Joined #{@files.length} files to #{@dir + handlePrefix(dest)}")
44
- end
45
-
46
- def processFile(file_name)
47
- @files << "/*\nCompiled with Join at #{Time.now}\nfrom #{file_name}\n*/\n\n" + read(file_name) + "\n\n"
48
- end
49
-
50
- protected
51
-
52
- def read(file_name)
53
- file = File.open(file_name, "r")
54
- contents = file.read
55
- contents
56
- end
57
-
58
- #Write to file
59
- def write(content, dest)
60
- File.open(dest, 'w') do |file|
61
- file.puts content
62
- end
63
- end
64
-
65
- end
66
-
67
- end
@@ -1,19 +0,0 @@
1
- module Join
2
- class Process
3
-
4
- attr_reader :required_modules
5
-
6
- def initialize
7
- @required_modules = []
8
- end
9
-
10
- #Return an array of required modules and files
11
- def required(file)
12
- expr = /^\/\/= require\s?(.*)$/
13
- file.scan(expr) do |m|
14
- @required_modules << m
15
- end
16
- end
17
-
18
- end
19
- end
@@ -1,22 +0,0 @@
1
- /*
2
- Compiled with Join at 2011-09-11 18:16:25 +0100
3
- from /Users/owainlewis/Projects/ruby/Gems/Join/test/include.js
4
- */
5
-
6
- (function (){
7
-
8
- /* Include.js */
9
-
10
- }).call(this);
11
-
12
- /*
13
- Compiled with Join at 2011-09-11 18:16:25 +0100
14
- from /Users/owainlewis/Projects/ruby/Gems/Join/test/index.js
15
- */
16
-
17
- //= require include.js
18
-
19
-
20
-
21
-
22
-
@@ -1,5 +0,0 @@
1
- (function (){
2
-
3
- /* Include.js */
4
-
5
- }).call(this);
@@ -1,4 +0,0 @@
1
- //= require include.js
2
-
3
-
4
-