2018-03-09 22:59:12 +02:00
|
|
|
const fs = require('fs-extra');
|
2018-10-13 12:09:03 +02:00
|
|
|
const { execCommand, githubRelease, githubOauthToken, isWindows, fileExists } = require('./tool-utils.js');
|
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
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
const rnDir = __dirname + '/../ReactNativeClient';
|
2018-01-17 22:16:13 +02:00
|
|
|
const rootDir = path.dirname(__dirname);
|
2018-03-09 22:59:12 +02:00
|
|
|
const releaseDir = rootDir + '/_releases';
|
2018-01-17 22:16:13 +02:00
|
|
|
|
|
|
|
function increaseGradleVersionCode(content) {
|
|
|
|
const newContent = content.replace(/versionCode\s+(\d+)/, function(a, versionCode, c) {
|
|
|
|
const n = Number(versionCode);
|
2018-03-09 22:59:12 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function increaseGradleVersionName(content) {
|
|
|
|
const newContent = content.replace(/(versionName\s+"\d+?\.\d+?\.)(\d+)"/, function(match, prefix, buildNum) {
|
|
|
|
const n = Number(buildNum);
|
2018-03-09 22:59:12 +02:00
|
|
|
if (isNaN(n) || !n) throw new Error('Invalid version code: ' + versionCode);
|
2018-01-17 22:16:13 +02:00
|
|
|
return prefix + (n + 1) + '"';
|
|
|
|
});
|
|
|
|
|
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() {
|
2018-03-09 22:59:12 +02:00
|
|
|
let content = fs.readFileSync(rnDir + '/android/app/build.gradle', 'utf8');
|
2018-01-17 22:16:13 +02:00
|
|
|
content = increaseGradleVersionCode(content);
|
|
|
|
content = increaseGradleVersionName(content);
|
2018-03-09 22:59:12 +02:00
|
|
|
fs.writeFileSync(rnDir + '/android/app/build.gradle', content);
|
2018-01-17 22:16:13 +02:00
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
function gradleVersionName(content) {
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
2018-12-29 04:12:23 +02:00
|
|
|
console.info('Updating version numbers in build.gradle...');
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
const projectName = 'joplin-android';
|
2018-01-17 22:16:13 +02:00
|
|
|
const newContent = updateGradleConfig();
|
|
|
|
const version = gradleVersionName(newContent);
|
2018-03-09 22:59:12 +02:00
|
|
|
const tagName = 'android-v' + version;
|
|
|
|
const apkFilename = 'joplin-v' + version + '.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);
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
console.info('Running from: ' + process.cwd());
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2018-12-29 04:12:23 +02:00
|
|
|
console.info('Building APK file v' + version + '...');
|
2018-10-13 00:25:11 +02:00
|
|
|
|
|
|
|
let restoreDir = null;
|
2018-12-29 04:12:23 +02:00
|
|
|
let apkBuildCmd = 'assembleRelease -PbuildDir=build'; // --console plain
|
2018-10-13 12:09:03 +02:00
|
|
|
if (await fileExists('/mnt/c/Windows/System32/cmd.exe')) {
|
2018-10-13 00:25:11 +02:00
|
|
|
apkBuildCmd = '/mnt/c/Windows/System32/cmd.exe /c "cd ReactNativeClient\\android && gradlew.bat ' + apkBuildCmd + '"';
|
|
|
|
} else {
|
|
|
|
process.chdir(rnDir + '/android');
|
|
|
|
apkBuildCmd = './gradlew ' + apkBuildCmd;
|
|
|
|
restoreDir = rootDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
// const output = await execCommand('/mnt/c/Windows/System32/cmd.exe /c "cd ReactNativeClient\\android && gradlew.bat assembleRelease -PbuildDir=build --console plain"');
|
2018-12-29 04:12:23 +02:00
|
|
|
console.info(apkBuildCmd);
|
2018-10-13 00:25:11 +02:00
|
|
|
const output = await execCommand(apkBuildCmd);
|
2018-01-17 22:16:13 +02:00
|
|
|
console.info(output);
|
|
|
|
|
2018-10-13 00:25:11 +02:00
|
|
|
if (restoreDir) process.chdir(restoreDir);
|
|
|
|
|
2018-01-17 22:16:13 +02:00
|
|
|
await fs.mkdirp(releaseDir);
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
console.info('Copying APK to ' + apkFilePath);
|
2018-10-02 19:19:27 +02:00
|
|
|
await fs.copy('ReactNativeClient/android/app/build/outputs/apk/release/app-release.apk', apkFilePath);
|
2019-02-24 19:26:46 +02:00
|
|
|
console.info('Copying APK to ' + releaseDir + '/joplin-latest.apk');
|
|
|
|
await fs.copy('ReactNativeClient/android/app/build/outputs/apk/release/app-release.apk', releaseDir + '/joplin-latest.apk');
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
console.info('Updating Readme URL...');
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
let readmeContent = await fs.readFile('README.md', 'utf8');
|
2018-02-07 19:54:06 +02:00
|
|
|
readmeContent = readmeContent.replace(/(https:\/\/github.com\/laurent22\/joplin-android\/releases\/download\/.*?\.apk)/, downloadUrl);
|
2018-03-09 22:59:12 +02:00
|
|
|
await fs.writeFile('README.md', readmeContent);
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
console.info(await execCommand('git pull'));
|
|
|
|
console.info(await execCommand('git add -A'));
|
2018-01-19 00:43:37 +02:00
|
|
|
console.info(await execCommand('git commit -m "Android release v' + version + '"'));
|
2018-03-09 22:59:12 +02:00
|
|
|
console.info(await execCommand('git tag ' + tagName));
|
|
|
|
console.info(await execCommand('git push'));
|
|
|
|
console.info(await execCommand('git push --tags'));
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
console.info('Creating GitHub release ' + tagName + '...');
|
2018-01-17 22:16:13 +02:00
|
|
|
|
2019-01-12 00:07:23 +02:00
|
|
|
const release = await githubRelease(projectName, tagName);
|
2018-02-04 19:42:33 +02:00
|
|
|
const uploadUrlTemplate = uriTemplate.parse(release.upload_url);
|
2018-01-17 22:16:13 +02:00
|
|
|
const uploadUrl = uploadUrlTemplate.expand({ name: apkFilename });
|
|
|
|
|
|
|
|
const binaryBody = await fs.readFile(apkFilePath);
|
|
|
|
|
2018-02-04 19:42:33 +02:00
|
|
|
const oauthToken = await githubOauthToken();
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
console.info('Uploading ' + apkFilename + ' to ' + uploadUrl);
|
2018-01-17 22:16:13 +02:00
|
|
|
|
|
|
|
const uploadResponse = await fetch(uploadUrl, {
|
2018-03-09 22:59:12 +02:00
|
|
|
method: 'POST',
|
2018-01-17 22:16:13 +02:00
|
|
|
body: binaryBody,
|
|
|
|
headers: {
|
2018-03-09 22:59:12 +02:00
|
|
|
'Content-Type': 'application/vnd.android.package-archive',
|
|
|
|
'Authorization': 'token ' + oauthToken,
|
|
|
|
'Content-Length': binaryBody.length,
|
2018-01-17 22:16:13 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const uploadResponseText = await uploadResponse.text();
|
|
|
|
console.info(uploadResponseText);
|
|
|
|
}
|
|
|
|
|
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);
|
2018-03-09 22:59:12 +02:00
|
|
|
});
|