Class: MultiRepo::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/multirepo/utility/utils.rb

Class Method Summary collapse

Class Method Details

.append_if_missing(path, pattern, string_to_append) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/multirepo/utility/utils.rb', line 98

def self.append_if_missing(path, pattern, string_to_append)
  if File.exist?(path)
    string_located = File.readlines(path).grep(pattern).any?
    File.open(path, 'a') { |f| f.puts(string_to_append) } unless string_located
  else
    File.open(path, 'w') { |f| f.puts(string_to_append) }
  end
end

.convert_to_windows_path(unix_path) ⇒ Object



77
78
79
80
# File 'lib/multirepo/utility/utils.rb', line 77

def self.convert_to_windows_path(unix_path)
  components = Pathname.new(unix_path).each_filename.to_a
  components.join(File::ALT_SEPARATOR)
end

.dependencies_clean?(config_entries) ⇒ Boolean

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/multirepo/utility/utils.rb', line 44

def self.dependencies_clean?(config_entries)
  clean = true
  missing = false
  config_entries.each do |e|
    # Ensure the dependency exists
    unless e.repo.exists?
      Console.log_error("Dependency '#{e.path}' does not exist on disk")
      missing |= true
      next
    end
    
    # Ensure it contains no uncommitted changes
    dependency_clean = e.repo.clean?
    clean &= dependency_clean
    Console.log_warning("Dependency '#{e.repo.path}' contains uncommitted changes") unless dependency_clean
  end
  
  fail MultiRepoException, "Some dependencies are not present on this machine." \
    " Run \"multi install\" to clone missing dependencies." if missing
  
  return clean
end

.ensure_working_copies_clean(repos) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/multirepo/utility/utils.rb', line 67

def self.ensure_working_copies_clean(repos)
  clean = true
  repos.each do |repo|
    dependency_clean = repo.clean?
    clean &= dependency_clean
    Console.log_warning("Repo '#{repo.path}' contains uncommitted changes") unless dependency_clean
  end
  return clean
end

.install_hook(name, path) ⇒ Object



31
32
33
34
35
36
# File 'lib/multirepo/utility/utils.rb', line 31

def self.install_hook(name, path)
  destination_path = File.join(path, ".git/hooks")
  destination_file = File.join(destination_path, name)
  FileUtils.cp(path_for_resource(name), destination_file)
  FileUtils.chmod(0755, destination_file) # -rwxr-xr-x
end

.multirepo_enabled?(path) ⇒ Boolean

Returns:



23
24
25
# File 'lib/multirepo/utility/utils.rb', line 23

def self.multirepo_enabled?(path)
  File.exist?(File.join(path, ".multirepo"))
end

.multirepo_tracked?(path) ⇒ Boolean

Returns:



27
28
29
# File 'lib/multirepo/utility/utils.rb', line 27

def self.multirepo_tracked?(path)
  multirepo_enabled?(path) && File.exist?(File.join(path, ".multirepo.lock"))
end

.only_one_true?(*flags) ⇒ Boolean

Returns:



14
15
16
# File 'lib/multirepo/utility/utils.rb', line 14

def self.only_one_true?(*flags)
  flags.reduce(0) { |count, flag| count += 1 if flag; count } <= 1
end

.open_in_default_app(unix_path) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/multirepo/utility/utils.rb', line 90

def self.open_in_default_app(unix_path)
  if OS.osx?
    system %(open "#{unix_path}")
  elsif OS.windows?
    system %(cmd /c "start C:\\#{Utils.convert_to_windows_path(unix_path)}")
  end
end

.path_for_resource(resource_name) ⇒ Object



18
19
20
21
# File 'lib/multirepo/utility/utils.rb', line 18

def self.path_for_resource(resource_name)
  gem_path = Gem::Specification.find_by_name("git-multirepo").gem_dir
  File.join(gem_path, "resources/#{resource_name}")
end

.reveal_in_default_file_browser(unix_path) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/multirepo/utility/utils.rb', line 82

def self.reveal_in_default_file_browser(unix_path)
  if OS.osx?
    system %(open "#{unix_path}")
  elsif OS.windows?
    system %(explorer "#{Utils.convert_to_windows_path(unix_path)}")
  end
end

.sibling_reposObject



38
39
40
41
42
# File 'lib/multirepo/utility/utils.rb', line 38

def self.sibling_repos
  sibling_directories = Dir['../*/']
  sibling_repos = sibling_directories.map{ |d| Repo.new(d) }.select(&:exists?)
  sibling_repos.delete_if{ |r| Pathname.new(r.path).realpath == Pathname.new(".").realpath }
end

.standard_path(p) ⇒ Object



7
8
9
10
11
12
# File 'lib/multirepo/utility/utils.rb', line 7

def self.standard_path(p)
  path = URI.parse(p).path
  path.insert(0, '/') if (path[0] != '/')
  path.chomp!('/') if (path > '/')
  path
end