require 'yaml' module Docman module Builders class Builder @@subclasses = {} def self.create(type, root, build_type, state, info) c = @@subclasses[type] if c c.new(root, build_type, state, info) else raise "Bad builder type: #{type}" end end def self.register_builder(name) @@subclasses[name] = self end def initialize(root, build_type, state, info) @root = root @build_type = build_type @state = state @info = info @before_build_actions = @build_type['before_build_actions'].nil? ? [] : @build_type['before_build_actions'] @after_build_actions = @build_type['after_build_actions'].nil? ? [] : @build_type['after_build_actions'] @before_build_actions << 'clean_if_changed' end def before_build_action_clean_if_changed if File.directory? @info['full_build_path'] FileUtils.rm_r @info['full_build_path'] if @info.need_rebuild? end end def do perform(@before_build_actions, 'before_build_action') # Dispatch to corresponding method. write_info self.send("#{@build_type['type']}") perform(@after_build_actions, 'after_build_action') end def write_info(result) to_save = {} # to_save = @info.clone to_save['state'] = @state to_save['version_type'] = @info.version_type(@state) unless @info.version_type(@state).nil? to_save['version'] = @info.version(@state) unless @info.version(@state).nil? to_save['ref'] = result to_save['build_type'] = @build_type['type'] # to_save.delete('full_build_path') # to_save.delete('full_path') # to_save.delete('temp_path') # to_save.delete('repo') # to_save.delete('states') File.open("#{@info['full_build_path']}/info.yaml", 'w') {|f| f.write to_save.to_yaml} end def perform(actions, method_prefix) unless actions.nil? actions.each do |action| method = "#{method_prefix}_#{action}" self.send(method) end end end def repo?(path) File.directory? File.join(path, '.git') end def after_build_action_git_commit message = "name: #{@info['name']} updated, state: #{@state}" GitUtil.commit(@root['full_build_path'], @info['full_build_path'], message) end end end end