Skip to content

Commit 03e48e8

Browse files
committed
CoffeeScript Processor
1 parent c52e326 commit 03e48e8

File tree

5 files changed

+458
-0
lines changed

5 files changed

+458
-0
lines changed

Rakefile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'bundler'
2+
require File.join(File.expand_path('..', __FILE__), 'coffeescript/processor')
23
Bundler::GemHelper.install_tasks
34

45
multitask :default => 'test:ruby'
@@ -35,6 +36,46 @@ namespace :test do
3536
end
3637
end
3738

39+
desc %{Regenerate and commit JavaScript file}
40+
task :regenerate_javascript do
41+
regenerate_javascript
42+
end
43+
44+
Rake::Task[:build].instance_eval { @actions.clear }
45+
task :build do
46+
regenerate_javascript
47+
perform_git_commit
48+
Bundler::GemHelper.new(Dir.pwd).build_gem
49+
end
50+
51+
def perform_git_commit
52+
sh_with_code('git add vendor')
53+
out, code = sh_with_code('git commit -m "Regenerated JavaScript"')
54+
if code == 0
55+
puts "Committed changes"
56+
else
57+
puts "Nothing to commit"
58+
end
59+
end
60+
61+
def regenerate_javascript
62+
ClientSideValidations::Processor.run
63+
puts 'Regenerated JavaScript'
64+
end
65+
66+
def sh_with_code(cmd, &block)
67+
cmd << " 2>&1"
68+
outbuf = ''
69+
Bundler.ui.debug(cmd)
70+
Dir.chdir(Dir.pwd) {
71+
outbuf = `#{cmd}`
72+
if $? == 0
73+
block.call(outbuf) if block
74+
end
75+
}
76+
[outbuf, $?]
77+
end
78+
3879
PORT = 4567
3980

4081
# Returns an array e.g.: ['open', 'http://example.com']

coffeescript/processor.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
$:.unshift(File.expand_path('../../lib', __FILE__))
2+
3+
require 'client_side_validations/version'
4+
require 'coffee_script'
5+
require 'erb'
6+
7+
module ClientSideValidations
8+
class Processor
9+
def self.run
10+
write_file
11+
end
12+
13+
def self.root_path
14+
File.expand_path('../..', __FILE__)
15+
end
16+
17+
def self.file_name
18+
'rails.validations'
19+
end
20+
21+
def self.template
22+
ERB.new(File.open(File.join(root_path, 'coffeescript', "#{file_name}.coffee")).read)
23+
end
24+
25+
def self.compiled_coffeescript
26+
CoffeeScript.compile(template.result(binding))
27+
end
28+
29+
def self.write_file
30+
file = File.new(File.join(root_path, "vendor/assets/javascripts/#{file_name}.js"), 'w')
31+
file << compiled_coffeescript
32+
file.close
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)