begin require 'bundler/setup' rescue LoadError puts 'You must `gem install bundler` and `bundle install` to run rake tasks' end require 'rdoc/task' RDoc::Task.new(:rdoc) do |rdoc| rdoc.rdoc_dir = 'rdoc' rdoc.title = 'Fiona7' rdoc.options << '--line-numbers' rdoc.rdoc_files.include('README.rdoc') rdoc.rdoc_files.include('lib/**/*.rb') end namespace "package" do desc "Package javascript" task "js" do require 'babel/transpiler' require 'handlebars_assets' puts "Transpiling javascript files" Dir["app/**/*"].each do |fname| if /\.es6\.js$/ =~ fname nfname = ::File.join(::File.dirname(fname), ::File.basename(fname, '.es6.js') + '.js') puts "#{fname} -> #{nfname}" ::File.open(nfname, 'w') do |f| f.write Babel::Transpiler.transform(File.read(fname))["code"] end end if /\.js\.jsx$/ =~ fname nfname = ::File.join(::File.dirname(fname), ::File.basename(fname, '.js.jsx') + '.js') puts "#{fname} -> #{nfname}" ::File.open(nfname, 'w') do |f| f.write Babel::Transpiler.transform(File.read(fname))["code"] end end end puts "Processing handlebars templates" templates = [] template_namespace = 'ScrivitoHandlebarsTemplates' Dir["app/assets/javascripts/fiona7/templates/**/*"].each do |fname| if /\.hbs$/ =~ fname puts "Compiling #{fname}" template_name = fname.sub(/.*javascripts\/fiona7\/templates\//, '').sub(/\.hbs$/, '').dump precompiled = HandlebarsAssets::Handlebars.precompile(File.read(fname)) template = "Handlebars.template(#{precompiled})" templates << <<-TEMPLATE (function() { this.#{template_namespace} || (this.#{template_namespace} = {}); this.#{template_namespace}[#{template_name}] = #{template}; return this.#{template_namespace}[#{template_name}]; }).call(this); TEMPLATE end end puts "Writing app/assets/javascripts/fiona7/templates.js" File.open('app/assets/javascripts/fiona7/templates.js', 'w') do |f| templates.each do |t| f.write(t) end end end end namespace "clean" do desc "Clean packaged javascript" task "js" do puts "Cleaning transpiled javascript files" Dir["app/**/*"].each do |fname| if /\.es6\.js$/ =~ fname nfname = ::File.join(::File.dirname(fname), ::File.basename(fname, '.es6.js') + '.js') puts "#{nfname}" ::FileUtils.rm(nfname) if ::File.exists?(nfname) end if /\.js\.jsx$/ =~ fname nfname = ::File.join(::File.dirname(fname), ::File.basename(fname, '.js.jsx') + '.js') puts "#{nfname}" ::FileUtils.rm(nfname) if ::File.exists?(nfname) end end end end task "build" => ["clean:js", "package:js"] do # dummy, needed for dependencies, the rest is added later by bundler end Bundler::GemHelper.install_tasks task "build" => ["clean:js"] do # dummy, needed to clean-up end