class Concourse

def create_tasks!

def create_tasks!
  unless Dir.exist? DIRECTORY
    mkdir_p DIRECTORY
  end
  unless File.exist? pipeline_erb_filename
    raise "ERROR: concourse pipeline template #{pipeline_erb_filename.inspect} does not exist"
  end
  CLOBBER.include pipeline_filename if defined?(CLOBBER)
  namespace :concourse do
    #
    #  pipeline commands
    #
    desc "generate and validate the pipeline file for #{project_name}"
    task "generate" do |t|
      File.open pipeline_filename, "w" do |f|
        f.write erbify(File.read(pipeline_erb_filename))
      end
      sh "fly validate-pipeline -c #{pipeline_filename}"
    end
    desc "upload the pipeline file for #{project_name}"
    task "set", [:fly_target] => ["generate"] do |t, args|
      fly_target = Concourse.validate_fly_target t, args
      options = [
        "-p '#{project_name}'",
        "-c '#{pipeline_filename}'",
      ]
      if File.exist? PRIVATE_VAR_FILE
        puts "using #{PRIVATE_VAR_FILE} to resolve template vars"
        options << "-l '#{PRIVATE_VAR_FILE}'"
      end
      sh "fly -t #{fly_target} set-pipeline #{options.join(" ")}"
    end
    %w[expose hide pause unpause].each do |command|
      desc "#{command} the #{project_name} pipeline"
      task "#{command}", [:fly_target] do |t, args|
        fly_target = Concourse.validate_fly_target t, args
        sh "fly -t #{fly_target} #{command}-pipeline -p #{project_name}"
      end
    end
    desc "remove generate pipeline file"
    task "clean" do |t|
      rm_f pipeline_filename
    end
    #
    #  task commands
    #
    desc "list all the available tasks from the #{project_name} pipeline"
    task "tasks" => "generate" do
      tasks = []
      each_task do |job, task|
        tasks << "#{job["name"]}/#{task["task"]}"
      end
      puts "Available Concourse tasks for #{project_name} are:"
      tasks.sort.each { |task| puts " * #{task}" }
    end
    desc "fly execute the specified task"
    task "task", [:fly_target, :job_task, :fly_execute_args] => "generate" do |t, args|
      fly_target = Concourse.validate_fly_target t, args
      job_task = args[:job_task]
      fly_execute_args = args[:fly_execute_args] || "--input=#{project_name}=."
      unless job_task
        raise "ERROR: must specify a task name, like `rake #{t.name}[target,taskname]`"
      end
      concourse_task = find_task job_task
      raise "ERROR: could not find task `#{job_task}`" unless concourse_task
      puts concourse_task.to_yaml
      Tempfile.create do |f|
        f.write concourse_task["config"].to_yaml
        f.close
        Bundler.with_clean_env do
          sh "fly -t #{fly_target} execute #{fly_execute_args} -c #{f.path} -x"
        end
      end
    end
    #
    #  badge commands
    #
    desc "display a list of jobs and badge urls"
    task "badges", [:fly_target, :team_name] => "generate" do |t, args|
      fly_target = Concourse.validate_fly_target t, args
      team_name = args[:team_name] || "main"
      puts ""
      puts "| Build | Status |"
      puts "|--|--|"
      each_job do |job|
        job_name = job["name"]
        puts "| #{titleize job_name} | #{markdown_badge fly_target, team_name, job_name} |"
      end
    end
  end
end