class Pod::Command::Dep

Constants

OUTFILE

输出依赖文件

Public Class Methods

arguments() click to toggle source
Calls superclass method
# File lib/cocoapods-dep/command/dep.rb, line 21
def self.arguments
  [
    CLAide::Argument.new('PODSPEC', false)
  ].concat(super)
end
new(argv) click to toggle source
Calls superclass method
# File lib/cocoapods-dep/command/dep.rb, line 27
def initialize(argv)
  @podspec_name = argv.shift_argument
  super
end
options() click to toggle source
Calls superclass method
# File lib/cocoapods-dep/command/dep.rb, line 17
def self.options
  [].concat(super)
end

Public Instance Methods

arr_ele_cutout(arr) click to toggle source
# File lib/cocoapods-dep/command/dep.rb, line 54
def arr_ele_cutout(arr)
  arr.map do |ele|
      if ele.to_s.include? '/'
          ele.to_s[0, ele.index('/')]
      else
          ele
      end
  end
end
dependencies() click to toggle source
# File lib/cocoapods-dep/command/dep.rb, line 64
def dependencies
  @dependencies ||= begin
    analyzer = Installer::Analyzer.new(
      sandbox,
      podfile,
      @podspec ? nil : config.lockfile
    )

    specs = config.with_changes(skip_repo_update: true) do
      analyzer.analyze(@podspec).specs_by_target.values.flatten(1)
    end

    generate_pods_data(specs)
  end
end
dependencies_need_tag() click to toggle source
# File lib/cocoapods-dep/command/dep.rb, line 187
def dependencies_need_tag
  change_dependencies = Array.new()
  IO.foreach("Podfile") { |line|
      # lstrip 去掉头部空格
      if line.lstrip.start_with?("dev_pod")
          if line.include?','
              arr = line.split(',')
              arr.each do |name|
                  change_dependencies << match_pod(name)
              end
          else
              change_dependencies << match_pod(line)
          end
      end
  }
  @change_dependencies = change_dependencies.uniq
  change_dependencies_priority = Hash.new()
  change_dependencies.each do |pod_name|
      change_dependencies_priority[pod_name] = 1000
  end
  @change_dependencies_priority = change_dependencies_priority
end
generate_pods_data(specs) click to toggle source
# File lib/cocoapods-dep/command/dep.rb, line 80
def generate_pods_data(specs)
  pods_and_deps_merged = specs.reduce({}) do |result, spec|
    name = spec.name
    result[name] ||= []
    result[name].concat(spec.all_dependencies.map(&:name))
    result
  end

  pod_and_deps = pods_and_deps_merged.map do |name, deps|
    deps.empty? ? name : { name => YAMLHelper.sorted_array(deps.uniq) }
  end
  YAMLHelper.sorted_array(pod_and_deps)
end
match_pod(str) click to toggle source
# File lib/cocoapods-dep/command/dep.rb, line 210
def match_pod(str)
    reg = /(?<=').*?(?=')/
    pod_name = reg.match(str).to_s
    if pod_name.include? '/'
        pod_name = pod_name[0, pod_name.index('/')]
    end
    pod_name
end
podfile() click to toggle source
# File lib/cocoapods-dep/command/dep.rb, line 94
def podfile
  @podfile ||= begin
    if podspec = @podspec
      platform = podspec.available_platforms.first
      platform_name, platform_version = platform.name, platform.deployment_target.to_s
      sources = SourcesManager.all.map(&:url)
      Podfile.new do
        install! :cocoapods, integrate_targets: false
        sources.each { |s| source s }
        platform platform_name, platform_version
        pod podspec.name, podspec: podspec.defined_in_file
      end
    else
      verify_podfile_exists!
      config.podfile
    end
  end
end
read_change_dependencies_from_file(path) click to toggle source
# File lib/cocoapods-dep/command/dep.rb, line 132
def read_change_dependencies_from_file(path)
  # 分析需要打tag的库
  dependencies_need_tag

  yaml_file = YAML.load(File.open(path))
  lines = Array.new()

  change_dependencies = Array.new()
  @change_dependencies.each do |spec_name|
    spec_dependency_hash = Hash.new()
    spec_name_key = spec_name
    spec_dependencies = Array.new()
    yaml_file.each do |line|
        if line.class == Hash
            line.each { |key, value|
                if key.to_s.include? spec_name.to_s or key.to_s.include? spec_name.to_s + '/'
                    spec_dependencies = spec_dependencies + value
                end
            }
        end
    end
    spec_dependency_hash[spec_name_key] = arr_ele_cutout(spec_dependencies)
    change_dependencies << spec_dependency_hash
  end

  dependencies_priority = Hash.new()
  change_dependencies.each do |dependency|
    dependency.each { |key, value|
        temp_arr = value & @change_dependencies
        if temp_arr.include? key
            temp_arr.delete(key)
        end
        dependencies_priority[key] = temp_arr
    }
  end

  temp_change_dependencies_priority = Hash.new()
  while temp_change_dependencies_priority != @change_dependencies_priority do
      temp_change_dependencies_priority = @change_dependencies_priority.clone
      dependencies_priority.each { |key, value|
          if value.length > 0
              priority = @change_dependencies_priority[key]
              value.each do |pod_name|
                  if priority >= @change_dependencies_priority[pod_name]
                      priority = @change_dependencies_priority[pod_name] - 1
                  end
              end
              @change_dependencies_priority[key] = priority
          end
      }
  end
  pp @change_dependencies_priority
  write_to_disk(config.installation_root.to_s + '/' + OUTFILE, @change_dependencies_priority.to_yaml)
end
run() click to toggle source
# File lib/cocoapods-dep/command/dep.rb, line 48
def run
  puts "开始分析依赖".green
  yaml_write
  puts "分析依赖完成,请查看 <-- #{Dir.pwd}/#{OUTFILE} -->文件".green
end
sandbox() click to toggle source
# File lib/cocoapods-dep/command/dep.rb, line 113
def sandbox
  if @podspec
    require 'tmpdir'
    Sandbox.new(Dir.mktmpdir)
  else
    config.sandbox
  end
end
validate!() click to toggle source
Calls superclass method
# File lib/cocoapods-dep/command/dep.rb, line 32
def validate!
  super
  if @podspec_name
    require 'pathname'
    path = Pathname.new(@podspec_name)
    if path.exist?
      @podspec = Specification.from_file(path)
    else
      @podspec = SourcesManager.
        search(Dependency.new(@podspec_name)).
        specification.
        subspec_by_name(@podspec_name)
    end
  end
end
write_to_disk(path, content) click to toggle source
# File lib/cocoapods-dep/command/dep.rb, line 128
def write_to_disk(path, content)
  File.open(path, 'w') { |f| f.write(content.to_yaml) }
end
yaml_write() click to toggle source
# File lib/cocoapods-dep/command/dep.rb, line 122
def yaml_write
  lock_path = config.installation_root.to_s + '/dependencies.lock'
  write_to_disk(lock_path, dependencies)
  read_change_dependencies_from_file(lock_path)
end