1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00

Tools: Allow uploading Android version to different repo

This commit is contained in:
Laurent Cozic 2023-05-11 14:48:33 +01:00
parent 19bdda25c6
commit 52bea115ac

View File

@ -1,11 +1,10 @@
import { execCommand } from '@joplin/utils';
import { copy, mkdirp, readFile, readFileSync, remove, stat, writeFile, writeFileSync } from 'fs-extra';
import { copy, mkdirp, move, readFile, readFileSync, remove, stat, writeFile, writeFileSync } from 'fs-extra';
import { execCommandVerbose, execCommandWithPipes, githubRelease, githubOauthToken, fileExists, gitPullTry, completeReleaseWithChangelog } from './tool-utils';
const path = require('path');
const fetch = require('node-fetch');
const uriTemplate = require('uri-template');
const projectName = 'joplin-android';
const rootDir = path.dirname(path.dirname(__dirname));
const rnDir = `${rootDir}/packages/app-mobile`;
const releaseDir = `${rnDir}/dist`;
@ -16,44 +15,46 @@ interface Release {
apkFilePath: string;
}
// class Patcher {
type PatcherCallback = (content: string)=> Promise<string>;
// private workDir_:string;
// private originalContents_:Record<string, string> = {};
// private removedFiles_:Record<string, string> = {};
class Patcher {
// public constructor(workDir:string) {
// this.workDir_ = workDir;
// }
private workDir_: string;
private originalContents_: Record<string, string> = {};
private removedFiles_: Record<string, string> = {};
// public removeFile = async (path:string) => {
// const targetPath = this.workDir_ + '/' + path.substring(1);
// await move(path, targetPath);
// this.removedFiles_[path] = targetPath;
// }
public constructor(workDir: string) {
this.workDir_ = workDir;
}
// public updateFileContent = async (path:string, callback:Function) => {
// const content = await readFile(path, 'utf8');
// this.originalContents_[path] = content;
// const newContent = callback(content);
// await writeFile(path, newContent);
// }
public removeFile = async (path: string) => {
const targetPath = `${this.workDir_}/${path.substring(1)}`;
await move(path, targetPath);
this.removedFiles_[path] = targetPath;
};
// public restore = async () => {
// for (const filename in this.originalContents_) {
// const content = this.originalContents_[filename];
// await writeFile(filename, content);
// }
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);
};
// for (const [originalPath, backupPath] of Object.entries(this.removedFiles_)) {
// await move(backupPath, originalPath);
// }
public restore = async () => {
for (const filename in this.originalContents_) {
const content = this.originalContents_[filename];
await writeFile(filename, content);
}
// this.removedFiles_ = {};
// this.originalContents_ = {};
// }
for (const [originalPath, backupPath] of Object.entries(this.removedFiles_)) {
await move(backupPath, originalPath);
}
// }
this.removedFiles_ = {};
this.originalContents_ = {};
};
}
function increaseGradleVersionCode(content: string) {
const newContent = content.replace(/versionCode\s+(\d+)/, (_a, versionCode: string) => {
@ -93,35 +94,32 @@ function gradleVersionName(content: string) {
return matches[1];
}
async function createRelease(name: string, tagName: string, version: string): Promise<Release> {
async function createRelease(projectName: string, name: string, tagName: string, version: string): Promise<Release> {
const originalContents: Record<string, string> = {};
const suffix = version + (name === 'main' ? '' : `-${name}`);
const patcher = new Patcher(`${rnDir}/patcher-work`);
console.info(`Creating release: ${suffix}`);
if (name === '32bit') {
const filename = `${rnDir}/android/app/build.gradle`;
let content = await readFile(filename, 'utf8');
originalContents[filename] = content;
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"');
await writeFile(filename, content);
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') {
{
const filename = `${rnDir}/services/voiceTyping/vosk.ts`;
originalContents[filename] = await readFile(filename, 'utf8');
const newContent = await readFile(`${rnDir}/services/voiceTyping/vosk.dummy.ts`, 'utf8');
await writeFile(filename, newContent);
}
{
const filename = `${rnDir}/package.json`;
let content = await readFile(filename, 'utf8');
originalContents[filename] = content;
await patcher.updateFileContent(`${rnDir}/android/app/build.gradle`, async (_content: string) => {
return readFile(`${rnDir}/services/voiceTyping/vosk.dummy.ts`, 'utf8');
});
await patcher.updateFileContent(`${rnDir}/android/app/build.gradle`, async (content: string) => {
content = content.replace(/\s+"react-native-vosk": ".*",/, '');
await writeFile(filename, content);
}
content = content.replace(/(\s+)"applicationId "net.cozic.joplin"/, '$1"applicationId "net.cozic.joplin-mod"');
return content;
});
}
const apkFilename = `joplin-v${suffix}.apk`;
@ -191,31 +189,7 @@ async function createRelease(name: string, tagName: string, version: string): Pr
};
}
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'];
const releaseNames = ['main', 'vosk'];
const releaseFiles: Record<string, Release> = {};
for (const releaseName of releaseNames) {
if (releaseNameOnly && releaseName !== releaseNameOnly) continue;
releaseFiles[releaseName] = await createRelease(releaseName, tagName, version);
}
const uploadToGitHubRelease = async (projectName: string, tagName: string, isPreRelease: boolean, releaseFiles: Record<string, Release>) => {
console.info(`Creating GitHub release ${tagName}...`);
const releaseOptions = { isPreRelease: isPreRelease };
@ -246,6 +220,41 @@ async function main() {
const uploadResponseObject = JSON.parse(uploadResponseText);
if (!uploadResponseObject || !uploadResponseObject.browser_download_url) throw new Error('Could not upload file to GitHub');
}
};
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'];
const releaseNames = ['main', 'vosk'];
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);
}
const voskRelease = releaseFiles['vosk'];
delete releaseFiles['vosk'];
await uploadToGitHubRelease(mainProjectName, tagName, isPreRelease, releaseFiles);
await uploadToGitHubRelease(modProjectName, tagName, isPreRelease, { 'vosk': voskRelease });
console.info(`Main download URL: ${releaseFiles['main'].downloadUrl}`);