Skip to content

Commit

Permalink
build: trial.
Browse files Browse the repository at this point in the history
  • Loading branch information
richardo2016x committed Oct 17, 2024
1 parent 151c421 commit daf37a0
Show file tree
Hide file tree
Showing 6 changed files with 494 additions and 99 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
"devDependencies": {
"@sentry/webpack-plugin": "2.16.1",
"@svgr/webpack": "5.5.0",
"@types/archiver": "^6.0.2",
"@types/chrome": "^0.0.263",
"@types/events": "3.0.0",
"@types/jest": "27.4.0",
Expand All @@ -167,6 +168,7 @@
"@typescript-eslint/typescript-estree": "7.1.1",
"@welldone-software/why-did-you-render": "6.2.1",
"antd-dayjs-webpack-plugin": "1.0.6",
"archiver": "^7.0.1",
"autoprefixer": "latest",
"copy-webpack-plugin": "11.0.0",
"craco-antd": "1.19.0",
Expand All @@ -183,6 +185,7 @@
"eslint-webpack-plugin": "2.5.4",
"file-loader": "6.2.0",
"fs-extra": "10.0.0",
"fs-readdir-recursive": "^1.1.0",
"html-webpack-plugin": "5.3.1",
"i18next": "23.4.1",
"jest": "29.7.0",
Expand Down
30 changes: 16 additions & 14 deletions scripts/autobuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ systype=$(uname -s)

. $script_dir/fns.sh --source-only

export VERSION=$(node --eval="process.stdout.write(require('./package.json').version)");
export RABBY_GIT_HASH=$(git rev-parse --short HEAD);
export CURRENT_TIME=$(date +%Y%m%d%H%M%S);
# debug, pro
if [ -z $build_type ]; then
build_type="debug"
fi

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;

Expand All @@ -18,23 +23,20 @@ echo "[pack] VERSION is $VERSION";
# for mingw, download zip.exe from http://stahlworks.com/dev/index.php?tool=zipunzip and add to your path
if [ -z $NO_BUILD ]; then
yarn;
yarn build:debug;
yarn build:${build_type};
fi
echo "[pack] built finished";

cd $project_dir;
rm -f $TARGET_FILE;
pack_dist_to_zip $TARGET_FILE;
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;

cd $project_dir;

systype=$(uname -s)
if [ $systype = "Darwin" ]; then
md5_value=$(md5 -q $TARGET_FILE);
elif [ $systype = "Linux" ]; then
md5_value=$(md5sum $TARGET_FILE | awk '{ print $1 }');
fi
echo "[pack] (md5: $TARGET_FILE) $md5_value";
get_md5 $TARGET_FILE;
target_md5_value=$last_md5_value;
echo "[pack] (md5: $TARGET_FILE) $target_md5_value";

# upload to storage
if [ -z $NO_UPLOAD ]; then
Expand All @@ -47,13 +49,13 @@ 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/RabbyDebug-$CURRENT_TIME --recursive --exclude="*" --include "*.zip" --acl public-read
echo "[pack] uploaded. DOWNLOAD_URL is $DOWNLOAD_URL";

if [ ! -z $notify_lark ]; then
echo "[pack] update latest link...";

node ./scripts/notify-lark.js "$DOWNLOAD_URL" "$md5_value"
node ./scripts/notify-lark.js "$DOWNLOAD_URL" "$target_md5_value"
else
echo "[pack] skip notify.";
fi
Expand Down
118 changes: 118 additions & 0 deletions scripts/fns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
const fs = require('fs');
const path = require('path');
const readdir = require('fs-readdir-recursive');
const archiver = require('archiver');
const chalk = require('chalk');

/**
*
* @returns {{
* totalBytes: number;
* }}
*/
async function createConsistentZip(
srcDir,
destZip,
gitUTC0Time = new Date(1980, 0, 1)
) {
fs.mkdirSync(path.dirname(destZip), { recursive: true });
const output = fs.createWriteStream(destZip, { flags: 'w+' });
const archive = archiver('zip', {
zlib: { level: 9 }
});

const pReturn = new Promise((resolve, reject) => {
output.on('close', () => {
console.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');
});

output.on('error', (err) => {
console.error(`[fns::error] Error creating ZIP file: ${err.message}`);
reject(err);
});

archive.on('finish', async () => {
resolve({
totalBytes: archive.pointer()
});
});

archive.on('error', (err) => {
console.error(`[fns::return] Error creating ZIP file: ${err.message}`);
reject(err);
});
});

const allItems = readdir(srcDir);
/**
* @type {{
* filePath: string;
* zipPath: string;
* time: Date;
* }[]}
*/
const asciiTable = [];

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

const stat = fs.statSync(itemPath);

if (stat.isDirectory()) {
await addDirectoryToZip(itemPath, itemZipPath);
} else if (stat.isFile()) {
const fileStream = fs.createReadStream(itemPath);
asciiTable.push({
time: gitUTC0Time,
zipPath: itemZipPath,
// filePath: itemPath,
});
// console.log(`\twill add ${chalk.green(itemZipPath)} \t\t ${chalk.yellow`(atime|mtime: ${gitUTC0Time})`}`);
archive.append(fileStream, { name: itemZipPath, date: gitUTC0Time });
}
}

console.table(asciiTable);

archive.pipe(output);

await archive.finalize();

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');
}

async function get_md5_file(filepath) {
const stream = fs.createReadStream(path.resolve(__dirname, filepath));
const hash = require('crypto').createHash('md5');
stream.on('data', chunk => {
hash.update(chunk, 'utf8');
});

return new Promise((resolve, reject) => {
stream.on('end', () => {
resolve(hash.digest('hex'));
});
stream.on('error', reject);
});
}

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)`);
});
47 changes: 10 additions & 37 deletions scripts/fns.sh
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
#!/usr/bin/env bash
script_dir="$( cd "$( dirname "$0" )" && pwd )"
project_dir=$(dirname "$script_dir")
systype=$(uname -s)

pack_dist_to_zip() {
local entry_dir=$project_dir;
local files=$(find "$entry_dir/dist" -type f | sort)
local dest_zip=$1

if [ -z $dest_zip ]; then
echo "dest_zip is required"
# read last_md5_value
get_md5() {
local TARGET_FILE=$1
if [ ! -f $TARGET_FILE ]; then
echo "[get_md5] file not found: $TARGET_FILE"
exit 1
fi

if [ -z $build_type ]; then
build_type="debug"
local systype=$(uname -s)
if [ $systype = "Darwin" ]; then
export last_md5_value=$(md5 -q $TARGET_FILE);
elif [ $systype = "Linux" ]; then
export last_md5_value=$(md5sum $TARGET_FILE | awk '{ print $1 }');
fi

git_committish=$(git log --format="%h" -n 1)
# e.g. '2024-01-01 01:01:01 +0000'
git_tz_time_linux=$(git log --format="%cd" -n 1 --date=format:'%Y-%m-%d %H:%M:%S %z')
# SetFile format, e.g. '01/01/2024 01:01:01 +0000'
git_tz_time_setfile=$(git log --format="%cd" -n 1 --date=format:'%m/%d/%Y %H:%M:%S %z')

for file in $files; do
if [ "$systype" = "Darwin" ]; then
SetFile -d "$git_tz_time_setfile" "$file"
SetFile -m "$git_tz_time_setfile" "$file"
elif [ "$systype" = "Linux" ]; then
touch -a -d "$git_tz_time_linux" "$file"
touch -m -d "$git_tz_time_linux" "$file"
fi

local relname=${file#$entry_dir/}
zip -X "$dest_zip" "$relname"
done

echo "git_tz_time_setfile is $git_tz_time_setfile"
echo "git_tz_time_linux is $git_tz_time_linux"
}
62 changes: 19 additions & 43 deletions scripts/pack-debug.sh
Original file line number Diff line number Diff line change
@@ -1,49 +1,25 @@
#!/bin/sh
#!/usr/bin/env bash

script_dir="$( cd "$( dirname "$0" )" && pwd )"
project_dir=$(dirname "$script_dir")
systype=$(uname -s)
if [ -z $build_type ]; then
build_type="debug"
fi
build_type="debug"

. $script_dir/fns.sh --source-only

cd $project_dir;
app_ver=$(node -p "require('./package.json').version")
git_committish=$(git log --format="%h" -n 1)
# e.g. '2024-01-01 01:01:01 +0000'
git_tz_time_linux=$(git log --format="%cd" -n 1 --date=format:'%Y-%m-%d %H:%M:%S %z')
# SetFile format, e.g. '01/01/2024 01:01:01 +0000'
git_tz_time_setfile=$(git log --format="%cd" -n 1 --date=format:'%m/%d/%Y %H:%M:%S %z')

pack_dist() {
rm -rf $project_dir/tmp/*.zip && mkdir -p $project_dir/tmp/;

local entry_dir=$project_dir;

if [ ! -d "$entry_dir/dist" ]; then
echo "dist not exists"
exit 1
fi

local files=$(find "$entry_dir/dist" -type f | sort)
local dest_zip="$project_dir/tmp/Rabby_v${app_ver}_${build_type}.${git_committish}.zip"
for file in $files; do
if [ "$systype" = "Darwin" ]; then
SetFile -d "$git_tz_time_setfile" "$file"
SetFile -m "$git_tz_time_setfile" "$file"
elif [ "$systype" = "Linux" ]; then
touch -c -d "$git_tz_time_linux" "$file"
touch -c -m "$git_tz_time_linux" "$file"
fi

local relname=${file#$entry_dir/}
zip -X "$dest_zip" "$relname"
done
local git_committish=$(git log --format="%h" -n 1)
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")

echo "git_tz_time_setfile is $git_tz_time_setfile"
echo "git_tz_time_linux is $git_tz_time_linux"
cd $project_dir;
node $script_dir/fns.js $project_dir/dist $target_file $git_utc0_time_linux;

get_md5 $target_file;
echo ""
echo "[pack] (md5: $target_file) $last_md5_value";
}

build() {
Expand All @@ -56,11 +32,11 @@ else
build && pack_dist;
fi

# case $systype in
# "Darwin")
# open ./tmp/
# ;;
# MSYS_NT*|MINGW*)
# start "" .\\tmp
# ;;
# esac
case $systype in
"Darwin")
open ./tmp/
;;
MSYS_NT*|MINGW*)
start "" .\\tmp
;;
esac
Loading

0 comments on commit daf37a0

Please sign in to comment.