Permalink
Browse files

CoffeeScript Processor

  • Loading branch information...
1 parent c52e326 commit 03e48e8260d8a0f95eb18f57da0992667fd028b2 @bcardarella bcardarella committed Jan 24, 2012
Showing with 458 additions and 0 deletions.
  1. +41 −0 Rakefile
  2. +35 −0 coffeescript/processor.rb
  3. +376 −0 coffeescript/rails.validations.coffee
  4. +1 −0 gemfiles/Gemfile.rails-3.1.x
  5. +5 −0 test/javascript/server.rb
View
@@ -1,4 +1,5 @@
require 'bundler'
+require File.join(File.expand_path('..', __FILE__), 'coffeescript/processor')
Bundler::GemHelper.install_tasks
multitask :default => 'test:ruby'
@@ -35,6 +36,46 @@ namespace :test do
end
end
+desc %{Regenerate and commit JavaScript file}
+task :regenerate_javascript do
+ regenerate_javascript
+end
+
+Rake::Task[:build].instance_eval { @actions.clear }
+task :build do
+ regenerate_javascript
+ perform_git_commit
+ Bundler::GemHelper.new(Dir.pwd).build_gem
+end
+
+def perform_git_commit
+ sh_with_code('git add vendor')
+ out, code = sh_with_code('git commit -m "Regenerated JavaScript"')
+ if code == 0
+ puts "Committed changes"
+ else
+ puts "Nothing to commit"
+ end
+end
+
+def regenerate_javascript
+ ClientSideValidations::Processor.run
+ puts 'Regenerated JavaScript'
+end
+
+def sh_with_code(cmd, &block)
+ cmd << " 2>&1"
+ outbuf = ''
+ Bundler.ui.debug(cmd)
+ Dir.chdir(Dir.pwd) {
+ outbuf = `#{cmd}`
+ if $? == 0
+ block.call(outbuf) if block
+ end
+ }
+ [outbuf, $?]
+end
+
PORT = 4567
# Returns an array e.g.: ['open', 'http://example.com']
View
@@ -0,0 +1,35 @@
+$:.unshift(File.expand_path('../../lib', __FILE__))
+
+require 'client_side_validations/version'
+require 'coffee_script'
+require 'erb'
+
+module ClientSideValidations
+ class Processor
+ def self.run
+ write_file
+ end
+
+ def self.root_path
+ File.expand_path('../..', __FILE__)
+ end
+
+ def self.file_name
+ 'rails.validations'
+ end
+
+ def self.template
+ ERB.new(File.open(File.join(root_path, 'coffeescript', "#{file_name}.coffee")).read)
+ end
+
+ def self.compiled_coffeescript
+ CoffeeScript.compile(template.result(binding))
+ end
+
+ def self.write_file
+ file = File.new(File.join(root_path, "vendor/assets/javascripts/#{file_name}.js"), 'w')
+ file << compiled_coffeescript
+ file.close
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 03e48e8

Please sign in to comment.