Skip to content

Instantly share code, notes, and snippets.

@vic
Created October 17, 2008 23:23
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save vic/17560 to your computer and use it in GitHub Desktop.
This example shows how to use a DRb client/server
with buildr 1.3.3.
Invoke this file the first time with a --drb flag
to start the server. If no --drb flag is present
we connect to the server.
Features:
- Execution of tasks on a previously loaded buildfile
- buildfile reloading if modified.# - NO CLIENT OUTPUT redirection is implemented.
#!/usr/bin/env ruby
exit !!!eval(DATA.read, binding) if $0 == __FILE__
repositories.remote << "http://repo1.maven.org/maven2"
define('foo') do
project.version = '1.3.3'
puts "Defined project Foo"
@time = 0
task('hello') { puts "Hello world #{@time += 1} time!" }
task('bye') do
$stderr.print "Your name? "
name = $stdin.gets; puts "See ya #{name}"
end
define('bar') do
package(:jar)
end
end
__END__
require File.expand_path('dbuildr', File.dirname(__FILE__))
#!/usr/bin/env ruby
$:.push '/home/vic/hk/buildr/lib', '/home/vic/hk/buildr/addon' # for testing on vic env.
require 'drb'
PORT = 9876
begin
buildr = DRbObject.new(nil, "druby://:#{PORT}")
buildr.remote_ping # test if the server is running
DRb.start_service("druby://:#{PORT + 1}")
buildr.remote_run Dir.pwd, $stdin, $stdout, $stderr, ARGV
rescue DRb::DRbConnError => e
require 'rubygems'
gem 'rake', '>=0.8.3'
require 'buildr'
module DistributedBuildr
# save the tasks,rules,layout defined by buildr
# based on the code from the sandbox
@tasks = Buildr.application.tasks.collect do |original|
prerequisites = original.send(:prerequisites).map(&:to_s)
actions = original.instance_eval { @actions }.clone
lambda do
original.class.send(:define_task, original.name=>prerequisites).tap do |task|
task.comment = original.comment
actions.each { |action| task.enhance &action }
end
end
end
@rules = Buildr.application.instance_variable_get(:@rules)
@layout = Layout.default.clone
class << self
attr_accessor :tasks, :rules, :layout
end
def remote_ping
true # tell the client we are alive
end
def remote_run(original_dir, stdin, stdout, stderr, argv)
$stdin, $stdout, $stderr = stdin, stdout, stderr
ARGV.replace argv
@original_dir = original_dir
init 'Distributed Buildr'
if @rakefile
if !@last_loaded || buildfile.timestamp > @last_loaded
# buildfile updated, need to reload
Project.clear
@tasks = {}
DistributedBuildr.tasks.each { |block| block.call }
@rules = DistributedBuildr.rules.clone
Layout.default = DistributedBuildr.layout.clone
load_buildfile
@last_loaded = buildfile.timestamp
else
clear_invoked_tasks
end
else
load_buildfile
@last_loaded = buildfile.timestamp
end
top_level
end
def clear_invoked_tasks
lookup('buildr:initialize').instance_eval do
@already_invoked = false
@actions = []
end
projects = Project.instance_variable_get(:@projects) || {}
@tasks.each_pair do |name, task|
is_project = projects.key?(task.name)
task.instance_variable_set(:@already_invoked, false) unless is_project
end
end
end # DistribuitedBuildr
class Buildr::Application; include DistributedBuildr; end
DRb.start_service("druby://:#{PORT}", Buildr.application)
puts "DistribuitedBuildr waiting for invocation on port #{PORT}."
DRb.thread.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment