Sha256: 40bee4e90aa4ca35d895c49dce73728b98f595ab701e8a992cab573fcc45b958

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

require 'pathname'

require 'mixlib/config'

require 'vendorificator/repo'

module Vendorificator
  class Config
    extend Mixlib::Config

    configure do |c|
      c[:basedir] = 'vendor'
      c[:branch_prefix] = 'vendor'
      c[:modules] = []
      c[:remotes] = %w(origin)
    end

    def self.from_file(filename)
      pathname = Pathname.new(filename).cleanpath.expand_path
      self[:root_dir] =
        if ( pathname.basename.to_s == 'vendor.rb' &&
               pathname.dirname.basename.to_s == 'config' )
          # Correctly recognize root dir if main config is 'config/vendor.rb'
          pathname.dirname.dirname
        else
          pathname.dirname
        end
      self[:vendorfile_path] = pathname
      super(pathname.to_s)
    end

    def self.repo
      @repo ||= begin
                  git_root_path = self[:repo_dir] || _find_git_root
                  raise "Can't find Git repository" unless git_root_path
                  Vendorificator::Repo.new( git_root_path.to_s )
                end
    end

    def self.each_module(*modules)
      module_paths = modules.map { |m| File.expand_path(m) }

      # We don't use self[:modules].each here, because mod.run! is
      # explicitly allowed to append to Config[:modules], and #each
      # fails to catch up on some Ruby implementations.
      i = 0
      while true
        break if i >= Vendorificator::Config[:modules].length
        mod = Vendorificator::Config[:modules][i]
        yield mod if
          modules.empty? ||
          modules.include?(mod.name) ||
          module_paths.include?(mod.work_dir)
        i += 1

        # Add dependencies
        work_dirs = Vendorificator::Config[:modules].map(&:work_dir)
        Vendorificator::Config[:modules] +=
          mod.dependencies.reject { |dep| work_dirs.include?(dep.work_dir) }
      end
    end

    def self._find_git_root
      self[:root_dir].ascend do |dir|
        return dir if dir.join('.git').exist?
      end
    end
    private_class_method :_find_git_root
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vendorificator-0.1.1 lib/vendorificator/config.rb
vendorificator-0.1.0 lib/vendorificator/config.rb