|
| 1 | +require 'open3' |
| 2 | + |
| 3 | +module Buck |
| 4 | + |
| 5 | + def self.buck_cmd |
| 6 | + @@buck_cmd ||= ( |
| 7 | + lambda { |command, target, &block| |
| 8 | + cmd = "buck #{command} #{target}" |
| 9 | + output = '' |
| 10 | + err = '' |
| 11 | + Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr| |
| 12 | + stdin.close |
| 13 | + |
| 14 | + while line = stderr.gets |
| 15 | + if command == 'build' |
| 16 | + puts line |
| 17 | + end |
| 18 | + err += line |
| 19 | + end |
| 20 | + |
| 21 | + while line = stdout.gets |
| 22 | + output += line |
| 23 | + end |
| 24 | + |
| 25 | + # In jruby, wait_thr always appears to be nil. *sigh* |
| 26 | +# if !wait_thr.value.success? |
| 27 | +# raise "Unable to execute command: " + err.to_s |
| 28 | +# end |
| 29 | + end |
| 30 | + |
| 31 | + block.call(output.to_s) if block |
| 32 | + } |
| 33 | + ) |
| 34 | + |
| 35 | + #block.call(output) if block |
| 36 | + end |
| 37 | + |
| 38 | + def self.enhance_task(task) |
| 39 | + class <<task |
| 40 | + def needed? |
| 41 | + true |
| 42 | + end |
| 43 | + |
| 44 | + attr_reader :out |
| 45 | + def out |
| 46 | + @out ||= Buck::find_buck_out(name) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + task |
| 51 | + end |
| 52 | + |
| 53 | + def self.find_buck_out(target) |
| 54 | + out = nil |
| 55 | + |
| 56 | + Buck::buck_cmd.call('targets', "--show-output #{target}") do |output| |
| 57 | + sections = output.chomp.split |
| 58 | + # Not all buck rules have an output file. |
| 59 | + if sections.size > 1 |
| 60 | + out = output.chomp.split[-1] |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + out |
| 65 | + end |
| 66 | + |
| 67 | +end |
| 68 | + |
| 69 | +def buck(*args, &block) |
| 70 | + case args[0] |
| 71 | + when String |
| 72 | + name = args[0].to_sym |
| 73 | + prereqs = [] |
| 74 | + when Hash |
| 75 | + name = args[0].keys[0] |
| 76 | + pres = args[0][name] |
| 77 | + case pres |
| 78 | + when String |
| 79 | + prereqs = [pres] |
| 80 | + when Array |
| 81 | + prereqs = pres |
| 82 | + else |
| 83 | + raise "I have no idea what to do with this: #{pres.class.to_s}" |
| 84 | + end |
| 85 | + else |
| 86 | + raise "Unknown arg type: #{args[0].class.to_s}" |
| 87 | + end |
| 88 | + |
| 89 | + task = Rake::Task.task_defined?(name) ? Rake::Task[name] : Rake::Task.define_task(name) |
| 90 | + task.enhance prereqs do |
| 91 | + Buck::buck_cmd.call('build', name) |
| 92 | + block.call if block |
| 93 | + end |
| 94 | + |
| 95 | + Buck::enhance_task(task) |
| 96 | +end |
| 97 | + |
| 98 | +rule /\/\/.*/ do |task| |
| 99 | + # Task is a FileTask, but that's not what we need. Instead, just delegate down to buck in all cases |
| 100 | + |
| 101 | + task.enhance do |
| 102 | + Buck::buck_cmd.call('build', task.name) |
| 103 | + end |
| 104 | + |
| 105 | + Buck::enhance_task(task) |
| 106 | +end |
0 commit comments