mx-jpush-expo
Version:
Expo 集成极光推送(JPush)一体化解决方案,支持 iOS/Android 厂商通道
230 lines • 10.5 kB
JavaScript
;
/**
* Android app/build.gradle 配置
* 添加 JPush 依赖和 manifestPlaceholders
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyAndroidAppBuildGradle = applyAndroidAppBuildGradle;
exports.withAndroidAppBuildGradle = withAndroidAppBuildGradle;
const config_plugins_1 = require("expo/config-plugins");
const generateCode_1 = require("../utils/generateCode");
const sourceCode_1 = require("../utils/sourceCode");
const LEGACY_DEFAULT_CONFIG_TAGS = [
'jpush-default-config',
'jpush-ndk-config',
'jpush-manifest-placeholders',
];
const LEGACY_DEPENDENCY_TAGS = ['jpush-libs-filetree'];
const gradleEnv = (key, fallback = '""') => `System.getenv("${key}") ?: (project.findProperty("${key}") ?: ${fallback})`;
function removeLegacyGeneratedSections(contents, tags) {
return tags.reduce((currentContents, tag) => {
return (0, generateCode_1.removeGeneratedContents)(currentContents, tag) ?? currentContents;
}, contents);
}
function getResolvedConfig({ packageName = '', appKey = '', channel = '', vendorChannels, }) {
return {
appKey,
channel,
packageName,
vendorChannels,
};
}
function getBlockRangeOrThrow(src, pattern, errorMessage) {
const range = (0, sourceCode_1.findBlockRange)(src, pattern);
if (!range) {
throw new Error(errorMessage);
}
return range;
}
function getDefaultConfigRange(src) {
return getBlockRangeOrThrow(src, /^\s*defaultConfig\s*\{/, '[MX_JPush_Expo] 未找到 defaultConfig 配置块');
}
function getDependenciesLine(src) {
const lineIndex = (0, sourceCode_1.findLineIndex)(src, /^\s*dependencies\s*\{/);
if (lineIndex < 0) {
throw new Error('[MX_JPush_Expo] 未找到 dependencies 配置块');
}
return lineIndex;
}
function getNdkRange(src) {
const range = (0, sourceCode_1.findNestedBlockRange)(src, /^\s*defaultConfig\s*\{/, /^\s*ndk\s*\{/);
if (!range) {
throw new Error('[MX_JPush_Expo] 未找到 defaultConfig.ndk 配置块');
}
return range;
}
function getChildIndent(src, pattern, errorMessage) {
const range = getBlockRangeOrThrow(src, pattern, errorMessage);
const lines = src.split('\n');
return `${(0, sourceCode_1.getLineIndent)(lines[range.startLine])} `;
}
function getManifestPlaceholders(packageName, appKey, channel, vendorChannels, indent) {
const placeholders = [
`JPUSH_PKGNAME: ${gradleEnv('JPUSH_PKGNAME', `"${packageName}"`)}`,
`JPUSH_APPKEY: ${gradleEnv('JPUSH_APP_KEY', `"${appKey}"`)}`,
`JPUSH_CHANNEL: ${gradleEnv('JPUSH_CHANNEL', `"${channel}"`)}`,
];
if (vendorChannels?.meizu) {
placeholders.push(`MEIZU_APPKEY: ${gradleEnv('JPUSH_MEIZU_APP_KEY')}`);
placeholders.push(`MEIZU_APPID: ${gradleEnv('JPUSH_MEIZU_APP_ID')}`);
}
if (vendorChannels?.xiaomi) {
placeholders.push(`XIAOMI_APPID: ${gradleEnv('JPUSH_XIAOMI_APP_ID')}`);
placeholders.push(`XIAOMI_APPKEY: ${gradleEnv('JPUSH_XIAOMI_APP_KEY')}`);
}
if (vendorChannels?.oppo) {
placeholders.push(`OPPO_APPKEY: ${gradleEnv('JPUSH_OPPO_APP_KEY')}`);
placeholders.push(`OPPO_APPID: ${gradleEnv('JPUSH_OPPO_APP_ID')}`);
placeholders.push(`OPPO_APPSECRET: ${gradleEnv('JPUSH_OPPO_APP_SECRET')}`);
}
if (vendorChannels?.vivo) {
placeholders.push(`VIVO_APPKEY: ${gradleEnv('JPUSH_VIVO_APP_KEY')}`);
placeholders.push(`VIVO_APPID: ${gradleEnv('JPUSH_VIVO_APP_ID')}`);
}
if (vendorChannels?.honor) {
placeholders.push(`HONOR_APPID: ${gradleEnv('JPUSH_HONOR_APP_ID')}`);
}
if (vendorChannels?.nio) {
placeholders.push(`NIO_APPID: ${gradleEnv('JPUSH_NIO_APP_ID')}`);
}
const valueIndent = `${indent} `;
return [
// Append after any host map so unrelated placeholders stay intact while
// JPUSH_* defaults can still take precedence when the same key is reused.
`${indent}manifestPlaceholders += [`,
placeholders
.map((placeholder, index) => {
const separator = index === placeholders.length - 1 ? '' : ',';
return `${valueIndent}${placeholder}${separator}`;
})
.join('\n'),
`${indent}]`,
].join('\n');
}
function getNdkAbiFilters(indent) {
return [
`${indent}// 选择要添加的对应 cpu 类型的 .so 库。`,
`${indent}abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'`,
].join('\n');
}
function getJPushDependencies(vendorChannels, indent) {
const isHuaweiEnabled = vendorChannels?.huawei?.enabled === true;
const isFcmEnabled = vendorChannels?.fcm?.enabled === true;
const dependencies = [
`implementation fileTree(include: ['*.jar','*.aar'], dir: 'libs')`,
``,
`// JPush React Native 桥接(已包含 JPush 核心 SDK)`,
`implementation project(':jpush-react-native')`,
`implementation project(':jcore-react-native')`,
];
if (vendorChannels) {
const hasVendorChannels = isHuaweiEnabled ||
isFcmEnabled ||
Boolean(vendorChannels.meizu ||
vendorChannels.vivo ||
vendorChannels.xiaomi ||
vendorChannels.oppo ||
vendorChannels.honor ||
vendorChannels.nio);
if (hasVendorChannels) {
dependencies.push('', `// 厂商通道 SDK(JPush 5.9.0)`);
}
if (isHuaweiEnabled) {
dependencies.push(`// 华为厂商`, `implementation 'com.huawei.hms:push:6.13.0.300'`, `implementation 'com.huawei.agconnect:agconnect-core:1.9.3.302'`, `implementation 'cn.jiguang.sdk.plugin:huawei:5.9.0'`);
}
if (isFcmEnabled) {
dependencies.push(`// FCM 厂商`, `implementation 'com.google.firebase:firebase-messaging:24.1.0'`, `implementation 'cn.jiguang.sdk.plugin:fcm:5.9.0'`);
}
if (vendorChannels.meizu) {
dependencies.push(`// 魅族厂商`, `implementation 'cn.jiguang.sdk.plugin:meizu:5.9.0'`);
}
if (vendorChannels.vivo) {
dependencies.push(`// VIVO 厂商`, `implementation 'cn.jiguang.sdk.plugin:vivo:5.9.0'`);
}
if (vendorChannels.xiaomi) {
dependencies.push(`// 小米厂商`, `implementation 'cn.jiguang.sdk.plugin:xiaomi:5.9.0'`);
}
if (vendorChannels.oppo) {
dependencies.push(`// OPPO 厂商`, `implementation 'cn.jiguang.sdk.plugin:oppo:5.9.0'`, `// OPPO 3.1.0 aar 及其以上版本需要添加以下依赖`, `implementation 'com.google.code.gson:gson:2.6.2'`, `implementation 'androidx.annotation:annotation:1.1.0'`);
}
if (vendorChannels.honor) {
dependencies.push(`// 荣耀厂商`, `implementation 'cn.jiguang.sdk.plugin:honor:5.9.0'`);
}
if (vendorChannels.nio) {
dependencies.push(`// 蔚来厂商`, `implementation 'cn.jiguang.sdk.plugin:nio:5.9.0'`);
}
}
return dependencies
.map((dependency) => (dependency ? `${indent}${dependency}` : dependency))
.join('\n');
}
function getApplyPlugins(vendorChannels) {
const isHuaweiEnabled = vendorChannels?.huawei?.enabled === true;
const isFcmEnabled = vendorChannels?.fcm?.enabled === true;
const plugins = [];
if (isFcmEnabled) {
plugins.push(`apply plugin: 'com.google.gms.google-services'`);
}
if (isHuaweiEnabled) {
plugins.push(`apply plugin: 'com.huawei.agconnect'`);
}
return plugins.join('\n');
}
function applyAndroidAppBuildGradle(contents, vendorChannels, packageName, appKey, channel) {
const resolvedConfig = getResolvedConfig({
packageName,
appKey,
channel,
vendorChannels,
});
let nextContents = removeLegacyGeneratedSections(contents, LEGACY_DEFAULT_CONFIG_TAGS);
nextContents = (0, sourceCode_1.ensureNestedBlock)(nextContents, /^\s*android\s*\{/, 'defaultConfig');
nextContents = (0, sourceCode_1.ensureNestedBlock)(nextContents, /^\s*defaultConfig\s*\{/, 'ndk');
nextContents = (0, sourceCode_1.ensureTopLevelBlock)(nextContents, 'dependencies');
nextContents = removeLegacyGeneratedSections(nextContents, LEGACY_DEPENDENCY_TAGS);
const ndkIndent = getChildIndent(nextContents, /^\s*ndk\s*\{/, '[MX_JPush_Expo] 未找到 defaultConfig.ndk 配置块');
nextContents = (0, generateCode_1.replaceGeneratedContentsAtLine)({
src: nextContents,
newSrc: getNdkAbiFilters(ndkIndent),
tag: 'jpush-ndk-abi-filters',
getLineIndex: (src) => getNdkRange(src).endLine,
offset: 0,
comment: '//',
}).contents;
const defaultConfigIndent = getChildIndent(nextContents, /^\s*defaultConfig\s*\{/, '[MX_JPush_Expo] 未找到 defaultConfig 配置块');
nextContents = (0, generateCode_1.replaceGeneratedContentsAtLine)({
src: nextContents,
newSrc: getManifestPlaceholders(resolvedConfig.packageName, resolvedConfig.appKey, resolvedConfig.channel, resolvedConfig.vendorChannels, defaultConfigIndent),
tag: 'jpush-manifest-placeholders',
getLineIndex: (src) => getDefaultConfigRange(src).endLine,
offset: 0,
comment: '//',
}).contents;
const dependenciesIndent = `${(0, sourceCode_1.getLineIndent)(nextContents.split('\n')[getDependenciesLine(nextContents)])} `;
nextContents = (0, generateCode_1.replaceGeneratedContentsAtLine)({
src: nextContents,
newSrc: getJPushDependencies(resolvedConfig.vendorChannels, dependenciesIndent),
tag: 'jpush-dependencies',
getLineIndex: getDependenciesLine,
offset: 1,
comment: '//',
}).contents;
nextContents = (0, generateCode_1.syncGeneratedContentsAtEnd)({
src: nextContents,
newSrc: getApplyPlugins(resolvedConfig.vendorChannels),
tag: 'jpush-apply-plugins',
comment: '//',
}).contents;
return nextContents;
}
/**
* 配置 Android build.gradle
*/
function withAndroidAppBuildGradle(config, props) {
return (0, config_plugins_1.withAppBuildGradle)(config, (nextConfig) => {
console.log('\n[MX_JPush_Expo] 配置 Android app/build.gradle ...');
nextConfig.modResults.contents = applyAndroidAppBuildGradle(nextConfig.modResults.contents, props.vendorChannels, props.packageName, props.appKey, props.channel);
return nextConfig;
});
}
//# sourceMappingURL=appBuildGradle.js.map