diff options
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/bind_package_dependencies.js | 50 | ||||
| -rw-r--r-- | scripts/bind_package_version.js | 42 | ||||
| -rw-r--r-- | scripts/build_sandbox.js | 8 | ||||
| -rw-r--r-- | scripts/check-versions.js | 65 | ||||
| -rw-r--r-- | scripts/clean.js | 1 | ||||
| -rw-r--r-- | scripts/prepare-core-package.js | 37 | ||||
| -rw-r--r-- | scripts/prepare-package.js | 51 | ||||
| -rw-r--r-- | scripts/prepublish.js | 9 | ||||
| -rw-r--r-- | scripts/publish-sandbox.js | 24 | ||||
| -rw-r--r-- | scripts/version.js | 68 |
10 files changed, 206 insertions, 149 deletions
diff --git a/scripts/bind_package_dependencies.js b/scripts/bind_package_dependencies.js deleted file mode 100644 index eb9a4cc6..00000000 --- a/scripts/bind_package_dependencies.js +++ /dev/null @@ -1,50 +0,0 @@ -/** - * @license - * Copyright 2025 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -import fs from 'node:fs'; -import path from 'node:path'; -import _ from 'lodash'; - -function bindPackageDependencies() { - const scriptDir = process.cwd(); - const currentPkgJsonPath = path.join(scriptDir, 'package.json'); - const currentPkg = JSON.parse(fs.readFileSync(currentPkgJsonPath)); - // assume packages are all under /<repo_root>/packages/ - const packagesDir = path.join(path.dirname(scriptDir)); - - const geminiCodePkgs = fs - .readdirSync(packagesDir) - .filter( - (name) => - fs.statSync(path.join(packagesDir, name)).isDirectory() && - fs.existsSync(path.join(packagesDir, name, 'package.json')), - ) - .map((packageDirname) => { - const packageJsonPath = path.join( - packagesDir, - packageDirname, - 'package.json', - ); - return JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); - }) - .reduce((pkgs, pkg) => ({ ...pkgs, [pkg.name]: pkg }), {}); - currentPkg.dependencies = _.mapValues( - currentPkg.dependencies, - (value, key) => { - if (geminiCodePkgs[key]) { - console.log( - `Package ${currentPkg.name} has a dependency on ${key}. Updating dependent version.`, - ); - return geminiCodePkgs[key].version; - } - return value; - }, - ); - const updatedPkgJson = JSON.stringify(currentPkg, null, 2) + '\n'; - fs.writeFileSync(currentPkgJsonPath, updatedPkgJson); -} - -bindPackageDependencies(); diff --git a/scripts/bind_package_version.js b/scripts/bind_package_version.js deleted file mode 100644 index 4e7a2ff7..00000000 --- a/scripts/bind_package_version.js +++ /dev/null @@ -1,42 +0,0 @@ -/** - * @license - * Copyright 2025 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -import fs from 'node:fs'; -import path from 'node:path'; - -// Assuming script is run from a package directory (e.g., packages/cli) -const packageDir = process.cwd(); -const rootDir = path.join(packageDir, '..', '..'); // Go up two directories to find the repo root - -function getRepoVersion() { - // Read root package.json - const rootPackageJsonPath = path.join(rootDir, 'package.json'); - const rootPackage = JSON.parse(fs.readFileSync(rootPackageJsonPath, 'utf8')); - return rootPackage.version; // This version is now expected to be the full version string -} - -const newVersion = getRepoVersion(); -console.log(`Setting package version to: ${newVersion}`); - -const packageJsonPath = path.join(packageDir, 'package.json'); - -if (fs.existsSync(packageJsonPath)) { - console.log(`Updating version for ${packageJsonPath}`); - const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); - packageJson.version = newVersion; - fs.writeFileSync( - packageJsonPath, - JSON.stringify(packageJson, null, 2) + '\n', - 'utf8', - ); -} else { - console.error( - `Error: package.json not found in the current directory: ${packageJsonPath}`, - ); - process.exit(1); -} - -console.log('Done.'); diff --git a/scripts/build_sandbox.js b/scripts/build_sandbox.js index e2c7a224..962f99d9 100644 --- a/scripts/build_sandbox.js +++ b/scripts/build_sandbox.js @@ -123,13 +123,17 @@ function buildImage(imageName, dockerfile) { readFileSync(join(process.cwd(), 'package.json'), 'utf-8'), ).version; + const imageTag = + process.env.GEMINI_SANDBOX_IMAGE_TAG || imageName.split(':')[1]; + const finalImageName = `${imageName.split(':')[0]}:${imageTag}`; + execSync( `${buildCommand} ${ process.env.BUILD_SANDBOX_FLAGS || '' - } --build-arg CLI_VERSION_ARG=${npmPackageVersion} -f "${dockerfile}" -t "${imageName}" .`, + } --build-arg CLI_VERSION_ARG=${npmPackageVersion} -f "${dockerfile}" -t "${finalImageName}" .`, { stdio: buildStdout, shell: '/bin/bash' }, ); - console.log(`built ${imageName}`); + console.log(`built ${finalImageName}`); } if (baseImage && baseDockerfile) { diff --git a/scripts/check-versions.js b/scripts/check-versions.js new file mode 100644 index 00000000..230743a0 --- /dev/null +++ b/scripts/check-versions.js @@ -0,0 +1,65 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { readFileSync } from 'fs'; +import path from 'path'; + +function readPackageJson(dir) { + const p = path.join(dir, 'package.json'); + return JSON.parse(readFileSync(p, 'utf-8')); +} + +const root = readPackageJson('.'); +const cli = readPackageJson('packages/cli'); +const core = readPackageJson('packages/core'); + +const errors = []; + +console.log('Checking version consistency...'); + +// 1. Check that all package versions are the same. +if (root.version !== cli.version || root.version !== core.version) { + errors.push( + `Version mismatch: root (${root.version}), cli (${cli.version}), core (${core.version})`, + ); +} else { + console.log(`- All packages are at version ${root.version}.`); +} + +// 2. Check that the cli's dependency on core matches the core version. +const coreDepVersion = cli.dependencies['@google/gemini-cli-core']; +const expectedCoreVersion = `^${core.version}`; +if ( + coreDepVersion !== expectedCoreVersion && + coreDepVersion !== 'file:../core' +) { + errors.push( + `CLI dependency on core is wrong: expected ${expectedCoreVersion} or "file:../core", got ${coreDepVersion}`, + ); +} else { + console.log(`- CLI dependency on core (${coreDepVersion}) is correct.`); +} + +// 3. Check that the sandbox image tag matches the root version. +const imageUri = root.config.sandboxImageUri; +const imageTag = imageUri.split(':').pop(); +if (imageTag !== root.version) { + errors.push( + `Sandbox image tag mismatch: expected ${root.version}, got ${imageTag}`, + ); +} else { + console.log(`- Sandbox image tag (${imageTag}) is correct.`); +} + +if (errors.length > 0) { + console.error('\nVersion consistency checks failed:'); + for (const error of errors) { + console.error(`- ${error}`); + } + process.exit(1); +} + +console.log('\nAll version checks passed!'); diff --git a/scripts/clean.js b/scripts/clean.js index bb2cb678..d53d097e 100644 --- a/scripts/clean.js +++ b/scripts/clean.js @@ -27,6 +27,7 @@ const root = join(__dirname, '..'); // remove npm install/build artifacts rmSync(join(root, 'node_modules'), { recursive: true, force: true }); +rmSync(join(root, 'bundle'), { recursive: true, force: true }); rmSync(join(root, 'packages/cli/src/generated/'), { recursive: true, force: true, diff --git a/scripts/prepare-core-package.js b/scripts/prepare-core-package.js deleted file mode 100644 index 7d2e1e08..00000000 --- a/scripts/prepare-core-package.js +++ /dev/null @@ -1,37 +0,0 @@ -/** - * @license - * Copyright 2025 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -import fs from 'fs'; -import path from 'path'; -import { fileURLToPath } from 'url'; - -// ES module equivalent of __dirname -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); - -// Copy README.md to packages/core -const rootReadmePath = path.resolve(__dirname, '../README.md'); -const coreReadmePath = path.resolve(__dirname, '../packages/core/README.md'); - -try { - fs.copyFileSync(rootReadmePath, coreReadmePath); - console.log('Copied root README.md to packages/core/'); -} catch (err) { - console.error('Error copying README.md:', err); - process.exit(1); -} - -// Copy README.md to packages/cli -const rootLicensePath = path.resolve(__dirname, '../LICENSE'); -const coreLicensePath = path.resolve(__dirname, '../packages/core/LICENSE'); - -try { - fs.copyFileSync(rootLicensePath, coreLicensePath); - console.log('Copied root LICENSE to packages/core/'); -} catch (err) { - console.error('Error copying LICENSE:', err); - process.exit(1); -} diff --git a/scripts/prepare-package.js b/scripts/prepare-package.js new file mode 100644 index 00000000..5498499b --- /dev/null +++ b/scripts/prepare-package.js @@ -0,0 +1,51 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +// ES module equivalent of __dirname +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const rootDir = path.resolve(__dirname, '..'); + +function copyFiles(packageName, filesToCopy) { + const packageDir = path.resolve(rootDir, 'packages', packageName); + if (!fs.existsSync(packageDir)) { + console.error(`Error: Package directory not found at ${packageDir}`); + process.exit(1); + } + + console.log(`Preparing package: ${packageName}`); + for (const [source, dest] of Object.entries(filesToCopy)) { + const sourcePath = path.resolve(rootDir, source); + const destPath = path.resolve(packageDir, dest); + try { + fs.copyFileSync(sourcePath, destPath); + console.log(`Copied ${source} to packages/${packageName}/`); + } catch (err) { + console.error(`Error copying ${source}:`, err); + process.exit(1); + } + } +} + +// Prepare 'core' package +copyFiles('core', { + 'README.md': 'README.md', + LICENSE: 'LICENSE', + '.npmrc': '.npmrc', +}); + +// Prepare 'cli' package +copyFiles('cli', { + 'README.md': 'README.md', + LICENSE: 'LICENSE', +}); + +console.log('Successfully prepared all packages.'); diff --git a/scripts/prepublish.js b/scripts/prepublish.js index f97e450f..e30901b6 100644 --- a/scripts/prepublish.js +++ b/scripts/prepublish.js @@ -19,9 +19,14 @@ if (!fs.existsSync(packageJsonPath)) { errors.push(`Error: package.json not found in ${process.cwd()}`); } else { const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); - if (packageJson.repository !== 'google-gemini/gemini-cli') { + if ( + !packageJson.repository || + typeof packageJson.repository !== 'object' || + packageJson.repository.type !== 'git' || + !packageJson.repository.url.includes('google-gemini/gemini-cli') + ) { errors.push( - `Error: The "repository" field in ${packageJsonPath} must be "google-gemini/gemini-cli".`, + `Error: The "repository" field in ${packageJsonPath} must be an object pointing to the "google-gemini/gemini-cli" git repository.`, ); } } diff --git a/scripts/publish-sandbox.js b/scripts/publish-sandbox.js index 916089be..079874ce 100644 --- a/scripts/publish-sandbox.js +++ b/scripts/publish-sandbox.js @@ -20,33 +20,25 @@ import { execSync } from 'child_process'; const { - SANDBOX_IMAGE_REGISTRY, - SANDBOX_IMAGE_NAME, - npm_package_version, + npm_package_config_sandboxImageUri, DOCKER_DRY_RUN, + GEMINI_SANDBOX_IMAGE_TAG, } = process.env; -if (!SANDBOX_IMAGE_REGISTRY) { +if (!npm_package_config_sandboxImageUri) { console.error( - 'Error: SANDBOX_IMAGE_REGISTRY environment variable is not set.', + 'Error: npm_package_config_sandboxImageUri environment variable is not set (should be run via npm).', ); process.exit(1); } -if (!SANDBOX_IMAGE_NAME) { - console.error('Error: SANDBOX_IMAGE_NAME environment variable is not set.'); - process.exit(1); -} +let imageUri = npm_package_config_sandboxImageUri; -if (!npm_package_version) { - console.error( - 'Error: npm_package_version environment variable is not set (should be run via npm).', - ); - process.exit(1); +if (GEMINI_SANDBOX_IMAGE_TAG) { + const [baseUri] = imageUri.split(':'); + imageUri = `${baseUri}:${GEMINI_SANDBOX_IMAGE_TAG}`; } -const imageUri = `${SANDBOX_IMAGE_REGISTRY}/${SANDBOX_IMAGE_NAME}:${npm_package_version}`; - if (DOCKER_DRY_RUN) { console.log(`DRY RUN: Would execute: docker push "${imageUri}"`); } else { diff --git a/scripts/version.js b/scripts/version.js new file mode 100644 index 00000000..a5d2c203 --- /dev/null +++ b/scripts/version.js @@ -0,0 +1,68 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { execSync } from 'child_process'; +import { readFileSync, writeFileSync } from 'fs'; +import { resolve } from 'path'; + +// A script to handle versioning and ensure all related changes are in a single, atomic commit. + +function run(command) { + console.log(`> ${command}`); + execSync(command, { stdio: 'inherit' }); +} + +function readJson(filePath) { + return JSON.parse(readFileSync(filePath, 'utf-8')); +} + +function writeJson(filePath, data) { + writeFileSync(filePath, JSON.stringify(data, null, 2) + '\n'); +} + +// 1. Get the version type from the command line arguments. +const versionType = process.argv[2]; +if (!versionType) { + console.error('Error: No version type specified.'); + console.error('Usage: npm run version <patch|minor|major|prerelease>'); + process.exit(1); +} + +// 2. Bump the version in the root and all workspace package.json files. +run(`npm version ${versionType} --no-git-tag-version --allow-same-version`); +run( + `npm version ${versionType} --workspaces --no-git-tag-version --allow-same-version`, +); + +// 3. Get the new version number from the root package.json +const rootPackageJsonPath = resolve(process.cwd(), 'package.json'); +const newVersion = readJson(rootPackageJsonPath).version; + +// 4. Update the sandboxImageUri in the root package.json +const rootPackageJson = readJson(rootPackageJsonPath); +if (rootPackageJson.config?.sandboxImageUri) { + rootPackageJson.config.sandboxImageUri = + rootPackageJson.config.sandboxImageUri.replace(/:.*$/, `:${newVersion}`); + console.log(`Updated sandboxImageUri in root to use version ${newVersion}`); + writeJson(rootPackageJsonPath, rootPackageJson); +} + +// 5. Update the sandboxImageUri in the cli package.json +const cliPackageJsonPath = resolve(process.cwd(), 'packages/cli/package.json'); +const cliPackageJson = readJson(cliPackageJsonPath); +if (cliPackageJson.config?.sandboxImageUri) { + cliPackageJson.config.sandboxImageUri = + cliPackageJson.config.sandboxImageUri.replace(/:.*$/, `:${newVersion}`); + console.log( + `Updated sandboxImageUri in cli package to use version ${newVersion}`, + ); + writeJson(cliPackageJsonPath, cliPackageJson); +} + +// 6. Run `npm install` to update package-lock.json. +run('npm install'); + +console.log(`Successfully bumped versions to v${newVersion}.`); |
