Sha256: 072442c0374f6b054032d6b87388745dd5fa1ae9a14b524b386ec62f8290d8eb

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

require 'rugged'

module Fulmar
  module Domain
    module Service
      # Manages dependencies to subrepositories
      class DependencyService
        def initialize(config)
          @config = config
        end

        def setup(env = @config.environment)
          shell = Fulmar::Infrastructure::Service::ShellService.new(@config[:local_path])
          @config.dependencies(env).each_pair do |_key, data|
            next unless data[:type].blank? || data[:type] == 'git'
            shell.run "git clone #{data[:source]} #{data[:path]} -q"
          end
        end

        def update(env = @config.environment)
          @config.dependencies(env).each_pair do |_key, data|
            next unless data[:type].blank? || data[:type] == 'git'
            git = Rugged::Repository.new(data[:path])

            # Switch to the configured branch/tag/commit
            if git.branches.select { |b| b.name.split('/').last == data[:ref] }.any?
              checkout_branch(git, data[:ref])
            elsif git.tags.map(&:name).include?(data[:ref])
              git.checkout('refs/tags/'+data[:ref])
            elsif data[:ref].match(/^[a-zA-Z0-9]{40}$/) && git.exists?(data[:ref])
              git.checkout(data[:ref])
            else
              fail "Cannot find ref #{data[:ref]} in repo #{data[:path]}"
            end

            # Pull
            shell = Fulmar::Infrastructure::Service::ShellService.new @config[:local_path]+'/'+data[:path]
            unless shell.run 'git pull --rebase -q'
              fail "Cannot update repository #{data[:path]}. Please update manually."
            end
          end
        end

        protected

        def checkout_branch(git, branch)
          if git.branches.collect(&:name).include? branch
            git.checkout(branch)
          else
            new_branch = git.branches.create(branch.split('/').last, branch)
            git.checkout(new_branch)
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fulmar-1.5.2 lib/fulmar/domain/service/dependency_service.rb
fulmar-1.5.1 lib/fulmar/domain/service/dependency_service.rb
fulmar-1.5.0 lib/fulmar/domain/service/dependency_service.rb
fulmar-1.4.2 lib/fulmar/domain/service/dependency_service.rb