Class: KCommercial::Resources::SourceManager

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sharedCocoaDepot::Resources::SourceManager

The shared manager

Returns:

  • (CocoaDepot::Resources::SourceManager)


10
11
12
# File 'lib/KCommercialPipeline/core/resource/source/source_manager.rb', line 10

def self.shared
  SourceManager.instance
end

Instance Method Details

#home_dirObject



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

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

#is_git_url(url) ⇒ Object

url是否是git仓库



44
45
46
47
# File 'lib/KCommercialPipeline/core/resource/source/source_manager.rb', line 44

def  is_git_url(url)
  regexp = Regexp.new('(?:git|ssh|https?|git@[-\w.]+):(\/\/)?(.*?)(\.git)(\/?|\#[-\d\w._]+?)$')
  return regexp.match(url)
end

#name_for_git_url(url) ⇒ Object

获取git资源仓库名字



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/KCommercialPipeline/core/resource/source/source_manager.rb', line 49

def name_for_git_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
  name
end

#name_for_platform_url(url) ⇒ Object

获取资源平台名字



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/KCommercialPipeline/core/resource/source/source_manager.rb', line 78

def name_for_platform_url(url)
  hash = Hash.new("")
  if url.index('?')
    str = url.split('?')[1]
    if str.split('&')
      array = str.split('&')
      array.each do |item|
        hash[item.split('=')[0]] = item.split('=')[1]
      end
    end
  end
  return "exploration_#{hash['space']}"
end

#repos_dirObject



21
22
23
24
25
# File 'lib/KCommercialPipeline/core/resource/source/source_manager.rb', line 21

def repos_dir
  @repos_dir ||= home_dir.join('repos')
  @repos_dir.mkpath unless @repos_dir.exist?
  @repos_dir
end

#source_with_url(url) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/KCommercialPipeline/core/resource/source/source_manager.rb', line 31

def source_with_url(url)
  if is_git_url(url)
    name = name_for_git_url(url)
    path = repos_dir.join(name)
    sources[name] ||= GitSource.new(url, path)
  else
    name = name_for_platform_url(url)
    path = repos_dir.join(name)
    sources[name] ||= StarSource.new(url, path)
  end
  sources[name]
end

#sourcesObject



27
28
29
# File 'lib/KCommercialPipeline/core/resource/source/source_manager.rb', line 27

def sources
  @sources ||= {}
end