Skip to content

Commit 6f1e0a7

Browse files
committed
Update reamde for CocoaPods usage
1 parent 3fde811 commit 6f1e0a7

2 files changed

Lines changed: 72 additions & 6 deletions

File tree

README.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,51 @@ let package = Package(
9393
<details>
9494
<summary>CocoaPods</summary>
9595
</br>
96-
<p>Since CocoaPods doesn't directly support Swift Macro, the macro implementation can be compiled into binary for use. The integration method is as follows, requiring <code>s.pod_target_xcconfig</code> to load the binary plugin of macro implementation:</p>
96+
<p>Since CocoaPods doesn't directly support Swift Macro, the macro implementation is compiled into a prebuilt binary plugin. ReerCodable automatically downloads the plugin during build. There are two integration scenarios:</p>
97+
98+
<h4>1. Use in Main App Only</h4>
99+
<p>If you only use <code>@Codable</code> in your main app (not inside other Pods), simply add to your Podfile:</p>
100+
<pre><code class="ruby language-ruby">
101+
pod 'ReerCodable', '1.7.1'
102+
</code></pre>
103+
104+
<h4>2. Use in a Pod Component</h4>
105+
<p>If your Pod needs to use <code>@Codable</code> and other ReerCodable macros, you <strong>must</strong> add <code>pod_target_xcconfig</code> to load the macro plugin binary, because CocoaPods does not propagate build settings from dependencies to dependent Pods:</p>
97106
<pre><code class="ruby language-ruby">
98107
Pod::Spec.new do |s|
99108
s.name = 'YourPod'
100109
s.dependency 'ReerCodable', '1.7.1'
101-
# Copy the following config to your pod
110+
# Required: load the ReerCodable macro plugin
102111
s.pod_target_xcconfig = {
103-
'OTHER_SWIFT_FLAGS' => '-Xfrontend -load-plugin-executable -Xfrontend $(PODS_BUILD_DIR)/ReerCodable/release/ReerCodableMacros#ReerCodableMacros'
112+
'OTHER_SWIFT_FLAGS' => '-Xfrontend -load-plugin-executable -Xfrontend ${PODS_BUILD_DIR}/ReerCodable/MacroPlugin/ReerCodableMacros#ReerCodableMacros'
104113
}
105114
end
106115
</code></pre>
107116

117+
<h4>3. Recommended: Auto-configure via Podfile (for multiple Pods)</h4>
118+
<p>If you have many Pods depending on ReerCodable, add this <code>post_install</code> hook to your Podfile to automatically inject the macro plugin configuration into all dependent Pods:</p>
119+
<pre><code class="ruby language-ruby">
120+
post_install do |installer|
121+
macro_flag = '-Xfrontend -load-plugin-executable -Xfrontend ${PODS_BUILD_DIR}/ReerCodable/MacroPlugin/ReerCodableMacros#ReerCodableMacros'
122+
reer_codable_dependents = Set.new
123+
installer.pod_targets.each do |pod_target|
124+
next if pod_target.name == 'ReerCodable'
125+
has_dep = pod_target.dependent_targets.any? { |dep| dep.name == 'ReerCodable' }
126+
reer_codable_dependents.add(pod_target.name) if has_dep
127+
end
128+
installer.pods_project.targets.each do |target|
129+
target.build_configurations.each do |config|
130+
if reer_codable_dependents.include?(target.name)
131+
flags = config.build_settings['OTHER_SWIFT_FLAGS'] || '$(inherited)'
132+
unless flags.include?('ReerCodableMacros')
133+
config.build_settings['OTHER_SWIFT_FLAGS'] = "#{flags} #{macro_flag}"
134+
end
135+
end
136+
end
137+
end
138+
end
139+
</code></pre>
140+
108141
<p><strong>⚠️ Important:</strong> If you encounter <code>rsync</code> permission errors with Xcode 14+, disable User Script Sandboxing:</p>
109142
<p>In your project's <strong>Build Settings</strong>, search for <code>User Script Sandboxing</code> and set <code>ENABLE_USER_SCRIPT_SANDBOXING</code> to <code>No</code>. This resolves CocoaPods script execution issues caused by Xcode's stricter sandbox restrictions.</p>
110143

README_CN.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,51 @@ let package = Package(
8787
<details>
8888
<summary>CocoaPods</summary>
8989
</br>
90-
<p>由于 CocoaPods 不支持直接使用 Swift Macro, 可以将宏实现编译为二进制提供使用, 接入方式如下, 需要设置<code>s.pod_target_xcconfig</code>来加载宏实现的二进制插件:</p>
90+
<p>由于 CocoaPods 不直接支持 Swift Macro,宏实现会编译为预构建的二进制插件,ReerCodable 在构建时自动下载。有以下几种接入场景:</p>
91+
92+
<h4>1. 仅在主工程中使用</h4>
93+
<p>如果只在主 App 中使用 <code>@Codable</code>(不在其他 Pod 组件中使用),只需在 Podfile 中添加:</p>
94+
<pre><code class="ruby language-ruby">
95+
pod 'ReerCodable', '1.7.1'
96+
</code></pre>
97+
98+
<h4>2. 在 Pod 组件中使用</h4>
99+
<p>如果你的 Pod 需要使用 <code>@Codable</code> 等 ReerCodable 宏,<strong>必须</strong>在你的 podspec 中添加 <code>pod_target_xcconfig</code> 来加载宏插件二进制。这是因为 CocoaPods 不会将依赖 Pod 的构建配置自动传递给依赖方:</p>
91100
<pre><code class="ruby language-ruby">
92101
Pod::Spec.new do |s|
93102
s.name = 'YourPod'
94103
s.dependency 'ReerCodable', '1.7.1'
95-
# 复制以下 config 到你的 pod
104+
# 必须添加:加载 ReerCodable 宏插件
96105
s.pod_target_xcconfig = {
97-
'OTHER_SWIFT_FLAGS' => '-Xfrontend -load-plugin-executable -Xfrontend $(PODS_BUILD_DIR)/ReerCodable/release/ReerCodableMacros#ReerCodableMacros'
106+
'OTHER_SWIFT_FLAGS' => '-Xfrontend -load-plugin-executable -Xfrontend ${PODS_BUILD_DIR}/ReerCodable/MacroPlugin/ReerCodableMacros#ReerCodableMacros'
98107
}
99108
end
100109
</code></pre>
101110

111+
<h4>3. 推荐:通过 Podfile 自动配置(适用于多个 Pod 组件)</h4>
112+
<p>如果你有多个 Pod 依赖 ReerCodable,可以在 Podfile 中添加如下 <code>post_install</code> hook,自动为所有依赖 ReerCodable 的 Pod 注入宏插件配置:</p>
113+
<pre><code class="ruby language-ruby">
114+
post_install do |installer|
115+
macro_flag = '-Xfrontend -load-plugin-executable -Xfrontend ${PODS_BUILD_DIR}/ReerCodable/MacroPlugin/ReerCodableMacros#ReerCodableMacros'
116+
reer_codable_dependents = Set.new
117+
installer.pod_targets.each do |pod_target|
118+
next if pod_target.name == 'ReerCodable'
119+
has_dep = pod_target.dependent_targets.any? { |dep| dep.name == 'ReerCodable' }
120+
reer_codable_dependents.add(pod_target.name) if has_dep
121+
end
122+
installer.pods_project.targets.each do |target|
123+
target.build_configurations.each do |config|
124+
if reer_codable_dependents.include?(target.name)
125+
flags = config.build_settings['OTHER_SWIFT_FLAGS'] || '$(inherited)'
126+
unless flags.include?('ReerCodableMacros')
127+
config.build_settings['OTHER_SWIFT_FLAGS'] = "#{flags} #{macro_flag}"
128+
end
129+
end
130+
end
131+
end
132+
end
133+
</code></pre>
134+
102135
<p><strong>⚠️ 重要提示:</strong>若在 Xcode 14+ 遇到 <code>rsync</code> 权限错误,需关闭用户脚本沙盒:</p>
103136
<p>在工程的 <strong>Build Settings</strong> 中搜索 <code>User Script Sandboxing</code>,将 <code>ENABLE_USER_SCRIPT_SANDBOXING</code> 设为 <code>No</code>。这可解决 Xcode 严格沙盒限制导致的 CocoaPods 脚本执行失败问题。</p>
104137

0 commit comments

Comments
 (0)