66import { ExpoConfig } from 'expo/config' ;
77import { withProjectBuildGradle } from 'expo/config-plugins' ;
88import { ResolvedJPushPluginProps , VendorChannelConfig } from '../types' ;
9- import { mergeContents } from '../utils/generateCode' ;
9+ import { mergeContents , removeGeneratedContents , syncGeneratedContentsAtLine } from '../utils/generateCode' ;
10+ import { ensureTopLevelBlock , ensureNestedBlock , findNestedBlockRange } from '../utils/sourceCode' ;
11+ import { getVendorChannels , getProjectVendorFlags , LEGACY_PROJECT_BUILD_TAGS , getBuildscriptRepositories } from '../utils/vendorChannels' ;
1012import { Validator } from '../utils/codeValidator' ;
1113
1214/**
13- * 生成厂商通道 classpath 依赖
15+ * 获取厂商通道开启标记
16+
1417 */
1518const getVendorClasspaths = ( vendorChannels ?: VendorChannelConfig ) : string => {
1619 const isHuaweiEnabled = vendorChannels ?. huawei ?. enabled === true ;
@@ -27,7 +30,7 @@ const getVendorClasspaths = (vendorChannels?: VendorChannelConfig): string => {
2730 classpaths . push ( `classpath 'com.huawei.agconnect:agcp:1.9.3.302'` ) ;
2831 }
2932
30- return classpaths . join ( '\n ' ) ;
33+ return classpaths . join ( '\\ n ' ) ;
3134} ;
3235
3336/**
@@ -45,19 +48,19 @@ const getAllprojectsRepositories = (): string => {
4548 repositories . push ( `maven { url 'https://developer.hihonor.com/repo' }` ) ;
4649 }
4750
48- return repositories . join ( '\n ' ) ;
51+ return repositories . join ( '\\ n ' ) ;
4952} ;
5053
5154function ensureProjectBuildscriptBlock ( src : string ) : string {
5255 let nextContents = ensureTopLevelBlock ( src , 'buildscript' ) ;
53- nextContents = ensureNestedBlock ( nextContents , / ^ \s * b u i l d s c r i p t \s * \{ / , 'repositories' ) ;
54- nextContents = ensureNestedBlock ( nextContents , / ^ \s * b u i l d s c r i p t \s * \{ / , 'dependencies' ) ;
56+ nextContents = ensureNestedBlock ( nextContents , / ^ \\ s * b u i l d s c r i p t \\ s * \ \{ / , 'repositories' ) ;
57+ nextContents = ensureNestedBlock ( nextContents , / ^ \\ s * b u i l d s c r i p t \\ s * \ \{ / , 'dependencies' ) ;
5558 return nextContents ;
5659}
5760
5861function ensureProjectAllprojectsBlock ( src : string ) : string {
5962 let nextContents = ensureTopLevelBlock ( src , 'allprojects' ) ;
60- nextContents = ensureNestedBlock ( nextContents , / ^ \s * a l l p r o j e c t s \s * \{ / , 'repositories' ) ;
63+ nextContents = ensureNestedBlock ( nextContents , / ^ \\ s * a l l p r o j e c t s \\ s * \ \{ / , 'repositories' ) ;
6164 return nextContents ;
6265}
6366
@@ -83,8 +86,8 @@ export function applyAndroidProjectBuildGradle(contents: string): string {
8386
8487 const buildscriptRepositoriesRange = findNestedBlockRange (
8588 nextContents ,
86- / ^ \s * b u i l d s c r i p t \s * \{ / ,
87- / ^ \s * r e p o s i t o r i e s \s * \{ /
89+ / ^ \\ s * b u i l d s c r i p t \\ s * \ \{ / ,
90+ / ^ \\ s * r e p o s i t o r i e s \\ s * \ \{ /
8891 ) ;
8992 if ( buildscriptRepositoriesRange ) {
9093 nextContents = syncGeneratedContentsAtLine ( {
@@ -99,8 +102,8 @@ export function applyAndroidProjectBuildGradle(contents: string): string {
99102
100103 const buildscriptDependenciesRange = findNestedBlockRange (
101104 nextContents ,
102- / ^ \s * b u i l d s c r i p t \s * \{ / ,
103- / ^ \s * d e p e n d e n c i e s \s * \{ /
105+ / ^ \\ s * b u i l d s c r i p t \\ s * \ \{ / ,
106+ / ^ \\ s * d e p e n d e n c i e s \\ s * \ \{ /
104107 ) ;
105108 if ( buildscriptDependenciesRange ) {
106109 nextContents = syncGeneratedContentsAtLine ( {
@@ -115,8 +118,8 @@ export function applyAndroidProjectBuildGradle(contents: string): string {
115118
116119 const allprojectsRepositoriesRange = findNestedBlockRange (
117120 nextContents ,
118- / ^ \s * a l l p r o j e c t s \s * \{ / ,
119- / ^ \s * r e p o s i t o r i e s \s * \{ /
121+ / ^ \\ s * a l l p r o j e c t s \\ s * \ \{ / ,
122+ / ^ \\ s * r e p o s i t o r i e s \\ s * \ \{ /
120123 ) ;
121124 if ( allprojectsRepositoriesRange ) {
122125 nextContents = syncGeneratedContentsAtLine ( {
@@ -145,33 +148,33 @@ export function withAndroidProjectBuildGradle(
145148 const validator = new Validator ( nextConfig . modResults . contents ) ;
146149
147150 if ( isHuaweiEnabled ) {
148- validator . register ( 'jpush-huawei-maven-buildscript' , ( src ) => {
151+ validator . register ( 'jpush-huawei-maven-buildscript' , ( src : string ) => {
149152 console . log (
150- '\n[MX_JPush_Expo] 配置 buildscript repositories 华为 Maven 仓库 ...'
153+ '\\ n[MX_JPush_Expo] 配置 buildscript repositories 华为 Maven 仓库 ...'
151154 ) ;
152155
153156 return mergeContents ( {
154157 src,
155158 newSrc : `maven { url 'https://developer.huawei.com/repo/' }` ,
156159 tag : 'jpush-huawei-maven-buildscript' ,
157- anchor : / b u i l d s c r i p t \s * \{ / ,
160+ anchor : / b u i l d s c r i p t \\ s * \ \{ / ,
158161 offset : 2 ,
159162 comment : '//' ,
160163 } ) ;
161164 } ) ;
162165 }
163166
164167 if ( vendorChannels ?. honor ) {
165- validator . register ( 'jpush-honor-maven-buildscript' , ( src ) => {
168+ validator . register ( 'jpush-honor-maven-buildscript' , ( src : string ) => {
166169 console . log (
167- '\n[MX_JPush_Expo] 配置 buildscript repositories 荣耀 Maven 仓库 ...'
170+ '\\ n[MX_JPush_Expo] 配置 buildscript repositories 荣耀 Maven 仓库 ...'
168171 ) ;
169172
170173 return mergeContents ( {
171174 src,
172175 newSrc : `maven { url 'https://developer.hihonor.com/repo' }` ,
173176 tag : 'jpush-honor-maven-buildscript' ,
174- anchor : / b u i l d s c r i p t \s * \{ / ,
177+ anchor : / b u i l d s c r i p t \\ s * \ \{ / ,
175178 offset : 2 ,
176179 comment : '//' ,
177180 } ) ;
@@ -180,40 +183,40 @@ export function withAndroidProjectBuildGradle(
180183
181184 const classpaths = getVendorClasspaths ( vendorChannels ) ;
182185 if ( classpaths ) {
183- validator . register ( 'classpath' , ( src ) => {
186+ validator . register ( 'classpath' , ( src : string ) => {
184187 console . log (
185- '\n[MX_JPush_Expo] 配置 buildscript dependencies classpath ...'
188+ '\\ n[MX_JPush_Expo] 配置 buildscript dependencies classpath ...'
186189 ) ;
187190
188191 return mergeContents ( {
189192 src,
190193 newSrc : classpaths ,
191194 tag : 'jpush-vendor-classpaths' ,
192- anchor : / d e p e n d e n c i e s \s * \{ / ,
195+ anchor : / d e p e n d e n c i e s \\ s * \ \{ / ,
193196 offset : 1 ,
194197 comment : '//' ,
195198 } ) ;
196199 } ) ;
197200 }
198201
199202 if ( isHuaweiEnabled ) {
200- validator . register ( 'jpush-huawei-maven-allprojects' , ( src ) => {
203+ validator . register ( 'jpush-huawei-maven-allprojects' , ( src : string ) => {
201204 console . log (
202- '\n[MX_JPush_Expo] 配置 allprojects repositories 华为 Maven 仓库 ...'
205+ '\\ n[MX_JPush_Expo] 配置 allprojects repositories 华为 Maven 仓库 ...'
203206 ) ;
204207
205- if ( ! / a l l p r o j e c t s \s * \{ / . test ( src ) ) {
208+ if ( ! / a l l p r o j e c t s \\ s * \ \{ / . test ( src ) ) {
206209 return { contents : src , didMerge : false , didClear : false } ;
207210 }
208211
209- const hasRepositories = / a l l p r o j e c t s \s * \{ [ ^ } ] * r e p o s i t o r i e s \s * \{ / . test ( src ) ;
212+ const hasRepositories = / a l l p r o j e c t s \\ s * \\ { [ ^ } ] * r e p o s i t o r i e s \\ s * \ \{ / . test ( src ) ;
210213
211214 if ( hasRepositories ) {
212215 return mergeContents ( {
213216 src,
214217 newSrc : `maven { url 'https://developer.huawei.com/repo/' }` ,
215218 tag : 'jpush-huawei-maven-allprojects' ,
216- anchor : / a l l p r o j e c t s \s * \{ / ,
219+ anchor : / a l l p r o j e c t s \\ s * \ \{ / ,
217220 offset : 2 ,
218221 comment : '//' ,
219222 } ) ;
@@ -225,7 +228,7 @@ export function withAndroidProjectBuildGradle(
225228 maven { url 'https://developer.huawei.com/repo/' }
226229 }` ,
227230 tag : 'jpush-huawei-maven-allprojects' ,
228- anchor : / a l l p r o j e c t s \s * \{ / ,
231+ anchor : / a l l p r o j e c t s \\ s * \ \{ / ,
229232 offset : 1 ,
230233 comment : '//' ,
231234 } ) ;
@@ -235,21 +238,21 @@ export function withAndroidProjectBuildGradle(
235238 if ( vendorChannels ?. honor ) {
236239 validator . register ( 'jpush-honor-maven-allprojects' , ( src ) => {
237240 console . log (
238- '\n[MX_JPush_Expo] 配置 allprojects repositories 荣耀 Maven 仓库 ...'
241+ '\\ n[MX_JPush_Expo] 配置 allprojects repositories 荣耀 Maven 仓库 ...'
239242 ) ;
240243
241- if ( ! / a l l p r o j e c t s \s * \{ / . test ( src ) ) {
244+ if ( ! / a l l p r o j e c t s \\ s * \ \{ / . test ( src ) ) {
242245 return { contents : src , didMerge : false , didClear : false } ;
243246 }
244247
245- const hasRepositories = / a l l p r o j e c t s \s * \{ [ ^ } ] * r e p o s i t o r i e s \s * \{ / . test ( src ) ;
248+ const hasRepositories = / a l l p r o j e c t s \\ s * \\ { [ ^ } ] * r e p o s i t o r i e s \\ s * \ \{ / . test ( src ) ;
246249
247250 if ( hasRepositories ) {
248251 return mergeContents ( {
249252 src,
250253 newSrc : `maven { url 'https://developer.hihonor.com/repo' }` ,
251254 tag : 'jpush-honor-maven-allprojects' ,
252- anchor : / a l l p r o j e c t s \s * \{ / ,
255+ anchor : / a l l p r o j e c t s \\ s * \ \{ / ,
253256 offset : 2 ,
254257 comment : '//' ,
255258 } ) ;
@@ -261,7 +264,7 @@ export function withAndroidProjectBuildGradle(
261264 maven { url 'https://developer.hihonor.com/repo' }
262265 }` ,
263266 tag : 'jpush-honor-maven-allprojects' ,
264- anchor : / a l l p r o j e c t s \s * \{ / ,
267+ anchor : / a l l p r o j e c t s \\ s * \ \{ / ,
265268 offset : 1 ,
266269 comment : '//' ,
267270 } ) ;
@@ -271,4 +274,4 @@ export function withAndroidProjectBuildGradle(
271274 nextConfig . modResults . contents = validator . invoke ( ) ;
272275 return nextConfig ;
273276 } ) ;
274- }
277+ }
0 commit comments