Class: Vendorificator::Vendor

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

Direct Known Subclasses

Archive, Download, Git

Defined Under Namespace

Classes: Archive, ChefCookbook, Download, Git

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, name, args = {}, &block) ⇒ Vendor

Returns a new instance of Vendor.



74
75
76
77
78
79
80
81
82
83
# File 'lib/vendorificator/vendor.rb', line 74

def initialize(environment, name, args={}, &block)
  @environment = environment
  @category = args.delete(:category) if args.key?(:category)

  @name = name
  @args = args
  @block = block

  self.class.instances << self
end

Class Attribute Details

.categoryObject

Returns the value of attribute category.



11
12
13
# File 'lib/vendorificator/vendor.rb', line 11

def category
  @category
end

.method_nameObject

Returns the value of attribute method_name.



11
12
13
# File 'lib/vendorificator/vendor.rb', line 11

def method_name
  @method_name
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



71
72
73
# File 'lib/vendorificator/vendor.rb', line 71

def args
  @args
end

#blockObject (readonly)

Returns the value of attribute block.



71
72
73
# File 'lib/vendorificator/vendor.rb', line 71

def block
  @block
end

#environmentObject (readonly)

Returns the value of attribute environment.



71
72
73
# File 'lib/vendorificator/vendor.rb', line 71

def environment
  @environment
end

#nameObject (readonly)

Returns the value of attribute name.



71
72
73
# File 'lib/vendorificator/vendor.rb', line 71

def name
  @name
end

Class Method Details

.[](*key) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vendorificator/vendor.rb', line 32

def [](*key)
  return key.map { |k| self[k] }.flatten if key.length > 1

  key = key.first

  if key.is_a?(Fixnum)
    self.instances[key]
  else
    instances.select { |i| i === key }
  end
end

.arg_reader(*names) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/vendorificator/vendor.rb', line 24

def arg_reader(*names)
  names.each do |name|
    define_method(name) do
      args[name]
    end
  end
end

.compute_dependencies!Object



66
67
68
# File 'lib/vendorificator/vendor.rb', line 66

def compute_dependencies!
  self.instances.each(&:compute_dependencies!)
end

.each(*modules) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vendorificator/vendor.rb', line 44

def each(*modules)
  modpaths = modules.map { |m| File.expand_path(m) }

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

.install!Object

Define a method on Vendorificator::Config to add the vendor module to the module definition list.



15
16
17
18
19
20
21
22
# File 'lib/vendorificator/vendor.rb', line 15

def install!
  @method_name ||= self.name.split('::').last.downcase.to_sym
  _cls = self # for self is obscured in define_method block's body
  ( class << Vendorificator::Config ; self ; end ).
      send(:define_method, @method_name ) do |name, *args, &block|
    _cls.new(self.environment, name.to_s, *args, &block)
  end
end

.instancesObject



62
63
64
# File 'lib/vendorificator/vendor.rb', line 62

def instances
  Vendorificator::Vendor.instance_eval { @instances ||= [] }
end

Instance Method Details

#===(other) ⇒ Object



85
86
87
# File 'lib/vendorificator/vendor.rb', line 85

def ===(other)
  other === self.name or File.expand_path(other.to_s) == self.work_dir
end

#branch_nameObject



106
107
108
# File 'lib/vendorificator/vendor.rb', line 106

def branch_name
  _join(environment.config[:branch_prefix], category, name)
end

#categoryObject



98
99
100
101
102
103
104
# File 'lib/vendorificator/vendor.rb', line 98

def category
  if instance_variable_defined?(:@category)
    @category
  else
    self.class.category
  end
end

#compute_dependencies!Object



299
# File 'lib/vendorificator/vendor.rb', line 299

def compute_dependencies! ; end

#conjure!Object



295
296
297
# File 'lib/vendorificator/vendor.rb', line 295

def conjure!
  block.call(self) if block
end

#conjure_commit_messageObject



287
288
289
# File 'lib/vendorificator/vendor.rb', line 287

def conjure_commit_message
  "Conjured vendor module #{name} version #{version}"
end

#headObject



126
127
128
129
130
# File 'lib/vendorificator/vendor.rb', line 126

def head
  environment.git.capturing.rev_parse({:verify => true}, "refs/heads/#{branch_name}").strip
rescue MiniGit::GitError
  nil
end

#in_branch(options = {}, &block) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/vendorificator/vendor.rb', line 206

def in_branch(options={}, &block)
  orig_branch = environment.current_branch

  # We want to be in repository's root now, as we may need to
  # remove stuff and don't want to have removed directory as cwd.
  Dir::chdir environment.git.git_work_tree do
    # If our branch exists, check it out; otherwise, create a new
    # orphaned branch.
    if self.head
      environment.git.checkout branch_name
      environment.git.rm( { :r => true, :f => true, :q => true, :ignore_unmatch => true }, '.') if options[:clean]
    else
      environment.git.checkout( { :orphan => true }, branch_name )
      environment.git.rm( { :r => true, :f => true, :q => true, :ignore_unmatch => true }, '.')
    end
  end

  yield
ensure
  # We should try to ensure we're back on original branch
  environment.git.checkout orig_branch if defined?(orig_branch) rescue nil
end

#inspectObject



114
115
116
# File 'lib/vendorificator/vendor.rb', line 114

def inspect
  "#<#{self.class} #{self}>"
end

#mergedObject



138
139
140
141
142
143
144
145
146
147
# File 'lib/vendorificator/vendor.rb', line 138

def merged
  unless @_has_merged
    if ( head = self.head )
      merged = environment.git.capturing.merge_base(head, 'HEAD').strip
      @merged = merged unless merged.empty?
    end
    @_has_merged = true
  end
  @merged
end

#merged_tagObject



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/vendorificator/vendor.rb', line 149

def merged_tag
  unless @_has_merged_tag
    if merged
      tag = environment.git.capturing.describe( {
          :exact_match => true,
          :match => _join(tag_name_base, '*') },
        merged).strip
      @merged_tag = tag unless tag.empty?
    end
    @_has_merged_tag = true
  end
  @merged_tag
end

#merged_versionObject



163
164
165
# File 'lib/vendorificator/vendor.rb', line 163

def merged_version
  merged_tag && merged_tag[(1+tag_name_base.length)..-1]
end

#needed?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/vendorificator/vendor.rb', line 202

def needed?
  return self.status != :up_to_date
end

#pathObject



89
90
91
# File 'lib/vendorificator/vendor.rb', line 89

def path
  args[:path] || _join(category, name)
end

#run!Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/vendorificator/vendor.rb', line 229

def run!
  case status

  when :up_to_date
    shell.say_status 'up to date', self.to_s

  when :unpulled, :unmerged
    shell.say_status 'merging', self.to_s, :yellow
    environment.git.merge({:no_edit => true, :no_ff => true}, tagged_sha1)
    compute_dependencies!

  when :outdated, :new
    shell.say_status 'fetching', self.to_s, :yellow
    begin
      shell.padding += 1
      in_branch(:clean => true) do
        FileUtils::mkdir_p work_dir

        # Actually fill the directory with the wanted content
        Dir::chdir work_dir do
          begin
            shell.padding += 1
            self.conjure!
          ensure
            shell.padding -= 1
          end

          subdir = args[:subdirectory]
          make_subdir_root subdir if subdir && !subdir.empty?
        end


        # Commit and tag the conjured module
        environment.git.add work_dir
        environment.git.commit :m => conjure_commit_message
        environment.git.tag( { :a => true, :m => tag_message }, tag_name )
        shell.say_status :tag, tag_name
      end
      # Merge back to the original branch
      environment.git.merge( {}, branch_name )
      compute_dependencies!
    ensure
      shell.padding -= 1
    end

  else
    say_status self.status, "I'm unsure what to do.", :red
  end
end

#shellObject



93
94
95
96
# File 'lib/vendorificator/vendor.rb', line 93

def shell
  @shell ||=
    environment.config[:shell] || Thor::Shell::Basic.new
end

#statusObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/vendorificator/vendor.rb', line 182

def status
  # If there's no branch yet, it's a completely new module
  return :new unless head

  # If there's a branch but no tag, it's a known module that's not
  # been updated for the new definition yet.
  return :outdated unless tagged_sha1

  # Well, this is awkward: branch is in config and exists, but is
  # not merged into current branch at all.
  return :unmerged unless merged

  # Merge base is tagged with our tag. We're good.
  return :up_to_date if tagged_sha1 == merged

  return :unpulled if environment.fast_forwardable?(tagged_sha1, merged)

  return :unknown
end

#tag_messageObject



291
292
293
# File 'lib/vendorificator/vendor.rb', line 291

def tag_message
  conjure_commit_message
end

#tag_nameObject



283
284
285
# File 'lib/vendorificator/vendor.rb', line 283

def tag_name
  _join(tag_name_base, version)
end

#tag_name_baseObject



279
280
281
# File 'lib/vendorificator/vendor.rb', line 279

def tag_name_base
  _join('vendor', category, name)
end

#tagged_sha1Object



132
133
134
135
136
# File 'lib/vendorificator/vendor.rb', line 132

def tagged_sha1
  @tagged_sha1 ||= environment.git.capturing.rev_parse({:verify => true}, "refs/tags/#{tag_name}^{commit}").strip
rescue MiniGit::GitError
  nil
end

#to_sObject



110
111
112
# File 'lib/vendorificator/vendor.rb', line 110

def to_s
  _join(name, version)
end

#updatable?Boolean

Returns:

  • (Boolean)


175
176
177
178
179
180
# File 'lib/vendorificator/vendor.rb', line 175

def updatable?
  return nil if self.status == :up_to_date
  return false if !head
  return false if head && merged == head
  environment.git.describe({:abbrev => 0, :always => true}, branch_name)
end

#upstream_versionObject



171
172
173
# File 'lib/vendorificator/vendor.rb', line 171

def upstream_version
  # To be overriden
end

#versionObject



167
168
169
# File 'lib/vendorificator/vendor.rb', line 167

def version
  @args[:version] || (!environment.config[:use_upstream_version] && merged_version) || upstream_version
end

#work_dirObject



122
123
124
# File 'lib/vendorificator/vendor.rb', line 122

def work_dir
  _join(environment.config[:root_dir], work_subdir)
end

#work_subdirObject



118
119
120
# File 'lib/vendorificator/vendor.rb', line 118

def work_subdir
  _join(environment.config[:basedir], path)
end