Class: Berkshelf::HgLocation

Inherits:
BaseLocation
  • Object
show all
Defined in:
lib/berkshelf/locations/hg.rb

Defined Under Namespace

Classes: HgCommandError, HgError, HgNotInstalled

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dependency, options = {}) ⇒ HgLocation

Returns a new instance of HgLocation.



34
35
36
37
38
39
40
41
42
43
# File 'lib/berkshelf/locations/hg.rb', line 34

def initialize(dependency, options = {})
  super

  @uri      = options[:hg]
  @branch   = options[:branch]
  @tag      = options[:tag]
  @ref      = options[:ref] || options[:branch] || options[:tag] || 'default'
  @revision = options[:revision]
  @rel      = options[:rel]
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



27
28
29
# File 'lib/berkshelf/locations/hg.rb', line 27

def branch
  @branch
end

#refObject (readonly)

Returns the value of attribute ref.



29
30
31
# File 'lib/berkshelf/locations/hg.rb', line 29

def ref
  @ref
end

#relObject (readonly)

Returns the value of attribute rel.



31
32
33
# File 'lib/berkshelf/locations/hg.rb', line 31

def rel
  @rel
end

#revisionObject (readonly)

Returns the value of attribute revision.



32
33
34
# File 'lib/berkshelf/locations/hg.rb', line 32

def revision
  @revision
end

#tagObject (readonly)

Returns the value of attribute tag.



28
29
30
# File 'lib/berkshelf/locations/hg.rb', line 28

def tag
  @tag
end

#uriObject (readonly)

Returns the value of attribute uri.



26
27
28
# File 'lib/berkshelf/locations/hg.rb', line 26

def uri
  @uri
end

Instance Method Details

#==(other) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/berkshelf/locations/hg.rb', line 111

def ==(other)
  other.is_a?(HgLocation) &&
  other.uri == uri &&
  other.branch == branch &&
  other.tag == tag &&
  other.ref == ref &&
  other.rel == rel
end

#cached_cookbookObject



45
46
47
48
49
50
51
# File 'lib/berkshelf/locations/hg.rb', line 45

def cached_cookbook
  if installed?
    @cached_cookbook ||= CachedCookbook.from_path(install_path)
  else
    nil
  end
end

#installObject

Download the cookbook from the remote hg repository

Returns:

  • void



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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/berkshelf/locations/hg.rb', line 56

def install
  if cached?
    # Update and checkout the correct ref
    Dir.chdir(cache_path) do
      hg %|pull|
    end
  else
    # Ensure the cache directory is present before doing anything
    FileUtils.mkdir_p(cache_path)

    Dir.chdir(cache_path) do
      hg %|clone #{uri} .|
    end
  end

  Dir.chdir(cache_path) do
    hg %|update --clean --rev #{revision || ref}|
    @revision ||= hg %|id -i|
  end

  # Gab the path where we should copy from (since it might be relative to
  # the root).
  copy_path = rel ? cache_path.join(rel) : cache_path

  begin 
    # Validate the thing we are copying is a Chef cookbook
    validate_cached!(copy_path)

    # Remove the current cookbook at this location (this is required or else
    # FileUtils will copy into a subdirectory in the next step)
    FileUtils.rm_rf(install_path)

    # Create the containing parent directory
    FileUtils.mkdir_p(install_path.parent)

    # Copy whatever is in the current cache over to the store
    FileUtils.cp_r(copy_path, install_path)

  ensure

    # Remove the .hg directory to save storage space
    # TODO this can have huge performance implications, 
    # make it a config option?
    if (hg_path = install_path.join('.hg')).exist?
      FileUtils.rm_r(hg_path)
    end

   FileUtils.rm_rf (copy_path)
  end
end

#installed?Boolean

Determine if this revision is installed.

Returns:

  • (Boolean)


142
143
144
# File 'lib/berkshelf/locations/hg.rb', line 142

def installed?
  revision && install_path.exist?
end

#scm_location?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/berkshelf/locations/hg.rb', line 107

def scm_location?
  true
end

#to_lockObject



130
131
132
133
134
135
136
137
# File 'lib/berkshelf/locations/hg.rb', line 130

def to_lock
  out =  "    hg: #{uri}\n"
  out << "    revision: #{revision}\n"
  out << "    branch: #{branch}\n" if branch
  out << "    tag: #{tag}\n"       if tag
  out << "    rel: #{rel}\n"       if rel
  out
end

#to_sObject



120
121
122
123
124
125
126
127
128
# File 'lib/berkshelf/locations/hg.rb', line 120

def to_s
  info = tag || branch || ref[0...7]

  if rel
    "#{uri} (at #{info}/#{rel})"
  else
    "#{uri} (at #{info})"
  end
end