juscr 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ Copyright (c) 2010 Dayton D. Nolan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+ No attribution is required by products that make use of this software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ Except as contained in this notice, the name(s) of the above copyright
24
+ holders shall not be used in advertising or otherwise to promote the sale,
25
+ use or other dealings in this Software without prior written authorization.
26
+
27
+ Contributors to this project agree to grant all rights to the copyright
28
+ holder of the primary product. Attribution is maintained in the source
29
+ control history of the product.
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ $LOAD_PATH << '../lib'
4
+ require "rubygems"
5
+ require "simpleconsole"
6
+ require "juscrcompiler.rb"
7
+ require 'juscrconfigparser.rb'
8
+
9
+ class Controller < SimpleConsole::Controller
10
+ params :bool => {:c => :compress,
11
+ :h => :help},
12
+ :string => {:f => :files,
13
+ :d => :directory,
14
+ :n => :name}
15
+ def default
16
+ puts <<DOC
17
+ Usage: juscr [action] [options]
18
+
19
+ Description:
20
+ The juscr command line tool will compile and compress your javascript application into modules.
21
+
22
+ To compile and compress your javascript application into module files:
23
+
24
+ juscr compile -c
25
+
26
+ DOC
27
+ end
28
+
29
+ def compile
30
+ begin
31
+ raise ArgumentError, "You must specify an output filename: -n 'compressed.js" if params[:name].nil?
32
+
33
+ @root = Dir.getwd + '/'
34
+ @filename = params[:name]
35
+ @compress = params[:compress] ? params[:compress] : false
36
+
37
+ files_string = params[:files] ? params[:files] : false
38
+ directory = params[:directory] ? params[:directory] : false
39
+
40
+ if File.exists? @root + 'juscr_config.rb'
41
+ config = JuscrConfigParser.new @root + 'juscr_config.rb'
42
+ @compress = config.compress
43
+ @files = config.files
44
+ end
45
+
46
+ if directory
47
+ dir_without_prefix = directory.gsub(/^(\.+|\/)\/?/, '')
48
+ dir_without_suffix = dir_without_prefix.gsub(/\/$/, '')
49
+ directory = dir_without_suffix
50
+ @files = get_directory_contents directory
51
+ puts @files
52
+ end
53
+
54
+ if files_string
55
+ raise ArgumentError, "files must be a comma dilineated string of filenames: juscr compile -f 'fileone.js, filetwo.js'" if files_string =~ /(\[|\]|\'|\{|\}|\=|\>)/
56
+ @files = files_string.split(/\,\s*/)
57
+ end
58
+
59
+ @compiler = JuscrCompiler.new(@compress)
60
+ @compiler.compile(@files, @filename)
61
+ rescue RuntimeError => e
62
+ puts e.message
63
+ puts e.backtrace.inspect
64
+ end
65
+ end
66
+
67
+ def get_directory_contents(directory)
68
+ files = Array.new
69
+ begin
70
+ puts directory
71
+ Dir.entries("#{@root}#{directory}/").each do |file|
72
+ files.push directory + '/' + file if not file =~ /^\./
73
+ end
74
+ files
75
+ rescue Exception => e
76
+ puts e.message
77
+ puts e.backtrace.inspect
78
+ end
79
+ end
80
+
81
+ end
82
+
83
+ SimpleConsole::Application.run(ARGV, Controller)
@@ -0,0 +1,75 @@
1
+ require 'rubygems'
2
+ require 'jsmin'
3
+
4
+ class JuscrCompiler
5
+
6
+ attr_accessor :directory
7
+
8
+ def compress?
9
+ @compress
10
+ end
11
+
12
+ def initialize(compress = true)
13
+ begin
14
+ compress = compress.nil? ? false : true;
15
+ @compress = compress
16
+ @root = Dir.getwd + '/'
17
+ rescue RuntimeError => e
18
+ puts e.message
19
+ puts e.backtrace.inspect
20
+ end
21
+ end
22
+
23
+ def compile(files, filename, message = "// File generated #{Time.now.to_s} by juscr")
24
+ @files = (files.is_a? String) ? [files] : files
25
+ @filename = filename
26
+ @message = message + "\n"
27
+
28
+ merge
29
+ compress if @compress
30
+
31
+ @compiled_content = @merged.join("\n")
32
+
33
+ create_file
34
+ end
35
+
36
+ def merge
37
+ @merged = Array.new
38
+ @files.each do |file|
39
+ begin
40
+ raise IOError, "#{file} does not exist", caller unless File.exists? "#{@root}#{file}"
41
+ File.open("#{@root}#{file}", "r") do |the_file|
42
+ @merged.push read_file the_file unless the_file.nil?
43
+ end
44
+ rescue RuntimeError => e
45
+ puts e.message
46
+ puts e.backtrace.inspect
47
+ end
48
+ end
49
+ end
50
+
51
+ def compress
52
+ @merged.each_index do |i|
53
+ @merged[i] = JSMin.minify(@merged[i])
54
+ end
55
+ end
56
+
57
+ def read_file(file)
58
+ n = File.size?(file)
59
+ file.sysread(n)
60
+ end
61
+
62
+ def create_file
63
+ begin
64
+ is_new_file = (File.exists? "#{@root}#{@filename}") ? false : true
65
+ File.open("#{@root}#{@filename}", "w+") do |the_file|
66
+ the_file.syswrite @message + @compiled_content
67
+ puts is_new_file ? "created #{@root}#{@filename}" : "updated #{@root}#{@filename}"
68
+ end
69
+ rescue IOError => e
70
+ puts "#{e.class} #{e.message} #{e.backtrace.join($/)}"
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,25 @@
1
+ class JuscrConfigParser
2
+
3
+ attr_reader :compress, :files
4
+
5
+ def initialize(config_file)
6
+ begin
7
+ raise IOError, "#{config_file} does not exist", caller unless File.exists? config_file
8
+ @compress = false
9
+ @files = Array.new
10
+ lines = IO.readlines config_file
11
+ lines.each do |line|
12
+ if line =~ /compress\:\s*/
13
+ compress_value = line.gsub(/compress\:\s*/, '').strip
14
+ @compress = compress_value == 'true' ? true : false
15
+ else
16
+ line.gsub!(/\s*/, '')
17
+ @files.push line unless line.length < 1
18
+ end
19
+ end
20
+ rescue IOError => e
21
+ puts e.message
22
+ puts e.backtrace.inspect
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ module Test::Unit
2
+
3
+ class TestCase
4
+
5
+ def self.must(name, &block)
6
+ test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
7
+ defined = instance_method(test_name) rescue false
8
+ raise "#{test_name} is already defined in #{self}" if defined
9
+ if block_given?
10
+ define_method(test_name, &block)
11
+ else
12
+ define_method(test_name) do
13
+ flunk "No implementation provided for #{name}"
14
+ end
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1 @@
1
+ var one = "this is file one"
@@ -0,0 +1,2 @@
1
+ var two = 'This is file two';
2
+ var twob = 'file two has two lines';
@@ -0,0 +1,3 @@
1
+ function file_three() {
2
+ var 'this is file three';
3
+ }
@@ -0,0 +1,4 @@
1
+ var four = "this is file four";
2
+ function file_four() {
3
+ document.writeln(four);
4
+ }
@@ -0,0 +1,5 @@
1
+ var five = "this is file five";
2
+ function file_five() {
3
+ var five += "\nindeed it is";
4
+ }
5
+ document.writeln(five);
File without changes
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby -w
2
+ require 'test/unit'
3
+ require 'extensions.rb'
4
+ require '../lib/juscrcompiler.rb'
5
+
6
+ class JuscrcompilerTest < Test::Unit::TestCase
7
+ def setup
8
+ end
9
+
10
+ def teardown
11
+ end
12
+
13
+ must "contain defaults when initialized with no options" do
14
+ compiler = JuscrCompiler.new
15
+ assert_equal true, compiler.compress?
16
+ end
17
+
18
+ must "raise error when initialized with bad second param" do
19
+ assert_raises(ArgumentError) do
20
+ compiler = JuscrCompiler.new(true, Array.new)
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ require 'test/unit/ui/console/testrunner'
27
+ Test::Unit::UI::Console::TestRunner.run(JuscrcompilerTest)
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: juscr
3
+ version: !ruby/object:Gem::Version
4
+ hash: 55
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 10
9
+ - 0
10
+ version: 0.10.0
11
+ platform: ruby
12
+ authors:
13
+ - Dayton Nolan
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-25 00:00:00 -05:00
19
+ default_executable: bin/juscr
20
+ dependencies: []
21
+
22
+ description: Juscr is a javascript compiler that helps you create javascript applications that are easier to write, manage and deploy.
23
+ email: daytonn@gmail.com
24
+ executables:
25
+ - juscr
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - LICENSE.markdown
32
+ - bin/juscr
33
+ - lib/juscrcompiler.rb
34
+ - lib/juscrconfigparser.rb
35
+ - tests/extensions.rb
36
+ - tests/test_juscr_command_line.rb
37
+ - tests/test_juscrcompiler.rb
38
+ - tests/fixtures/files/file1.js
39
+ - tests/fixtures/files/file2.js
40
+ - tests/fixtures/files/file3.js
41
+ - tests/fixtures/files/file4.js
42
+ - tests/fixtures/files/file5.js
43
+ has_rdoc: true
44
+ homepage: http://daytonnolan.com/jucr
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project: juscr
73
+ rubygems_version: 1.3.7
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: A Javascript compiler
77
+ test_files: []
78
+