sstephenson-sprockets 0.1.1
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.
- data/Rakefile +16 -0
- data/bin/sprocketize +58 -0
- data/lib/sprockets/environment.rb +35 -0
- data/lib/sprockets/error.rb +4 -0
- data/lib/sprockets/output_file.rb +20 -0
- data/lib/sprockets/pathname.rb +33 -0
- data/lib/sprockets/preprocessor.rb +75 -0
- data/lib/sprockets/source_file.rb +26 -0
- data/lib/sprockets/source_line.rb +31 -0
- data/lib/sprockets/version.rb +9 -0
- data/lib/sprockets.rb +10 -0
- data/test/fixtures/double_slash_comments_that_are_not_requires_should_be_ignored_when_strip_comments_is_false.js +1 -0
- data/test/fixtures/double_slash_comments_that_are_not_requires_should_be_removed_by_default.js +2 -0
- data/test/fixtures/requiring_a_file_after_it_has_already_been_required_should_do_nothing.js +5 -0
- data/test/fixtures/requiring_a_file_that_does_not_exist_should_raise_an_error.js +1 -0
- data/test/fixtures/requiring_a_single_file_should_replace_the_require_comment_with_the_file_contents.js +3 -0
- data/test/fixtures/requiring_the_current_file_should_do_nothing.js +1 -0
- data/test/fixtures/src/foo/bar.js +4 -0
- data/test/fixtures/src/foo/foo.js +1 -0
- data/test/fixtures/src/foo.js +1 -0
- data/test/test_environment.rb +58 -0
- data/test/test_helper.rb +43 -0
- data/test/test_output_file.rb +20 -0
- data/test/test_pathname.rb +43 -0
- data/test/test_preprocessor.rb +92 -0
- data/test/test_source_file.rb +29 -0
- data/test/test_source_line.rb +44 -0
- metadata +89 -0
data/Rakefile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require "rubygems"
|
|
2
|
+
require "rake/testtask"
|
|
3
|
+
require "rake/gempackagetask"
|
|
4
|
+
|
|
5
|
+
task :default => :test
|
|
6
|
+
|
|
7
|
+
Rake::TestTask.new do |t|
|
|
8
|
+
t.libs << "test"
|
|
9
|
+
t.test_files = FileList["test/test_*.rb"]
|
|
10
|
+
t.verbose = true
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
Rake::GemPackageTask.new(eval(IO.read(File.join(File.dirname(__FILE__), "sprockets.gemspec")))) do |pkg|
|
|
14
|
+
pkg.need_zip = true
|
|
15
|
+
pkg.need_tar = true
|
|
16
|
+
end
|
data/bin/sprocketize
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require File.join(File.dirname(__FILE__), *%w".. lib sprockets")
|
|
4
|
+
require "optparse"
|
|
5
|
+
|
|
6
|
+
load_path = (ENV["SPROCKETS_PATH"] || "").split(":")
|
|
7
|
+
filenames = []
|
|
8
|
+
|
|
9
|
+
OptionParser.new do |opts|
|
|
10
|
+
opts.summary_width = 28
|
|
11
|
+
opts.banner = "Usage: sprocketize [options] filename [filename ...]"
|
|
12
|
+
|
|
13
|
+
def opts.show_usage
|
|
14
|
+
puts self
|
|
15
|
+
exit
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
opts.on("-C DIRECTORY", "--directory=DIRECTORY", "Change to DIRECTORY before doing anything") do |directory|
|
|
19
|
+
Dir.chdir(directory)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
opts.on("-I DIRECTORY", "--include-dir=DIRECTORY", "Adds the directory to the Sprockets load path") do |directory|
|
|
23
|
+
load_path << directory
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
opts.on_tail("-h", "--help", "Shows this help message") do
|
|
27
|
+
opts.show_usage
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
opts.on_tail("-v", "--version", "Shows version") do
|
|
31
|
+
puts Sprockets::Version::STRING
|
|
32
|
+
exit
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
opts.show_usage if ARGV.empty?
|
|
36
|
+
|
|
37
|
+
begin
|
|
38
|
+
opts.order(ARGV) do |filename|
|
|
39
|
+
filenames << filename
|
|
40
|
+
end
|
|
41
|
+
rescue OptionParser::ParseError => e
|
|
42
|
+
opts.warn e.message
|
|
43
|
+
opts.show_usage
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
environment = Sprockets::Environment.new(".", load_path)
|
|
48
|
+
preprocessor = Sprockets::Preprocessor.new(environment)
|
|
49
|
+
|
|
50
|
+
filenames.each do |filename|
|
|
51
|
+
if pathname = environment.find(filename)
|
|
52
|
+
preprocessor.require(pathname.source_file)
|
|
53
|
+
else
|
|
54
|
+
raise Sprockets::LoadError, "no such file `#{filename}'"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
print preprocessor.output_file
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module Sprockets
|
|
2
|
+
class Environment
|
|
3
|
+
attr_reader :root, :load_path
|
|
4
|
+
|
|
5
|
+
def initialize(root, load_path = [])
|
|
6
|
+
@load_path = [@root = Pathname.new(self, root)]
|
|
7
|
+
|
|
8
|
+
load_path.reverse_each do |location|
|
|
9
|
+
register_load_location(location)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def pathname_from(location)
|
|
14
|
+
Pathname.new(self, absolute_location_from(location))
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def register_load_location(location)
|
|
18
|
+
pathname = pathname_from(location)
|
|
19
|
+
load_path.delete(pathname)
|
|
20
|
+
load_path.unshift(pathname)
|
|
21
|
+
location
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def find(location)
|
|
25
|
+
load_path.map { |pathname| pathname.find(location) }.compact.first
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
protected
|
|
29
|
+
def absolute_location_from(location)
|
|
30
|
+
location = location.to_s
|
|
31
|
+
location = File.join(root.absolute_location, location) unless location[/^\//]
|
|
32
|
+
File.expand_path(location)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module Sprockets
|
|
2
|
+
class OutputFile
|
|
3
|
+
attr_reader :source_lines
|
|
4
|
+
|
|
5
|
+
def initialize
|
|
6
|
+
@source_lines = []
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def record(source_line)
|
|
10
|
+
@source_lines << source_line
|
|
11
|
+
source_line
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def to_s
|
|
15
|
+
@source_lines.map do |source_line|
|
|
16
|
+
source_line.line
|
|
17
|
+
end.join
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module Sprockets
|
|
2
|
+
class Pathname
|
|
3
|
+
attr_reader :environment, :absolute_location
|
|
4
|
+
|
|
5
|
+
def initialize(environment, absolute_location)
|
|
6
|
+
@environment = environment
|
|
7
|
+
@absolute_location = File.expand_path(absolute_location)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# Returns a Pathname for the location relative to this pathname's absolute location.
|
|
11
|
+
def find(location)
|
|
12
|
+
location = File.join(absolute_location, location)
|
|
13
|
+
File.file?(location) ? Pathname.new(environment, location) : nil
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def parent_pathname
|
|
17
|
+
Pathname.new(environment, File.dirname(absolute_location))
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def source_file
|
|
21
|
+
SourceFile.new(environment, self)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def ==(pathname)
|
|
25
|
+
environment == pathname.environment &&
|
|
26
|
+
absolute_location == pathname.absolute_location
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def to_s
|
|
30
|
+
absolute_location
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
module Sprockets
|
|
2
|
+
class Preprocessor
|
|
3
|
+
attr_reader :environment, :output_file, :source_files
|
|
4
|
+
|
|
5
|
+
def initialize(environment, options = {})
|
|
6
|
+
@environment = environment
|
|
7
|
+
@output_file = OutputFile.new
|
|
8
|
+
@source_files = []
|
|
9
|
+
@options = options
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def require(source_file)
|
|
13
|
+
return if source_files.include?(source_file)
|
|
14
|
+
source_files << source_file
|
|
15
|
+
|
|
16
|
+
source_file.each_source_line do |source_line|
|
|
17
|
+
if source_line.require?
|
|
18
|
+
require_from_source_line(source_line)
|
|
19
|
+
else
|
|
20
|
+
record_source_line(source_line)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
protected
|
|
26
|
+
attr_reader :options
|
|
27
|
+
|
|
28
|
+
def require_from_source_line(source_line)
|
|
29
|
+
require pathname_from(source_line).source_file
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def record_source_line(source_line)
|
|
33
|
+
unless source_line.comment? && strip_comments?
|
|
34
|
+
output_file.record(source_line)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def strip_comments?
|
|
39
|
+
options[:strip_comments] != false
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def pathname_from(source_line)
|
|
43
|
+
pathname = send(pathname_finder_from(source_line), source_line)
|
|
44
|
+
raise_load_error_for(source_line) unless pathname
|
|
45
|
+
pathname
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def pathname_for_require_from(source_line)
|
|
49
|
+
environment.find(location_from(source_line))
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def pathname_for_relative_require_from(source_line)
|
|
53
|
+
source_line.source_file.find(location_from(source_line))
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def pathname_finder_from(source_line)
|
|
57
|
+
"pathname_for_#{kind_of_require_from(source_line)}_from"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def kind_of_require_from(source_line)
|
|
61
|
+
source_line.require[/^(.)/, 1] == '"' ? :relative_require : :require
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def location_from(source_line)
|
|
65
|
+
location = source_line.require[/^.(.*).$/, 1]
|
|
66
|
+
File.join(File.dirname(location), File.basename(location, ".js") + ".js")
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def raise_load_error_for(source_line)
|
|
70
|
+
kind = kind_of_require_from(source_line).to_s.tr("_", " ")
|
|
71
|
+
file = File.split(location_from(source_line)).last
|
|
72
|
+
raise LoadError, "can't find file for #{kind} `#{file}' (#{source_line.inspect})"
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module Sprockets
|
|
2
|
+
class SourceFile
|
|
3
|
+
attr_reader :environment, :pathname
|
|
4
|
+
|
|
5
|
+
def initialize(environment, pathname)
|
|
6
|
+
@environment = environment
|
|
7
|
+
@pathname = pathname
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def each_source_line
|
|
11
|
+
File.open(pathname.absolute_location) do |file|
|
|
12
|
+
file.each do |line|
|
|
13
|
+
yield SourceLine.new(self, line, file.lineno)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def find(location)
|
|
19
|
+
pathname.parent_pathname.find(location)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def ==(source_file)
|
|
23
|
+
pathname == source_file.pathname
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module Sprockets
|
|
2
|
+
class SourceLine
|
|
3
|
+
attr_reader :source_file, :line, :number
|
|
4
|
+
|
|
5
|
+
def initialize(source_file, line, number)
|
|
6
|
+
@source_file = source_file
|
|
7
|
+
@line = line
|
|
8
|
+
@number = number
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def comment
|
|
12
|
+
@comment ||= line[/^\s*\/\/(.*)/, 1]
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def comment?
|
|
16
|
+
!!comment
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def require
|
|
20
|
+
@require ||= (comment || "")[/^=\s+require\s+(\"(.*?)\"|<(.*?)>)\s*$/, 1]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def require?
|
|
24
|
+
!!require
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def inspect
|
|
28
|
+
"line #@number of #{@source_file.pathname}"
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
data/lib/sprockets.rb
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
|
2
|
+
|
|
3
|
+
require "sprockets/version"
|
|
4
|
+
require "sprockets/error"
|
|
5
|
+
require "sprockets/environment"
|
|
6
|
+
require "sprockets/pathname"
|
|
7
|
+
require "sprockets/source_line"
|
|
8
|
+
require "sprockets/source_file"
|
|
9
|
+
require "sprockets/output_file"
|
|
10
|
+
require "sprockets/preprocessor"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// This is a double-slash comment that should appear in the resulting output file.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//= require <nonexistent>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//= require "requiring_the_current_file_should_do_nothing"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var FooFoo = { };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var Foo = { };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
require "test_helper"
|
|
2
|
+
|
|
3
|
+
class EnvironmentTest < Test::Unit::TestCase
|
|
4
|
+
def test_load_path_locations_become_pathnames_for_absolute_locations_from_the_root
|
|
5
|
+
environment = Sprockets::Environment.new("/root", ["/a", "b"])
|
|
6
|
+
assert_load_path_equals ["/a", "/root/b", "/root"], environment
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def test_pathname_from_for_location_with_leading_slash_should_return_a_pathname_with_the_location_unchanged
|
|
10
|
+
environment = Sprockets::Environment.new("/root")
|
|
11
|
+
assert_absolute_location "/a", environment.pathname_from("/a")
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_pathname_from_for_relative_location_should_return_a_pathname_for_the_expanded_absolute_location_from_root
|
|
15
|
+
environment = Sprockets::Environment.new("/root")
|
|
16
|
+
assert_absolute_location "/root/a", environment.pathname_from("a")
|
|
17
|
+
assert_absolute_location "/root/a", environment.pathname_from("./a")
|
|
18
|
+
assert_absolute_location "/a", environment.pathname_from("../a")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_register_load_location_should_unshift_the_location_onto_the_load_path
|
|
22
|
+
environment = Sprockets::Environment.new("/root")
|
|
23
|
+
environment.register_load_location("a")
|
|
24
|
+
assert_load_path_equals ["/root/a", "/root"], environment
|
|
25
|
+
environment.register_load_location("b")
|
|
26
|
+
assert_load_path_equals ["/root/b", "/root/a", "/root"], environment
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_register_load_location_should_remove_already_existing_locations_before_unshifting
|
|
30
|
+
environment = Sprockets::Environment.new("/root")
|
|
31
|
+
environment.register_load_location("a")
|
|
32
|
+
environment.register_load_location("b")
|
|
33
|
+
assert_load_path_equals ["/root/b", "/root/a", "/root"], environment
|
|
34
|
+
environment.register_load_location("a")
|
|
35
|
+
assert_load_path_equals ["/root/a", "/root/b", "/root"], environment
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_find_should_return_the_first_matching_pathname_in_the_load_path
|
|
39
|
+
environment = environment_for_fixtures
|
|
40
|
+
first_pathname = environment.find("foo.js")
|
|
41
|
+
assert_absolute_location_ends_with "src/foo.js", first_pathname
|
|
42
|
+
|
|
43
|
+
environment.register_load_location(File.join(FIXTURES_PATH, "src", "foo"))
|
|
44
|
+
second_pathname = environment.find("foo.js")
|
|
45
|
+
assert_not_equal first_pathname, second_pathname
|
|
46
|
+
assert_absolute_location_ends_with "foo/foo.js", second_pathname
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def test_find_should_return_nil_when_no_matching_source_file_is_found
|
|
50
|
+
environment = environment_for_fixtures
|
|
51
|
+
assert_nil environment.find("nonexistent.js")
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
protected
|
|
55
|
+
def assert_load_path_equals(load_path_absolute_locations, environment)
|
|
56
|
+
assert load_path_absolute_locations.zip(environment.load_path).map { |location, pathname| location == pathname.absolute_location }.all?
|
|
57
|
+
end
|
|
58
|
+
end
|
data/test/test_helper.rb
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), *%w".. lib sprockets")
|
|
2
|
+
require "test/unit"
|
|
3
|
+
|
|
4
|
+
class Test::Unit::TestCase
|
|
5
|
+
FIXTURES_PATH = File.expand_path(File.join(File.dirname(__FILE__), "fixtures")) unless defined?(FIXTURES_PATH)
|
|
6
|
+
|
|
7
|
+
protected
|
|
8
|
+
def location_for_fixture(fixture)
|
|
9
|
+
File.join(FIXTURES_PATH, fixture)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def content_of_fixture(fixture)
|
|
13
|
+
IO.read(location_for_fixture(fixture))
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def environment_for_fixtures
|
|
17
|
+
Sprockets::Environment.new(FIXTURES_PATH, source_directories_in_fixtures_path)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def source_directories_in_fixtures_path
|
|
21
|
+
Dir[File.join(FIXTURES_PATH, "**", "src")]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def assert_absolute_location(location, pathname)
|
|
25
|
+
assert_equal location, pathname.absolute_location
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def assert_absolute_location_ends_with(location_ending, pathname)
|
|
29
|
+
assert pathname.absolute_location[/#{Regexp.escape(location_ending)}$/]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def pathname(location, environment = @environment)
|
|
33
|
+
Sprockets::Pathname.new(environment, File.join(FIXTURES_PATH, location))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def source_file(location, environment = @environment)
|
|
37
|
+
Sprockets::SourceFile.new(environment, pathname(location, environment))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def source_line(line, source_file = nil, line_number = 1)
|
|
41
|
+
Sprockets::SourceLine.new(source_file, line, line_number)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require "test_helper"
|
|
2
|
+
|
|
3
|
+
class OutputFileTest < Test::Unit::TestCase
|
|
4
|
+
def setup
|
|
5
|
+
@output_file = Sprockets::OutputFile.new
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def test_record
|
|
9
|
+
assert_equal [], @output_file.source_lines
|
|
10
|
+
assert_equal "hello\n", @output_file.record("hello\n")
|
|
11
|
+
assert_equal "world\n", @output_file.record("world\n")
|
|
12
|
+
assert_equal ["hello\n", "world\n"], @output_file.source_lines
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_to_s
|
|
16
|
+
@output_file.record(source_line("hello\n"))
|
|
17
|
+
@output_file.record(source_line("world\n"))
|
|
18
|
+
assert_equal "hello\nworld\n", @output_file.to_s
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require "test_helper"
|
|
2
|
+
|
|
3
|
+
class PathnameTest < Test::Unit::TestCase
|
|
4
|
+
def setup
|
|
5
|
+
@environment = environment_for_fixtures
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def test_absolute_location_is_automatically_expanded
|
|
9
|
+
expanded_location = File.expand_path(File.join(FIXTURES_PATH, "foo"))
|
|
10
|
+
assert_absolute_location expanded_location, pathname("foo")
|
|
11
|
+
assert_absolute_location expanded_location, pathname("./foo")
|
|
12
|
+
assert_absolute_location expanded_location, pathname("./foo/../foo")
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_find_should_return_a_pathname_for_the_location_relative_to_the_absolute_location_of_the_pathname
|
|
16
|
+
assert_absolute_location_ends_with "src/foo/bar.js", pathname("src/foo").find("bar.js")
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_find_should_return_nil_when_the_location_relative_to_the_absolute_location_of_the_pathname_is_not_a_file_or_does_not_exist
|
|
20
|
+
assert_nil pathname("src/foo").find("nonexistent.js")
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_parent_pathname_should_return_a_pathname_for_the_parent_directory
|
|
24
|
+
assert_absolute_location_ends_with "src", pathname("src/foo").parent_pathname
|
|
25
|
+
assert_absolute_location_ends_with "foo", pathname("src/foo/foo.js").parent_pathname
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_source_file_should_return_a_source_file_for_the_pathname
|
|
29
|
+
source_file = pathname("src/foo.js").source_file
|
|
30
|
+
assert_kind_of Sprockets::SourceFile, source_file
|
|
31
|
+
assert_equal pathname("src/foo.js"), source_file.pathname
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def test_equality_of_pathnames
|
|
35
|
+
assert_equal pathname("src/foo.js"), pathname("src/foo.js")
|
|
36
|
+
assert_equal pathname("src/foo.js"), pathname("src/foo/../foo.js")
|
|
37
|
+
assert_not_equal pathname("src/foo.js"), pathname("src/foo/foo.js")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def test_to_s_should_return_absolute_location
|
|
41
|
+
assert_equal pathname("src/foo.js").to_s, pathname("src/foo.js").absolute_location
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
require "test_helper"
|
|
2
|
+
|
|
3
|
+
class PreprocessorTest < Test::Unit::TestCase
|
|
4
|
+
def setup
|
|
5
|
+
@environment = environment_for_fixtures
|
|
6
|
+
@preprocessor = Sprockets::Preprocessor.new(@environment)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def test_double_slash_comments_that_are_not_requires_should_be_removed_by_default
|
|
10
|
+
require_file_for_this_test
|
|
11
|
+
assert_output_file_does_not_contain_line "// This is a double-slash comment that should not appear in the resulting output file."
|
|
12
|
+
assert_output_file_contains_line "/* This is a slash-star comment that should appear in the resulting output file. */"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_double_slash_comments_that_are_not_requires_should_be_ignored_when_strip_comments_is_false
|
|
16
|
+
@preprocessor = Sprockets::Preprocessor.new(@environment, :strip_comments => false)
|
|
17
|
+
require_file_for_this_test
|
|
18
|
+
assert_output_file_contains_line "// This is a double-slash comment that should appear in the resulting output file."
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_requiring_a_single_file_should_replace_the_require_comment_with_the_file_contents
|
|
22
|
+
require_file_for_this_test
|
|
23
|
+
assert_output_file_contains <<-LINES
|
|
24
|
+
var before_require;
|
|
25
|
+
var Foo = { };
|
|
26
|
+
var after_require;
|
|
27
|
+
LINES
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def test_requiring_a_file_that_does_not_exist_should_raise_an_error
|
|
31
|
+
assert_raises(Sprockets::LoadError) do
|
|
32
|
+
require_file_for_this_test
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_requiring_the_current_file_should_do_nothing
|
|
37
|
+
require_file_for_this_test
|
|
38
|
+
assert_equal "", output_text
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_requiring_a_file_after_it_has_already_been_required_should_do_nothing
|
|
42
|
+
require_file_for_this_test
|
|
43
|
+
assert_output_file_contains <<-LINES
|
|
44
|
+
var before_first_require;
|
|
45
|
+
var Foo = { };
|
|
46
|
+
var after_first_require_and_before_second_require;
|
|
47
|
+
var after_second_require;
|
|
48
|
+
LINES
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
protected
|
|
52
|
+
attr_reader :environment, :preprocessor
|
|
53
|
+
|
|
54
|
+
def output_file
|
|
55
|
+
preprocessor.output_file
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def output_text
|
|
59
|
+
preprocessor.output_file.to_s
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def source_lines_matching(line)
|
|
63
|
+
output_file.source_lines.select { |source_line| source_line.line.strip == line }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def require_file(location)
|
|
67
|
+
preprocessor.require(environment.find(location).source_file)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def require_file_for_this_test
|
|
71
|
+
require_file(file_for_this_test)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def file_for_this_test
|
|
75
|
+
caller.map { |c| c[/`(.*?)'$/, 1] }.grep(/^test_/).first[5..-1] + ".js"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def assert_output_file_does_not_contain_line(line)
|
|
79
|
+
assert source_lines_matching(line).empty?
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def assert_output_file_contains_line(line)
|
|
83
|
+
assert source_lines_matching(line).any?
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def assert_output_file_contains(indented_text)
|
|
87
|
+
lines = indented_text.split($/)
|
|
88
|
+
initial_indent = lines.first[/^\s*/].length
|
|
89
|
+
unindented_text = lines.map { |line| line[initial_indent..-1] }.join($/)
|
|
90
|
+
assert output_text[unindented_text]
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require "test_helper"
|
|
2
|
+
require "enumerator"
|
|
3
|
+
|
|
4
|
+
class SourceFileTest < Test::Unit::TestCase
|
|
5
|
+
def setup
|
|
6
|
+
@environment = environment_for_fixtures
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def test_each_source_line
|
|
10
|
+
source_file_lines = Enumerable::Enumerator.new(source_file("src/foo/bar.js"), :each_source_line).to_a
|
|
11
|
+
assert_equal content_of_fixture("src/foo/bar.js"), source_file_lines.map { |line| line.line }.join
|
|
12
|
+
assert_equal 4, source_file_lines.length
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_find_should_return_pathname_for_file_relative_to_the_current_pathname
|
|
16
|
+
assert_absolute_location_ends_with "test/fixtures/src/foo/bar.js", source_file("src/foo/foo.js").find("bar.js")
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_find_should_return_nil_for_nonexistent_file
|
|
20
|
+
assert_nil source_file("src/foo/foo.js").find("nonexistent.js")
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_equality_of_source_files
|
|
24
|
+
assert_equal source_file("src/foo/foo.js"), source_file("src/foo/foo.js")
|
|
25
|
+
assert_equal source_file("src/foo/foo.js"), source_file("src/foo/../foo/foo.js")
|
|
26
|
+
assert_not_equal source_file("src/foo/foo.js"), source_file("src/foo.js")
|
|
27
|
+
assert_not_equal source_file("src/foo/foo.js"), source_file("src/foo/bar.js")
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require "test_helper"
|
|
2
|
+
|
|
3
|
+
class SourceLineTest < Test::Unit::TestCase
|
|
4
|
+
def test_line_that_begins_with_double_slash_should_be_a_comment
|
|
5
|
+
assert source_line("//").comment?
|
|
6
|
+
assert source_line("//test").comment?
|
|
7
|
+
assert source_line("//= require").comment?
|
|
8
|
+
assert source_line("//= require <foo>").comment?
|
|
9
|
+
assert source_line(" //").comment?
|
|
10
|
+
assert source_line("\t//").comment?
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_line_that_contains_but_does_not_begin_with_double_slash_should_not_be_a_comment
|
|
14
|
+
assert !source_line("f //").comment?
|
|
15
|
+
assert !source_line("f //= require <foo>").comment?
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_comment_should_be_extracted_from_comment_lines
|
|
19
|
+
assert_equal "test", source_line("//test").comment
|
|
20
|
+
assert_equal " test", source_line("// test").comment
|
|
21
|
+
assert_equal nil, source_line("f //test").comment
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_line_that_contains_require_comment_should_be_a_require
|
|
25
|
+
assert source_line("//= require <foo>").require?
|
|
26
|
+
assert !source_line("//= require<foo>").require?
|
|
27
|
+
assert source_line("//= require \"foo\"").require?
|
|
28
|
+
assert !source_line("//= require <foo> f").require?
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_require_should_be_extracted_from_require_lines
|
|
32
|
+
assert_nil source_line("//= require").require
|
|
33
|
+
assert_equal "<foo>", source_line("//= require <foo>").require
|
|
34
|
+
assert_equal "<foo>", source_line("//= require <foo> ").require
|
|
35
|
+
assert_equal "\"foo\"", source_line("//= require \"foo\"").require
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_inspect_should_include_source_file_location_and_line_number
|
|
39
|
+
environment = environment_for_fixtures
|
|
40
|
+
pathname = Sprockets::Pathname.new(environment, "/a/b/c.js")
|
|
41
|
+
source_file = Sprockets::SourceFile.new(environment, pathname)
|
|
42
|
+
assert_equal "line 25 of /a/b/c.js", source_line("hello", source_file, 25).inspect
|
|
43
|
+
end
|
|
44
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sstephenson-sprockets
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Sam Stephenson
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2008-06-01 00:00:00 -07:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description: Sprockets is a Ruby library that preprocesses and concatenates JavaScript source files.
|
|
17
|
+
email: sstephenson@gmail.com
|
|
18
|
+
executables:
|
|
19
|
+
- sprocketize
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files: []
|
|
23
|
+
|
|
24
|
+
files:
|
|
25
|
+
- Rakefile
|
|
26
|
+
- bin/sprocketize
|
|
27
|
+
- lib/sprockets.rb
|
|
28
|
+
- lib/sprockets
|
|
29
|
+
- lib/sprockets/preprocessor.rb
|
|
30
|
+
- lib/sprockets/source_file.rb
|
|
31
|
+
- lib/sprockets/error.rb
|
|
32
|
+
- lib/sprockets/output_file.rb
|
|
33
|
+
- lib/sprockets/environment.rb
|
|
34
|
+
- lib/sprockets/version.rb
|
|
35
|
+
- lib/sprockets/source_line.rb
|
|
36
|
+
- lib/sprockets/pathname.rb
|
|
37
|
+
- test/test_preprocessor.rb
|
|
38
|
+
- test/test_source_line.rb
|
|
39
|
+
- test/test_source_file.rb
|
|
40
|
+
- test/test_output_file.rb
|
|
41
|
+
- test/fixtures
|
|
42
|
+
- test/fixtures/src
|
|
43
|
+
- test/fixtures/src/foo
|
|
44
|
+
- test/fixtures/src/foo/bar.js
|
|
45
|
+
- test/fixtures/src/foo/foo.js
|
|
46
|
+
- test/fixtures/src/foo.js
|
|
47
|
+
- test/fixtures/requiring_a_single_file_should_replace_the_require_comment_with_the_file_contents.js
|
|
48
|
+
- test/fixtures/double_slash_comments_that_are_not_requires_should_be_removed_by_default.js
|
|
49
|
+
- test/fixtures/requiring_a_file_that_does_not_exist_should_raise_an_error.js
|
|
50
|
+
- test/fixtures/requiring_a_file_after_it_has_already_been_required_should_do_nothing.js
|
|
51
|
+
- test/fixtures/double_slash_comments_that_are_not_requires_should_be_ignored_when_strip_comments_is_false.js
|
|
52
|
+
- test/fixtures/requiring_the_current_file_should_do_nothing.js
|
|
53
|
+
- test/test_pathname.rb
|
|
54
|
+
- test/test_helper.rb
|
|
55
|
+
- test/test_environment.rb
|
|
56
|
+
has_rdoc: false
|
|
57
|
+
homepage: http://github.com/sstephenson/sprockets
|
|
58
|
+
post_install_message:
|
|
59
|
+
rdoc_options: []
|
|
60
|
+
|
|
61
|
+
require_paths:
|
|
62
|
+
- lib
|
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: "0"
|
|
68
|
+
version:
|
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - ">="
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: "0"
|
|
74
|
+
version:
|
|
75
|
+
requirements: []
|
|
76
|
+
|
|
77
|
+
rubyforge_project:
|
|
78
|
+
rubygems_version: 1.0.1
|
|
79
|
+
signing_key:
|
|
80
|
+
specification_version: 2
|
|
81
|
+
summary: JavaScript dependency management and concatenation
|
|
82
|
+
test_files:
|
|
83
|
+
- test/test_preprocessor.rb
|
|
84
|
+
- test/test_source_line.rb
|
|
85
|
+
- test/test_source_file.rb
|
|
86
|
+
- test/test_output_file.rb
|
|
87
|
+
- test/test_pathname.rb
|
|
88
|
+
- test/test_helper.rb
|
|
89
|
+
- test/test_environment.rb
|