stylus_rails 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,11 @@
1
1
  ## Changelog
2
2
 
3
+ ### 0.1.3 (2011-04-20)
4
+ [Compare view](http://github.com/lucasmazza/stylus_rails/compare/v0.1.2...v0.1.3)
5
+
6
+ * `Stylus` will raise an exception if the compilation fails. You can turn this off with `Stylus.silent = true`.
7
+ * Added `Stylus.logger` to handle the output from stylus. On Rails applications this will be the standard Rails logger.
8
+
3
9
  ### 0.1.2 (2011-04-10)
4
10
  [Compare view](http://github.com/lucasmazza/stylus_rails/compare/v0.1.1...v0.1.2)
5
11
 
data/README.md CHANGED
@@ -68,6 +68,13 @@ And a `application.styl`
68
68
 
69
69
  Stylus will compile your `application.styl` into a `application.css` and your `_vendor.styl` will be ignored.
70
70
 
71
+ ## Error Handling
72
+
73
+ If the stylus binary exits with error code 1 stylus_rails will throw an error exception. In case you don't want
74
+ this the behaviour can be switched to silent by setting the following variable to false:
75
+
76
+ Stylus.silent = true
77
+
71
78
  ## Changelog
72
79
  [here.](https://github.com/lucasmazza/stylus_rails/blob/master/CHANGELOG.md)
73
80
 
@@ -1,3 +1,4 @@
1
+ require 'logger'
1
2
  require 'stylus_rails/core_ext'
2
3
  require 'stylus_rails/stylus'
3
4
  require 'stylus_rails/runner'
@@ -8,12 +8,13 @@ module Stylus
8
8
  initializer "stylus.reloader" do
9
9
  Stylus.root = File.join(Rails.public_path,"stylesheets")
10
10
  Stylus.compile_directory ||= File.join(Rails.public_path, 'stylesheets')
11
+ Stylus.logger = Rails.logger
11
12
  if !$rails_rake_task
12
13
  config.to_prepare do
13
14
  if `which stylus` && $?.success?
14
15
  Stylus.compile
15
16
  else
16
- Rails.logger.warn(Stylus.warning)
17
+ Stylus.logger.warn(Stylus.warning)
17
18
  end
18
19
  end
19
20
  end
@@ -11,11 +11,30 @@ module Stylus
11
11
 
12
12
  directories.each_pair do |directory, files|
13
13
  FileUtils.mkdir_p(directory) unless File.directory?(directory)
14
- system("stylus #{files.join(" ")} -o #{directory}#{' -c' if Stylus.compress?}")
14
+ output = `stylus #{files.join(" ")} -o #{directory}#{' -c' if Stylus.compress?} 2>&1`
15
+
16
+ if failed?
17
+ handle_failure(output)
18
+ else
19
+ Stylus.logger.info(output)
20
+ end
15
21
  end
16
22
  end
17
23
 
18
24
  private
25
+
26
+ def handle_failure(output)
27
+ if Stylus.silent?
28
+ Stylus.logger.error(output)
29
+ else
30
+ raise CompilationError, "Stylus compilation error #{output}"
31
+ end
32
+ end
33
+
34
+ def failed?
35
+ !$?.success?
36
+ end
37
+
19
38
  def output_folder(path)
20
39
  dirname = File.dirname(path)
21
40
  if Stylus.compile_directory.nil?
@@ -1,6 +1,11 @@
1
1
  module Stylus
2
2
  extend CoreExt
3
- # Directory to look for .stylus files
3
+
4
+ # Error messages
5
+ Error = Class.new(StandardError)
6
+ CompilationError = Class.new(Error)
7
+
8
+ # Directory to look for .stylus files.
4
9
  mattr_accessor :directory
5
10
  @@directory = "stylus"
6
11
 
@@ -8,18 +13,31 @@ module Stylus
8
13
  mattr_accessor :root
9
14
  @@root = Dir.pwd
10
15
 
11
- # Root path for the output
16
+ # Root path for the generated output.
12
17
  mattr_accessor :compile_directory
13
18
  @@compile_directory = nil
14
19
 
15
20
  mattr_accessor :extension
16
21
  @@extension = "styl"
17
22
 
23
+ # Checks if the -c flag should be passed to Stylus to render single line CSS statements.
18
24
  mattr_accessor :compress
19
25
  @@compress = true
20
26
 
27
+ # Checks if an CompilationError should be raised if the compilation fails.
28
+ mattr_accessor :silent
29
+ @@silent = false
30
+
31
+ # Logger instance to write the stylus output.
32
+ mattr_accessor :logger
33
+
21
34
  class << self
22
35
  alias_method :compress?, :compress
36
+ alias_method :silent?, :silent
37
+
38
+ def logger
39
+ @@logger ||= Logger.new($stdout)
40
+ end
23
41
 
24
42
  def compile
25
43
  paths = Dir[File.join(folder, "**", "*.#{extension}")]
@@ -1,3 +1,3 @@
1
1
  module Stylus
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -34,22 +34,55 @@ describe Stylus::Runner do
34
34
  end
35
35
 
36
36
  it "calls the stylus cli" do
37
- subject.should_receive(:system).with("stylus #{stylus_file} -o #{target_folder}")
37
+ subject.should_receive(:`).with("stylus #{stylus_file} -o #{target_folder} 2>&1")
38
38
  subject.call
39
39
  end
40
40
 
41
41
  it "runs files on the same folder on a single command" do
42
42
  paths = fixtures("stylus/simple.styl", "stylus/foo.styl")
43
43
  subject = Stylus::Runner.new(paths)
44
- subject.should_receive(:system).with("stylus #{paths.join(" ")} -o #{target_folder}")
44
+ subject.should_receive(:`).with("stylus #{paths.join(" ")} -o #{target_folder} 2>&1")
45
45
  subject.call
46
46
  end
47
47
 
48
48
  it "uses the compress flag when configured to" do
49
49
  Stylus.compress = true
50
- subject.should_receive(:system).with("stylus #{stylus_file} -o #{target_folder} -c")
50
+ subject.should_receive(:`).with("stylus #{stylus_file} -o #{target_folder} -c 2>&1")
51
51
  subject.call
52
52
  end
53
+
54
+ it "writes the output to the associated logger" do
55
+ subject.stub(:`) { "some output" }
56
+ Stylus.logger.should_receive(:info).with("some output")
57
+ subject.call
58
+ end
59
+
60
+ context "#call when stylus(1) fails" do
61
+ before do
62
+ subject.stub!(:failed?).and_return(true)
63
+ end
64
+
65
+ context "when Stylus.silent is false" do
66
+ before { Stylus.silent = false }
67
+ it "raises an exception" do
68
+ expect { subject.call }.to raise_error Stylus::CompilationError
69
+ end
70
+ end
71
+
72
+ context "when Stylus.silent is true" do
73
+ before { Stylus.silent = true }
74
+
75
+ it "doesn't raise an exception" do
76
+ expect { subject.call }.to_not raise_error Stylus::CompilationError
77
+ end
78
+
79
+ it "writes the error output to the logger" do
80
+ subject.stub(:`) { "FAIL" }
81
+ Stylus.logger.should_receive(:error).with("FAIL")
82
+ subject.call
83
+ end
84
+ end
85
+ end
53
86
  end
54
87
 
55
88
  describe "#call with different compile_dir" do
@@ -65,20 +98,20 @@ describe Stylus::Runner do
65
98
  end
66
99
 
67
100
  it "calls the stylus cli" do
68
- subject.should_receive(:system).with("stylus #{stylus_file} -o #{target_folder}")
101
+ subject.should_receive(:`).with("stylus #{stylus_file} -o #{target_folder} 2>&1")
69
102
  subject.call
70
103
  end
71
104
 
72
105
  it "runs files on the same folder on a single command" do
73
106
  paths = fixtures("stylus/simple.styl", "stylus/foo.styl")
74
107
  subject = Stylus::Runner.new(paths)
75
- subject.should_receive(:system).with("stylus #{paths.join(" ")} -o #{target_folder}")
108
+ subject.should_receive(:`).with("stylus #{paths.join(" ")} -o #{target_folder} 2>&1")
76
109
  subject.call
77
110
  end
78
111
 
79
112
  it "uses the compress flag when configured to" do
80
113
  Stylus.compress = true
81
- subject.should_receive(:system).with("stylus #{stylus_file} -o #{target_folder} -c")
114
+ subject.should_receive(:`).with("stylus #{stylus_file} -o #{target_folder} -c 2>&1")
82
115
  subject.call
83
116
  end
84
117
  end
@@ -2,9 +2,12 @@ require 'stylus_rails'
2
2
 
3
3
  RSpec.configure do |config|
4
4
 
5
+ Stylus.logger = Logger.new("/dev/null")
6
+
5
7
  def fixtures(*paths)
6
8
  paths.map { |path| File.expand_path(path, "spec/fixtures") }
7
9
  end
10
+
8
11
  alias :fixture :fixtures
9
12
  end
10
13
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stylus_rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lucas Mazza
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-10 00:00:00 -03:00
18
+ date: 2011-04-20 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency