How to write coffee-script and sass in Rhodes / RhoMobile? - coffeescript

I would like to write sass and coffee-script code in Rhodes/RhoMobile and let the compiler automatically compile to js and css file before run or build task, for every platforms I am targeting

In this example you place your sass files in assets/stylesheets directory. Processed and compressed files will output in public/css. This also preserve the sub directory structure
An error during compilation will stop the process
Adjust the before hook depending on the platform your are targetting
Add this task at then very end of your Rakefile:
task :precompile_css do
Dir.chdir $app_path do
exit_code = system 'scss --force --update assets/stylesheets:public/css --style compressed'
raise 'Precompile error' unless exit_code
end
end
# Add all platforms you need.
# To get the exact name of the task to hook you can execute
# $ rake -n run:my_platform
task 'config:android' => :precompile_css
task 'config:wm' => :precompile_css
etc ...
Also possible with coffeescript
task :precompile_js do
Dir.chdir $app_path do
exit_code = system 'coffee --compile --output public/js/ assets/javascripts/'
raise 'Precompile error' unless exit_code
end
end
task 'config:android' => :precompile_js
task 'config:wm' => :precompile_js
etc...
Then just invoke your run or build tasks as usual

Related

rake executes file task when file already exists

I have a rakefile that executes some (but not all) of it's file tasks even if the files of interest have already been built. The frustrating thing, is that paring down my rake file to a MWE resolves the problem---even though I haven't altered anything wrt the filetask definition, how the files are being selected, the dependencies, or anything else. It seems that simply removing other (file)tasks from the rakefile remedies the problem.
I realize this is a really awful question, but does anyone have ideas about what might be going on here? I'd post sample code, but my MWE works as expected and I don't have any sense for what is causing the problem in the full rake file. All I can think to do is demonstrate that my MWE is literally an excerpt from the full Rakefile, unaltered...
➜ solutionmaps cat mwe/Rakefile|sed '/^$/d'|tee a
require 'rake'
require 'rake/clean'
require 'pathname'
HOME = ENV['HOME']
SHARED_ATLAS = "#{HOME}/MRI/Manchester/data/CommonBrains/MNI_EPI_funcRes.nii"
TXT = Rake::FileList["txt/nodestrength/??.mni"]
AFNI_RAW = TXT.pathmap("afni/nodestrength/%n_raw+tlrc.HEAD")
AFNI_RAW.zip(TXT).each do |target,source|
file target => [source] do
sh("3dUndump -master #{SHARED_ATLAS} -xyz -datum float -prefix #{target.sub("+tlrc.HEAD","")} #{source}")
end
CLOBBER.push(target)
CLOBBER.push(target.sub(".HEAD",".BRIK"))
CLOBBER.push(target.sub(".HEAD",".BRIK.gz"))
end
➜ solutionmaps perl -ne 'print if ($seen{$_} .= #ARGV) =~ /10$/' Rakefile mwe/Rakefile|sed '/^$/d'|tee b
require 'rake'
require 'rake/clean'
require 'pathname'
HOME = ENV['HOME']
SHARED_ATLAS = "#{HOME}/MRI/Manchester/data/CommonBrains/MNI_EPI_funcRes.nii"
TXT = Rake::FileList["txt/nodestrength/??.mni"]
AFNI_RAW = TXT.pathmap("afni/nodestrength/%n_raw+tlrc.HEAD")
AFNI_RAW.zip(TXT).each do |target,source|
file target => [source] do
sh("3dUndump -master #{SHARED_ATLAS} -xyz -datum float -prefix #{target.sub("+tlrc.HEAD","")} #{source}")
end
CLOBBER.push(target)
CLOBBER.push(target.sub(".HEAD",".BRIK"))
CLOBBER.push(target.sub(".HEAD",".BRIK.gz"))
end
➜ solutionmaps diff a b
➜ solutionmaps
And that my mwe works as expected (that is, it does not execute the file task).
➜ mwe rake --trace --dry-run afni/nodestrength/02_raw+tlrc.HEAD
** Invoke afni/nodestrength/02_raw+tlrc.HEAD (first_time, not_needed)
** Invoke txt/nodestrength/02.mni (first_time, not_needed)
But the full rakefile does not.
rake --trace --dry-run afni/nodestrength/02_raw+tlrc.HEAD
** Invoke afni/nodestrength/02_raw+tlrc.HEAD (first_time)
** Invoke txt/nodestrength/02.mni (first_time, not_needed)
** Execute (dry run) afni/nodestrength/02_raw+tlrc.HEAD
➜ solutionmaps ls afni/nodestrength/02_raw+tlrc.HEAD
afni/nodestrength/02_raw+tlrc.HEAD
Finally happened across a possible answer:
Rake determines that a file task needs to be run if the file doesn’t exist or if any of the prerequisite file tasks are newer.
Quoted from: http://madewithenvy.com/ecosystem/articles/2013/rake-file-tasks/
Since my Rakefiles are under heavy development, and my Filetasks are all pretty interrelated, this is probably why my Rakefile always wanted to rebuild everything.

capistrano v2 not failing / rolling back when custom task fails

i have to compile a custom c coded binary used by our rails app.
this setup is held in a custom rake file (ourapp.rake, below)
running cap v2 i noticed the make was failing but the deploy didn't "fail".
i since just made the task
system "cd #{thedir} && exit 1" # simulate failing of custom task
but the deploy:cold doesn't fail, the debug output (below) clearly shows make failing
am i missing something? i've tried
searching for error codes/failing scenario of capistrano - nothing (lots of mentions of trying to run custom scripts on failing)
system v run v invoke
help appreciated, code below
# ourapp.rake
namespace :ourapp do
desc "Compile and Install Performant Parser"
task :compile_performant_parser do
thedir=File.join(Rails.root, 'parser')
system "cd #{thedir} && make clean && make && make install"
end # compile
desc "Compile and Install Compareplans process"
task :compile_binary do
thedir=File.join(Rails.root, 'compareplans_process/src')
#system "cd #{thedir} && make clean && make && make install"
system "exit 1"
end # compile
task :install => [:compile_performant_parser, :compile_binary ] do
puts "Preparing Ourapp for run"
end
end
additions to deploy.rb
namespace :deploy do
desc "setup ourapp dependencies, dir, binaries and (later data)"
task :setup_ourapp do
run "cd #{current_release} && /usr/bin/env bundle exec rake our app:install RAILS_ENV=#{rails_env}"
end
after 'deploy:update_code', 'deploy:setup_ourapp'
end
so i figured out that capistrano does not exit, so you need to test the error code
Capistrano run local command exit on failure
and to rollback you need to use a transaction type
How do I use transactions within custom capistrano tasks?

Creating and calling custom deployment task using run_locally in Capistrano version 3

I have a static page that I want to compile locally using gulp. The command I would run in the local shell, from the directory that contains gulp and the gulpfile (set by compile_path in this example) would be "$> gulp build".
# config valid only for Capistrano 3.1
lock '3.1.0'
set :application, 'appname'
set :repo_url, 'git#bitbucket.org/appname.git'
set :compile_path, '/Users/nico/DevOps/repo/appname'
# Default branch is :master
set :branch, 'cap3'
namespace :deploy do
after :started, :notify do
desc 'Run gulp to compile the static site'
task :gulp_build do
run_locally do
execute "#{fetch(:compile_path)}/gulp", " build"
end
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
# execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
Basically, what I'm trying to achieve is a local precompile so that my deployment consists of simply sending the locally compiled files to a deployment location. when I execute "bundle exec cap staging deploy:gulp_build" I get:
cap aborted!
Don't know how to build task 'deploy:gulp_build'
/Users/nico/.rvm/gems/ruby-1.9.3-p545/gems/capistrano-3.1.0/lib/capistrano/application.rb:15:in run'
/Users/nico/.rvm/gems/ruby-1.9.3-p545/gems/capistrano-3.1.0/bin/cap:3:in'
/Users/nico/.rvm/gems/ruby-1.9.3-p545/bin/cap:23:in load'
/Users/nico/.rvm/gems/ruby-1.9.3-p545/bin/cap:23:in'
/Users/nico/.rvm/gems/ruby-1.9.3-p545/bin/ruby_executable_hooks:15:in eval'
/Users/nico/.rvm/gems/ruby-1.9.3-p545/bin/ruby_executable_hooks:15:in'
(See full trace by running task with --trace)
I realize that there are probably much better ways to deploy this, but it's a companion static site to a rails app which is being deployed successfully via capistrano, and I'd like to just use the same deployment method for both.
This was handled pretty well by creating a new task in the deploy namespace. in my code below are placeholders for real values that I didn't want to post on SO.
lib/capistrano/tasks/gulp_build_local.cap:
#assumes the gulpfile is in root of your cap install
namespace :deploy do
desc 'Run gulp to compile the static site'
task :gulp_build do
#run_locally doesn't play nice with the 'on' directive (it's 'on' localhost)
run_locally do
execute "gulp build"
end
end
end
deploy.rb:
# config valid only for Capistrano 3.1
lock '3.1.0'
set :application, '<appname>'
set :repo_url, 'git#bitbucket.org/<appname>.git'
namespace :deploy do
#custom tasks to build via gulp
before :deploy, 'gulp_build_local'
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
#nothing here, because there's no app server for this static site.
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
#nothing here
end
end
end
of course, once I figured this out I immediately deprecated it in favor of new tasks to install gulp in the release dir on the target, compiling there and linking the site root to the pub folder generated by the gulp process. Hopefully this learning experience will be useful for someone working though the use of run_locally, though.

run rake ts:index from within a rake task

I am running Rails 2.3.5.
In my project I have in lib/tasks the following rake task (test_task.rake):
desc 'test_synchro_task'
task :test_synchro_task => :environment do
# A bunch of things that are properly executed (basically I am inserting
# in the database)...
# ...
# and once the above is done, I want the following to be executed,
# the sphinx index to be rebuild, but it is NOT :
system("cd /sites/project/app")
system("RAILS_ENV='staging' rake ts:index")
end
I trigger the execution of the task via a crontab containing the following entry:
13 14 * * * cd /sites/project/app && /sites/ruby/bin/rake RAILS_ENV=staging test_task
which id correctly called and executed except for 2 system lines in the task.
Please note that when I place those 2 system lines in a ruby test.rb file in my project script directory, and run it manually using the ruby command:
ruby test.rb
those 2 system commands are properly executed and the index is rebuilt correctly.
In my rake task I tried replacing those 2 system lines by:
%x["cd /sites/project/app"]
%x["RAILS_ENV='staging' rake ts:index"]
or by
#cmd="cd /sites/project/app; RAILS_ENV='staging' rake ts:index"
`#{#cmd}`
but the rake ts:index is still not executed.
Any idea why?
Many thanks.
Yves
Problem resolved:
1- In the ruby script, I found that the $?.exitstatus was 127, which is "command not found".
2- This hinted me to a PATH problem occurring in the context of cron.
3- Found that post: http://dewful.com/?p=157 titled "Ruby - Cron Not Working For Ruby Script".
4- Added the PATH in the crontab and everything works fine.
Yves

Rails 3.1 - how can I tell if assets are precompiling on production?

Trying to get the hang of deploying a rails 3.1 App ...
Based on what I've read, I've put the following code in my deploy.rb:
before "deploy:symlink", "assets:precompile"
namespace :assets do
desc "Compile assets"
task :precompile, :roles => :app do
run "cd #{release_path} && rake RAILS_ENV=#{rails_env} assets:precompile"
end
end
But to tell you the truth, I can't notice any difference with or without it. Is there something I'm missing here?
EDIT* found the answer:
http://spreecommerce.com/blog
To pre-compile assets for production you would normally execute the following rake task (on the production server).
$ bundle exec rake assets:precompile
This would write all the assets to the public/assets directory while including an MD5 fingerprint in the filename for added caching benefits.
NOTE: In production all references to assets from views using image_tag, asset_path, javascript_include_tag, etc. will automatically include this fingerprint in the file name so the correct version will be served.
There is configuration to do, but it should be correctly set by default. Get in your config/application.rb and see if you find this:
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
...
config.assets.enabled = true
You should also have those in your production.rb file:
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
This should be set that way. Is it?