require_relative 'helper' module Dply class TaskDsl include Helper def initialize(app_config) @app_config = app_config end def run(name) @app_config.run_task name end def sh(command, env: {}, bundled_env: false) extra_env = base_env.merge env super(command, env: extra_env, bundled_env: bundled_env, display: :arrow) end def set_env(key, value) ENV[key.to_s] = value.to_s base_env[key.to_s] = value.to_s end def rake(task, *args) logger.arrow "rake #{task}" rake_app[task].invoke(*args) end def archive(name, **opts, &block) require_relative 'code_archive' revision = ENV["BUILD_NUMBER"] || "unknown" archive = CodeArchive.new(name, revision: revision) archive.build(**opts, &block) return archive end def role(*roles, &block) roles << :all if r = roles.find { |r| dply_roles.include? r } yield r end end def role?(*roles) roles << :all roles.find { |r| dply_roles.include? r } end def venv @venv ||= begin require_relative 'venv' Venv.new end end private def base_env @base_env ||= begin env = {} env.merge! env_from_yml(".env.yml") env.merge! env_from_yml("config/env.yml") env end end def env_from_yml(path) if not File.readable? path logger.debug "skipped loading env from #{path}" return {} end require 'yaml' YAML.load_file(path) end def rake_app @rake_app ||= begin require 'rake' app = Rake.application app.init silence_warnings { app.load_rakefile } app end end def dply_roles @dply_roles ||= (ENV["DPLY_ROLES"] || "").strip.split(",").map { |r| r.to_sym } end def silence_warnings(&block) warn_level = $VERBOSE $VERBOSE = nil yield ensure $VERBOSE = warn_level end end end