barista 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ begin
7
7
  require 'jeweler'
8
8
  Jeweler::Tasks.new do |gem|
9
9
  gem.name = "barista"
10
- gem.summary = %Q{Transparent coffeescript support for rails 3}
10
+ gem.summary = %Q{Transparent coffeescript support for rails 3.}
11
11
  gem.description = %Q{Automatically compiles app/scripts/*.coffee to javascript for rails awesomesauce.}
12
12
  gem.email = "sutto@sutto.net"
13
13
  gem.homepage = "http://github.com/Sutto/barista"
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{barista}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Darcy Laycock"]
12
- s.date = %q{2010-06-11}
12
+ s.date = %q{2010-07-06}
13
13
  s.description = %q{Automatically compiles app/scripts/*.coffee to javascript for rails awesomesauce.}
14
14
  s.email = %q{sutto@sutto.net}
15
15
  s.extra_rdoc_files = [
@@ -39,7 +39,7 @@ Gem::Specification.new do |s|
39
39
  s.rdoc_options = ["--charset=UTF-8"]
40
40
  s.require_paths = ["lib"]
41
41
  s.rubygems_version = %q{1.3.7}
42
- s.summary = %q{Transparent coffeescript support for rails 3}
42
+ s.summary = %q{Transparent coffeescript support for rails 3.}
43
43
 
44
44
  if s.respond_to? :specification_version then
45
45
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -3,6 +3,10 @@ require 'pathname'
3
3
 
4
4
  module Barista
5
5
 
6
+ Error = Class.new(StandardError)
7
+ CompilationError = Class.new(Error)
8
+ CompilerUnavailableError = Class.new(Error)
9
+
6
10
  autoload :Compiler, 'barista/compiler'
7
11
  autoload :Filter, 'barista/filter'
8
12
  autoload :Framework, 'barista/framework'
@@ -13,6 +17,15 @@ module Barista
13
17
  yield self if block_given?
14
18
  end
15
19
 
20
+ def exception_on_error?
21
+ @exception_on_error = true if !defined?(@exception_on_error)
22
+ !!@exception_on_error
23
+ end
24
+
25
+ def exception_on_error=(value)
26
+ @exception_on_error = value
27
+ end
28
+
16
29
  def root
17
30
  @root ||= Rails.root.join("app", "coffeescripts")
18
31
  end
@@ -30,78 +43,90 @@ module Barista
30
43
  @output_root = Pathname(value.to_s)
31
44
  end
32
45
 
33
- def compile_file!(file, force = false)
46
+ def compile_file!(file, force = false, silence_error = false)
47
+ # Ensure we have a coffeescript compiler available.
48
+ if !Compiler.check_availability!(silence_error)
49
+ debug "The coffeescript compiler at '#{Compiler.bin_path}' is currently unavailable."
50
+ return ""
51
+ end
52
+ # Expand the path from the framework.
34
53
  origin_path, framework = Framework.full_path_for(file)
35
54
  return if origin_path.blank?
36
55
  destination_path = self.output_path_for(file)
37
56
  return unless force || Compiler.dirty?(origin_path, destination_path)
38
57
  debug "Compiling #{file} from framework '#{framework.name}'"
39
58
  FileUtils.mkdir_p File.dirname(destination_path)
40
- content = Compiler.compile(origin_path)
59
+ content = Compiler.compile(origin_path, :silence_error => silence_error)
41
60
  # Write the rendered content to file.
42
- File.open(destination_path, "w+") { |f| f.write content }
43
- content
61
+ # nil is only when silence_error is true.
62
+ if content.nil?
63
+ debug "An error occured compiling '#{file}' - Leaving file as is."
64
+ else
65
+ File.open(destination_path, "w+") { |f| f.write content }
66
+ content
67
+ end
44
68
  rescue SystemCallError
69
+ debug "An unknown error occured attempting to compile '#{file}'"
45
70
  ""
46
71
  end
47
-
48
- def compile_all!(force = false)
72
+
73
+ def compile_all!(force = false, silence_error = true)
49
74
  debug "Compiling all coffeescripts"
50
75
  Framework.exposed_coffeescripts.each do |coffeescript|
51
- compile_file! coffeescript, force
76
+ compile_file! coffeescript, force, silence_error
52
77
  end
53
78
  true
54
79
  end
55
-
80
+
56
81
  def change_output_prefix!(framework, prefix = nil)
57
82
  framework = framework.is_a?(Barista::Framework) ? framework : Barista::Framework[framework]
58
83
  return unless framework
59
84
  framework.output_prefix = prefix
60
85
  end
61
-
86
+
62
87
  def each_framework(include_default = false)
63
88
  Framework.all(include_default).each { yield if block_given? }
64
89
  end
65
-
90
+
66
91
  def output_path_for(file)
67
92
  output_root.join(file.to_s.gsub(/^\/+/, '')).to_s.gsub(/\.coffee$/, '.js')
68
93
  end
69
-
94
+
70
95
  def debug(message)
71
96
  Rails.logger.debug "[Barista] #{message}" if defined?(Rails.logger) && Rails.logger
72
97
  end
73
-
98
+
74
99
  # By default, only add it in dev / test
75
100
  def add_filter?
76
101
  Rails.env.test? || Rails.env.development?
77
102
  end
78
-
103
+
79
104
  def no_wrap?
80
105
  defined?(@no_wrap) && @no_wrap
81
106
  end
82
-
107
+
83
108
  def no_wrap!
84
109
  self.no_wrap = true
85
110
  end
86
-
111
+
87
112
  def no_wrap=(value)
88
113
  @no_wrap = !!value
89
114
  end
90
-
115
+
91
116
  end
92
-
117
+
93
118
  if defined?(Rails::Engine)
94
119
  class Engine < Rails::Engine
95
-
120
+
96
121
  rake_tasks do
97
122
  load File.expand_path('./barista/tasks/barista.rake', File.dirname(__FILE__))
98
123
  end
99
-
124
+
100
125
  initializer "barista.wrap_filter" do
101
126
  ActionController::Base.before_filter(Barista::Filter) if Barista.add_filter?
102
127
  end
103
-
128
+
104
129
  end
105
130
  end
106
-
131
+
107
132
  end
@@ -2,50 +2,71 @@ require 'digest/sha2'
2
2
 
3
3
  module Barista
4
4
  class Compiler
5
-
5
+
6
6
  class << self; attr_accessor :bin_path; end
7
7
  self.bin_path ||= "coffee"
8
-
9
- def self.compile(path)
10
- new(path).to_js
8
+
9
+ def self.available?
10
+ @coffee_available ||= system("command -v '#{self.bin_path}' >/dev/null 2>&1")
11
+ end
12
+
13
+ def self.check_availability!(silence = false)
14
+ available?.tap do |available|
15
+ if Barista.exception_on_error? && !silence
16
+ raise CompilerUnavailableError, "The coffeescript compiler '#{self.bin_path}' could not be found."
17
+ end
18
+ end
11
19
  end
12
-
13
- def initialize(path)
20
+
21
+ def self.compile(path, options = {})
22
+ new(path, options).to_js
23
+ end
24
+
25
+ def initialize(path, options = {})
14
26
  @compiled = false
27
+ @options = {}
15
28
  @path = path
16
29
  end
17
-
30
+
18
31
  def compile!
19
32
  # Compiler code thanks to bistro_car.
20
33
  @compiled_content = invoke_coffee(@path)
21
34
  @compiled = true
22
35
  end
23
-
36
+
24
37
  def to_js
25
38
  compile! unless @compiled
26
- @compiled_content.to_s
39
+ @compiled_content
27
40
  end
28
-
41
+
29
42
  def self.dirty?(from, to)
30
43
  File.exist?(from) && (!File.exist?(to) || File.mtime(to) < File.mtime(from))
31
44
  end
32
-
45
+
33
46
  protected
34
-
47
+
35
48
  def coffee_options
36
49
  ["-p"].tap do |options|
37
50
  options << "--no-wrap" if Barista.no_wrap?
38
51
  end.join(" ")
39
52
  end
40
-
53
+
41
54
  def invoke_coffee(path)
42
55
  command = "#{self.class.bin_path} #{coffee_options} '#{path}'".squeeze(' ')
43
- %x(#{command})
56
+ result = %x(#{command}).to_s
57
+ if !$?.success?
58
+ if Barista.exception_on_error? && !@options[:silence]
59
+ raise CompilationError, "\"#{command}\" exited with a non-zero status."
60
+ else
61
+ result = nil
62
+ end
63
+ end
64
+ result
44
65
  end
45
-
66
+
46
67
  def content_hash
47
68
  @content_hash ||= Digest::SHA256.hexdigest(@content)
48
69
  end
49
-
70
+
50
71
  end
51
72
  end
@@ -1,10 +1,10 @@
1
1
  module Barista
2
2
  class Filter
3
-
3
+
4
4
  def self.filter(controller)
5
5
  Rails.logger.debug "[Barista] Compiling all scripts"
6
6
  Barista.compile_all!
7
7
  end
8
-
8
+
9
9
  end
10
- end
10
+ end
@@ -1,26 +1,26 @@
1
1
  module Barista
2
2
  class Framework
3
-
3
+
4
4
  def self.default_framework
5
5
  @default_framework ||= self.new("default", Barista.root)
6
6
  end
7
-
7
+
8
8
  def self.default_framework=(value)
9
9
  @default_framework = value
10
10
  end
11
-
11
+
12
12
  def self.all(include_default = false)
13
13
  all = (@all ||= [])
14
14
  all = [default_framework] + all if include_default
15
15
  all
16
16
  end
17
-
17
+
18
18
  def self.exposed_coffeescripts
19
19
  all(true).inject([]) do |collection, fw|
20
20
  collection + fw.exposed_coffeescripts
21
21
  end.uniq.sort_by { |f| f.length }
22
22
  end
23
-
23
+
24
24
  def self.full_path_for(script)
25
25
  script = script.to_s.gsub(/\.js$/, '.coffee').gsub(/^\/+/, '')
26
26
  all(true).each do |fw|
@@ -29,52 +29,52 @@ module Barista
29
29
  end
30
30
  nil
31
31
  end
32
-
32
+
33
33
  def self.register(name, root)
34
34
  (@all ||= []) << self.new(name, root)
35
35
  end
36
-
36
+
37
37
  def self.[](name)
38
38
  name = name.to_s
39
39
  (@all ||= []).detect { |fw| fw.name == name }
40
40
  end
41
-
41
+
42
42
  attr_reader :name, :framework_root, :output_prefix
43
-
43
+
44
44
  def initialize(name, root, output_prefix = nil)
45
45
  @name = name.to_s
46
46
  @output_prefix = nil
47
47
  @framework_root = File.expand_path(root)
48
48
  end
49
-
49
+
50
50
  def coffeescripts
51
51
  Dir[File.join(@framework_root, "**", "*.coffee")]
52
52
  end
53
-
53
+
54
54
  def short_name(script)
55
55
  short_name = remove_prefix script, @framework_root
56
56
  File.join *[@output_prefix, short_name].compact
57
57
  end
58
-
58
+
59
59
  def exposed_coffeescripts
60
60
  coffeescripts.map { |script| short_name(script) }
61
61
  end
62
-
62
+
63
63
  def output_prefix=(value)
64
64
  value = value.to_s.gsub /(^\/|\/$)/, ''
65
65
  @output_prefix = value.blank? ? nil : value
66
66
  end
67
-
67
+
68
68
  def full_path_for(name)
69
69
  full_path = File.join(@framework_root, remove_prefix(name, @output_prefix.to_s))
70
70
  File.exist?(full_path) ? full_path : nil
71
71
  end
72
-
72
+
73
73
  protected
74
-
74
+
75
75
  def remove_prefix(path, prefix)
76
76
  path.to_s.gsub /^#{Regexp.escape(prefix.to_s)}\/?/, ''
77
77
  end
78
-
78
+
79
79
  end
80
- end
80
+ end
@@ -1,8 +1,12 @@
1
1
  namespace :barista do
2
-
2
+
3
3
  desc "Compiles coffeescripts from app/scripts into public/javascripts"
4
4
  task :brew => :environment do
5
- Barista.compile_all!(true)
5
+ if !Barista::Compiler.available?
6
+ $stderr.puts "'#{Barista::Compiler.bin_path}' was unavailable."
7
+ exit 1
8
+ end
9
+ Barista.compile_all! true, false
6
10
  end
7
-
8
- end
11
+
12
+ end
@@ -1,7 +1,7 @@
1
1
  module Barista
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 3
4
+ MINOR = 4
5
5
  PATCH = 0
6
6
  STRING = [MAJOR, MINOR, PATCH].join(".")
7
7
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barista
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 4
9
9
  - 0
10
- version: 0.3.0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Darcy Laycock
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-11 00:00:00 +08:00
18
+ date: 2010-07-06 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -79,6 +79,6 @@ rubyforge_project:
79
79
  rubygems_version: 1.3.7
80
80
  signing_key:
81
81
  specification_version: 3
82
- summary: Transparent coffeescript support for rails 3
82
+ summary: Transparent coffeescript support for rails 3.
83
83
  test_files: []
84
84