Class: Vendorificator::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/vendorificator/environment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vendorfile = nil) ⇒ Environment

Returns a new instance of Environment.



12
13
14
15
16
17
# File 'lib/vendorificator/environment.rb', line 12

def initialize(vendorfile=nil)
  @config = Vendorificator::Config
  config.environment = self
  config.from_file(self.class.find_vendorfile(vendorfile))
  Vendorificator::Vendor.compute_dependencies!
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/vendorificator/environment.rb', line 9

def config
  @config
end

#shellObject

Returns the value of attribute shell.



10
11
12
# File 'lib/vendorificator/environment.rb', line 10

def shell
  @shell
end

Class Method Details

.find_vendorfile(given = nil) ⇒ Object

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/vendorificator/environment.rb', line 88

def self.find_vendorfile(given=nil)
  given = [ given, ENV['VENDORFILE'] ].find do |candidate|
    candidate && !(candidate.respond_to?(:empty?) && candidate.empty?)
  end
  return given if given

  Pathname.pwd.ascend do |dir|
    vf = dir.join('Vendorfile')
    return vf if vf.exist?

    vf = dir.join('config/vendor.rb')
    return vf if vf.exist?

    # avoid stepping above the tmp directory when testing
    if ENV['VENDORIFICATOR_SPEC_RUN'] &&
        dir.join('vendorificator.gemspec').exist?
      raise ArgumentError, "Vendorfile not found"
    end
  end

  raise ArgumentError, "Vendorfile not found"
end

Instance Method Details

#clean?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
# File 'lib/vendorificator/environment.rb', line 41

def clean?
  # copy code from http://stackoverflow.com/a/3879077/16390
  git.update_index :q => true, :ignore_submodules => true, :refresh => true
  git.diff_files '--quiet', '--ignore-submodules', '--'
  git.diff_index '--cached', '--quiet', 'HEAD', '--ignore-submodules', '--'
  true
rescue MiniGit::GitError
  false
end

#current_branchObject



33
34
35
# File 'lib/vendorificator/environment.rb', line 33

def current_branch
  git.capturing.rev_parse({:abbrev_ref => true}, 'HEAD').strip
end

#fast_forwardable?(to, from) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/vendorificator/environment.rb', line 37

def fast_forwardable?(to, from)
  git.capturing.merge_base(to, from).strip == from
end

#gitObject

Main MiniGit instance



24
25
26
# File 'lib/vendorificator/environment.rb', line 24

def git
  @git ||= MiniGit::new(config[:vendorfile_path])
end

#pull(remote, options = {}) ⇒ Object

Raises:

  • (RuntimeError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vendorificator/environment.rb', line 51

def pull(remote, options={})
  raise RuntimeError, "Unknown remote #{remote}" unless remotes.include?(remote)

  git.fetch(remote)
  git.fetch({:tags => true}, remote)

  ref_rx = /^refs\/remotes\/#{Regexp.quote(remote)}\//
  remote_branches = Hash[ git.capturing.show_ref.
    lines.
    map(&:split).
    map { |sha, name| name =~ ref_rx ? [$', sha] : nil }.
    compact ]

  Vendorificator::Vendor.each do |mod|
    ours = mod.head
    theirs = remote_branches[mod.branch_name]
    if theirs
      if not ours
        say_status 'new', mod.branch_name, :yellow
        git.branch({:track=>true}, mod.branch_name, remote_head.name) unless options[:dry_run]
      elsif ours == theirs
        say_status 'unchanged', mod.branch_name
      elsif fast_forwardable?(theirs, ours)
        say_status 'updated', mod.name, :yellow
        mod.in_branch { git.merge({:ff_only => true}, theirs) } unless options[:dry_run]
      elsif fast_forwardable?(ours, theirs)
        say_status 'older', mod.branch_name
      else
        say_status 'complicated', mod.branch_name, :red
      end
    else
      say_status 'unknown', mod.branch_name
    end
  end

end

#remotesObject

Git helpers



29
30
31
# File 'lib/vendorificator/environment.rb', line 29

def remotes
  @remotes ||= git.capturing.remote.lines.map(&:strip)
end

#say_status(*args) ⇒ Object



19
20
21
# File 'lib/vendorificator/environment.rb', line 19

def say_status(*args)
  shell.say_status(*args) if shell
end