Class: Pod::Command::Dep

Inherits:
Pod::Command show all
Includes:
Command::ProjectDirectory
Defined in:
lib/cocoapods-dep/command/dep.rb

Constant Summary collapse

OUTFILE =

输出依赖文件

'dep_analyze_out.lock'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Dep

Returns a new instance of Dep.



27
28
29
30
# File 'lib/cocoapods-dep/command/dep.rb', line 27

def initialize(argv)
  @podspec_name = argv.shift_argument
  super
end

Class Method Details

.argumentsObject



21
22
23
24
25
# File 'lib/cocoapods-dep/command/dep.rb', line 21

def self.arguments
  [
    CLAide::Argument.new('PODSPEC', false)
  ].concat(super)
end

.optionsObject



17
18
19
# File 'lib/cocoapods-dep/command/dep.rb', line 17

def self.options
  [].concat(super)
end

Instance Method Details

#arr_ele_cutout(arr) ⇒ Object



54
55
56
57
58
59
60
61
62
# 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

#dependenciesObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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_tagObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# 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
end

#generate_pods_data(specs) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# 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) ⇒ Object



209
210
211
212
213
214
215
216
# File 'lib/cocoapods-dep/command/dep.rb', line 209

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

#podfileObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# 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

#runObject



48
49
50
51
52
# File 'lib/cocoapods-dep/command/dep.rb', line 48

def run
  puts "开始分析依赖".green
  yaml_write
  puts "分析依赖完成,请查看 <-- #{Dir.pwd}/#{OUTFILE} -->文件".green
end

#sandboxObject



113
114
115
116
117
118
119
120
# 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!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



128
129
130
# 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_writeObject



122
123
124
125
126
# 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