Class: KCommercial::Resources::GitSourceManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/KCommercialPipeline/core/resource/source/git_source_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#repos_dirObject (readonly)

Returns the value of attribute repos_dir.



15
16
17
# File 'lib/KCommercialPipeline/core/resource/source/git_source_manager.rb', line 15

def repos_dir
  @repos_dir
end

Class Method Details

.sharedCocoaDepot::Resources::GitSourceManager

The shared manager

Returns:

  • (CocoaDepot::Resources::GitSourceManager)


11
12
13
# File 'lib/KCommercialPipeline/core/resource/source/git_source_manager.rb', line 11

def self.shared
  GitSourceManager.instance
end

Instance Method Details

#home_dirObject



17
18
19
20
21
# File 'lib/KCommercialPipeline/core/resource/source/git_source_manager.rb', line 17

def home_dir
  @home_dir ||= Pathname.new(ENV['CP_HOME_DIR'] || '~/.cocoadepot-resources').expand_path
  @home_dir.mkpath unless @home_dir.exist?
  @home_dir
end

#name_for_url(url) ⇒ String

Returns a suitable repository name for url.

Examples:

A GitHub.com URL


name_for_url('https://github.com/Artsy/Specs.git')
  # "artsy"
name_for_url('https://github.com/Artsy/Specs.git')
  # "artsy-1"

A non-Github.com URL


name_for_url('https://sourceforge.org/Artsy/Specs.git')
  # sourceforge-artsy-specs

A file URL


name_for_url('file:///Artsy/Specs.git')
  # artsy-specs

Parameters:

  • url (#to_s)

    The URL of the source.

Returns:

  • (String)

    A suitable repository name for url.



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
# File 'lib/KCommercialPipeline/core/resource/source/git_source_manager.rb', line 69

def name_for_url(url)
  base_from_host_and_path = lambda do |host, path|
    if host && !host.empty?
      base = host.split('.')[-2] || host
      base += '-'
    else
      base = ''
    end

    base + path.gsub(/.git$/, '').gsub(%r{^/}, '').split('/').join('-')
  end

  case url.to_s.downcase
  when %r{github.com[:/]+(.+)/(.+)}
    base = Regexp.last_match[1]
  when %r{^\S+@(\S+)[:/]+(.+)$}
    host, path = Regexp.last_match.captures
    base = base_from_host_and_path[host, path]
  when URI.regexp
    url = URI(url.downcase)
    base = base_from_host_and_path[url.host, url.path]
  else
    base = url.to_s.downcase
  end

  name = base
  # infinity = 1.0 / 0
  # (1..infinity).each do |i|
  #   break unless source_dir(name).exist?
  #   name = "#{base}-#{i}"
  # end
  name
end

#source_dir(name) ⇒ Pathname

Returns The path of the source with the given name.

Parameters:

  • name (String)

    The name of the source.

Returns:

  • (Pathname)

    The path of the source with the given name.



108
109
110
# File 'lib/KCommercialPipeline/core/resource/source/git_source_manager.rb', line 108

def source_dir(name)
  repos_dir + name
end

#source_with_url(url) ⇒ GitSource

Get the git source with name or url

Parameters:

Returns:



36
37
38
39
40
41
42
43
# File 'lib/KCommercialPipeline/core/resource/source/git_source_manager.rb', line 36

def source_with_url(url)
  name = name_for_url(url)
  sources[name] ||= begin
                       path = repos_dir.join(name)
                       GitSource.new(url, path)
                     end
  sources[name]
end

#sourcesObject



29
30
31
# File 'lib/KCommercialPipeline/core/resource/source/git_source_manager.rb', line 29

def sources
  @sources ||= {}
end