Class: Metabuild::Builder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/metabuild.rb

Overview

The builder class is responsible for triggerring targets and reporting failures or exceptions via the logging facilities

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = nil, targets = []) ⇒ Builder

Returns a new instance of Builder.



911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
# File 'lib/metabuild.rb', line 911

def initialize(name, options = nil, targets = [])
  @name, @options, @targets  = name, options, targets
  raise "Builder needs Options (none given)!" if options.nil?
  title = "Report for #{name}"
  @log = case @options.fetch("logtype", "text")
         when "text" then LogText.new(title)
         when "html" 
           raise "Need a logfile for html reports" if @options["logfile"].nil? or @options["logfile"].empty?
           LogHtml.new(title, @options["logfile"])
         else raise "Unknown log type #{@options["logtype"]}"
         end  
  @default_targets = []
  @env = {"WORKSPACE" => @options.fetch("workspace", Dir.pwd)}
  @subprocesses = []
end

Instance Attribute Details

#default_targetsObject

Returns the value of attribute default_targets.



908
909
910
# File 'lib/metabuild.rb', line 908

def default_targets
  @default_targets
end

#envObject

Returns the value of attribute env.



908
909
910
# File 'lib/metabuild.rb', line 908

def env
  @env
end

#nameObject

Returns the value of attribute name.



908
909
910
# File 'lib/metabuild.rb', line 908

def name
  @name
end

#targetsObject

Returns the value of attribute targets.



908
909
910
# File 'lib/metabuild.rb', line 908

def targets
  @targets
end

Instance Method Details

#add_target(target) ⇒ Object

Add target, either a single one, or an array of targets



1072
1073
1074
1075
1076
1077
1078
# File 'lib/metabuild.rb', line 1072

def add_target(target)
  if target.kind_of? Array
    @targets = @targets | target
  else
    @targets.push target
  end
end

#get_target(name) ⇒ Object



1084
1085
1086
1087
1088
1089
# File 'lib/metabuild.rb', line 1084

def get_target(name)
  @targets.each do |t|
    return t if t.name == name
  end
  raise "Unknown target #{name} for build #{@name}"
end

#launchObject

Run targets according to what the user specified in ARGV Dependencies among targets can be propagated or not (default = no) Outpout log and stop



938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
# File 'lib/metabuild.rb', line 938

def launch
  begin
    
    if @options.help?
      puts @options.help
      puts "Default targets are built if no targets are given as argument. You can specify any of the targets listed below"
      puts "Default targets :"
      @default_targets.each { |t| puts t.name }
      puts "The following targets are defined :"
      @targets.each { |t| puts t.name }
      exit
    end

    if @options.fetch("dep_graph", "no").downcase == "yes"
      output_graph
    end

    @options.from_targets.each do |targ|
      get_target(targ).propagate_disable
    end

    use_cache = (@options.fetch("cache", "no").downcase == "yes")
    parallel = (@options.fetch("parallel", "no").downcase == "yes")
    targs = @options.rest.empty? ? @default_targets.map {|t| t.to_s} : @options.rest
    if @options.restart_at
       targs.each do |t|
          break if get_target(t).propagate_restart_at(@options.restart_at)
       end
    end         

    if @options.fetch("dependency", "yes").downcase != "yes"
      targs.each { |t| targ = get_target(t); targ.run(use_cache) unless targ.nil? }
    else
      targs.each { |t| targ = get_target(t); targ.propagate_run(use_cache, parallel) unless targ.nil? }
    end
    @log.finish
  rescue Exception => exception
    # Uses @report_done in Log to prevent reporting several times
    if exception.kind_of? SystemExit 
      @log.failure "Error : target exited with failure code" unless exception.success?
    else
      @log.failure("Error in target : (#{exception.class}: #{exception.message})\n" + exception.backtrace.join("\n")) 
    end
  end
end

#logtitleObject



931
932
933
# File 'lib/metabuild.rb', line 931

def logtitle
  @log.name
end

#logtitle=(title) ⇒ Object



927
928
929
# File 'lib/metabuild.rb', line 927

def logtitle=(title)
  @log.name = title
end

#output_graphObject

Output dependencies between targets in Graphviz dot format



985
986
987
988
989
990
991
# File 'lib/metabuild.rb', line 985

def output_graph
  puts "digraph Dependencies {"
  @targets.each {|t| puts "node [shape=ellipse];#{t.name};"}
  @targets.each {|t| t.output_graph}
  puts "}"
  exit
end

#parallel_valid(h) ⇒ Object

Run a shell command in subprocess, giving it (optional) env variables (a hashmap), and (optional) failure message Use parallel_wait on the returned object P . :call-seq:

parallel_valid(:cmd => "cmd", :env => env, :fail_msg => "msg", :success_msg => "msg")    -> P
parallel_valid("cmd")                                                                    -> P


1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
# File 'lib/metabuild.rb', line 1046

def parallel_valid(h)
  if h.kind_of? String
    p = @log.env_spawn(h, @env, "#{h} FAIL", "#{h} SUCCESS")
  else
    raise "valid command needs a non nil command" if h[:cmd].nil?
    p = @log.env_spawn(h[:cmd], @env.merge(h.fetch(:env, {})), h.fetch(:fail_msg, "#{h[:cmd]} FAIL"), h.fetch(:success_msg, "#{h[:cmd]} SUCCESS"))
  end
  @subprocess.push p
  return p
end

#parallel_wait(p) ⇒ Object

Wait for a subprocess, Log success and failure (like valid)



1058
1059
1060
1061
# File 'lib/metabuild.rb', line 1058

def parallel_wait(p)
  @log.parallel_wait(p)
  @subprocess.delete(p)
end

#parallel_waitallObject

Wait for a all subprocess, Log success and failure (like valid)



1064
1065
1066
1067
1068
1069
# File 'lib/metabuild.rb', line 1064

def parallel_waitall
  @subprocess.each do |p|
    @log.parallel_wait(p)
  end
  @subprocess = []
end

#remove_target(target) ⇒ Object



1080
1081
1082
# File 'lib/metabuild.rb', line 1080

def remove_target(target)
  @targets.delete target
end

#run(h) ⇒ Object

Run a shell command, giving it (optional) env variables (a hashmap), and (optional) failure message Log success and failure. Abort on failure. :call-seq:

run(:cmd => "cmd", :env => env, :msg => "msg", :skip => bool)  -> nil
run("cmd")                                      -> nil


1005
1006
1007
1008
1009
1010
1011
1012
# File 'lib/metabuild.rb', line 1005

def run(h)
  if h.kind_of? String
    @log.run(h, @env, "#{h} FAIL", false)
  else
    raise "run command needs a non nil command" if h[:cmd].nil?
    @log.run(h[:cmd], @env.merge(h.fetch(:env, @env)), h.fetch(:msg, "#{h[:cmd]} FAIL"), h.fetch(:skip, false))
  end
end

#script(script, args, fail_msg = "FAILED running #{script} #{args}", use_default = true) ⇒ Object

Run another ruby build script Be careful with global variables !!! By default, the default options values are transmitted to the subscript



996
997
998
# File 'lib/metabuild.rb', line 996

def script(script, args, fail_msg = "FAILED running #{script} #{args}", use_default = true)
  @log.script(script, args, @options, fail_msg, use_default)
end

#silent(h) ⇒ Object

Run a shell command, no logging. Give it (optional) env variables (hashmap) :call-seq:

silent(:cmd => "cmd", :env => env)  -> nil
silent("cmd")                       -> nil


1018
1019
1020
1021
1022
1023
1024
1025
# File 'lib/metabuild.rb', line 1018

def silent(h)
  if h.kind_of? String
    @log.silent(h, @env)
  else
    raise "silent command needs a non nil command" if h[:cmd].nil?
    @log.silent(h[:cmd], @env.merge(h.fetch(:env, {})))
  end
end

#target(target, &block) ⇒ Object

Set a block of code to be run as target, catching doing logging



1092
1093
1094
# File 'lib/metabuild.rb', line 1092

def target(target, &block)
  get_target(target).block = block
end

#valid(h) ⇒ Object

Run a shell command, giving it (optional) env variables (a hashmap), and (optional) failure message Log success and failure. Do not abort on failure. :call-seq:

valid(:cmd => "cmd", :env => env, :fail_msg => "msg", :success_msg => "msg", :skip => bool)    -> nil
valid("cmd")                                                                    -> nil


1032
1033
1034
1035
1036
1037
1038
1039
# File 'lib/metabuild.rb', line 1032

def valid(h)
  if h.kind_of? String
    @log.valid(h, @env, "#{h} FAIL", "#{h} SUCCESS", false)
  else
    raise "valid command needs a non nil command" if h[:cmd].nil?
    @log.valid(h[:cmd], @env.merge(h.fetch(:env, {})), h.fetch(:fail_msg, "#{h[:cmd]} FAIL"), h.fetch(:success_msg, "#{h[:cmd]} SUCCESS"), h.fetch(:skip, false))
  end
end