require 'singleton' require_relative 'star_source' require_relative 'git_source' module KCommercial module Resources class SourceManager include Singleton # The shared manager # @return [CocoaDepot::Resources::SourceManager] def self.shared SourceManager.instance end 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 def repos_dir @repos_dir ||= home_dir.join('repos') @repos_dir.mkpath unless @repos_dir.exist? @repos_dir end def sources @sources ||= {} end 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 # url是否是git仓库 def is_git_url(url) regexp = Regexp.new('(?:git|ssh|https?|git@[-\w.]+):(\/\/)?(.*?)(\.git)(\/?|\#[-\d\w._]+?)$') return regexp.match(url) end # 获取git资源仓库名字 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 # 获取资源平台名字 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 end end end