require 'ctpl/compiler'

module Ctpl
  class Generate < Thor
    include Thor::Actions
    class_option :verbose, :type => :boolean, :aliases => '-v', :default => false, :banner => 'Activate verbose mode', :desc => 'If used show errors, stacktraces and more'

    desc 'compile', 'Compile the pipeline.yaml file from the templates'
    method_option :basefolder, :aliases => 'b', :required => false, :type => :string, :default => './pipeline', :desc => "Optional - defaults to ./pipeline, falls back to PWD. The base-folder the pipeline-template.yaml and aliases.yaml are located. \nIf you pass /tmp/foo we will look for /tmp/foo/pipeline.yaml and optionally for /tmp/foo/aliases.yaml files"
    method_option :partials, :aliases => 'p', :required => false, :type => :string, :default => './pipeline', :desc => "Optional - defaults to ./pipeline, falls back to PWD. The base-folder the partials are located. \nIf you pass /tmp/foo we will look for /tmp/foo/*_.yaml files as partials"
    method_option :output, :aliases => 'o', :required => false, :type => :string, :default => './pipeline.yaml', :desc => "Optional, defaults to pipeline.yaml. Output path to save the merged yaml in"
    def compile
      baseFolder = options[:basefolder]
      partialsFolder = options[:partials]
      merger = PipelineCompiler.new("#{baseFolder}/pipeline-template.yaml", "#{baseFolder}/aliases.yaml", partialsFolder, options[:verbose])
      p = merger.merge
      say_status 'ok', "Transforming back to yaml ", :white
      yaml = "# DO NOT CHANGE THIS FILE\n# Generated by ctp (https://github.com/EugenMayer/concourse-pipeline-templateer)\n# See '#{baseFolder}' for the source files\n" + p.to_yaml
      say_status 'ok', "Saving to #{options[:output]} ", :white
      File.write(options[:output], yaml)
    end
  end
end

