lib/vendorificator/environment.rb in vendorificator-0.3.0 vs lib/vendorificator/environment.rb in vendorificator-0.4.0

- old
+ new

@@ -1,22 +1,24 @@ require 'pathname' - require 'minigit' - +require 'awesome_print' require 'vendorificator/config' module Vendorificator class Environment attr_reader :config attr_accessor :shell, :vendor_instances - def initialize(vendorfile=nil) + def initialize(vendorfile=nil, &block) @vendor_instances = [] @config = Vendorificator::Config.new @config.environment = self - @config.read_file(find_vendorfile(vendorfile).to_s) + if vendorfile || !block_given? + @config.read_file(find_vendorfile(vendorfile).to_s) + end + @config.instance_eval(&block) if block_given? self.each_vendor_instance{ |mod| mod.compute_dependencies! } end def say_status(*args) @@ -64,10 +66,11 @@ def pull(remote, options={}) raise RuntimeError, "Unknown remote #{remote}" unless remotes.include?(remote) git.fetch(remote) git.fetch({:tags => true}, remote) + git.fetch(remote, 'refs/notes/vendor:refs/notes/vendor') ref_rx = /^refs\/remotes\/#{Regexp.quote(remote)}\// remote_branches = Hash[ git.capturing.show_ref. lines. map(&:split). @@ -95,10 +98,30 @@ say_status 'unknown', mod.branch_name end end end + # Public: Displays info about the last merged version of module. + # + # mod - String with the module name + # options - Hash containing options + # + # Returns nothing. + def info(mod_name, options = {}) + if vendor = find_vendor_instance_by_name(mod_name) + shell.say "Module name: #{vendor.name}\n" + shell.say "Module category: #{vendor.category}\n" + shell.say "Module merged version: #{vendor.merged_version}\n" + shell.say "Module merged notes: #{vendor.merged_notes.ai}\n" + elsif (commit = Commit.new(mod_name, git)).exists? + shell.say "Branches that contain this commit: #{commit.branches.join(', ')}\n" + shell.say "Vendorificator notes on this commit: #{commit.notes.ai}\n" + else + shell.say "Module or ref #{mod_name.inspect} not found." + end + end + # Public: Push changes on module branches. # # options - The Hash containing options # # Returns nothing. @@ -107,28 +130,31 @@ pushable = [] each_vendor_instance{ |mod| pushable += mod.pushable_refs } remotes = options[:remote] ? options[:remote].split(',') : config[:remotes] - remotes.each{ |remote| git.push remote, pushable } - - git.push :tags => true + remotes.each do |remote| + git.push remote, pushable + git.push remote, :tags => true + git.push remote, 'refs/notes/vendor' + end end # Public: Runs all the vendor modules. # # options - The Hash of options. # # Returns nothing. def sync(options = {}) ensure_clean! config[:use_upstream_version] = options[:update] + metadata = metadata_snapshot each_vendor_instance(*options[:modules]) do |mod| say_status :module, mod.name indent do - mod.run! + mod.run!(:metadata => metadata) end end end # Public: Goes through all the Vendor instances and runs the block @@ -164,12 +190,44 @@ true rescue MiniGit::GitError false end + def metadata_snapshot + { + :vendorificator_version => ::Vendorificator::VERSION, + :current_branch => git.capturing.rev_parse({:abbrev_ref => true}, 'HEAD').strip, + :current_sha => git.capturing.rev_parse('HEAD').strip, + :git_describe => (git.capturing.describe.strip rescue '') + } + end + + # Public: returns `config[:root_dir]` relative to Git repository root + def relative_root_dir + @relative_root_dir ||= config[:root_dir].relative_path_from( + Pathname.new(git.git_work_tree)) + end + + # Public: Returns module with given name + def [](name) + vendor_instances.find { |v| v.name == name } + end + private + # Private: Finds a vendor instance by module name. + # + # mod_name - The String containing the module name. + # + # Returns Vendor instance. + def find_vendor_instance_by_name(mod_name) + each_vendor_instance do |mod| + return mod if mod.name == mod_name + end + nil + end + # Private: Finds the vendorfile to use. # # given - the optional String containing vendorfile path. # # Returns a String containing the vendorfile path. @@ -211,8 +269,7 @@ shell.padding += 1 if shell yield ensure shell.padding -= 1 if shell end - end end