HELLO, I’M SERHAT AND THIS IS MY FANCY TITLE.

Running Rake Tasks in a Loop


Rake tasks in a loop, will only executed once when they are not re-enabled. Take a look to this example:


namespace :yoksis do
  desc 'fetches all references'
  task :references do
    mapping = {
      get_instruction_language: 'UnitInstructionLanguage',
      get_instruction_type: 'UnitInstructionType'
    }

    mapping.each do |action, klass|
      Rake::Task['yoksis:reference'].invoke(action, klass)
    end
  end

  desc 'fetch an individual reference'
  task :reference, %i[soap_method klass] => [:environment] do |_, args|
    puts args[:soap_method]
    puts args[:klass]
  end
end

When you run the yoksis:references task, it will only print out {get_instruction_language: 'UnitInstructionLanguage'} and will skip the second item of the mapping hash.

Continue reading
Date: | Tags: ruby, ruby on rails, rake, rake in loop | Categories: tech, ruby, ruby on rails

Running Shell Commands with Rake


We all owe to Jim Weirich (RIP) - the father of the Rake, developer of many great projects such as builder, RubyKoans (I also contributed to this project lately with some PRs) and Git Immersion. I learned a lot from his talks on YouTube and really admire him.

In this post, I'm going to share a small tip about Rake for his memory and his awesome work.

Running shell commands with Rake is actually pretty simple and straightforward. sh followed by some bash commands is what we are going to be using.

Here is a basic example:

task :gs do
  sh "git status"
end

After defining the task like shown above, just run the task with:

$ rake gs

and you will see exactly the same output as git status does.

Continue reading
Date: | Tags: rake, ruby and rake, rake examples, shell commands with rake | Categories: tech, ruby