sstephenson-sprockets 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/sprocketize CHANGED
@@ -44,15 +44,5 @@ OptionParser.new do |opts|
44
44
  end
45
45
  end
46
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
47
+ secretary = Sprockets::Secretary.new(:load_path => load_path, :source_files => filenames, :expand_paths => false)
48
+ print secretary.output_file
@@ -22,7 +22,11 @@ module Sprockets
22
22
  end
23
23
 
24
24
  def find(location)
25
- find_all(location).first
25
+ if absolute?(location) && File.exists?(location)
26
+ pathname_from(location)
27
+ else
28
+ find_all(location).first
29
+ end
26
30
  end
27
31
 
28
32
  def constants(reload = false)
@@ -35,9 +39,13 @@ module Sprockets
35
39
  end
36
40
 
37
41
  protected
42
+ def absolute?(location)
43
+ location[0, 1] == File::SEPARATOR
44
+ end
45
+
38
46
  def absolute_location_from(location)
39
47
  location = location.to_s
40
- location = File.join(root.absolute_location, location) unless location[/^\//]
48
+ location = File.join(root.absolute_location, location) unless absolute?(location)
41
49
  File.expand_path(location)
42
50
  end
43
51
 
@@ -0,0 +1,66 @@
1
+ module Sprockets
2
+ class Secretary
3
+ DEFAULT_OPTIONS = {
4
+ :root => ".",
5
+ :load_path => [],
6
+ :source_files => [],
7
+ :expand_paths => true
8
+ }
9
+
10
+ attr_reader :environment, :preprocessor
11
+
12
+ def initialize(options = {})
13
+ @options = DEFAULT_OPTIONS.merge(options)
14
+ @environment = Sprockets::Environment.new(@options[:root])
15
+ @preprocessor = Sprockets::Preprocessor.new(@environment)
16
+
17
+ add_load_locations(@options[:load_path])
18
+ add_source_files(@options[:source_files])
19
+ end
20
+
21
+ def add_load_location(load_location, options = {})
22
+ add_load_locations([load_location], options)
23
+ end
24
+
25
+ def add_load_locations(load_locations, options = {})
26
+ expand_paths(load_locations, options).each do |load_location|
27
+ environment.register_load_location(load_location)
28
+ end
29
+ end
30
+
31
+ def add_source_file(source_file, options = {})
32
+ add_source_files([source_file], options)
33
+ end
34
+
35
+ def add_source_files(source_files, options = {})
36
+ expand_paths(source_files, options).each do |source_file|
37
+ if pathname = environment.find(source_file)
38
+ preprocessor.require(pathname.source_file)
39
+ else
40
+ raise Sprockets::LoadError, "no such file `#{source_file}'"
41
+ end
42
+ end
43
+ end
44
+
45
+ def output_file
46
+ preprocessor.output_file
47
+ end
48
+
49
+ protected
50
+ def expand_paths(paths, options = {})
51
+ if options.has_key?(:expand_paths) ? options[:expand_paths] : @options[:expand_paths]
52
+ paths.map { |path| Dir[from_root(path)].sort }.flatten.compact
53
+ else
54
+ paths.map { |path| from_root(path) }
55
+ end
56
+ end
57
+
58
+ def from_root(path)
59
+ if path[0, 1] == File::SEPARATOR
60
+ path
61
+ else
62
+ File.join(@options[:root], path)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -1,8 +1,8 @@
1
1
  module Sprockets
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 1
5
- TINY = 1
4
+ MINOR = 4
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join(".")
8
8
  end
data/lib/sprockets.rb CHANGED
@@ -10,3 +10,4 @@ require "sprockets/source_line"
10
10
  require "sprockets/source_file"
11
11
  require "sprockets/output_file"
12
12
  require "sprockets/preprocessor"
13
+ require "sprockets/secretary"
@@ -0,0 +1,34 @@
1
+ require "test_helper"
2
+
3
+ class SecretaryTest < Test::Unit::TestCase
4
+ def test_load_locations_are_not_expanded_when_expand_paths_is_false
5
+ secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH)
6
+ secretary.add_load_locations("src/**/", :expand_paths => false)
7
+
8
+ assert_equal [File.join(FIXTURES_PATH, "src/**"), FIXTURES_PATH],
9
+ secretary.environment.load_path.map { |pathname| pathname.absolute_location }
10
+ end
11
+
12
+ def test_load_locations_are_expanded_when_expand_paths_is_true
13
+ secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH)
14
+ secretary.add_load_locations("src/**/", :expand_paths => true)
15
+
16
+ assert_equal [File.join(FIXTURES_PATH, "src", "foo"), File.join(FIXTURES_PATH, "src"), FIXTURES_PATH],
17
+ secretary.environment.load_path.map { |pathname| pathname.absolute_location }
18
+ end
19
+
20
+ def test_source_files_are_not_expanded_when_expand_paths_is_false
21
+ secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH)
22
+ assert_raises(Sprockets::LoadError) do
23
+ secretary.add_source_files("src/f*.js", :expand_paths => false)
24
+ end
25
+ end
26
+
27
+ def test_source_files_are_expanded_when_expand_paths_is_true
28
+ secretary = Sprockets::Secretary.new(:root => FIXTURES_PATH)
29
+ secretary.add_source_files("src/f*.js", :expand_paths => true)
30
+
31
+ assert_equal [File.join(FIXTURES_PATH, "src", "foo.js")],
32
+ secretary.preprocessor.source_files.map { |source_file| source_file.pathname.absolute_location }
33
+ end
34
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sstephenson-sprockets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Stephenson
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-28 00:00:00 -08:00
12
+ date: 2009-01-29 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -30,11 +30,13 @@ files:
30
30
  - lib/sprockets/source_file.rb
31
31
  - lib/sprockets/error.rb
32
32
  - lib/sprockets/output_file.rb
33
+ - lib/sprockets/secretary.rb
33
34
  - lib/sprockets/environment.rb
34
35
  - lib/sprockets/version.rb
35
36
  - lib/sprockets/source_line.rb
36
37
  - lib/sprockets/pathname.rb
37
38
  - test/test_preprocessor.rb
39
+ - test/test_secretary.rb
38
40
  - test/test_source_line.rb
39
41
  - test/test_source_file.rb
40
42
  - test/test_output_file.rb