72 lines
2.9 KiB
JavaScript
72 lines
2.9 KiB
JavaScript
/**
|
|
* ~ Copyright (c) 2023 Beijing Shuto Technology Co,. Ltd. All rights reserved.
|
|
*/
|
|
|
|
const path = require("path");
|
|
const fs = require("fs");
|
|
|
|
/**
|
|
* 读取文件内容,然后替换部分内容后写回
|
|
* @param file
|
|
* @param callback
|
|
*/
|
|
function replaceContents(file, callback) {
|
|
var data = fs.readFileSync(file, 'utf8');
|
|
data = callback(data);
|
|
fs.writeFileSync(file, data, 'utf8');
|
|
}
|
|
|
|
/**
|
|
* 一些自定义操作
|
|
* @param ctx
|
|
*/
|
|
module.exports = function (ctx) {
|
|
console.log('============install huawei message service hook=================');
|
|
var rootDir = ctx.opts.projectRoot;
|
|
var project = path.join(rootDir, 'platforms/android');
|
|
var app = path.join(rootDir, 'platforms/android/app');
|
|
|
|
if(!fs.existsSync(path.join(rootDir, 'agconnect-services.json'))){
|
|
throw new Error('请从华为推送平台获取的应用配置文件 agconnect-services.json 到项目根目录!');
|
|
}
|
|
if(fs.existsSync(path.join(app, 'agconnect-services.json'))){
|
|
console.log("已经添加hms配置,跳过");
|
|
return;
|
|
}
|
|
fs.copyFileSync(path.join(rootDir, 'agconnect-services.json'),path.join(app, 'agconnect-services.json'));
|
|
|
|
//1. 添加华为 maven 仓库地址 buildscript allprojects repositories
|
|
//1.1 如果repositories.gradle文件存在,则添加相关配置
|
|
var repositoriesFiles = [
|
|
path.join(rootDir, "repositories.gradle"),
|
|
path.join(project, "repositories.gradle"),
|
|
path.join(app, "repositories.gradle")
|
|
];
|
|
for (const repositoriesFile of repositoriesFiles) {
|
|
if (fs.existsSync(repositoriesFile)) {
|
|
replaceContents(repositoriesFile, (data) => {
|
|
var data = data.replace(/ext\.repos = \{/g, "ext.repos = {\n maven {url 'https://developer.huawei.com/repo/'}");
|
|
return data;
|
|
});
|
|
}
|
|
}
|
|
//1.2 在所有的gradle文件中替换相关配置
|
|
var repository = data => data.replace(/repositories \{/g,"repositories {\n maven {url 'https://developer.huawei.com/repo/'}");
|
|
fs.readdirSync(project).forEach(function (name) {
|
|
var filePath = path.join(project, name);
|
|
var stat = fs.statSync(filePath);
|
|
if (stat.isFile() && name.endsWith('.gradle')) {
|
|
replaceContents(filePath, repository);
|
|
} else if (stat.isDirectory()) {
|
|
fs.readdirSync(filePath).forEach(function (subName) {
|
|
var subFilePath = path.join(filePath, subName);
|
|
var subStat = fs.statSync(subFilePath);
|
|
if (subStat.isFile() && subName.endsWith('.gradle')) {
|
|
replaceContents(subFilePath, repository);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
//2. 添加华为推送 gradle 插件依赖
|
|
replaceContents(path.join(project,'build.gradle'), data => data.replace(/dependencies \{/g, "dependencies {\n classpath 'com.huawei.agconnect:agcp:1.6.0.300'"));
|
|
}; |