fix cargo version gh-actions
This commit is contained in:
72
scripts/sync-versions.js
Executable file
72
scripts/sync-versions.js
Executable file
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Version synchronization script for ailog
|
||||
* Syncs version from Cargo.toml to all package.json files
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// Read version from Cargo.toml
|
||||
function getCargoVersion() {
|
||||
const cargoPath = path.join(__dirname, '../Cargo.toml');
|
||||
const cargoContent = fs.readFileSync(cargoPath, 'utf8');
|
||||
const versionMatch = cargoContent.match(/^version\s*=\s*"([^"]+)"/m);
|
||||
|
||||
if (!versionMatch) {
|
||||
throw new Error('Could not find version in Cargo.toml');
|
||||
}
|
||||
|
||||
return versionMatch[1];
|
||||
}
|
||||
|
||||
// Update package.json version
|
||||
function updatePackageVersion(packagePath, version) {
|
||||
if (!fs.existsSync(packagePath)) {
|
||||
console.warn(`Package.json not found: ${packagePath}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
const packageData = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
|
||||
const oldVersion = packageData.version;
|
||||
packageData.version = version;
|
||||
|
||||
fs.writeFileSync(packagePath, JSON.stringify(packageData, null, 2) + '\n');
|
||||
console.log(`Updated ${path.relative(process.cwd(), packagePath)}: ${oldVersion} → ${version}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Main function
|
||||
function main() {
|
||||
try {
|
||||
const version = getCargoVersion();
|
||||
console.log(`Cargo.toml version: ${version}`);
|
||||
|
||||
// List of package.json files to update
|
||||
const packageFiles = [
|
||||
path.join(__dirname, '../oauth/package.json'),
|
||||
path.join(__dirname, '../pds/package.json')
|
||||
];
|
||||
|
||||
let updatedCount = 0;
|
||||
for (const packageFile of packageFiles) {
|
||||
if (updatePackageVersion(packageFile, version)) {
|
||||
updatedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`✅ Successfully updated ${updatedCount} package.json files`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error syncing versions:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Run if called directly
|
||||
if (require.main === module) {
|
||||
main();
|
||||
}
|
||||
|
||||
module.exports = { getCargoVersion, updatePackageVersion };
|
81
scripts/sync-versions.sh
Executable file
81
scripts/sync-versions.sh
Executable file
@@ -0,0 +1,81 @@
|
||||
#!/bin/bash
|
||||
# Version synchronization script for ailog
|
||||
# Syncs version from Cargo.toml to all package.json files
|
||||
|
||||
set -e
|
||||
|
||||
# Get the directory of this script
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
|
||||
|
||||
# Function to extract version from Cargo.toml
|
||||
get_cargo_version() {
|
||||
grep '^version = ' "$PROJECT_ROOT/Cargo.toml" | cut -d'"' -f2
|
||||
}
|
||||
|
||||
# Function to update package.json version using jq
|
||||
update_package_version() {
|
||||
local package_file="$1"
|
||||
local version="$2"
|
||||
|
||||
if [[ ! -f "$package_file" ]]; then
|
||||
echo "⚠️ Package.json not found: $package_file"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Check if jq is available
|
||||
if ! command -v jq &> /dev/null; then
|
||||
echo "❌ jq is required but not installed. Please install jq first."
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Get current version using jq
|
||||
local old_version=$(jq -r '.version' "$package_file")
|
||||
|
||||
# Update version using jq (with proper formatting)
|
||||
local temp_file=$(mktemp)
|
||||
jq --arg version "$version" '.version = $version' "$package_file" > "$temp_file"
|
||||
|
||||
# Replace original file if jq succeeded
|
||||
if [[ $? -eq 0 ]]; then
|
||||
mv "$temp_file" "$package_file"
|
||||
echo "✅ Updated $(basename "$package_file"): $old_version → $version"
|
||||
return 0
|
||||
else
|
||||
rm -f "$temp_file"
|
||||
echo "❌ Failed to update $package_file"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Main execution
|
||||
main() {
|
||||
local version
|
||||
version=$(get_cargo_version)
|
||||
|
||||
if [[ -z "$version" ]]; then
|
||||
echo "❌ Could not find version in Cargo.toml"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "📦 Cargo.toml version: $version"
|
||||
echo "🔄 Syncing versions..."
|
||||
|
||||
# List of package.json files to update
|
||||
local packages=(
|
||||
"$PROJECT_ROOT/oauth/package.json"
|
||||
"$PROJECT_ROOT/pds/package.json"
|
||||
)
|
||||
|
||||
local updated_count=0
|
||||
for package in "${packages[@]}"; do
|
||||
if update_package_version "$package" "$version"; then
|
||||
((updated_count++))
|
||||
fi
|
||||
done
|
||||
|
||||
echo "✅ Successfully synced $updated_count package.json files"
|
||||
}
|
||||
|
||||
# Run main function
|
||||
main "$@"
|
Reference in New Issue
Block a user