cocoapods-yaml 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/README.md +41 -5
- data/lib/cocoapods/yaml.rb +6 -1
- data/lib/cocoapods_plugin.rb +17 -6
- data/lib/core/check/project.rb +53 -17
- data/lib/core/check/xcodeproj.rb +6 -2
- data/lib/core/check/yaml_lint.rb +116 -0
- data/lib/core/check/yaml_status.rb +175 -0
- data/lib/core/command/yaml_lint.rb +28 -0
- data/lib/core/command/yaml_relation.rb +1 -1
- data/lib/core/command/yaml_status.rb +45 -0
- data/lib/core/logger.rb +3 -0
- data/lib/core/setup/podfile_setup.rb +132 -16
- data/lib/core/thin/compatibility.rb +93 -0
- data/lib/core/thin/installer.rb +30 -11
- data/lib/core/thin/local.rb +6 -2
- data/lib/core/thin/shell.rb +3 -0
- data/lib/core/yaml/working_directory.rb +51 -0
- data/lib/core/yaml/yaml_dependencies.rb +264 -68
- data/lib/version.rb +24 -1
- metadata +38 -5
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require 'cocoapods'
|
|
2
|
+
require_relative '../check/yaml_lint'
|
|
3
|
+
|
|
4
|
+
module Pod
|
|
5
|
+
class Command
|
|
6
|
+
class Yaml < Command
|
|
7
|
+
class Lint < Yaml
|
|
8
|
+
|
|
9
|
+
self.summary = '校验依赖yaml文件(不执行pod install)'
|
|
10
|
+
|
|
11
|
+
self.description = <<-DESC
|
|
12
|
+
离线校验各target依赖文件与LocalDependencies.yaml,不拉取依赖、不访问私有源:
|
|
13
|
+
* 顶层字段(拼写错误会导致目标解析出0个依赖)
|
|
14
|
+
* 组件的引用方式(path/podspec/version/git+tag/branch)与字段互斥关系
|
|
15
|
+
* SOURCES 声明、文件内重复组件
|
|
16
|
+
* 缺少依赖文件的target、与任何target都对不上的孤儿文件
|
|
17
|
+
校验未通过时以非0退出码结束,可直接用于CI前置检查。
|
|
18
|
+
DESC
|
|
19
|
+
|
|
20
|
+
def run
|
|
21
|
+
passed = YamlCheck::Linter.new.run
|
|
22
|
+
raise Pod::Informative, '依赖yaml文件校验未通过,请按上方提示修正' unless passed
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -24,7 +24,7 @@ module Pod
|
|
|
24
24
|
def initialize(argv)
|
|
25
25
|
@name = argv.shift_argument
|
|
26
26
|
super
|
|
27
|
-
CocoaPods::Yaml.log.info("插件版本号: #{CocoaPods::Yaml
|
|
27
|
+
CocoaPods::Yaml.log.info("插件版本号: #{CocoaPods::Yaml.version_display}")
|
|
28
28
|
@sort_count = argv.flag?('sort', false)
|
|
29
29
|
@forward = argv.flag?('forward', false)
|
|
30
30
|
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require 'cocoapods'
|
|
2
|
+
require_relative '../check/yaml_status'
|
|
3
|
+
|
|
4
|
+
module Pod
|
|
5
|
+
class Command
|
|
6
|
+
class Yaml < Command
|
|
7
|
+
class Status < Yaml
|
|
8
|
+
|
|
9
|
+
self.summary = '对比YAML声明与Podfile.lock的实际安装情况'
|
|
10
|
+
|
|
11
|
+
self.description = <<-DESC
|
|
12
|
+
读取各target依赖文件与Podfile.lock,输出三类差异:
|
|
13
|
+
1. 声明了但锁文件中不存在(漏装 / 组件名写错 / 尚未 pod install)
|
|
14
|
+
2. 锁文件直接依赖里存在但YAML未声明(直接改了Podfile,或删了组件没重新install)
|
|
15
|
+
3. YAML写死的版本与锁文件解析出的版本不一致
|
|
16
|
+
只读取本地文件,不访问网络与私有源。默认存在差异也返回0,加 --strict 时以非0退出(供CI使用)。
|
|
17
|
+
DESC
|
|
18
|
+
|
|
19
|
+
def self.options
|
|
20
|
+
[
|
|
21
|
+
%w[--strict 存在差异时以非0退出码结束(供CI使用)],
|
|
22
|
+
].concat(super).reject { |(name, _)| (name == '--no-ansi' or name == '--silent' or name == '--allow-root' or name == '--verbose') }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def initialize(argv)
|
|
26
|
+
@strict = argv.flag?('strict', false)
|
|
27
|
+
super
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def run
|
|
31
|
+
result = YamlCheck::Status.new.run
|
|
32
|
+
|
|
33
|
+
if result == :error
|
|
34
|
+
raise Pod::Informative, '无法完成声明与安装的对比,请按上方提示处理'
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
if result == :diff && @strict
|
|
38
|
+
raise Pod::Informative, 'YAML声明与Podfile.lock存在差异(--strict 模式)'
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
data/lib/core/logger.rb
CHANGED
|
@@ -2,18 +2,34 @@
|
|
|
2
2
|
|
|
3
3
|
require 'fileutils'
|
|
4
4
|
require 'time'
|
|
5
|
+
require 'pathname'
|
|
6
|
+
require 'cocoapods'
|
|
7
|
+
require 'xcodeproj'
|
|
5
8
|
require_relative '../yaml/yaml_dependencies'
|
|
6
9
|
|
|
7
10
|
module CocoaPods
|
|
8
11
|
module Yaml
|
|
9
12
|
# Podfile初始化: 生成接入cocoapods-yaml的模板Podfile
|
|
13
|
+
#
|
|
14
|
+
# 所有路径都基于工程根目录(默认取 CocoaPods 的 installation_root),
|
|
15
|
+
# 不再依赖进程当前工作目录——否则 pod install --project-directory=xxx 时
|
|
16
|
+
# 会把模板 Podfile 写到执行命令的目录里。
|
|
10
17
|
class PodfileSetup
|
|
11
|
-
|
|
18
|
+
# 读不到工程设置时的兜底值。
|
|
19
|
+
# 取值偏保守:默认值高于工程实际版本会导致 pods 按更高版本编译,
|
|
20
|
+
# 链接时报 "building for iOS X, but linking in object file built for iOS Y"。
|
|
21
|
+
DEFAULT_DEPLOYMENT_TARGET = '15.0'
|
|
22
|
+
|
|
23
|
+
# 模板内容。platform 在生成时按工程实际设置替换:
|
|
24
|
+
# 写死版本会把版本带偏——在 13.0 的工程里 init 会生成 15.5 的 Podfile,
|
|
25
|
+
# pod target 按 15.5 编译,链接时报
|
|
26
|
+
# "building for iOS 13.0, but linking in object file built for iOS 15.5"。
|
|
27
|
+
TEMPLATE_BODY = <<~'PODFILE'
|
|
12
28
|
# frozen_string_literal: true
|
|
13
29
|
# cocoapods-yaml 插件会被CocoaPods自动加载,无需require;
|
|
14
30
|
# 若 pod 命令提示找不到 yaml_dependencies!,请执行: gem install cocoapods-yaml
|
|
15
31
|
|
|
16
|
-
platform :ios, '
|
|
32
|
+
platform :ios, '__PLATFORM__'
|
|
17
33
|
|
|
18
34
|
yaml_dependencies! do |target_name|
|
|
19
35
|
# target_name: 当前集成的target名称(App/扩展),每个声明了依赖的target回调一次
|
|
@@ -31,16 +47,30 @@ module CocoaPods
|
|
|
31
47
|
end
|
|
32
48
|
PODFILE
|
|
33
49
|
|
|
50
|
+
# @param platform [String] 工程最低系统版本,如 '15.5'
|
|
51
|
+
# @return [String] 完整 Podfile 内容
|
|
52
|
+
def self.template(platform = DEFAULT_DEPLOYMENT_TARGET)
|
|
53
|
+
TEMPLATE_BODY.sub('__PLATFORM__', platform.to_s)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @param root [Pathname, String, nil] 工程根目录,默认 CocoaPods 的 installation_root
|
|
57
|
+
def initialize(root = nil)
|
|
58
|
+
@root = Pathname(root || Pod::Config.instance.installation_root).expand_path
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# @return [Pathname] 工程根目录
|
|
62
|
+
attr_reader :root
|
|
63
|
+
|
|
34
64
|
# pod yaml init: 显式初始化
|
|
35
65
|
# 无Podfile直接生成;已有Podfile但未接入时先备份再替换
|
|
36
66
|
def self.run
|
|
37
67
|
setup = new
|
|
38
68
|
unless setup.xcodeproj?
|
|
39
|
-
CocoaPods::Yaml.log.error(
|
|
69
|
+
CocoaPods::Yaml.log.error("当前目录未找到.xcodeproj工程文件,请在iOS工程根目录执行 pod yaml init(当前: #{setup.root})")
|
|
40
70
|
exit 1
|
|
41
71
|
end
|
|
42
72
|
|
|
43
|
-
if
|
|
73
|
+
if setup.podfile_exist?
|
|
44
74
|
if setup.integrated?
|
|
45
75
|
CocoaPods::Yaml.log.info('当前工程已接入cocoapods-yaml,无需初始化')
|
|
46
76
|
return
|
|
@@ -60,7 +90,7 @@ module CocoaPods
|
|
|
60
90
|
setup = new
|
|
61
91
|
return unless setup.xcodeproj?
|
|
62
92
|
|
|
63
|
-
if
|
|
93
|
+
if setup.podfile_exist?
|
|
64
94
|
unless setup.integrated?
|
|
65
95
|
CocoaPods::Yaml.log.warn('检测到Podfile未接入cocoapods-yaml,执行 pod yaml init 可备份后生成模板(不会自动替换现有Podfile)')
|
|
66
96
|
end
|
|
@@ -68,40 +98,126 @@ module CocoaPods
|
|
|
68
98
|
end
|
|
69
99
|
|
|
70
100
|
setup.write_template
|
|
71
|
-
CocoaPods::Yaml.log.detail(
|
|
101
|
+
CocoaPods::Yaml.log.detail("未找到Podfile,已在#{setup.root}自动生成接入cocoapods-yaml的模板Podfile,依赖yaml文件将在解析时生成")
|
|
102
|
+
rescue StandardError => e
|
|
103
|
+
# 自动初始化失败不应阻断 pod install 本身
|
|
104
|
+
CocoaPods::Yaml.log.warn("模板Podfile自动生成失败(不影响 pod install 流程): #{e.message}")
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# @return [Pathname]
|
|
108
|
+
def podfile_path
|
|
109
|
+
@root.join('Podfile')
|
|
72
110
|
end
|
|
73
111
|
|
|
74
|
-
|
|
112
|
+
def podfile_exist?
|
|
113
|
+
File.exist?(podfile_path)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# 工程根目录下是否存在xcodeproj
|
|
75
117
|
def xcodeproj?
|
|
76
|
-
Dir.glob('*.xcodeproj').any?
|
|
118
|
+
Dir.glob(@root.join('*.xcodeproj').to_s).any?
|
|
77
119
|
end
|
|
78
120
|
|
|
79
121
|
# Podfile是否已接入yaml_dependencies!
|
|
80
122
|
def integrated?
|
|
81
|
-
File.read(
|
|
123
|
+
File.read(podfile_path).include?('yaml_dependencies!')
|
|
82
124
|
rescue StandardError
|
|
83
125
|
false
|
|
84
126
|
end
|
|
85
127
|
|
|
86
128
|
def backup_podfile
|
|
87
|
-
|
|
88
|
-
FileUtils.cp(
|
|
89
|
-
CocoaPods::Yaml.log.detail("已备份原Podfile为: #{
|
|
129
|
+
backup_path = @root.join("Podfile.bak.#{Time.now.strftime('%Y%m%d%H%M%S')}")
|
|
130
|
+
FileUtils.cp(podfile_path, backup_path)
|
|
131
|
+
CocoaPods::Yaml.log.detail("已备份原Podfile为: #{backup_path}")
|
|
90
132
|
end
|
|
91
133
|
|
|
92
134
|
def write_template
|
|
93
|
-
File.write(
|
|
94
|
-
CocoaPods::Yaml.log.detail(
|
|
135
|
+
File.write(podfile_path, self.class.template(deployment_target))
|
|
136
|
+
CocoaPods::Yaml.log.detail("已生成模板Podfile: #{podfile_path}(platform :ios, '#{deployment_target}')")
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# 模板使用的 iOS 最低版本:取工程主 App target 的设置,读不到时用默认值
|
|
140
|
+
# @return [String]
|
|
141
|
+
def deployment_target
|
|
142
|
+
@deployment_target ||= detect_deployment_target
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# 从 .xcodeproj 读取主 App target 的 IPHONEOS_DEPLOYMENT_TARGET。
|
|
146
|
+
# 多个 App target 并存时取最小值,保证生成的 platform 不会高于任何一个。
|
|
147
|
+
# 优先级: App target 的显式值 > 工程级配置 > 默认值
|
|
148
|
+
# @return [String]
|
|
149
|
+
def detect_deployment_target
|
|
150
|
+
xcodeproj_path = Dir.glob(@root.join('*.xcodeproj').to_s).first
|
|
151
|
+
return DEFAULT_DEPLOYMENT_TARGET unless xcodeproj_path
|
|
152
|
+
|
|
153
|
+
project = Xcodeproj::Project.open(xcodeproj_path)
|
|
154
|
+
|
|
155
|
+
app_targets = project.targets.select do |target|
|
|
156
|
+
target.respond_to?(:product_type) && target.product_type.to_s.end_with?('.application')
|
|
157
|
+
end
|
|
158
|
+
# 只用 App target,避免把测试 target(可能写成更高的值)带进模板
|
|
159
|
+
probe_targets = app_targets.empty? ? project.targets : app_targets
|
|
160
|
+
|
|
161
|
+
target_versions = numeric_versions(probe_targets.flat_map do |target|
|
|
162
|
+
target.build_configurations.map { |config| config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] }
|
|
163
|
+
end)
|
|
164
|
+
return target_versions.min_by { |value| Gem::Version.new(value) } if target_versions.any?
|
|
165
|
+
|
|
166
|
+
# target 未显式设置(如 $(inherited))时继承工程级配置
|
|
167
|
+
project_versions = numeric_versions(project.build_configurations.map do |config|
|
|
168
|
+
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET']
|
|
169
|
+
end)
|
|
170
|
+
return project_versions.min_by { |value| Gem::Version.new(value) } if project_versions.any?
|
|
171
|
+
|
|
172
|
+
DEFAULT_DEPLOYMENT_TARGET
|
|
173
|
+
rescue StandardError => e
|
|
174
|
+
CocoaPods::Yaml.log.detail("读取工程deployment target失败,使用默认值#{DEFAULT_DEPLOYMENT_TARGET}: #{e.message}")
|
|
175
|
+
DEFAULT_DEPLOYMENT_TARGET
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# 过滤 $(inherited)、空值等非数字版本号
|
|
179
|
+
# @return [Array<String>]
|
|
180
|
+
def numeric_versions(values)
|
|
181
|
+
values.compact.map(&:to_s).select { |value| value.match?(/\A\d+(\.\d+)*\z/) }
|
|
95
182
|
end
|
|
96
183
|
|
|
97
184
|
# 生成本地覆盖文件与各target依赖文件模板
|
|
98
185
|
def generate_yamls
|
|
99
186
|
yaml_dep = Pod::YamlDependencies.new
|
|
100
|
-
|
|
101
|
-
|
|
187
|
+
# YamlDependencies 内部按相对文件名读写,需切换到工程根目录执行
|
|
188
|
+
Dir.chdir(@root) do
|
|
189
|
+
yaml_dep.init_main_targets
|
|
190
|
+
yaml_dep.generate_yaml
|
|
191
|
+
end
|
|
102
192
|
rescue StandardError => e
|
|
103
193
|
CocoaPods::Yaml.log.warn("依赖yaml模板生成失败(不影响已生成的Podfile): #{e.message}")
|
|
104
194
|
end
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
module Pod
|
|
200
|
+
class Command
|
|
201
|
+
# pod install/update 的自动初始化钩子。
|
|
202
|
+
#
|
|
203
|
+
# 必须在命令执行阶段(validate!)触发,不能放在插件加载(require)阶段:
|
|
204
|
+
# 加载插件时 --project-directory 尚未解析,按进程当前目录判断会把 Podfile
|
|
205
|
+
# 写到执行命令的目录,而不是工程目录。
|
|
206
|
+
module YamlPodfileSetupHook
|
|
207
|
+
def validate!
|
|
208
|
+
unless @yaml_podfile_setup_done
|
|
209
|
+
@yaml_podfile_setup_done = true
|
|
210
|
+
CocoaPods::Yaml::PodfileSetup.auto_on_install
|
|
211
|
+
end
|
|
212
|
+
super
|
|
213
|
+
end
|
|
105
214
|
end
|
|
106
215
|
end
|
|
107
216
|
end
|
|
217
|
+
|
|
218
|
+
require 'cocoapods/command/install'
|
|
219
|
+
require 'cocoapods/command/update'
|
|
220
|
+
|
|
221
|
+
[Pod::Command::Install, Pod::Command::Update].each do |command_class|
|
|
222
|
+
command_class.prepend(Pod::Command::YamlPodfileSetupHook)
|
|
223
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
require 'cocoapods'
|
|
2
|
+
require_relative '../../version'
|
|
3
|
+
|
|
4
|
+
module Pod
|
|
5
|
+
# 未使用依赖裁剪(trim)依赖 CocoaPods 的内部实现:
|
|
6
|
+
# 它重写了 Installer#download_dependencies,并直接读写 sandbox_state / pod_targets /
|
|
7
|
+
# aggregate_targets / analysis_result 这些非公开接口(其中多个是 private 方法)。
|
|
8
|
+
# CocoaPods 升级后若结构发生变化,直接调用会抛 NoMethodError,把 pod install 整个打断。
|
|
9
|
+
#
|
|
10
|
+
# 因此裁剪前先做一次能力探测:
|
|
11
|
+
# - 缺少必要接口 -> 降级为完整安装(不裁剪,编译慢一点,但结果正确)
|
|
12
|
+
# - 版本超出验证区间但接口齐全 -> 提示后继续(结构大概率没变,不强行禁用)
|
|
13
|
+
module TrimCompatibility
|
|
14
|
+
# 裁剪流程直接调用的 Installer 方法
|
|
15
|
+
REQUIRED_INSTALLER_METHODS = %i[
|
|
16
|
+
download_dependencies
|
|
17
|
+
install_pod_sources
|
|
18
|
+
run_podfile_pre_install_hooks
|
|
19
|
+
clean_pod_sources
|
|
20
|
+
sandbox_state
|
|
21
|
+
pod_targets
|
|
22
|
+
aggregate_targets
|
|
23
|
+
analysis_result
|
|
24
|
+
].freeze
|
|
25
|
+
|
|
26
|
+
# 引用计数递归依赖的能力(定义在插件对 PodTarget 的扩展里,用于确认基类未被改名)
|
|
27
|
+
REQUIRED_POD_TARGET_METHODS = %i[dependent_targets].freeze
|
|
28
|
+
|
|
29
|
+
# 与 gemspec 共用同一份版本区间定义
|
|
30
|
+
VERIFIED_VERSION_REQUIREMENT = Gem::Requirement.new(*CocoaPods::Yaml::COCOAPODS_VERSION_REQUIREMENTS)
|
|
31
|
+
|
|
32
|
+
class << self
|
|
33
|
+
# @return [Array<String>] 缺失的内部接口
|
|
34
|
+
def missing_requirements
|
|
35
|
+
missing = []
|
|
36
|
+
|
|
37
|
+
REQUIRED_INSTALLER_METHODS.each do |name|
|
|
38
|
+
missing << "Pod::Installer##{name}" unless defined_on?(Pod::Installer, name)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
REQUIRED_POD_TARGET_METHODS.each do |name|
|
|
42
|
+
missing << "Pod::PodTarget##{name}" unless defined_on?(Pod::PodTarget, name)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
missing
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# @return [Boolean] 内部接口是否齐全
|
|
49
|
+
def compatible?
|
|
50
|
+
missing_requirements.empty?
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# @return [String] 当前 CocoaPods 版本
|
|
54
|
+
def version
|
|
55
|
+
Pod::VERSION
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# 版本是否在插件验证区间内。超出不阻断,仅提示。
|
|
59
|
+
def version_verified?
|
|
60
|
+
VERIFIED_VERSION_REQUIREMENT.satisfied_by?(Gem::Version.new(version))
|
|
61
|
+
rescue ArgumentError
|
|
62
|
+
false
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# @return [String] 验证区间的可读形式
|
|
66
|
+
def verified_range_text
|
|
67
|
+
CocoaPods::Yaml::COCOAPODS_VERSION_REQUIREMENTS.join(', ')
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# @return [Array<String>] 版本超出验证区间时的提示
|
|
71
|
+
def version_warning
|
|
72
|
+
["当前 CocoaPods #{version} 超出插件验证范围(#{verified_range_text})",
|
|
73
|
+
'裁剪会按兼容逻辑继续执行;如遇异常请升级 cocoapods-yaml 或反馈']
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# @param missing [Array<String>] missing_requirements 的结果
|
|
77
|
+
# @return [Array<String>] 降级提示
|
|
78
|
+
def incompatible_warning(missing)
|
|
79
|
+
["当前 CocoaPods #{version} 的内部结构与插件验证范围(#{verified_range_text})不一致",
|
|
80
|
+
"缺少接口: #{missing.join('、')}",
|
|
81
|
+
'已自动降级为完整安装(不裁剪未使用依赖,编译变慢但结果正确)',
|
|
82
|
+
'如需恢复裁剪,请升级插件: gem install cocoapods-yaml']
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
# 需要区分 public/private:trim 依赖的多半就是 private 方法
|
|
88
|
+
def defined_on?(klass, name)
|
|
89
|
+
klass.method_defined?(name) || klass.private_method_defined?(name)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
data/lib/core/thin/installer.rb
CHANGED
|
@@ -3,25 +3,44 @@ require 'yaml'
|
|
|
3
3
|
require_relative 'shell'
|
|
4
4
|
require_relative 'local'
|
|
5
5
|
require_relative 'pod_target'
|
|
6
|
+
require_relative 'compatibility'
|
|
6
7
|
|
|
7
8
|
module Pod
|
|
8
9
|
# 裁剪开关的下载流程补丁。必须用prepend而不是重开class重定义:
|
|
9
10
|
# 重开class定义同名方法是覆盖,super找不到原方法;prepend后super即CocoaPods原始实现
|
|
10
11
|
module InstallerTrim
|
|
11
12
|
def download_dependencies
|
|
12
|
-
|
|
13
|
-
UI.section '下载依赖...' do
|
|
13
|
+
return super unless Pod::YamlDependencies.new.trim_unused_pod?
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
clean_pod_sources
|
|
15
|
+
# 能力探测:裁剪读写的是 Installer 的私有接口,CocoaPods 升级后结构可能变化。
|
|
16
|
+
# 缺接口时降级为完整安装,而不是让 pod install 抛 NoMethodError 中断。
|
|
17
|
+
unless Pod::TrimCompatibility.compatible?
|
|
18
|
+
Pod::TrimCompatibility.incompatible_warning(Pod::TrimCompatibility.missing_requirements).each do |line|
|
|
19
|
+
UI.warn(line)
|
|
21
20
|
end
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
return super
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
unless Pod::TrimCompatibility.version_verified?
|
|
25
|
+
Pod::TrimCompatibility.version_warning.each { |line| UI.warn(line) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# 只读分析放在任何状态修改之前:
|
|
29
|
+
# 这一步失败时安装状态尚未被改动,可以安全回退到完整安装。
|
|
30
|
+
# 注意必须写 ::StandardError:Pod 命名空间下存在 Pod::StandardError(CocoaPods 自己的类,
|
|
31
|
+
# 与 NameError/NoMethodError 是兄弟关系),裸写 StandardError 会解析到它,导致异常接不住。
|
|
32
|
+
begin
|
|
33
|
+
thin_pod_unused #自定义
|
|
34
|
+
rescue ::StandardError => e
|
|
35
|
+
UI.warn("未使用依赖分析失败,已降级为完整安装: #{e.class}: #{e.message}")
|
|
36
|
+
return super
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
UI.section '下载依赖...' do
|
|
40
|
+
thin_delete_pods_unused #自定义
|
|
41
|
+
install_pod_sources
|
|
42
|
+
run_podfile_pre_install_hooks
|
|
43
|
+
clean_pod_sources
|
|
25
44
|
end
|
|
26
45
|
end
|
|
27
46
|
end
|
data/lib/core/thin/local.rb
CHANGED
|
@@ -17,8 +17,12 @@ module Pod
|
|
|
17
17
|
|
|
18
18
|
yaml_dep = YamlDependencies.new
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
# 这里只读取,不生成文件:
|
|
21
|
+
# 本方法在 pod install 的裁剪阶段被调用,属于只读路径。
|
|
22
|
+
# 原实现调了 generate_yaml,会在安装过程中悄悄补出缺失的
|
|
23
|
+
# LocalDependencies.yaml / <Target>Dependencies.yaml 模板。
|
|
24
|
+
# 这些文件在 Podfile 评估阶段(install_yaml_dep -> analysis_yaml_dep)已经生成过,
|
|
25
|
+
# 文件不存在就说明确实没有本地依赖,返回空列表即可。
|
|
22
26
|
local_yaml = yaml_dep.local_yaml_name
|
|
23
27
|
yaml_dep.read_yaml_deps_file(local_yaml)
|
|
24
28
|
|
data/lib/core/thin/shell.rb
CHANGED
|
@@ -27,6 +27,9 @@ module Pod
|
|
|
27
27
|
# @return [String]
|
|
28
28
|
def deps_shell
|
|
29
29
|
init_project_shell
|
|
30
|
+
# 找不到壳工程时返回空列表:裁剪会把“没有引用记录”当成未使用,
|
|
31
|
+
# 这里如果直接解引用 nil 会中断整个 pod install
|
|
32
|
+
return reference_pods unless project_shell
|
|
30
33
|
|
|
31
34
|
reference_code_file(project_shell.root_object.main_group)
|
|
32
35
|
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
require 'cocoapods'
|
|
2
|
+
require 'pathname'
|
|
3
|
+
|
|
4
|
+
module CocoaPods
|
|
5
|
+
module Yaml
|
|
6
|
+
# 工程目录守卫。
|
|
7
|
+
#
|
|
8
|
+
# 插件的依赖yaml、壳工程、Pods目录全部按“相对当前工作目录”的路径读写,
|
|
9
|
+
# 而 CocoaPods 自身不会 chdir 到 installation_root:
|
|
10
|
+
# pod install --project-directory=/path 从别的目录执行时,进程 cwd 仍是调用目录,
|
|
11
|
+
# 所有相对路径都会指向错误位置(解析阶段直接 nil 崩溃、或把文件写到错误工程)。
|
|
12
|
+
#
|
|
13
|
+
# 与其在每个路径上做兼容,不如在入口直接拦下并给出明确指引。
|
|
14
|
+
module WorkingDirectory
|
|
15
|
+
class << self
|
|
16
|
+
# 当前目录是否就是工程根目录
|
|
17
|
+
def at_project_root?
|
|
18
|
+
same_path?(Dir.pwd, Pod::Config.instance.installation_root)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# 校验当前目录,不是工程根目录时直接抛出
|
|
22
|
+
# @raise [Pod::Informative]
|
|
23
|
+
def validate!
|
|
24
|
+
return if at_project_root?
|
|
25
|
+
|
|
26
|
+
root = Pod::Config.instance.installation_root
|
|
27
|
+
raise Pod::Informative, <<~MSG
|
|
28
|
+
cocoapods-yaml 只能在工程根目录执行
|
|
29
|
+
当前目录: #{Dir.pwd}
|
|
30
|
+
工程目录: #{root}
|
|
31
|
+
Podfile、<Target>Dependencies.yaml 与 Pods 均按相对路径读写,
|
|
32
|
+
从其他目录执行(包括 --project-directory)会读写到错误的工程。
|
|
33
|
+
请先切换目录再执行:
|
|
34
|
+
cd #{root} && pod install
|
|
35
|
+
MSG
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
# 用 realpath 比较,兼容软链接路径与 NFD/NFC 差异
|
|
41
|
+
def same_path?(lhs, rhs)
|
|
42
|
+
return false if rhs.nil?
|
|
43
|
+
|
|
44
|
+
File.realpath(lhs.to_s) == File.realpath(rhs.to_s)
|
|
45
|
+
rescue StandardError
|
|
46
|
+
lhs.to_s == rhs.to_s
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|