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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82dbb70f4e4aed183d28e3155d99048f2fb4fab56b2c6937d705c0ff2254216f
4
- data.tar.gz: 9dd087ceaa7cb278628592b8941965cee7e8708c543b5a6f889ad074d06a8f85
3
+ metadata.gz: 7ef51da620d2fc2e82d500aab957d5a6118bc6ae49b36db969260672ab34638f
4
+ data.tar.gz: 5680ed96a73bf643083a270a09b8ebda6c322aa2c62727f11a3ff100e397fe55
5
5
  SHA512:
6
- metadata.gz: 3559c6d25653e343f7c6776fdd0f7a7cd047ea24482f4171759e272a9cedb5cc6c922d8a9414e9971a7b3b0946c5ddb40e827e49545764f5a4bc176e3d1cbd6b
7
- data.tar.gz: 05acafb2d925b7a0c9d785ceb623fe8d4a24264d2b3d1c090170db54d11061ae8c1e7567de6867e792493febcf58ed93460ee704ca067970ea0fcc5ff62a343b
6
+ metadata.gz: a33b7a6cd3ea13b70539ac715e9e1f5ac84273155eebe336b7d1141ee998c5931310cca2275d6ea6ab56fe7309092288e0a0f0bfec39a5e81836f5907cf8196f
7
+ data.tar.gz: bfb6813dd25f84a75fe3a8db6dd584a469f738db2165ba95e9945b20e135d4e5de413f6087628e7638e13ccd09006e971050926f494dfc616d0f203a4388e645
data/CHANGELOG.md CHANGED
@@ -1,5 +1,38 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.1.0 - 2026-12-04
4
+
5
+ ### 新增
6
+
7
+ - `pod yaml lint`:离线校验依赖 yaml(顶层字段、引用方式互斥、SOURCES 完整性、文件内重复组件、缺失与孤儿文件),不执行 install、不访问私有源,可直接用于 CI 前置检查
8
+ - `pod yaml status`:对比 YAML 声明与 `Podfile.lock` 的实际安装差异(声明未安装 / 锁中未声明 / 版本不一致),加 `--strict` 时以非 0 退出
9
+ - 生成 Podfile 时 `platform :ios` 取自主 App target 的 `IPHONEOS_DEPLOYMENT_TARGET`(多个 App target 取最小,读不到时用 15.0),不再写死版本号
10
+ - podspec 检查补充从 `Pods/<组件>/` 目录扫描 xcframework/framework 的兜底,二进制组件缺少 podspec 缓存时也能识别依赖
11
+ - 默认开启并发下载(线程池 20,可在 `LocalDependencies.yaml` 调整)
12
+
13
+ ### 变更
14
+
15
+ - **收紧 CocoaPods 依赖为 `>= 1.13, < 1.18`**:裁剪重写了 Installer 的安装流程,仅在该区间验证过;升级 CocoaPods 前请先回归。运行时另有接口探测,不匹配时自动降级为完整安装
16
+ - **`pod install` / `pod update` 必须在工程根目录执行**:从其他目录(包括 `--project-directory`)执行会直接报错并给出 `cd` 指引,不再读写到错误的工程
17
+ - 路径依赖仓库的 SNAPSHOT tag 自动同步,首次改动会打印变更内容、跳过方式与撤销命令;新增环境变量 `COCOAPODS_YAML_SKIP_TAG_SYNC=1` 可跳过
18
+
19
+ ### 修复
20
+
21
+ - 顶层字段拼错(如把 `PODS` 写成 `POD`)不再静默解析出 0 个依赖,改为直接报错;`PODS` 缺失或不是数组时给出显式告警
22
+ - `git` 与 `path` / `podspec` / `version` 同时配置时不再静默忽略 git,改为报错
23
+ - 布尔开关解析统一(`TRIM_UNUSED_POD` / `ENABLE_TRUNK` / `DISABLE_CHECK_PODSPEC` / `inhibit_warnings`),带引号的 `"false"` 不再被当作 true
24
+ - 裁剪增加 CocoaPods 接口探测:内部结构变化时提示并降级为完整安装,不再以 NoMethodError 中断 `pod install`
25
+ - podspec 缓存路径改用 `Pod::Config.instance.cache_root`,支持自定义缓存目录(`CP_CACHE_DIR`)
26
+ - 裁剪阶段读取本地依赖不再生成文件,只读路径无副作用
27
+ - 校验失败的日志主动 flush,避免输出被重定向或进 CI 时丢失错误信息
28
+ - Pods 工程 / 壳工程缺失时降级为空结果,不再抛 NoMethodError
29
+
30
+ ### 其他
31
+
32
+ - 新增单元测试(`rake test`,37 例)与 `Rakefile`
33
+ - 本地安装脚本支持 `(本地构建)` 版本标识,便于与线上发布版区分
34
+ - 依赖清单输出的日志层级与配色调整
35
+
3
36
  ## 1.0.0 - 2026-10-16
4
37
 
5
38
  以 YAML 文件按 target 管理 CocoaPods 依赖的插件首个正式版本,安装后由 CocoaPods 自动加载。
data/README.md CHANGED
@@ -12,12 +12,13 @@
12
12
  - **私有源管理**:自动注册私有 specs 源并屏蔽官方 CDN trunk,间接依赖优先命中二进制源
13
13
  - **内置检查**:import 写法、podspec 依赖完整性、模拟器架构兼容、三方库源码编译兼容补丁,post_install 自动执行
14
14
  - **Debug 依赖清单**:依赖清单 json 仅打入 Debug 包,便于排查组件版本
15
+ - **离线校验与安装对比**:`pod yaml lint` 不跑 install 校验字段与引用关系(CI 可用);`pod yaml status` 对比 YAML 声明与 Podfile.lock 的实际安装差异
15
16
 
16
17
  ## 接入
17
18
 
18
19
  ```ruby
19
20
  # Podfile
20
- platform :ios, '15.0'
21
+ platform :ios, '15.5'
21
22
 
22
23
  yaml_dependencies! do |target_name|
23
24
  # target_name: 当前集成的 target 名称(App/扩展),每个声明了依赖的 target 回调一次
@@ -27,7 +28,9 @@ yaml_dependencies! do |target_name|
27
28
  end
28
29
  ```
29
30
 
30
- 未接入的工程可执行 `pod yaml init` 自动生成模板 Podfile 与依赖文件(已有 Podfile 会先备份为 `Podfile.bak.时间戳` 再替换)。
31
+ 未接入的工程可执行 `pod yaml init` 自动生成模板 Podfile 与依赖文件(已有 Podfile 会先备份为 `Podfile.bak.时间戳` 再替换)。生成的 `platform :ios` 取自工程主 App target 的 `IPHONEOS_DEPLOYMENT_TARGET`(取不到时用 15.0),避免模板版本与工程实际最低版本不一致导致链接告警。
32
+
33
+ 必须在**工程根目录**执行 `pod install` / `pod update`(先 `cd` 进去)。插件按相对路径读写 Podfile、依赖 yaml 与 Pods,从其他目录执行——包括 `pod install --project-directory=/path`——会直接报错并给出正确的工程目录,不会去猜或静默读写错误的工程。
31
34
 
32
35
  ## 依赖文件
33
36
 
@@ -68,20 +71,34 @@ PODS:
68
71
  | `configurations` | 仅在指定 Build Configuration 集成,如 `[Debug]` |
69
72
  | `inhibit_warnings` | 屏蔽警告,target 文件默认 true、本地覆盖默认 false |
70
73
 
71
- `LocalDependencies.yaml` 的引用方式与字段规则相同,并额外支持顶层开关:
74
+ `LocalDependencies.yaml` 的引用方式与字段规则相同,并额外支持以下顶层开关:
75
+
76
+ | 字段 | 默认 | 说明 |
77
+ | --- | --- | --- |
78
+ | `TRIM_UNUSED_POD` | false | 裁剪未使用依赖(等价 `pod install --trim`) |
79
+ | `ENABLE_TRUNK` | false | 恢复官方 CDN trunk 源 |
80
+ | `DISABLE_CHECK_PODSPEC` | false | 关闭 podspec 依赖完整性检查 |
81
+ | `PARALLEL_POD_DOWNLOADS` | true | 并发下载 pod |
82
+ | `PARALLEL_POD_DOWNLOAD_THREAD_POOL_SIZE` | 20 | 并发下载线程数 |
83
+ | `INCREMENTAL_INSTALLATION` | false | 增量安装,只重建有变化的 pod target |
84
+ | `GENERATE_MULTIPLE_POD_PROJECTS` | false | 每个 pod 生成独立 Xcode 工程;开启增量安装时自动联动为 true |
72
85
 
73
86
  ```yaml
74
- TRIM_UNUSED_POD: false # 是否裁剪未使用依赖
75
- ENABLE_TRUNK: false # 是否恢复官方 CDN trunk 源
87
+ TRIM_UNUSED_POD: false
88
+ ENABLE_TRUNK: false
76
89
  PODS: []
77
90
  ```
78
91
 
92
+ > 开关取值兼容 `true/false`、带引号的 `"false"`,以及 `yes/no`、`on/off`、`1/0`。优先级:Podfile 中显式写的 `install!` > 本文件 > 插件默认值。
93
+
79
94
  ## 本地覆盖与联调
80
95
 
81
96
  - **同名覆盖**:LocalDependencies 中按 pod 名声明,覆盖任意 `<Target>Dependencies.yaml` 中的同名组件(通常写 `path` 做本地联调)
82
97
  - **全新组件**:target 依赖文件都未声明的组件写入 LocalDependencies 后自动加入主 App target(不会被裁剪);App 扩展 target 不自动注入,需在对应依赖文件显式声明
83
98
  - path 引用的本地仓库自动开启 SNAPSHOT tag 强制同步(`git fetch/pull` 自动前移被删除重建的 tag)
84
99
 
100
+ > 该同步会修改本地组件仓库的 `.git/config`(`remote.origin.fetch` 追加 `+refs/tags/*:refs/tags/*`,`remote.origin.tagOpt` 设为 `--no-tags`),只影响本机、不影响远端。首次修改时会打印变更内容与撤销命令;设置环境变量 `COCOAPODS_YAML_SKIP_TAG_SYNC=1` 可跳过。
101
+
85
102
  ## 未使用依赖裁剪
86
103
 
87
104
  开发模式下可开启,移除未被源码引用的依赖库以加速编译:
@@ -94,6 +111,8 @@ pod update --trim # update 同样支持
94
111
 
95
112
  或在 `LocalDependencies.yaml` 中设置 `TRIM_UNUSED_POD: true` 长期开启。
96
113
 
114
+ > 裁剪重写了 CocoaPods 安装流程的内部接口,插件只在 `>= 1.13, < 1.18` 验证过(gemspec 已按此收紧)。运行时还会做一次能力探测:升级 CocoaPods 后若接口不匹配,会自动降级为完整安装并提示,**不会中断 `pod install`**;版本超出验证区间但接口齐全时仅提示、继续裁剪。
115
+
97
116
  ## 私有源管理
98
117
 
99
118
  插件按各 target 依赖文件 `SOURCES` 的声明顺序注册 Podfile 顶层 source(二进制源优先,间接依赖优先命中二进制产物),并屏蔽官方 CDN trunk——trunk 既会因网络问题导致 `pod install` 失败,也会与私有源同名 podspec 产生多版本告警。确需 trunk 时在 `LocalDependencies.yaml` 设置 `ENABLE_TRUNK: true`。
@@ -115,14 +134,31 @@ pod update --trim # update 同样支持
115
134
  pod yaml init # 初始化/备份替换 Podfile 与依赖文件模板
116
135
  pod yaml relation [组件名] # 从 Podfile.lock 查询正/反向依赖
117
136
  # --forward 只看正向;--sort 按依赖数量排序
137
+ pod yaml lint # 离线校验依赖yaml(字段/引用方式/重复/缺失/孤儿文件)
138
+ # 不执行install、不访问私有源,可直接用于CI前置检查
139
+ pod yaml status # 对比YAML声明与Podfile.lock的实际安装情况
140
+ # --strict 存在差异时以非0退出(供CI使用)
118
141
  ```
119
142
 
143
+ ### 本地调试安装
144
+
145
+ 本机改动代码后执行 `ruby scripts/install_local.rb`,会构建 gem 并覆盖安装到当前 Ruby 环境,之后在壳工程 `pod install` 即可验证。
146
+
147
+ 此时版本号会显示为 `1.0.0 (本地构建)`(`pod yaml relation` 等处同样),便于区分"本地改的代码"和"线上拉的发布版"——两者版本号可能一样。该标识来自安装目录里的标记文件,从 rubygems 重新安装后自动消失。
148
+
149
+ ## 顶层字段校验
150
+
151
+ 依赖文件顶层只允许 `SOURCES`、`PODS`;`LocalDependencies.yaml` 额外允许 `TRIM_UNUSED_POD`、`ENABLE_TRUNK`、`DISABLE_CHECK_PODSPEC`、`PARALLEL_POD_DOWNLOADS`、`PARALLEL_POD_DOWNLOAD_THREAD_POOL_SIZE`、`INCREMENTAL_INSTALLATION`、`GENERATE_MULTIPLE_POD_PROJECTS`。
152
+
153
+ 出现未知顶层字段会直接报错终止(例如把 `PODS` 误写成 `POD`)——这类笔误会让该 target 解析出 0 个依赖,`pod install` 会把已安装的组件全部卸载且不报错。`PODS` 字段缺失或不是数组时也会给出显式告警。
154
+
120
155
  ## 环境变量
121
156
 
122
157
  | 变量 | 说明 |
123
158
  | --- | --- |
124
159
  | `COCOAPODS_YAML_VERBOSE` | 非空时输出调试细节(等效 `pod install --verbose`) |
125
160
  | `COCOAPODS_YAML_SKIP_SOURCE_PATCH` | 非空时跳过全部三方库源码兼容补丁 |
161
+ | `COCOAPODS_YAML_SKIP_TAG_SYNC` | 非空时跳过路径仓库的 SNAPSHOT tag 自动同步,不修改本地仓库 git 配置 |
126
162
 
127
163
  ## License
128
164
 
@@ -5,6 +5,7 @@ require_relative '../version'
5
5
  core_path = Core::CoreHelper.core_path
6
6
 
7
7
  require "#{core_path}/logger"
8
+ require "#{core_path}/yaml/working_directory"
8
9
  require "#{core_path}/yaml/yaml_dependencies"
9
10
 
10
11
  # 裁剪未使用依赖的下载流程补丁,是否裁剪由TRIM_UNUSED_POD/pod install/update --trim判定
@@ -15,10 +16,14 @@ module Pod
15
16
  # DSL入口: 在Podfile中通过 yaml_dependencies! 接入
16
17
  # block的参数为当前集成的target名(App/扩展),可按target定制 use_frameworks! 等配置
17
18
  def yaml_dependencies!
19
+ # 依赖文件、壳工程、Pods 目录都按相对路径访问,当前目录不是工程根目录时
20
+ # 直接报错退出,而不是读到/写到错误的工程
21
+ CocoaPods::Yaml::WorkingDirectory.validate!
22
+
18
23
  plugin('cocoapods-yaml')
19
24
 
20
25
  yaml_dep = Pod::YamlDependencies.new
21
- CocoaPods::Yaml.log.info("cocoapods-yaml版本:#{CocoaPods::Yaml::VERSION}")
26
+ CocoaPods::Yaml.log.info("cocoapods-yaml版本:#{CocoaPods::Yaml.version_display}")
22
27
  yaml_dep.install_yaml_dep(self) do |target_name|
23
28
  yield(target_name) if block_given?
24
29
  end
@@ -9,6 +9,8 @@ require_relative 'cocoapods/yaml'
9
9
 
10
10
  require "#{Core::CoreHelper.core_path}/command/yaml_init"
11
11
  require "#{Core::CoreHelper.core_path}/command/yaml_relation"
12
+ require "#{Core::CoreHelper.core_path}/command/yaml_lint"
13
+ require "#{Core::CoreHelper.core_path}/command/yaml_status"
12
14
  require "#{Core::CoreHelper.core_path}/command/trim_mode"
13
15
  require "#{Core::CoreHelper.core_path}/check/project"
14
16
  require "#{Core::CoreHelper.core_path}/check/podspec"
@@ -19,15 +21,21 @@ require "#{Core::CoreHelper.core_path}/indirect/indirect"
19
21
  require "#{Core::CoreHelper.core_path}/reference/unused"
20
22
  require "#{Core::CoreHelper.core_path}/check/import"
21
23
 
22
- # pod install/update 在工程目录但缺少Podfile时自动生成模板(零风险初始化);
23
- # 已有Podfile但未接入时仅提示 pod yaml init,不自动替换
24
- if ARGV.include?('install') || ARGV.include?('update')
25
- CocoaPods::Yaml::PodfileSetup.auto_on_install
26
- end
24
+ # pod install/update 的自动初始化(无Podfile时生成模板)已改为命令执行阶段的钩子,
25
+ # 见 core/setup/podfile_setup.rb 的 YamlPodfileSetupHook:
26
+ # 插件加载时 --project-directory 尚未解析,按进程当前目录判断会写错位置。
27
27
 
28
28
  module CocoapodsPlugin
29
29
 
30
- Pod::HooksManager.register('cocoapods-yaml', :post_install) do |context|
30
+ # post_install 各检查/注入步骤都按相对路径读写工程文件(Pods/、依赖yaml、.gitignore)。
31
+ # 当前目录不是工程根目录时直接跳过并提示——这里不能用异常中断:
32
+ # 未接入本插件的工程同样会走到这个 hook,不该因为它的目录用法让别人的 pod install 失败。
33
+ def self.run_post_install(context)
34
+ unless CocoaPods::Yaml::WorkingDirectory.at_project_root?
35
+ CocoaPods::Yaml.log.warn("当前目录不是工程根目录(工程目录: #{Pod::Config.instance.installation_root})," \
36
+ '已跳过 cocoapods-yaml 的 post_install 检查;请 cd 到工程根目录后重新执行')
37
+ return
38
+ end
31
39
 
32
40
  project = Xcodeproj::XCodeProject.new
33
41
 
@@ -58,7 +66,10 @@ module CocoapodsPlugin
58
66
 
59
67
  # 裁剪未使用依赖的开关提示
60
68
  Unused::UnusedManager.check_unused
69
+ end
61
70
 
71
+ Pod::HooksManager.register('cocoapods-yaml', :post_install) do |context|
72
+ run_post_install(context)
62
73
  end
63
74
 
64
75
  end
@@ -99,6 +99,10 @@ module Xcodeproj
99
99
 
100
100
  # 展开模块
101
101
  def expand_module
102
+ # Pods 工程缺失(尚未安装 / cwd 不在工程根目录)时不能直接解引用 nil,
103
+ # 原实现会在 post_install 阶段抛 NoMethodError 把整个安装流程中断
104
+ return unless pod_project
105
+
102
106
  self.expand_with_module(pod_project.main_group, nil)
103
107
  end
104
108
 
@@ -294,6 +298,15 @@ module Xcodeproj
294
298
  end
295
299
  end
296
300
 
301
+ # CocoaPods 缓存根目录下的 Pods/Specs。
302
+ # 必须跟随 CocoaPods 自身的配置(CP_CACHE_DIR / CP_HOME_DIR),
303
+ # 写死 ~/Library/Caches/CocoaPods 会让自定义缓存目录的机器上所有 podspec 定位失效,
304
+ # 表现为依赖完整性检查静默跳过。
305
+ # @return [Pathname]
306
+ def pods_specs_cache_root
307
+ Pod::Config.instance.cache_root.join('Pods', 'Specs')
308
+ end
309
+
297
310
  # 读取某个模块的podspec路径
298
311
  def get_podspec_path(module_name)
299
312
  check_options = lock_content["CHECKOUT OPTIONS"]
@@ -302,7 +315,7 @@ module Xcodeproj
302
315
  request = Pod::Downloader::Request.new(released:false, name:module_name, params:check_option)
303
316
  slug_opts = {}
304
317
  cache_path = request.slug(**slug_opts)
305
- return "~/Library/Caches/CocoaPods/Pods/Specs/#{cache_path}.podspec.json"
318
+ return pods_specs_cache_root.join("#{cache_path}.podspec.json")
306
319
  end
307
320
 
308
321
  # :path: localPods/XX
@@ -330,8 +343,7 @@ module Xcodeproj
330
343
  version = @version_map[module_name]
331
344
  if version
332
345
  podspec_json_name = "#{version}-#{md5_to5}.podspec.json"
333
- path = "~/Library/Caches/CocoaPods/Pods/Specs/Release/#{module_name}/#{podspec_json_name}"
334
- return Pathname(path).expand_path
346
+ return pods_specs_cache_root.join('Release', module_name, podspec_json_name)
335
347
  end
336
348
 
337
349
  nil
@@ -343,27 +355,51 @@ module Xcodeproj
343
355
  @module_items.each_value do |module_item|
344
356
  module_item.frameworks << module_item.module_name
345
357
 
346
- unless module_item.podspec_path
347
- CocoaPods::Yaml.log.warn("读取#{module_item.module_name}的podspec路径为空,请发送Podfile.lock文件联系开发者修复")
348
- next
349
- end
358
+ if module_item.podspec_path
359
+ podspec_content = Util.get_podspec_content(module_item.podspec_path)
350
360
 
351
- podspec_content = Util.get_podspec_content(module_item.podspec_path)
361
+ if podspec_content
362
+ update_dep_frameworks(module_item.frameworks, podspec_content)
352
363
 
353
- unless podspec_content
354
- next
364
+ subspec_content = podspec_content["subspecs"]
365
+ if subspec_content and subspec_content.class == Array
366
+ subspec_content.each { |json_content|
367
+ update_dep_frameworks(module_item.frameworks, json_content)
368
+ }
369
+ end
370
+ end
355
371
  end
356
372
 
357
- update_dep_frameworks(module_item.frameworks, podspec_content)
373
+ # 兜底:从 Pods/<module_name>/ 目录扫描 xcframework/framework 文件名。
374
+ # 二进制 pod(如 WechatOpenSDK-XCFramework)的 podspec.json 缓存可能不存在,
375
+ # 导致上面的 podspec 解析取不到 vendored_frameworks,这里从磁盘扫描保证 framework 名被正确识别。
376
+ scan_vendor_frameworks_from_pods_dir(module_item.frameworks, module_item.module_name)
377
+ end
358
378
 
359
- subspec_content = podspec_content["subspecs"]
360
- if subspec_content and subspec_content.class == Array
361
- subspec_content.each { |json_content|
362
- update_dep_frameworks(module_item.frameworks, json_content)
363
- }
379
+ end
380
+
381
+ # 扫描 Pods/<module_name>/ 目录下的 .xcframework 和 .framework 文件名,补充到 frameworks 数组
382
+ # @param item_frameworks [Array<String>] 待补充的 frameworks 数组
383
+ # @param module_name [String] 模块名
384
+ def scan_vendor_frameworks_from_pods_dir(item_frameworks, module_name)
385
+ return unless pod_project
386
+
387
+ pods_dir = pod_project.root_object.main_group&.real_path
388
+ return unless pods_dir
389
+
390
+ module_dir = File.join(pods_dir, module_name)
391
+ return unless File.directory?(module_dir)
392
+
393
+ Dir.entries(module_dir).each do |entry|
394
+ next if entry.start_with?(".")
395
+
396
+ if entry.end_with?(".xcframework") || entry.end_with?(".framework")
397
+ framework_name = entry.gsub(".xcframework", "").gsub(".framework", "")
398
+ unless item_frameworks.include?(framework_name)
399
+ item_frameworks << framework_name
400
+ end
364
401
  end
365
402
  end
366
-
367
403
  end
368
404
 
369
405
  def update_dep_frameworks(item_frameworks, json_content)
@@ -52,8 +52,9 @@ module XCProject
52
52
  def get_shell_targets
53
53
  results = []
54
54
  project = self.get_shell_project
55
- targets = project.root_object.targets
56
- targets.each do |target|
55
+ return results unless project
56
+
57
+ project.root_object.targets.each do |target|
57
58
  results << target.name
58
59
  end
59
60
  results
@@ -62,6 +63,9 @@ module XCProject
62
63
  #获取主工程全部顶层target(含App与扩展,是否可通过yaml集成由调用方按product_type过滤)
63
64
  def get_app_targets
64
65
  project = self.get_shell_project
66
+ # 找不到 .xcodeproj 时返回空数组,避免调用方 project.root_object 撞 nil
67
+ return [] unless project
68
+
65
69
  project.root_object.targets
66
70
  end
67
71
 
@@ -0,0 +1,116 @@
1
+ require_relative '../yaml/yaml_dependencies'
2
+ require_relative '../my_constants'
3
+
4
+ module YamlCheck
5
+ # 依赖yaml文件的离线校验(不执行 pod install,不访问私有源)
6
+ #
7
+ # 复用安装流程的解析与字段校验逻辑:YamlDependencies 开启 lint 模式后,
8
+ # 校验失败不再 exit! 整个进程,而是抛 ValidationError,由本类逐文件/逐条目收敛。
9
+ class Linter
10
+ def initialize
11
+ @log = CocoaPods::Yaml.log
12
+ @deps = Pod::YamlDependencies.new
13
+ @deps.enable_lint_mode
14
+ @checked_files = []
15
+ @failed_files = []
16
+ @missing_target_files = []
17
+ @orphan_files = []
18
+ end
19
+
20
+ # @return [Boolean] 是否全部通过
21
+ def run
22
+ files = discover_files
23
+
24
+ if files.empty? && @missing_target_files.empty?
25
+ @log.warn('未找到任何依赖yaml文件,请确认在iOS工程根目录执行(或先执行 pod yaml init)')
26
+ return false
27
+ end
28
+
29
+ @log.step('校验依赖文件')
30
+ files.each { |file| lint_file(file) }
31
+
32
+ report
33
+ @failed_files.empty?
34
+ end
35
+
36
+ private
37
+
38
+ # 待校验文件:各target依赖文件 + LocalDependencies + 工程内其它 *Dependencies.yaml(孤儿文件)
39
+ def discover_files
40
+ target_files = target_yaml_files
41
+ local_file = MyConstants::LOCAL_DEPENDENCIES_YAML
42
+ files = []
43
+
44
+ target_files.each do |file|
45
+ if File.exist?(file)
46
+ files << file
47
+ else
48
+ @missing_target_files << file
49
+ end
50
+ end
51
+
52
+ # LocalDependencies.yaml 同样以 Dependencies.yaml 结尾,必须显式排除,
53
+ # 否则会被当成"未匹配到target的孤儿文件"重复校验
54
+ Dir.glob("*#{MyConstants::DEPENDENCIES_YAML_SUFFIX}").sort.each do |file|
55
+ next if file == local_file
56
+ next if files.include?(file)
57
+ files << file
58
+ # 有工程时才判断孤儿;纯 yaml 目录下无法判断归属,不误报
59
+ @orphan_files << file unless target_files.empty? || target_files.include?(file)
60
+ end
61
+
62
+ files << local_file if File.exist?(local_file)
63
+
64
+ files
65
+ end
66
+
67
+ # 由工程target推导出的依赖文件名;无 .xcodeproj 时返回空数组
68
+ # @return [Array<String>]
69
+ def target_yaml_files
70
+ @deps.init_main_targets
71
+ @deps.targets.map { |target| @deps.target_yaml_name(target.name) }
72
+ rescue StandardError
73
+ []
74
+ end
75
+
76
+ def lint_file(file)
77
+ @checked_files << file
78
+ # 条目级校验失败在 lint 模式里只跳过该条(不中断整个文件),
79
+ # 因此要靠累计错误数变化判断这个文件是否通过
80
+ errors_before = @deps.lint_errors.size
81
+
82
+ @deps.read_yaml_deps_file(file)
83
+
84
+ error_count = @deps.lint_errors.size - errors_before
85
+ if error_count.positive?
86
+ @failed_files << file
87
+ @log.info("❌ #{file}(#{error_count}处错误)")
88
+ else
89
+ deps = @deps.yaml_deps[file]
90
+ @log.info("✅ #{file}#{deps.is_a?(Hash) ? "(#{deps.size}个依赖)" : '(0个依赖)'}")
91
+ end
92
+ rescue Pod::YamlDependencies::ValidationError
93
+ @failed_files << file
94
+ @log.info("❌ #{file}")
95
+ end
96
+
97
+ def report
98
+ @log.info('')
99
+
100
+ @missing_target_files.each do |file|
101
+ @log.warn("缺少依赖文件: #{file}(安装时会自动生成空模板,若该target应有依赖请补充)")
102
+ end
103
+
104
+ @orphan_files.each do |file|
105
+ @log.warn("未匹配到任何target的依赖文件: #{file}(target改名或删除后的残留,建议删除)")
106
+ end
107
+
108
+ if @failed_files.empty?
109
+ @log.success("校验通过: 共#{@checked_files.size}个文件")
110
+ else
111
+ @log.error("校验失败: #{@checked_files.size}个文件中有#{@failed_files.size}个存在错误")
112
+ @failed_files.each { |file| @log.error(" - #{file}") }
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,175 @@
1
+ require 'yaml'
2
+ require_relative '../yaml/yaml_dependencies'
3
+ require_relative '../my_constants'
4
+
5
+ module YamlCheck
6
+ # YAML 声明 与 Podfile.lock 实际解析结果的对比。
7
+ #
8
+ # 只依赖 Podfile.lock(安装产物),不访问网络与私有源,可在CI或本地随时执行。
9
+ # 能发现三类问题:
10
+ # 1. 声明了但锁文件里没有(漏装/组件名写错/尚未 pod install)
11
+ # 2. 锁文件的直接依赖里有、但YAML未声明(Podfile 被手改,或YAML删了组件没重新install)
12
+ # 3. YAML写死的版本与锁文件解析出的版本不一致(发版后忘记更新yaml)
13
+ class Status
14
+ def initialize
15
+ @log = CocoaPods::Yaml.log
16
+ end
17
+
18
+ # @return [Symbol] :ok 一致 / :diff 存在差异 / :error 无法完成对比
19
+ def run
20
+ unless Dir.glob('*.xcodeproj').any?
21
+ @log.error('当前目录未找到 .xcodeproj,请在iOS工程根目录执行')
22
+ return :error
23
+ end
24
+
25
+ unless File.exist?(MyConstants::PODFILE_LOCK)
26
+ @log.error("未找到#{MyConstants::PODFILE_LOCK},请先执行 pod install")
27
+ return :error
28
+ end
29
+
30
+ records = load_records
31
+ return :error if records.nil?
32
+
33
+ lock = load_lock
34
+ return :error if lock.nil?
35
+
36
+ resolved = parse_pods(lock['PODS'])
37
+ direct = parse_dependencies(lock['DEPENDENCIES'])
38
+ declared = collect_declared(records)
39
+
40
+ report(records, declared, resolved, direct) ? :ok : :diff
41
+ end
42
+
43
+ private
44
+
45
+ def load_records
46
+ deps = Pod::YamlDependencies.new
47
+ deps.enable_lint_mode
48
+ deps.build_dependency_records
49
+ rescue Pod::YamlDependencies::ValidationError
50
+ @log.error('依赖yaml文件存在错误,请先执行 pod yaml lint 修正')
51
+ nil
52
+ end
53
+
54
+ def load_lock
55
+ YAML.load_file(MyConstants::PODFILE_LOCK, aliases: true)
56
+ rescue StandardError => e
57
+ @log.error("#{MyConstants::PODFILE_LOCK} 解析失败: #{e.message}")
58
+ nil
59
+ end
60
+
61
+ # @return {String => Hash} key 为组件根名
62
+ def collect_declared(records)
63
+ declared = {}
64
+ records.each do |target, items|
65
+ items.each do |record|
66
+ root = record['pod'].to_s.split('/').first
67
+ next if root.nil? || root.empty?
68
+
69
+ entry = declared[root] ||= { 'version' => nil, 'local' => false, 'targets' => [] }
70
+ entry['targets'] << target unless entry['targets'].include?(target)
71
+ entry['version'] ||= record['version']
72
+ entry['local'] ||= record['local'] == true
73
+ end
74
+ end
75
+ declared
76
+ end
77
+
78
+ # "AFNetworking (4.0.1)" => ["AFNetworking", "4.0.1"]
79
+ # "Foo (from `../Foo`)" => ["Foo", nil]
80
+ def split_locked(entry)
81
+ text = entry.to_s
82
+ name = text.split(' (').first.to_s.strip
83
+ version = text[/\(([^)]*)\)/, 1]
84
+ version = nil if version.to_s.include?('from') || version.to_s.strip.empty?
85
+ [name, version]
86
+ end
87
+
88
+ # PODS 段(含子组件) => {组件根名 => 版本}
89
+ def parse_pods(pods)
90
+ result = {}
91
+ Array(pods).each do |entry|
92
+ key = entry.is_a?(Hash) ? entry.keys.first : entry
93
+ name, version = split_locked(key)
94
+ next if name.empty?
95
+ root = name.split('/').first
96
+ result[root] = version if result[root].nil?
97
+ end
98
+ result
99
+ end
100
+
101
+ # DEPENDENCIES 段(直接依赖) => {组件根名 => true}
102
+ def parse_dependencies(dependencies)
103
+ result = {}
104
+ Array(dependencies).each do |entry|
105
+ name, = split_locked(entry)
106
+ next if name.empty?
107
+ result[name.split('/').first] = true
108
+ end
109
+ result
110
+ end
111
+
112
+ def report(records, declared, resolved, direct)
113
+ @log.step('YAML 声明 vs Podfile.lock')
114
+
115
+ records.each do |target, items|
116
+ @log.info("-> #{target}: 声明#{items.size}个组件")
117
+ end
118
+ @log.info("-> Podfile.lock: 解析出#{resolved.size}个组件,其中直接依赖#{direct.size}个")
119
+
120
+ issue_count = 0
121
+
122
+ not_installed = declared.keys.reject { |root| resolved.key?(root) }.sort
123
+ unless not_installed.empty?
124
+ issue_count += 1
125
+ @log.warn("声明了但锁文件中不存在 (#{not_installed.size})")
126
+ not_installed.each do |root|
127
+ entry = declared[root]
128
+ extra = entry['local'] ? ' [本地覆盖]' : ''
129
+ @log.info(" #{root}#{extra} (来自 #{entry['targets'].join('、')})")
130
+ end
131
+ end
132
+
133
+ undeclared = direct.keys.reject { |root| declared.key?(root) }.sort
134
+ unless undeclared.empty?
135
+ issue_count += 1
136
+ @log.warn("Podfile.lock 直接依赖中存在但YAML未声明 (#{undeclared.size})")
137
+ undeclared.each { |root| @log.info(" #{root}") }
138
+ @log.info(' 可能是直接改了Podfile,或从YAML删除组件后未重新 pod install')
139
+ end
140
+
141
+ mismatches = collect_version_mismatches(declared, resolved)
142
+ unless mismatches.empty?
143
+ issue_count += 1
144
+ @log.warn("YAML版本与锁文件不一致 (#{mismatches.size})")
145
+ mismatches.each { |root, yaml_version, lock_version| @log.info(" #{root} yaml: #{yaml_version} lock: #{lock_version}") }
146
+ end
147
+
148
+ @log.info('')
149
+ if issue_count.zero?
150
+ @log.success('声明与安装一致')
151
+ return true
152
+ end
153
+
154
+ @log.info('提示: "声明了但锁文件中不存在"通常是尚未 pod install,或组件名写错。')
155
+ @log.info(' TRIM_UNUSED_POD 裁剪不改变 Podfile.lock,裁剪结果需查看 pod install 日志。')
156
+ false
157
+ end
158
+
159
+ # 只比较写死的版本号;带 ~>/>= 等运算符的声明交给 CocoaPods 解析,不判为不一致
160
+ def collect_version_mismatches(declared, resolved)
161
+ mismatches = []
162
+ declared.each do |root, entry|
163
+ yaml_version = entry['version'].to_s.strip
164
+ next if yaml_version.empty?
165
+ next if yaml_version =~ /[~<>=]/
166
+
167
+ lock_version = resolved[root]
168
+ next if lock_version.nil? || lock_version == yaml_version
169
+
170
+ mismatches << [root, yaml_version, lock_version]
171
+ end
172
+ mismatches.sort_by(&:first)
173
+ end
174
+ end
175
+ end