2023-03-19 17:37:07 +02:00
|
|
|
import { execCommand } from '@joplin/utils';
|
2023-05-11 15:48:33 +02:00
|
|
|
import { copy, mkdirp, move, readFile, readFileSync, remove, stat, writeFile, writeFileSync } from 'fs-extra';
|
2023-03-19 17:37:07 +02:00
|
|
|
import { execCommandVerbose, execCommandWithPipes, githubRelease, githubOauthToken, fileExists, gitPullTry, completeReleaseWithChangelog } from './tool-utils';
|
2018-03-09 22:59:12 +02:00
|
|
|
const path = require('path');
|
|
|
|
const fetch = require('node-fetch');
|
|
|
|
const uriTemplate = require('uri-template');
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2020-11-26 21:42:05 +02:00
|
|
|
const rootDir = path.dirname(path.dirname(__dirname));
|
|
|
|
const rnDir = `${rootDir}/packages/app-mobile`;
|
2020-11-06 20:45:45 +02:00
|
|
|
const releaseDir = `${rnDir}/dist`;
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2021-05-16 19:38:36 +02:00
|
|
|
interface Release {
|
|
|
|
downloadUrl: string;
|
|
|
|
apkFilename: string;
|
|
|
|
apkFilePath: string;
|
|
|
|
}
|
|
|
|
|
2023-05-11 15:48:33 +02:00
|
|
|
type PatcherCallback = (content: string)=> Promise<string>;
|
2023-05-09 12:01:43 +02:00
|
|
|
|
2023-05-11 15:48:33 +02:00
|
|
|
class Patcher {
|
2023-05-09 12:01:43 +02:00
|
|
|
|
2023-05-11 15:48:33 +02:00
|
|
|
private workDir_: string;
|
|
|
|
private originalContents_: Record<string, string> = {};
|
|
|
|
private removedFiles_: Record<string, string> = {};
|
2023-05-09 12:01:43 +02:00
|
|
|
|
2023-05-11 15:48:33 +02:00
|
|
|
public constructor(workDir: string) {
|
|
|
|
this.workDir_ = workDir;
|
|
|
|
}
|
2023-05-09 12:01:43 +02:00
|
|
|
|
2023-05-11 15:48:33 +02:00
|
|
|
public removeFile = async (path: string) => {
|
|
|
|
const targetPath = `${this.workDir_}/${path.substring(1)}`;
|
|
|
|
await move(path, targetPath);
|
|
|
|
this.removedFiles_[path] = targetPath;
|
|
|
|
};
|
2023-05-09 12:01:43 +02:00
|
|
|
|
2023-05-11 15:48:33 +02:00
|
|
|
public updateFileContent = async (path: string, callback: PatcherCallback) => {
|
|
|
|
const content = await readFile(path, 'utf8');
|
|
|
|
this.originalContents_[path] = content;
|
|
|
|
const newContent = await callback(content);
|
|
|
|
await writeFile(path, newContent);
|
|
|
|
};
|
2023-05-09 12:01:43 +02:00
|
|
|
|
2023-05-11 15:48:33 +02:00
|
|
|
public restore = async () => {
|
|
|
|
for (const filename in this.originalContents_) {
|
|
|
|
const content = this.originalContents_[filename];
|
|
|
|
await writeFile(filename, content);
|
|
|
|
}
|
2023-05-09 12:01:43 +02:00
|
|
|
|
2023-05-11 15:48:33 +02:00
|
|
|
for (const [originalPath, backupPath] of Object.entries(this.removedFiles_)) {
|
|
|
|
await move(backupPath, originalPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.removedFiles_ = {};
|
|
|
|
this.originalContents_ = {};
|
|
|
|
};
|
2023-05-09 12:01:43 +02:00
|
|
|
|
2023-05-11 15:48:33 +02:00
|
|
|
}
|
2023-05-09 12:01:43 +02:00
|
|
|
|
2021-05-16 19:38:36 +02:00
|
|
|
function increaseGradleVersionCode(content: string) {
|
2023-02-20 17:02:29 +02:00
|
|
|
const newContent = content.replace(/versionCode\s+(\d+)/, (_a, versionCode: string) => {
|
2018-01-17 22:16:13 +02:00
|
|
|
const n = Number(versionCode);
|
2019-09-19 23:51:18 +02:00
|
|
|
if (isNaN(n) || !n) throw new Error(`Invalid version code: ${versionCode}`);
|
|
|
|
return `versionCode ${n + 1}`;
|
2018-01-17 22:16:13 +02:00
|
|
|
});
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (newContent === content) throw new Error('Could not update version code');
|
2018-01-17 22:16:13 +02:00
|
|
|
|
|
|
|
return newContent;
|
|
|
|
}
|
|
|
|
|
2021-05-16 19:38:36 +02:00
|
|
|
function increaseGradleVersionName(content: string) {
|
2023-02-20 17:02:29 +02:00
|
|
|
const newContent = content.replace(/(versionName\s+"\d+?\.\d+?\.)(\d+)"/, (_match, prefix: string, buildNum: string) => {
|
2018-01-17 22:16:13 +02:00
|
|
|
const n = Number(buildNum);
|
2020-09-12 01:10:18 +02:00
|
|
|
if (isNaN(n)) throw new Error(`Invalid version code: ${buildNum}`);
|
2019-09-19 23:51:18 +02:00
|
|
|
return `${prefix + (n + 1)}"`;
|
2018-01-17 22:16:13 +02:00
|
|
|
});
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (newContent === content) throw new Error('Could not update version name');
|
2018-01-17 22:16:13 +02:00
|
|
|
|
|
|
|
return newContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateGradleConfig() {
|
2023-05-09 12:01:43 +02:00
|
|
|
let content = readFileSync(`${rnDir}/android/app/build.gradle`, 'utf8');
|
2018-01-17 22:16:13 +02:00
|
|
|
content = increaseGradleVersionCode(content);
|
|
|
|
content = increaseGradleVersionName(content);
|
2023-05-09 12:01:43 +02:00
|
|
|
writeFileSync(`${rnDir}/android/app/build.gradle`, content);
|
2018-01-17 22:16:13 +02:00
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2021-05-16 19:38:36 +02:00
|
|
|
function gradleVersionName(content: string) {
|
2018-01-17 22:16:13 +02:00
|
|
|
const matches = content.match(/versionName\s+"(\d+?\.\d+?\.\d+)"/);
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!matches || matches.length < 1) throw new Error('Cannot get gradle version name');
|
2018-01-17 22:16:13 +02:00
|
|
|
return matches[1];
|
|
|
|
}
|
|
|
|
|
2023-05-11 15:48:33 +02:00
|
|
|
async function createRelease(projectName: string, name: string, tagName: string, version: string): Promise<Release> {
|
2020-09-04 20:11:46 +02:00
|
|
|
const suffix = version + (name === 'main' ? '' : `-${name}`);
|
2018-12-29 04:12:23 +02:00
|
|
|
|
2023-05-11 15:48:33 +02:00
|
|
|
const patcher = new Patcher(`${rnDir}/patcher-work`);
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
console.info(`Creating release: ${suffix}`);
|
2019-07-28 18:19:41 +02:00
|
|
|
|
2023-06-20 16:07:33 +02:00
|
|
|
// if (name === '32bit') {
|
|
|
|
// await patcher.updateFileContent(`${rnDir}/android/app/build.gradle`, async (content: string) => {
|
|
|
|
// content = content.replace(/abiFilters "armeabi-v7a", "x86", "arm64-v8a", "x86_64"/, 'abiFilters "armeabi-v7a", "x86"');
|
|
|
|
// content = content.replace(/include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"/, 'include "armeabi-v7a", "x86"');
|
|
|
|
// return content;
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (name !== 'vosk') {
|
|
|
|
// await patcher.updateFileContent(`${rnDir}/services/voiceTyping/vosk.android.ts`, async (_content: string) => {
|
|
|
|
// return readFile(`${rnDir}/services/voiceTyping/vosk.ios.ts`, 'utf8');
|
|
|
|
// });
|
|
|
|
|
|
|
|
// await patcher.updateFileContent(`${rnDir}/android/app/build.gradle`, async (content: string) => {
|
|
|
|
// content = content.replace(/\s+"react-native-vosk": ".*",/, '');
|
|
|
|
// return content;
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (name === 'vosk') {
|
|
|
|
// await patcher.updateFileContent(`${rnDir}/android/app/build.gradle`, async (content: string) => {
|
|
|
|
// content = content.replace(/(\s+)applicationId "net.cozic.joplin"/, '$1applicationId "net.cozic.joplin.mod"');
|
|
|
|
// content = content.replace(/(\s+)versionName "(\d+\.\d+\.\d+)"/, '$1versionName "$2-mod"');
|
|
|
|
// return content;
|
|
|
|
// });
|
|
|
|
// }
|
2023-05-13 19:25:29 +02:00
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
const apkFilename = `joplin-v${suffix}.apk`;
|
|
|
|
const apkFilePath = `${releaseDir}/${apkFilename}`;
|
|
|
|
const downloadUrl = `https://github.com/laurent22/${projectName}/releases/download/${tagName}/${apkFilename}`;
|
2018-01-17 22:16:13 +02:00
|
|
|
|
|
|
|
process.chdir(rootDir);
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
console.info(`Running from: ${process.cwd()}`);
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2023-05-07 13:05:41 +02:00
|
|
|
await execCommand('yarn install', { showStdout: false });
|
|
|
|
await execCommand('yarn run tsc', { showStdout: false });
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
console.info(`Building APK file v${suffix}...`);
|
2018-10-13 00:25:11 +02:00
|
|
|
|
2023-05-07 13:05:41 +02:00
|
|
|
const buildDirName = `build-${name}`;
|
|
|
|
const buildDirBasePath = `${rnDir}/android/app/${buildDirName}`;
|
2023-05-09 12:01:43 +02:00
|
|
|
await remove(buildDirBasePath);
|
2023-05-07 13:05:41 +02:00
|
|
|
|
2018-10-13 00:25:11 +02:00
|
|
|
let restoreDir = null;
|
2020-11-06 20:45:45 +02:00
|
|
|
let apkBuildCmd = '';
|
2023-05-07 13:05:41 +02:00
|
|
|
let apkCleanBuild = '';
|
2023-05-13 19:25:29 +02:00
|
|
|
const apkBuildCmdArgs = ['assembleRelease', `-PbuildDir=${buildDirName}`];
|
2019-06-15 10:44:34 +02:00
|
|
|
if (await fileExists('/mnt/c/Windows/System32/cmd.exe')) {
|
2020-11-05 18:58:23 +02:00
|
|
|
await execCommandWithPipes('/mnt/c/Windows/System32/cmd.exe', ['/c', `cd packages\\app-mobile\\android && gradlew.bat ${apkBuildCmd}`]);
|
2019-10-13 01:21:56 +02:00
|
|
|
apkBuildCmd = '';
|
2023-05-07 13:05:41 +02:00
|
|
|
throw new Error('TODO: apkCleanBuild must be set');
|
2019-06-15 10:44:34 +02:00
|
|
|
} else {
|
2019-09-19 23:51:18 +02:00
|
|
|
process.chdir(`${rnDir}/android`);
|
2020-11-06 20:45:45 +02:00
|
|
|
apkBuildCmd = './gradlew';
|
2023-05-07 13:05:41 +02:00
|
|
|
apkCleanBuild = `./gradlew clean -PbuildDir=${buildDirName}`;
|
2019-06-15 10:44:34 +02:00
|
|
|
restoreDir = rootDir;
|
|
|
|
}
|
|
|
|
|
2019-06-15 19:58:09 +02:00
|
|
|
if (apkBuildCmd) {
|
2023-05-07 13:05:41 +02:00
|
|
|
await execCommand(apkCleanBuild);
|
2020-11-06 20:45:45 +02:00
|
|
|
await execCommandVerbose(apkBuildCmd, apkBuildCmdArgs);
|
2019-06-15 19:58:09 +02:00
|
|
|
}
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2018-10-13 00:25:11 +02:00
|
|
|
if (restoreDir) process.chdir(restoreDir);
|
|
|
|
|
2023-05-09 12:01:43 +02:00
|
|
|
await mkdirp(releaseDir);
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2023-05-07 13:05:41 +02:00
|
|
|
const builtApk = `${buildDirBasePath}/outputs/apk/release/app-release.apk`;
|
2023-05-09 12:01:43 +02:00
|
|
|
const builtApkStat = await stat(builtApk);
|
2023-05-07 13:05:41 +02:00
|
|
|
|
|
|
|
console.info(`Built APK at ${builtApk}`);
|
|
|
|
console.info('APK size:', builtApkStat.size);
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
console.info(`Copying APK to ${apkFilePath}`);
|
2023-05-09 12:01:43 +02:00
|
|
|
await copy(builtApk, apkFilePath);
|
2019-07-28 18:29:07 +02:00
|
|
|
|
|
|
|
if (name === 'main') {
|
2019-09-19 23:51:18 +02:00
|
|
|
console.info(`Copying APK to ${releaseDir}/joplin-latest.apk`);
|
2023-05-09 12:01:43 +02:00
|
|
|
await copy(builtApk, `${releaseDir}/joplin-latest.apk`);
|
2019-07-28 18:29:07 +02:00
|
|
|
}
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2023-05-11 16:06:34 +02:00
|
|
|
await patcher.restore();
|
2019-07-28 18:19:41 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
downloadUrl: downloadUrl,
|
2019-07-28 18:31:31 +02:00
|
|
|
apkFilename: apkFilename,
|
2019-07-28 18:33:48 +02:00
|
|
|
apkFilePath: apkFilePath,
|
2019-07-28 18:19:41 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-05-11 15:48:33 +02:00
|
|
|
const uploadToGitHubRelease = async (projectName: string, tagName: string, isPreRelease: boolean, releaseFiles: Record<string, Release>) => {
|
2019-09-19 23:51:18 +02:00
|
|
|
console.info(`Creating GitHub release ${tagName}...`);
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2020-09-04 20:11:46 +02:00
|
|
|
const releaseOptions = { isPreRelease: isPreRelease };
|
2020-09-04 18:28:18 +02:00
|
|
|
|
2019-07-28 18:33:48 +02:00
|
|
|
const oauthToken = await githubOauthToken();
|
2020-09-04 18:28:18 +02:00
|
|
|
const release = await githubRelease(projectName, tagName, releaseOptions);
|
2019-07-28 18:36:36 +02:00
|
|
|
const uploadUrlTemplate = uriTemplate.parse(release.upload_url);
|
2019-07-28 18:33:48 +02:00
|
|
|
|
2019-07-28 18:31:31 +02:00
|
|
|
for (const releaseFilename in releaseFiles) {
|
|
|
|
const releaseFile = releaseFiles[releaseFilename];
|
|
|
|
const uploadUrl = uploadUrlTemplate.expand({ name: releaseFile.apkFilename });
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2023-05-09 12:01:43 +02:00
|
|
|
const binaryBody = await readFile(releaseFile.apkFilePath);
|
2018-02-04 19:42:33 +02:00
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
console.info(`Uploading ${releaseFile.apkFilename} to ${uploadUrl}`);
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2019-07-28 18:29:07 +02:00
|
|
|
const uploadResponse = await fetch(uploadUrl, {
|
2019-07-30 09:35:42 +02:00
|
|
|
method: 'POST',
|
2019-07-28 18:29:07 +02:00
|
|
|
body: binaryBody,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/vnd.android.package-archive',
|
2019-09-19 23:51:18 +02:00
|
|
|
'Authorization': `token ${oauthToken}`,
|
2019-07-28 18:29:07 +02:00
|
|
|
'Content-Length': binaryBody.length,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const uploadResponseText = await uploadResponse.text();
|
2019-07-28 22:43:33 +02:00
|
|
|
const uploadResponseObject = JSON.parse(uploadResponseText);
|
|
|
|
if (!uploadResponseObject || !uploadResponseObject.browser_download_url) throw new Error('Could not upload file to GitHub');
|
2019-07-28 18:29:07 +02:00
|
|
|
}
|
2023-05-11 15:48:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const argv = require('yargs').argv;
|
|
|
|
|
|
|
|
await gitPullTry(false);
|
|
|
|
|
|
|
|
const isPreRelease = !('type' in argv) || argv.type === 'prerelease';
|
|
|
|
const releaseNameOnly = argv['release-name'];
|
|
|
|
|
|
|
|
process.chdir(rnDir);
|
|
|
|
|
|
|
|
if (isPreRelease) console.info('Creating pre-release');
|
|
|
|
console.info('Updating version numbers in build.gradle...');
|
|
|
|
|
|
|
|
const newContent = updateGradleConfig();
|
|
|
|
const version = gradleVersionName(newContent);
|
|
|
|
const tagName = `android-v${version}`;
|
|
|
|
// const releaseNames = ['main', '32bit', 'vosk'];
|
2023-06-20 16:07:33 +02:00
|
|
|
const releaseNames = ['main'];
|
2023-05-11 15:48:33 +02:00
|
|
|
const releaseFiles: Record<string, Release> = {};
|
|
|
|
const mainProjectName = 'joplin-android';
|
|
|
|
const modProjectName = 'joplin-android-mod';
|
|
|
|
|
|
|
|
for (const releaseName of releaseNames) {
|
|
|
|
if (releaseNameOnly && releaseName !== releaseNameOnly) continue;
|
|
|
|
const projectName = releaseName === 'vosk' ? modProjectName : mainProjectName;
|
|
|
|
releaseFiles[releaseName] = await createRelease(projectName, releaseName, tagName, version);
|
|
|
|
}
|
|
|
|
|
2023-05-13 19:25:29 +02:00
|
|
|
console.info('Created releases:');
|
|
|
|
console.info(releaseFiles);
|
|
|
|
|
2023-05-11 15:48:33 +02:00
|
|
|
const voskRelease = releaseFiles['vosk'];
|
2023-06-02 16:47:42 +02:00
|
|
|
delete releaseFiles['vosk'];
|
2023-05-11 15:48:33 +02:00
|
|
|
|
|
|
|
await uploadToGitHubRelease(mainProjectName, tagName, isPreRelease, releaseFiles);
|
2023-05-14 15:33:13 +02:00
|
|
|
|
|
|
|
if (voskRelease) {
|
|
|
|
await uploadToGitHubRelease(modProjectName, tagName, isPreRelease, { 'vosk': voskRelease });
|
|
|
|
}
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
console.info(`Main download URL: ${releaseFiles['main'].downloadUrl}`);
|
2021-05-16 19:38:36 +02:00
|
|
|
|
|
|
|
const changelogPath = `${rootDir}/readme/changelog_android.md`;
|
|
|
|
await completeReleaseWithChangelog(changelogPath, version, tagName, 'Android', isPreRelease);
|
2018-01-17 22:16:13 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
main().catch((error) => {
|
|
|
|
console.error('Fatal error');
|
2018-01-17 22:16:13 +02:00
|
|
|
console.error(error);
|
2018-03-23 19:32:29 +02:00
|
|
|
process.exit(1);
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|