barista 1.1.0.pre1 → 1.1.0
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/README.md +1 -0
- data/barista.gemspec +3 -3
- data/lib/barista.rb +16 -1
- data/lib/barista/compiler.rb +49 -23
- data/lib/barista/framework.rb +40 -8
- data/lib/barista/version.rb +1 -1
- data/lib/generators/barista/install/templates/initializer.rb +10 -4
- metadata +8 -12
data/README.md
CHANGED
@@ -146,6 +146,7 @@ and `#option!` (to set the value to true):
|
|
146
146
|
* `root` – The folder path to read CoffeeScripts from. (Defaults to `app/coffeescripts`.)
|
147
147
|
* `output_root` – The folder to write compiled JS files to. (Defaults to `public/javascripts`.)
|
148
148
|
* `change_output_prefix!` – Method to change the output prefix for a framework.
|
149
|
+
* `change_output_root!` - Method to change the output root for a given framework.
|
149
150
|
* `verbose` – Whether or not Barista will add a preamble to files.
|
150
151
|
* `js_path` – Path to the pure-JavaScript compiler.
|
151
152
|
* `env` – The application environment. (Defaults to `Rails.env`.)
|
data/barista.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{barista}
|
8
|
-
s.version = "1.1.0
|
8
|
+
s.version = "1.1.0"
|
9
9
|
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new("
|
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{2011-
|
12
|
+
s.date = %q{2011-06-01}
|
13
13
|
s.description = %q{Barista provides simple, integrated support for CoffeeScript in Rack and Rails applications.
|
14
14
|
|
15
15
|
Much like Compass does for Sass, It also provides Frameworks (bundleable code which can be shared via Gems).
|
data/lib/barista.rb
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
require 'pathname'
|
2
|
-
|
2
|
+
require 'time' # Required for httpdate
|
3
3
|
require 'coffee_script'
|
4
4
|
|
5
|
+
# Setup ExecJS extras if present
|
6
|
+
if defined?(ExecJS::ExternalRuntime)
|
7
|
+
ExecJS::ExternalRuntime.send :attr_accessor, :binary
|
8
|
+
end
|
9
|
+
|
5
10
|
module Barista
|
6
11
|
|
7
12
|
Error = Class.new(StandardError)
|
@@ -54,6 +59,7 @@ module Barista
|
|
54
59
|
|
55
60
|
has_boolean_options :verbose, :bare, :add_filter, :add_preamble, :exception_on_error, :embedded_interpreter, :auto_compile
|
56
61
|
has_delegate_methods Compiler, :bin_path, :bin_path=, :js_path, :js_path=
|
62
|
+
has_delegate_methods Framework, :register
|
57
63
|
has_deprecated_methods :compiler, :compiler=, :compiler_klass, :compiler_klass=
|
58
64
|
|
59
65
|
def configure
|
@@ -182,6 +188,10 @@ module Barista
|
|
182
188
|
Framework.exposed_coffeescripts.each do |coffeescript|
|
183
189
|
Compiler.autocompile_file coffeescript, force, silence_error
|
184
190
|
end
|
191
|
+
debug "Copying all javascripts"
|
192
|
+
Framework.exposed_javascripts.each do |javascript|
|
193
|
+
Compiler.autocompile_file javascript, force, silence_error
|
194
|
+
end
|
185
195
|
Barista.invoke_hook :all_compiled
|
186
196
|
true
|
187
197
|
end
|
@@ -191,6 +201,11 @@ module Barista
|
|
191
201
|
framework.output_prefix = prefix if framework
|
192
202
|
end
|
193
203
|
|
204
|
+
def change_output_root!(framework, root)
|
205
|
+
framework = Barista::Framework[framework] unless framework.is_a?(Barista::Framework)
|
206
|
+
framework.output_root = root if framework
|
207
|
+
end
|
208
|
+
|
194
209
|
def each_framework(include_default = false, &blk)
|
195
210
|
Framework.all(include_default).each(&blk)
|
196
211
|
end
|
data/lib/barista/compiler.rb
CHANGED
@@ -13,21 +13,38 @@ module Barista
|
|
13
13
|
def js_path=(value)
|
14
14
|
CoffeeScript::Source.path = value
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def bin_path
|
18
|
-
CoffeeScript::Engines::Node
|
18
|
+
if defined?(CoffeeScript::Engines::Node)
|
19
|
+
CoffeeScript::Engines::Node.binary
|
20
|
+
else
|
21
|
+
execjs_runtime_call :binary
|
22
|
+
end
|
19
23
|
end
|
20
|
-
|
24
|
+
|
21
25
|
def bin_path=(path)
|
22
|
-
CoffeeScript::Engines::Node
|
26
|
+
if defined?(CoffeeScript::Engines::Node)
|
27
|
+
CoffeeScript::Engines::Node.binary = path
|
28
|
+
else
|
29
|
+
execjs_runtime_call :binary=, path
|
30
|
+
end
|
23
31
|
end
|
24
|
-
|
32
|
+
|
33
|
+
def execjs_runtime_call(method, *args)
|
34
|
+
runtime = ExecJS.runtime
|
35
|
+
if runtime.respond_to?(method, true)
|
36
|
+
runtime.send method, *args
|
37
|
+
else
|
38
|
+
nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
25
42
|
def available?
|
26
43
|
CoffeeScript.engine && CoffeeScript.engine.supported?
|
27
44
|
end
|
28
45
|
|
29
46
|
def check_availability!(silence = false)
|
30
|
-
available = available?
|
47
|
+
available = available?
|
31
48
|
if !available && Barista.exception_on_error? && !silence
|
32
49
|
raise CompilerUnavailableError, "No method of compiling cofffescript is currently available. Please install therubyracer or node."
|
33
50
|
end
|
@@ -43,12 +60,12 @@ module Barista
|
|
43
60
|
origin_path, framework = Framework.full_path_for(file)
|
44
61
|
return if origin_path.nil?
|
45
62
|
destination_path = framework.output_path_for(file)
|
46
|
-
|
63
|
+
|
47
64
|
# read file directly if auto_compile is disabled
|
48
|
-
if !Barista.auto_compile?
|
49
|
-
if File.exist?(destination_path)
|
65
|
+
if !Barista.auto_compile?
|
66
|
+
if File.exist?(destination_path)
|
50
67
|
return File.read(destination_path)
|
51
|
-
else
|
68
|
+
else
|
52
69
|
return nil
|
53
70
|
end
|
54
71
|
end
|
@@ -65,7 +82,7 @@ module Barista
|
|
65
82
|
compiler.save
|
66
83
|
content
|
67
84
|
end
|
68
|
-
|
85
|
+
|
69
86
|
def compile_as(file, type)
|
70
87
|
origin_path, framework = Framework.full_path_for(file)
|
71
88
|
return if origin_path.nil?
|
@@ -75,11 +92,11 @@ module Barista
|
|
75
92
|
return autocompile_file(file), Time.now
|
76
93
|
end
|
77
94
|
end
|
78
|
-
|
95
|
+
|
79
96
|
def dirty?(from, to)
|
80
97
|
File.exist?(from) && (!File.exist?(to) || File.mtime(to) < File.mtime(from))
|
81
98
|
end
|
82
|
-
|
99
|
+
|
83
100
|
def setup_default_error_logger
|
84
101
|
Barista.on_compilation_error do |where, message|
|
85
102
|
if Barista.verbose?
|
@@ -88,9 +105,9 @@ module Barista
|
|
88
105
|
end
|
89
106
|
end
|
90
107
|
end
|
91
|
-
|
108
|
+
|
92
109
|
end
|
93
|
-
|
110
|
+
|
94
111
|
def initialize(context, options = {})
|
95
112
|
@compiled = false
|
96
113
|
@options = options
|
@@ -108,11 +125,19 @@ module Barista
|
|
108
125
|
compile! unless defined?(@compiled) && @compiled
|
109
126
|
@compiled_content
|
110
127
|
end
|
111
|
-
|
128
|
+
|
129
|
+
def copyable?(location)
|
130
|
+
location != 'inline' && File.extname(location) == '.js'
|
131
|
+
end
|
132
|
+
|
112
133
|
def compile(script, where = 'inline')
|
113
|
-
|
114
|
-
|
115
|
-
|
134
|
+
if copyable?(where)
|
135
|
+
out = script
|
136
|
+
else
|
137
|
+
Barista.invoke_hook :before_compilation, where
|
138
|
+
out = CoffeeScript.compile script, :bare => Barista.bare?
|
139
|
+
Barista.invoke_hook :compiled, where
|
140
|
+
end
|
116
141
|
out
|
117
142
|
rescue CoffeeScript::Error => e
|
118
143
|
Barista.invoke_hook :compilation_failed, where, e.message
|
@@ -139,14 +164,15 @@ module Barista
|
|
139
164
|
protected
|
140
165
|
|
141
166
|
def preamble(location)
|
142
|
-
|
167
|
+
inner_message = copyable?(location) ? "copied" : "compiled"
|
168
|
+
"/* DO NOT MODIFY. This file was #{inner_message} #{Time.now.httpdate} from\n * #{location.strip}\n */\n\n"
|
143
169
|
end
|
144
|
-
|
170
|
+
|
145
171
|
def compilation_error_for(location, message)
|
146
172
|
details = "Compilation of '#{location}' failed:\n#{message}"
|
147
173
|
Barista.verbose? ? "alert(#{details.to_json});" : nil
|
148
174
|
end
|
149
|
-
|
175
|
+
|
150
176
|
def setup_compiler_context(context)
|
151
177
|
if context.respond_to?(:read)
|
152
178
|
@context = context.read
|
@@ -163,6 +189,6 @@ module Barista
|
|
163
189
|
@options[:origin] ||= 'inline'
|
164
190
|
end
|
165
191
|
end
|
166
|
-
|
192
|
+
|
167
193
|
end
|
168
194
|
end
|
data/lib/barista/framework.rb
CHANGED
@@ -21,21 +21,33 @@ module Barista
|
|
21
21
|
end.uniq.sort_by { |f| f.length }
|
22
22
|
end
|
23
23
|
|
24
|
+
def self.exposed_javascripts
|
25
|
+
all(true).inject([]) do |collection, fw|
|
26
|
+
collection + fw.exposed_javascripts
|
27
|
+
end.uniq.sort_by { |f| f.length }
|
28
|
+
end
|
29
|
+
|
24
30
|
def self.coffeescript_glob_paths
|
25
31
|
all(true).map { |fw| fw.coffeescript_glob_path }
|
26
32
|
end
|
27
33
|
|
28
34
|
def self.full_path_for(script)
|
29
|
-
|
35
|
+
javascript = script.to_s.gsub(/\.coffee$/, '.js').gsub(/^\/+/, '')
|
36
|
+
coffeescript = script.to_s.gsub(/\.js$/, '.coffee').gsub(/^\/+/, '')
|
30
37
|
all(true).each do |fw|
|
31
|
-
full_path = fw.full_path_for(
|
38
|
+
full_path = fw.full_path_for(coffeescript) || fw.full_path_for(javascript)
|
32
39
|
return full_path, fw if full_path
|
33
40
|
end
|
34
41
|
nil
|
35
42
|
end
|
36
43
|
|
37
|
-
def self.register(name,
|
38
|
-
|
44
|
+
def self.register(name, options = nil)
|
45
|
+
if options.is_a?(Hash)
|
46
|
+
framework = self.new(options.merge(:name => name))
|
47
|
+
else
|
48
|
+
framework = self.new(:name => name, :root => options)
|
49
|
+
end
|
50
|
+
(@all ||= []) << framework
|
39
51
|
end
|
40
52
|
|
41
53
|
def self.[](name)
|
@@ -56,20 +68,28 @@ module Barista
|
|
56
68
|
end
|
57
69
|
# actually setup the framework.
|
58
70
|
check_options! options, :name, :root
|
59
|
-
@name
|
60
|
-
@output_prefix
|
61
|
-
@framework_root
|
62
|
-
|
71
|
+
@name = options[:name].to_s
|
72
|
+
@output_prefix = options[:output_prefix]
|
73
|
+
@framework_root = File.expand_path(options[:root].to_s)
|
74
|
+
self.output_root = options[:output_root]
|
63
75
|
end
|
64
76
|
|
65
77
|
def coffeescripts
|
66
78
|
Dir[coffeescript_glob_path]
|
67
79
|
end
|
68
80
|
|
81
|
+
def javascripts
|
82
|
+
Dir[javascript_glob_path]
|
83
|
+
end
|
84
|
+
|
69
85
|
def coffeescript_glob_path
|
70
86
|
@coffeescript_glob_path ||= File.join(@framework_root, "**", "*.coffee")
|
71
87
|
end
|
72
88
|
|
89
|
+
def javascript_glob_path
|
90
|
+
@javascript_glob_path ||= File.join(@framework_root, "**", "*.js")
|
91
|
+
end
|
92
|
+
|
73
93
|
def short_name(script)
|
74
94
|
short_name = remove_prefix script, @framework_root
|
75
95
|
File.join(*[@output_prefix, short_name].compact)
|
@@ -79,6 +99,10 @@ module Barista
|
|
79
99
|
coffeescripts.map { |script| short_name(script) }
|
80
100
|
end
|
81
101
|
|
102
|
+
def exposed_javascripts
|
103
|
+
javascripts.map { |script| short_name(script) }
|
104
|
+
end
|
105
|
+
|
82
106
|
def output_prefix=(value)
|
83
107
|
value = value.to_s.gsub(/(^\/|\/$)/, '').strip
|
84
108
|
@output_prefix = value.empty? ? nil : value
|
@@ -93,6 +117,14 @@ module Barista
|
|
93
117
|
@output_root || Barista.output_root
|
94
118
|
end
|
95
119
|
|
120
|
+
def output_root=(value)
|
121
|
+
if value.nil?
|
122
|
+
@output_root = nil
|
123
|
+
else
|
124
|
+
@output_root = Pathname(value.to_s)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
96
128
|
def output_path_for(file, format = 'js')
|
97
129
|
# Strip the leading slashes
|
98
130
|
file = file.to_s.gsub(/^\/+/, '')
|
data/lib/barista/version.rb
CHANGED
@@ -10,12 +10,14 @@ Barista.configure do |c|
|
|
10
10
|
# Disable auto compile, use generated file directly:
|
11
11
|
# c.auto_compile = false
|
12
12
|
|
13
|
-
#
|
13
|
+
# Add a new framework:
|
14
|
+
|
15
|
+
# c.register :tests, :root => Rails.root.join('test', 'coffeescript'), :output_prefix => 'test'
|
14
16
|
|
15
17
|
# Disable wrapping in a closure:
|
16
|
-
# c.
|
18
|
+
# c.bare = true
|
17
19
|
# ... or ...
|
18
|
-
# c.
|
20
|
+
# c.bare!
|
19
21
|
|
20
22
|
# Change the output root for a framework:
|
21
23
|
|
@@ -24,13 +26,17 @@ Barista.configure do |c|
|
|
24
26
|
# or for all frameworks...
|
25
27
|
|
26
28
|
# c.each_framework do |framework|
|
27
|
-
# c.change_output_prefix! framework
|
29
|
+
# c.change_output_prefix! framework, "vendor/#{framework.name}"
|
28
30
|
# end
|
29
31
|
|
30
32
|
# or, prefix the path for the app files:
|
31
33
|
|
32
34
|
# c.change_output_prefix! :default, 'my-app-name'
|
33
35
|
|
36
|
+
# or, change the directory the framework goes into full stop:
|
37
|
+
|
38
|
+
# c.change_output_prefix! :tests, Rails.root.join('spec', 'javascripts')
|
39
|
+
|
34
40
|
# or, hook into the compilation:
|
35
41
|
|
36
42
|
# c.before_compilation { |path| puts "Barista: Compiling #{path}" }
|
metadata
CHANGED
@@ -1,15 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: barista
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
9
|
- 0
|
10
|
-
|
11
|
-
- 1
|
12
|
-
version: 1.1.0.pre1
|
10
|
+
version: 1.1.0
|
13
11
|
platform: ruby
|
14
12
|
authors:
|
15
13
|
- Darcy Laycock
|
@@ -17,7 +15,7 @@ autorequire:
|
|
17
15
|
bindir: bin
|
18
16
|
cert_chain: []
|
19
17
|
|
20
|
-
date: 2011-
|
18
|
+
date: 2011-06-01 00:00:00 +08:00
|
21
19
|
default_executable:
|
22
20
|
dependencies:
|
23
21
|
- !ruby/object:Gem::Dependency
|
@@ -150,14 +148,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
150
148
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
149
|
none: false
|
152
150
|
requirements:
|
153
|
-
- - "
|
151
|
+
- - ">="
|
154
152
|
- !ruby/object:Gem::Version
|
155
|
-
hash:
|
153
|
+
hash: 3
|
156
154
|
segments:
|
157
|
-
-
|
158
|
-
|
159
|
-
- 1
|
160
|
-
version: 1.3.1
|
155
|
+
- 0
|
156
|
+
version: "0"
|
161
157
|
requirements: []
|
162
158
|
|
163
159
|
rubyforge_project:
|