Sha256: 294f0d7f52b33165a79908bface503e54754620fec0feb864186e684e67c3571

Contents?: true

Size: 1.16 KB

Versions: 9

Compression:

Stored size: 1.16 KB

Contents

# See https://code.jjb.cc/running-rake-tasks-from-within-ruby-on-rails-code
module RubyClock::Rake
  def prepare_rake
    if defined?(::Rails) && Rails.application
      Rails.application.load_tasks
      Rake::Task.tasks.each{|t| t.prerequisites.delete 'environment' }
      @rake_mutex = Mutex.new
    else
      puts <<~MESSAGE
        Because this is not a rails application, we do not know how to load your
        rake tasks. You can do this yourself at the top of your Clockfile if you want
        to run rake tasks from ruby-clock.
      MESSAGE
    end
  end

  # for tasks that don't have dependencies
  def rake_execute(task)
    Rake::Task[task].execute
  end

  # If the task doesn't share dependencies with another task,
  # or if it does and you know you'll never run tasks such that any overlap
  def rake_async(task)
    Rake::Task[task].invoke
  ensure
    Rake::Task[task].reenable
    Rake::Task[task].all_prerequisite_tasks.each(&:reenable)
  end

  # If the task has shared dependencies and you might run more than one at the same time
  # This is the safest option and hence the default.
  def rake(task)
    @rake_mutex.synchronize { rake_async(task) }
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ruby-clock-2.0.1 lib/ruby-clock/rake.rb
ruby-clock-2.0.0 lib/ruby-clock/rake.rb
ruby-clock-2.0.0.beta10 lib/ruby-clock/rake.rb
ruby-clock-2.0.0.beta9 lib/ruby-clock/rake.rb
ruby-clock-2.0.0.beta8 lib/ruby-clock/rake.rb
ruby-clock-2.0.0.beta7 lib/ruby-clock/rake.rb
ruby-clock-2.0.0.beta6 lib/ruby-clock/rake.rb
ruby-clock-2.0.0.beta5 lib/ruby-clock/rake.rb
ruby-clock-2.0.0.beta4 lib/ruby-clock/rake.rb