module PowerStencil::Engine::BuildHandling

Attributes

last_build_report[R]

Public Instance Methods

build(entities_to_build, fail_on_error: false, parallelized: false) click to toggle source
# File lib/power_stencil/engine/build_handling.rb, line 10
def build(entities_to_build, fail_on_error: false, parallelized: false)
  logger.debug 'Starting build process...'
  raise PowerStencil::Error, 'Parallel builds are not yet supported !' if parallelized

  build_target_seed =  entities_to_build.map(&:as_path).map do |entity_full_name|
    entity_full_name.tr '/', '_'
  end .join '_'
  build_target_path = project.build_run_path build_target_seed

  @last_build_report = {}

  entities_to_build.each do |entity_to_build|
    begin
      entity_build_report = []
      unless entity_to_build.buildable?
        msg = "Entity '#{entity_to_build.as_path}' is not buildable !"
        entity_build_report << msg
        raise PowerStencil::Error, msg
      end
      build_entity_target_path = if config[:'target-path']
                                   specified_target_path = File.expand_path config[:'target-path']
                                   if File.exists? specified_target_path
                                     raise PowerStencil::Error, "Specified target path '#{specified_target_path}' already exists ! Aborting..."
                                   end
                                   specified_target_path
                                 else
                                   File.join build_target_path, entity_to_build.as_path.tr('/', '_')
                                 end

      build_entity entity_to_build, build_entity_target_path

      entity_build_report << 'Ok'
    rescue SyntaxError, StandardError => e
      entity_build_report << e.message
      if fail_on_error
        raise e
      else
        logger.debug PowerStencil::Error.report_error e
      end
    ensure
      last_build_report[entity_to_build.as_path] = entity_build_report
    end
  end
end
generate_build_files(entity_to_build, target_path) click to toggle source
# File lib/power_stencil/engine/build_handling.rb, line 55
def generate_build_files(entity_to_build, target_path)
  logger.info "Rendering '#{entity_to_build.as_path}' templates in '#{target_path}'"
  render_source entity_to_build.templates_path, target_path, main_entry_point: entity_to_build.as_path
  logger.info "Detemplatized files generated in '#{target_path}'"
  manage_build_links target_path
end
post_build_hook(entity_to_build, files_path) click to toggle source
# File lib/power_stencil/engine/build_handling.rb, line 62
def post_build_hook(entity_to_build, files_path)
  case entity_to_build.type
  when :simple_exec
    script_to_execute = File.expand_path File.join(files_path, entity_to_build.post_process.process)
    exec_options = {message: "Running '#{script_to_execute}'"}
    exec_options[:show_output] = true if config[:verbose]
    process_report = safely_exec_command "'#{script_to_execute}'", exec_options
    unless process_report.exit_status.exitstatus == 0
      raise PowerStencil::Error, "Process `#{process_report.command}` failed and returned exit code '#{process_report.exit_status.exitstatus}'"
    end
    process_report
  end
end

Private Instance Methods

build_entity(entity_to_build, target_path) click to toggle source
# File lib/power_stencil/engine/build_handling.rb, line 119
def build_entity(entity_to_build, target_path)
  initial_directory = Dir.pwd
  target_plugin_name =  entity_to_build.buildable_by.to_s

  # Files generation
  if target_plugin_name.empty?
    generate_build_files entity_to_build, target_path
  else
    target_plugin = project.plugins[target_plugin_name]
    raise PowerStencil::Error, "Could not find plugin '#{target_plugin_name}' !" if target_plugin.nil?
    if target_plugin.capabilities[:generate_build_files]
      target_plugin.generate_build_files entity_to_build, target_path
    else
      logger.info 'Falling back to PowerStencil core build files generation process...'
      generate_build_files entity_to_build, target_path
    end
  end

  # Post processing executed from generated directory (#11)
  Dir.chdir target_path

  # Post generation action
  if target_plugin_name.empty?
    post_build_hook entity_to_build, target_path
  else
    target_plugin = project.plugins[target_plugin_name]
    raise PowerStencil::Error, "Could not find plugin '#{target_plugin_name}' !" if target_plugin.nil?
    raise PowerStencil::Error, "Plugin '#{target_plugin_name}' has no post build capability !" unless target_plugin.capabilities[:post_build]
    target_plugin.post_build_hook entity_to_build, target_path
  end

ensure
  Dir.chdir initial_directory
end