diff --git a/utils/typescript/version/scripts/change-version.ts b/utils/typescript/version/scripts/change-version.ts new file mode 100644 index 0000000..e5892e9 --- /dev/null +++ b/utils/typescript/version/scripts/change-version.ts @@ -0,0 +1,61 @@ +import path from 'path'; +import fs from 'fs'; + +export function changePackageJsonMainVersion(path: string, version: string): void { + const contents = fs.readFileSync(path, 'utf-8'); + + const regex = /"version": "(\d+\.\d+\.\d+)",/; + const match = contents.match(regex); + if (!match) { + throw new Error(`Could not find version in ${path}`); + } + const oldVersion = match[1]; + const newContents = contents.replace(oldVersion, version); + fs.writeFileSync(path, newContents); + console.log(`Updated ${path} version`); +} + +export function changePyProjectTomlVersion(path: string, version: string): void { + const contents = fs.readFileSync(path, 'utf-8'); + + const regex = /version = "(\d+\.\d+\.\d+)"/; + const match = contents.match(regex); + if (!match) { + throw new Error(`Could not find version in ${path}`); + } + const oldVersion = match[1]; + const newContents = contents.replace(oldVersion, version); + fs.writeFileSync(path, newContents); + console.log(`Updated ${path} version`); +} + +export function changePythonRequirementsVersion(path: string, key: string, version: string): void { + const contents = fs.readFileSync(path, 'utf-8'); + + const regex = new RegExp(`${key}==\\d+\\.\\d+\\.\\d+`); + const match = contents.match(regex); + + if (!match) { + throw new Error(`Could not find ${key} in ${path}`); + } + + const oldVersion = match[0]; + const newContents = contents.replace(oldVersion, `${key}==${version}`); + fs.writeFileSync(path, newContents); + console.log(`Updated ${path} version`); +} + +export function changePackageJsonDependenciesVersion(path: string, key: string, version: string): void { + const contents = fs.readFileSync(path, 'utf-8'); + + const regex = new RegExp(`"${key}": "\\d+\\.\\d+\\.\\d+"`); + const match = contents.match(regex); + if (!match) { + throw new Error(`Could not find ${key} in ${path}`); + } + + const oldVersion = match[0]; + const newContents = contents.replace(oldVersion, `"${key}": "${version}"`); + fs.writeFileSync(path, newContents); + console.log(`Updated ${path} version`); +} diff --git a/utils/typescript/version/scripts/version.ts b/utils/typescript/version/scripts/version.ts index cdb9b6a..5471e0a 100644 --- a/utils/typescript/version/scripts/version.ts +++ b/utils/typescript/version/scripts/version.ts @@ -1,6 +1,14 @@ import { program } from 'commander'; import path from 'path'; import fs from 'fs'; +import { + changePackageJsonDependenciesVersion, + changePackageJsonMainVersion, + changePyProjectTomlVersion, + changePythonRequirementsVersion, +} from './change-version'; + +const INVALID_PATH = 'INVALID_PATH'; const ROOT_PATH = path.resolve(__dirname, '..', '..', '..', '..'); const UNITY_SERVER_VERSION_FILE_PATH = path.resolve(ROOT_PATH, 'engine/unity/Runtime/Private/Network/Server.cs'); @@ -9,6 +17,14 @@ const UNITY_SERVER_DOCS_GITREF_VERSION_FILE_PATH = path.resolve(ROOT_PATH, 'docs const PYTHON_CLIENT_VERSION_FILE_PATH = path.resolve(ROOT_PATH, 'client/python/gamium/pyproject.toml'); const TYPESCRIPT_CLIENT_VERSION_FILE_PATH = path.resolve(ROOT_PATH, 'client/typescript/gamium/package.json'); +const GAMIUM_UNITY_SAMPLES_PATH = process.env.GAMIUM_UNITY_SAMPLES_PATH ?? INVALID_PATH; +const GAMIUM_UNITY_SAMPLES_PYTHON_REQUIREMENTS_PATH = path.resolve(GAMIUM_UNITY_SAMPLES_PATH, 'client/python/requirements.txt'); +const GAMIUM_UNITY_SAMPLES_TYPESCRIPT_PACKAGEJSON_PATH = path.resolve(GAMIUM_UNITY_SAMPLES_PATH, 'client/typescript/package.json'); + +const DOGU_ROUTINE_EXAMPLES_PATH = process.env.DOGU_ROUTINE_EXAMPLES_PATH ?? INVALID_PATH; +const DOGU_ROUTINE_EXAMPLES_PYTHON_REQUIREMENTS_PATH = path.resolve(DOGU_ROUTINE_EXAMPLES_PATH, 'gamium/python/pytest/requirements.txt'); +const DOGU_ROUTINE_EXAMPLES_TYPESCRIPT_PACKAGEJSON_PATH = path.resolve(DOGU_ROUTINE_EXAMPLES_PATH, 'gamium/typescript/jest/package.json'); + function changeUnityServerVersion(version: string) { const contents = fs.readFileSync(UNITY_SERVER_VERSION_FILE_PATH, 'utf-8'); @@ -24,17 +40,7 @@ function changeUnityServerVersion(version: string) { } function changeUnityServerPackageVersion(version: string) { - const contents = fs.readFileSync(UNITY_SERVER_PACKAGE_VERSION_FILE_PATH, 'utf-8'); - - const regex = /"version": "(\d+\.\d+\.\d+)",/; - const match = contents.match(regex); - if (!match) { - throw new Error('Could not find version in Unity Server Package'); - } - const oldVersion = match[1]; - const newContents = contents.replace(oldVersion, version); - fs.writeFileSync(UNITY_SERVER_PACKAGE_VERSION_FILE_PATH, newContents); - console.log('Updated Unity Server Package version'); + changePackageJsonMainVersion(UNITY_SERVER_PACKAGE_VERSION_FILE_PATH, version); } function changeUnityServerDocsPackageVersion(version: string) { @@ -53,31 +59,11 @@ function changeUnityServerDocsPackageVersion(version: string) { } function changePythonClientVersion(version: string) { - const contents = fs.readFileSync(PYTHON_CLIENT_VERSION_FILE_PATH, 'utf-8'); - - const regex = /version = "(\d+\.\d+\.\d+)"/; - const match = contents.match(regex); - if (!match) { - throw new Error('Could not find version in Python Client'); - } - const oldVersion = match[1]; - const newContents = contents.replace(oldVersion, version); - fs.writeFileSync(PYTHON_CLIENT_VERSION_FILE_PATH, newContents); - console.log('Updated Python Client version'); + changePyProjectTomlVersion(PYTHON_CLIENT_VERSION_FILE_PATH, version); } function changeTypescriptClientVersion(version: string) { - const contents = fs.readFileSync(TYPESCRIPT_CLIENT_VERSION_FILE_PATH, 'utf-8'); - - const regex = /"version": "(\d+\.\d+\.\d+)",/; - const match = contents.match(regex); - if (!match) { - throw new Error('Could not find version in Typescript Client'); - } - const oldVersion = match[1]; - const newContents = contents.replace(oldVersion, version); - fs.writeFileSync(TYPESCRIPT_CLIENT_VERSION_FILE_PATH, newContents); - console.log('Updated Typescript Client version'); + changePackageJsonMainVersion(TYPESCRIPT_CLIENT_VERSION_FILE_PATH, version); } program.name('version'); @@ -86,12 +72,26 @@ program .command('upgrade') .argument('', 'version to upgrade to') .action((version) => { - console.log(`Upgrading to ${version}`); + console.log(`>> Upgrading to ${version} << `); changeUnityServerVersion(version); changeUnityServerPackageVersion(version); changeUnityServerDocsPackageVersion(version); changePythonClientVersion(version); changeTypescriptClientVersion(version); + + if (INVALID_PATH === GAMIUM_UNITY_SAMPLES_PATH) { + console.warn('[WARNING] GAMIUM_UNITY_SAMPLES_PATH is not set, skipping, please set it to upgrade the samples'); + } else { + changePythonRequirementsVersion(GAMIUM_UNITY_SAMPLES_PYTHON_REQUIREMENTS_PATH, 'gamium', version); + changePackageJsonDependenciesVersion(GAMIUM_UNITY_SAMPLES_TYPESCRIPT_PACKAGEJSON_PATH, 'gamium', version); + } + + if (INVALID_PATH === DOGU_ROUTINE_EXAMPLES_PATH) { + console.warn('[WARNING] DOGU_ROUTINE_EXAMPLES_PATH is not set, skipping, please set it to upgrade the examples'); + } else { + changePythonRequirementsVersion(DOGU_ROUTINE_EXAMPLES_PYTHON_REQUIREMENTS_PATH, 'gamium', version); + changePackageJsonDependenciesVersion(DOGU_ROUTINE_EXAMPLES_TYPESCRIPT_PACKAGEJSON_PATH, 'gamium', version); + } }); program.parse();