class Concourse

def create_tasks!

def create_tasks!
  unless Dir.exist? directory
    mkdir_p directory
  end
  pipelines.each do |pipeline|
    CLOBBER.include pipeline.filename if defined?(CLOBBER)
    unless File.exist? pipeline.erb_filename
      warn "WARNING: concourse template #{pipeline.erb_filename.inspect} does not exist, run `rake concourse:init`"
    end
  end
  namespace :concourse do
    #
    #  project commands
    #
    desc "bootstrap a concourse config"
    task :init do
      rake_init
    end
    #
    #  pipeline commands
    #
    desc "generate and validate all pipeline files"
    task "generate" => pipeline_subcommands("generate")
    pipelines.each do |pipeline|
      desc "generate and validate the #{pipeline.name} pipeline file"
      task "generate:#{pipeline.name}" do
        File.open pipeline.filename, "w" do |f|
          f.write erbify(File.read(pipeline.erb_filename))
        end
        sh "fly validate-pipeline -c #{pipeline.filename}"
      end
    end
    desc "upload all pipeline files"
    task "set" => pipeline_subcommands("set")
    pipelines.each do |pipeline|
      desc "upload the #{pipeline.name} pipeline file"
      task "set:#{pipeline.name}" => "generate:#{pipeline.name}" do
        options = [
          "-p '#{pipeline.name}'",
          "-c '#{pipeline.filename}'",
        ]
        if File.exist? secrets_filename
          note "using #{secrets_filename} to resolve template vars in #{pipeline.filename}"
          options << "-l '#{secrets_filename}'"
        end
        sh "fly -t #{fly_target} set-pipeline #{options.join(" ")}"
      end
    end
    %w[expose hide pause unpause destroy].each do |command|
      desc "#{command} all pipelines"
      task command => pipeline_subcommands(command)
      pipelines.each do |pipeline|
        desc "#{command} the #{pipeline.name} pipeline"
        task "#{command}:#{pipeline.name}" do
          sh "fly -t #{fly_target} #{command}-pipeline -p #{pipeline.name}"
        end
      end
    end
    desc "remove generated pipeline files"
    task "clean" do
      pipelines.each do |pipeline|
        rm_f pipeline.filename
      end
    end
    #
    #  task commands
    #
    desc "list all the available tasks from the #{project_name} pipelines"
    task "tasks" => "generate" do
      tasks = []
      pipelines.each do |pipeline|
        each_task(pipeline) do |job, task|
          tasks << "#{job["name"]}/#{task["task"]}"
        end
      end
      note "Available Concourse tasks for #{project_name} are:"
      tasks.sort.each { |task| puts " * #{task}" }
    end
    desc "fly execute the specified task"
    task "task", [:job_task, :fly_execute_args] => "generate" do |t, args|
      job_task = args[:job_task]
      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
      fly_execute_args = args[:fly_execute_args] || Concourse.default_execute_args(concourse_task)
      puts concourse_task.to_yaml
      Tempfile.create("concourse-task") 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}"
        end
      end
    end
    #
    #  builds commands
    #
    desc "abort all running builds for this concourse team"
    task "abort-builds" do |t, args|
      `fly -t #{fly_target} builds`.each_line do |line|
        pipeline_job, build_id, status = *line.split(/\s+/)[1,3]
        next unless status == "started"
        sh "fly -t #{fly_target} abort-build -j #{pipeline_job} -b #{build_id}"
      end
    end
    #
    #  worker commands
    #
    desc "prune any stalled workers"
    task "prune-stalled-workers" do
      `fly -t #{fly_target} workers | fgrep stalled`.each_line do |line|
        worker_id = line.split.first
        system("fly -t #{fly_target} prune-worker -w #{worker_id}")
      end
    end
  end
end