Sha256: 9f69be12e6853746d92423eb07b2d793bdd7145570d0f2aff99ce9a76639f430

Contents?: true

Size: 1.67 KB

Versions: 20

Compression:

Stored size: 1.67 KB

Contents

require 'pathname'
require 'fileutils'

class StackCommands < Commands
  def initialize(stack)
    @stack = stack
  end

  def fetch
    create_directories
    if Dir.exist?(@stack.git_path)
      git('fetch', 'origin', '--tags', @stack.branch, env: env, chdir: @stack.git_path)
    else
      git_clone(@stack.repo_git_url, @stack.git_path, branch: @stack.branch, env: env, chdir: @stack.deploys_path)
    end
  end

  def fetch_deployed_revision
    with_temporary_working_directory do |dir|
      spec = DeploySpec::FileSystem.new(dir, @stack.environment)
      outputs = spec.fetch_deployed_revision_steps!.map do |command_line|
        Command.new(command_line, env: env, chdir: dir).run
      end
      outputs.find(&:present?).try(:strip)
    end
  end

  def build_cacheable_deploy_spec
    with_temporary_working_directory do |dir|
      DeploySpec::FileSystem.new(dir, @stack.environment).cacheable
    end
  end

  def with_temporary_working_directory(commit: nil)
    @stack.acquire_git_cache_lock do
      fetch.run!
      git('checkout', '--force', "origin/#{@stack.branch}", env: env, chdir: @stack.git_path).run!
    end
    Dir.mktmpdir do |dir|
      git('clone', @stack.git_path, @stack.repo_name, chdir: dir).run!
      git('checkout', commit.sha, chdir: dir) if commit
      yield Pathname.new(File.join(dir, @stack.repo_name))
    end
  end

  def git_clone(url, path, branch: 'master', **kwargs)
    git('clone', *modern_git_args, '--recursive', '--branch', branch, url, path, **kwargs)
  end

  def modern_git_args
    return [] unless git_version >= Gem::Version.new('1.7.10')
    %w(--single-branch)
  end

  def create_directories
    FileUtils.mkdir_p(@stack.deploys_path)
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
shipit-engine-0.5.2 lib/stack_commands.rb
shipit-engine-0.5.1 lib/stack_commands.rb
shipit-engine-0.5.0 lib/stack_commands.rb
shipit-engine-0.4.10 lib/stack_commands.rb
shipit-engine-0.4.9 lib/stack_commands.rb
shipit-engine-0.4.8 lib/stack_commands.rb
shipit-engine-0.4.7 lib/stack_commands.rb
shipit-engine-0.4.6 lib/stack_commands.rb
shipit-engine-0.4.5 lib/stack_commands.rb
shipit-engine-0.4.4 lib/stack_commands.rb
shipit-engine-0.4.3 lib/stack_commands.rb
shipit-engine-0.4.2 lib/stack_commands.rb
shipit-engine-0.4.1 lib/stack_commands.rb
shipit-engine-0.4.0 lib/stack_commands.rb
shipit-engine-0.3.1 lib/stack_commands.rb
shipit-engine-0.3.0 lib/stack_commands.rb
shipit-engine-0.2.3 lib/stack_commands.rb
shipit-engine-0.2.2 lib/stack_commands.rb
shipit-engine-0.2.1 lib/stack_commands.rb
shipit-engine-0.2.0 lib/stack_commands.rb