Skip to content

Commit

Permalink
Revert "Revert "build: normalize packgae workflow. (#2611)" (#2664)"
Browse files Browse the repository at this point in the history
This reverts commit 40f56ee.
  • Loading branch information
richardo2016x committed Dec 6, 2024
1 parent d4e35cc commit a21955e
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 1,900 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/autobuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ on:
required: true
type: boolean
default: false
build_type:
required: true
description: Build Type
default: debug
options:
- pro
- debug


defaults:
run:
Expand Down Expand Up @@ -81,6 +89,7 @@ jobs:
RABBY_LARK_CHAT_URL: ${{ secrets.RABBY_LARK_CHAT_URL }}
RABBY_LARK_CHAT_SECRET: ${{ secrets.RABBY_LARK_CHAT_SECRET }}
notify_lark: ${{ inputs.notify_lark }}
build_type: ${{ inputs.build_type }}
# see more details on https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
ACTIONS_JOB_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
GIT_COMMIT_URL: ${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}
Expand Down
71 changes: 52 additions & 19 deletions build/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ const { prompt, BooleanPrompt } = require('enquirer');
const fs = require('fs-extra');
const shell = require('shelljs');
const pkg = require('../package.json');
const { createConsistentZip, get_md5_file } = require('../scripts/fns');

const PROJECT_ROOT = path.resolve(__dirname, '..');

const NO_BUILD=process.env.NO_BUILD;
const NO_PACK=process.env.NO_PACK;

function updateManifestVersion(version, p) {
const manifestPath = path.resolve(
PROJECT_ROOT,
Expand All @@ -18,18 +22,31 @@ function updateManifestVersion(version, p) {
fs.writeJSONSync(manifestPath, manifest, { spaces: 2 });
}

async function release([version, isDebug, isRelease]) {
async function release({ version, isRelease = false }) {
if (isRelease) {
shell.exec(`npm version ${version} --force`);
shell.exec('git add -A');
shell.exec(`git commit -m "[release] ${version}"`);
shell.exec(`git push origin refs/tags/v${version}`);
shell.exec('git push origin master');
}
return [version, isDebug, isRelease];
}

async function parse_git_info() {
const gitCommittish = shell.exec('git rev-parse HEAD', { silent: true }).stdout;
const gitUTC0Time = shell.exec('TZ=UTC0 git show --quiet --date="format-local:%Y-%m-%dT%H:%M:%S+00:00" --format="%cd"', { silent: true }).stdout;

return {
gitCommittish,
gitUTC0Time,
}
}

async function bundle() {
const { gitCommittish, gitUTC0Time } = await parse_git_info();

const cmd_prefix=`[Rabby::${gitCommittish.slice(0, 7)}]`

const oldVersion = pkg.version;
const plus1Version = oldVersion
.split('.')
Expand All @@ -38,42 +55,58 @@ async function bundle() {
const { version } = await prompt({
type: 'input',
name: 'version',
message: '[Rabby] Please input the release version:',
message: `${cmd_prefix} Please input the release version:`,
initial: plus1Version,
});

const isMV3 = await new BooleanPrompt({
message: '[Rabby] Do you want to release to MV3? (y/N)',
message: `${cmd_prefix} Do you want to release to MV3? (y/N)`,
initial: 'y',
}).run();

const isDebug = await new BooleanPrompt({
message: '[Rabby] Do you want to build a debug version? (y/N)',
message: `${cmd_prefix} Do you want to build a debug version? (y/N)`,
initial: 'N',
}).run();

const isRelease = await new BooleanPrompt({
message: '[Rabby] Do you want to release? (y/N)',
message: `${cmd_prefix} Do you want to create tag & push to remote? (y/N)`,
initial: 'N',
}).run();

const buildStr = isDebug ? 'build:debug' : 'build:pro';

updateManifestVersion(version, 'mv3');
updateManifestVersion(version, 'mv2');

if (isRelease) {
await release({ version, isDebug, isRelease });
}

// shell.env['sourcemap'] = true;
if (isMV3) {
shell.exec(`cross-env VERSION=${version} yarn ${buildStr}`);
} else {
shell.exec(`cross-env VERSION=${version} yarn ${buildStr}:mv2`);
if (NO_BUILD !== 'true') {
if (isMV3) {
shell.exec(`cross-env VERSION=${version} yarn ${buildStr}`);
} else {
shell.exec(`cross-env VERSION=${version} yarn ${buildStr}:mv2`);
}
}
shell.rm('-rf', './dist/*.js.map');
return [version, isDebug, isRelease];
}
async function packed([version, isDebug]) {
import('./zip.mjs').then((re) => {
re.createZipTask(
'dist/**',
`Rabby_v${version}${isDebug ? '_debug' : ''}.zip`

do_package: {
const hashed_zip = path.resolve(PROJECT_ROOT, `tmp/Rabby_v${version}${isDebug ? '_debug' : ''}.${gitCommittish.slice(0, 7)}.zip`);
const package_for_release = path.resolve(PROJECT_ROOT, `Rabby_v${version}${isDebug ? '_debug' : ''}.zip`);
await createConsistentZip(
path.resolve(PROJECT_ROOT, 'dist'),
hashed_zip,
gitUTC0Time,
{ silent: false, printFileTable: false }
);
});

shell.exec(`cp ${hashed_zip} ${package_for_release}`);

console.log(`${cmd_prefix} md5 of ${package_for_release}: ${await get_md5_file(package_for_release)}`);
}
}

bundle().then(release).then(packed);
bundle();
15 changes: 0 additions & 15 deletions build/zip.mjs

This file was deleted.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,6 @@
"file-loader": "6.2.0",
"fs-extra": "10.0.0",
"fs-readdir-recursive": "1.1.0",
"gulp": "4.0.2",
"gulp-sort": "2.0.0",
"gulp-zip": "5.1.0",
"html-webpack-plugin": "5.3.1",
"i18next": "23.4.1",
"jest": "29.7.0",
Expand Down
11 changes: 7 additions & 4 deletions scripts/autobuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ if [ -z $build_type ]; then
build_type="debug"
fi

PKG_SLUG="RabbyDebug";
[ $build_type == "pro" ] && PKG_SLUG="Rabby";

VERSION=$(node --eval="process.stdout.write(require('./package.json').version)");
RABBY_GIT_HASH=$(git rev-parse --short HEAD);
CURRENT_TIME=$(date +%Y%m%d%H%M%S);

TARGET_FILE=$project_dir/tmp/RabbyDebug-v${VERSION}-${RABBY_GIT_HASH}.zip;
TARGET_FILE=$project_dir/tmp/$PKG_SLUG-v${VERSION}-${RABBY_GIT_HASH}.zip;

echo "[pack] VERSION is $VERSION";

Expand All @@ -30,7 +33,7 @@ echo "[pack] built finished";
cd $project_dir;
rm -rf $project_dir/tmp/*.zip && mkdir -p $project_dir/tmp/;
git_utc0_time_linux=$(TZ=UTC0 git show --quiet --date='format-local:%Y-%m-%dT%H:%M:%S+00:00' --format="%cd")
node $script_dir/fns.js $project_dir/dist $TARGET_FILE $git_utc0_time_linux;
node $script_dir/pkg-zip.js $project_dir/dist $TARGET_FILE $git_utc0_time_linux;

cd $project_dir;

Expand All @@ -40,7 +43,7 @@ echo "[pack] (md5: $TARGET_FILE) $target_md5_value";

# upload to storage
if [ -z $NO_UPLOAD ]; then
DOWNLOAD_URL="https://download.rabby.io/autobuild/RabbyDebug-$CURRENT_TIME/RabbyDebug-v${VERSION}-${RABBY_GIT_HASH}.zip"
DOWNLOAD_URL="https://download.rabby.io/autobuild/$PKG_SLUG-$CURRENT_TIME/$PKG_SLUG-v${VERSION}-${RABBY_GIT_HASH}.zip"

if [ ! -z $CI ]; then
QUIET_PARASM="--quiet"
Expand All @@ -49,7 +52,7 @@ if [ -z $NO_UPLOAD ]; then
fi

echo "[pack] start upload...";
aws s3 cp $QUIET_PARASM $project_dir/tmp/ s3://$RABBY_BUILD_BUCKET/rabby/autobuild/RabbyDebug-$CURRENT_TIME --recursive --exclude="*" --include "*.zip" --acl public-read
aws s3 cp $QUIET_PARASM $project_dir/tmp/ s3://$RABBY_BUILD_BUCKET/rabby/autobuild/$PKG_SLUG-$CURRENT_TIME --recursive --exclude="*" --include "*.zip" --acl public-read
echo "[pack] uploaded. DOWNLOAD_URL is $DOWNLOAD_URL";

if [ ! -z $notify_lark ]; then
Expand Down
47 changes: 27 additions & 20 deletions scripts/fns.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@ const fs = require('fs');
const path = require('path');
const readdir = require('fs-readdir-recursive');
const archiver = require('archiver');
const chalk = require('chalk');

const loggerSlient = {
log: () => {},
error: () => {},
warn: () => {},
info: () => {},
table: () => {},
};

/**
*
* @returns {{
* @returns {Promise<{
* totalBytes: number;
* }}
* }>}
*/
async function createConsistentZip(
srcDir,
destZip,
gitUTC0Time = new Date(1980, 0, 1)
gitUTC0Time = new Date(1980, 0, 1),
{
printFileTable = true,
silent = false,
} = {}
) {
const logger = silent ? loggerSlient : console;

fs.mkdirSync(path.dirname(destZip), { recursive: true });
const output = fs.createWriteStream(destZip, { flags: 'w+' });
const archive = archiver('zip', {
Expand All @@ -23,11 +36,11 @@ async function createConsistentZip(

const pReturn = new Promise((resolve, reject) => {
output.on('close', () => {
console.log('[fns::close] archiver has been finalized and the output file descriptor has closed.');
logger.log('[fns::close] archiver has been finalized and the output file descriptor has closed.');
});

output.on('end', () => {
console.log('[fns::end] Data has been drained');
logger.log('[fns::end] Data has been drained');
});

output.on('error', (err) => {
Expand Down Expand Up @@ -59,7 +72,7 @@ async function createConsistentZip(

for (const item of allItems) {
const itemPath = path.join(srcDir, item);
const itemZipPath = path.join('dist/', item);
const itemZipPath = path.join('./', item);

const stat = fs.statSync(itemPath);

Expand All @@ -72,12 +85,14 @@ async function createConsistentZip(
zipPath: itemZipPath,
// filePath: itemPath,
});
// console.log(`\twill add ${chalk.green(itemZipPath)} \t\t ${chalk.yellow`(atime|mtime: ${gitUTC0Time})`}`);
// logger.log(`\twill add ${chalk.green(itemZipPath)} \t\t ${chalk.yellow`(atime|mtime: ${gitUTC0Time})`}`);
archive.append(fileStream, { name: itemZipPath, date: gitUTC0Time });
}
}

console.table(asciiTable);
if (printFileTable) {
logger.table(asciiTable);
}

archive.pipe(output);

Expand All @@ -86,12 +101,6 @@ async function createConsistentZip(
return pReturn;
}

const [, , srcDir, destZip, gitUTC0Time] = process.argv;

console.log(
`[fns] will pack ${srcDir} to ${destZip} with gitUTC0Time ${gitUTC0Time}`
);

function get_md5(buf) {
return require('crypto').createHash('md5').update(buf, 'utf8').digest('hex');
}
Expand All @@ -111,8 +120,6 @@ async function get_md5_file(filepath) {
});
}

createConsistentZip(srcDir, destZip, gitUTC0Time)
.then(async (result) => {
const md5Value = await get_md5_file(destZip);
console.log(`[fns] ZIP file created at ${destZip} (md5: ${chalk.yellow(md5Value)}, size: ${chalk.yellow(result.totalBytes)} bytes)`);
});
exports.get_md5_file = get_md5_file;
exports.createConsistentZip = createConsistentZip;

2 changes: 1 addition & 1 deletion scripts/pack-debug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pack_dist() {
local target_file=$project_dir/tmp/Rabby_v${app_ver}_debug.${git_committish}.zip
local git_utc0_time_linux=$(TZ=UTC0 git show --quiet --date='format-local:%Y-%m-%dT%H:%M:%S+00:00' --format="%cd")

node $script_dir/fns.js $project_dir/dist $target_file $git_utc0_time_linux;
node $script_dir/pkg-zip.js $project_dir/dist $target_file $git_utc0_time_linux;

get_md5 $target_file;
echo ""
Expand Down
16 changes: 16 additions & 0 deletions scripts/pkg-zip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const chalk = require('chalk');
const { get_md5_file, createConsistentZip } = require("./fns");

if (process.argv.length > 2) {
const [, , srcDir, destZip, gitUTC0Time] = process.argv;

console.log(
`[fns] will pack ${srcDir} to ${destZip} with gitUTC0Time ${gitUTC0Time}`
);

createConsistentZip(srcDir, destZip, gitUTC0Time)
.then(async (result) => {
const md5Value = await get_md5_file(destZip);
console.log(`[fns] ZIP file created at ${destZip} (md5: ${chalk.yellow(md5Value)}, size: ${chalk.yellow(result.totalBytes)} bytes)`);
});
}
Loading

0 comments on commit a21955e

Please sign in to comment.