Skip to content

Commit

Permalink
🐳 chore: Add script change version
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Sep 27, 2024
1 parent 1a4124c commit 180400f
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/macos_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ on:
- "docs/**"

env:
BETTER_MEDIA_INFO_VERSION: 0.4.0
BETTER_MEDIA_INFO_VERSION: 0.3.0

jobs:
build_package:
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ vite.config.ts.timestamp-*
*.dll
*.so
*.dylib

# ------------------------------------------------------------------------------
# Deno

deno.lock
112 changes: 112 additions & 0 deletions scripts/ts/change-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
* All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as fs from "https://deno.land/std/fs/mod.ts";
import * as path from "https://deno.land/std/path/mod.ts";

class ChangeVersion {
private productionVersion: string;
private rootDirPath: string;
private snapshotVersion: string;

constructor(productionVersion: string, snapshotVersion: string) {
this.productionVersion = productionVersion;
this.snapshotVersion = snapshotVersion;
this.rootDirPath = path.join(
path.dirname(path.fromFileUrl(import.meta.url)),
"../../"
);
}

private _change(
filePath: string,
patterns: Array<RegExp>,
isSnapshot = true
) {
const sourceFilePath = path.join(this.rootDirPath, filePath);
if (fs.existsSync(sourceFilePath)) {
console.info(`Processing ${sourceFilePath}.`);
} else {
console.error(`%c${sourceFilePath} is not found.`, "color: red");
return;
}
const newVersion = isSnapshot
? this.snapshotVersion
: this.productionVersion;
const positionGroups: Array<{ start: number; end: number }> = [];
const currentContent = Deno.readTextFileSync(sourceFilePath);
patterns.map((pattern) => {
[...currentContent.matchAll(pattern)].map((match) => {
const matchedString = match[0];
const currentVersion = match.groups["version"];
if (newVersion === currentVersion) {
console.warn(`%c Ignored ${matchedString}`, "color: yellow");
} else {
console.info(` ${matchedString} => ${newVersion}`);
const start = match.index + matchedString.indexOf(currentVersion);
const end = start + currentVersion.length;
positionGroups.push({ start: start, end: end });
}
});
});
if (positionGroups.length > 0) {
let newContent = "";
let lastEnd = 0;
positionGroups.sort((pg1, pg2) => pg1.start - pg2.start);
positionGroups.map((positionGroup) => {
if (positionGroup.start > lastEnd) {
newContent += currentContent.substring(lastEnd, positionGroup.start);
}
newContent += newVersion;
lastEnd = positionGroup.end;
});
if (lastEnd < currentContent.length) {
newContent += currentContent.substring(lastEnd, currentContent.length);
}
if (newContent !== currentContent) {
Deno.writeTextFileSync(sourceFilePath, newContent);
}
}
}

change() {
this._change("package.json", [
/^ "version":\s*"(?<version>\d+\.\d+\.\d+)"/gim,
]);
this._change(".github/workflows/linux_build.yml", [
/^\s*BETTER_MEDIA_INFO_VERSION:\s*(?<version>\d+\.\d+\.\d+)/gim,
]);
this._change(".github/workflows/macos_build.yml", [
/^\s*BETTER_MEDIA_INFO_VERSION:\s*(?<version>\d+\.\d+\.\d+)/gim,
]);
this._change(".github/workflows/windows_build.yml", [
/^\s*BETTER_MEDIA_INFO_VERSION:\s*(?<version>\d+\.\d+\.\d+)/gim,
]);
this._change("src-tauri/Cargo.toml", [
/^version\s*=\s*"(?<version>\d+\.\d+\.\d+)"/gim,
]);
this._change("src-tauri/tauri.conf.json", [
/^ "version":\s*"(?<version>\d+\.\d+\.\d+)"/gim,
]);
this._change("src-tauri/src/main.rs", [
/"BetterMediaInfo v(?<version>\d+\.\d+\.\d+)"/gi,
]);
}
}

const changeVersion = new ChangeVersion("0.2.0", "0.3.0");
changeVersion.change();

0 comments on commit 180400f

Please sign in to comment.