Skip to content

Commit 1da345e

Browse files
committed
fix js-binding version
1 parent dd76f03 commit 1da345e

7 files changed

Lines changed: 172 additions & 12 deletions

File tree

.github/workflows/npm-release.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ jobs:
2323
- name: Checkout code
2424
uses: actions/checkout@v4
2525

26+
- name: Synchronize Node.js binding version
27+
run: |
28+
echo "Synchronizing Node.js binding version with main project..."
29+
python3 scripts/sync_node_version.py
30+
2631
- name: Get package version
2732
id: package
2833
working-directory: libCacheSim-node
@@ -79,6 +84,11 @@ jobs:
7984
with:
8085
fetch-depth: 0
8186

87+
- name: Synchronize Node.js binding version
88+
run: |
89+
echo "Synchronizing Node.js binding version with main project..."
90+
python3 scripts/sync_node_version.py
91+
8292
- name: Setup Node.js
8393
uses: actions/setup-node@v4
8494
with:
@@ -137,6 +147,11 @@ jobs:
137147
- name: Checkout code
138148
uses: actions/checkout@v4
139149

150+
- name: Synchronize Node.js binding version
151+
run: |
152+
echo "Synchronizing Node.js binding version with main project..."
153+
python3 scripts/sync_node_version.py
154+
140155
- name: Setup Node.js
141156
uses: actions/setup-node@v4
142157
with:

libCacheSim-node/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ console.log('Simulation results:', result);
4646

4747
## API Reference
4848

49+
### `getVersion()`
50+
51+
Get the version of the libCacheSim Node.js binding.
52+
53+
**Returns:** String containing the version number (e.g., "1.0.1")
54+
4955
### `runSimulation(tracePath, traceType, algorithm, cacheSize)`
5056

5157
Run a cache simulation with specified parameters.
@@ -100,6 +106,9 @@ npm install -g libcachesim-node
100106

101107
# Run simulation from command line
102108
cachesim-js --trace /path/to/trace.vscsi --algorithm s3fifo --size 1mb
109+
110+
# Check version
111+
cachesim-js --version
103112
```
104113

105114
## Development
@@ -132,6 +141,16 @@ npm test
132141
- Linux: `build-essential cmake libglib2.0-dev libzstd-dev`
133142
- macOS: `cmake glib zstd` (via Homebrew)
134143

144+
### Version Synchronization
145+
146+
The Node.js binding version is automatically synchronized with the main libCacheSim project version. To manually sync versions, run:
147+
148+
```bash
149+
python3 scripts/sync_node_version.py
150+
```
151+
152+
This ensures that the Node.js binding version in `package.json` matches the version in the main project's `version.txt` file.
153+
135154
## Architecture
136155

137156
This package uses `prebuild-install` for binary distribution:

libCacheSim-node/cli.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ function parseArgs() {
1111
type: null,
1212
algorithm: null,
1313
size: null,
14-
help: false
14+
help: false,
15+
version: false
1516
};
1617

1718
for (let i = 0; i < args.length; i++) {
@@ -35,6 +36,10 @@ function parseArgs() {
3536
case '-h':
3637
options.help = true;
3738
break;
39+
case '--version':
40+
case '-v':
41+
options.version = true;
42+
break;
3843
default:
3944
console.error(`Unknown option: ${args[i]}`);
4045
process.exit(1);
@@ -60,6 +65,7 @@ Options:
6065
--size, -s <size> Cache size (required)
6166
Examples: 1mb, 512kb, 2gb, 1024 (bytes)
6267
--help, -h Show this help message
68+
--version, -v Show version information
6369
6470
Examples:
6571
cachesim-js -t trace.vscsi --type vscsi -a lru -s 10mb
@@ -75,6 +81,11 @@ function main() {
7581
return;
7682
}
7783

84+
if (options.version) {
85+
console.log(`libcachesim-node v${libCacheSim.getVersion()}`);
86+
return;
87+
}
88+
7889
// Check that all required parameters are provided
7990
if (!options.trace || !options.type || !options.algorithm || !options.size) {
8091
console.error('Error: All parameters are required.');

libCacheSim-node/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,30 @@ function getSupportedTraceTypes() {
3737
return ['vscsi', 'csv', 'txt', 'binary', 'oracle'];
3838
}
3939

40+
/**
41+
* Get the version of the libCacheSim Node.js binding
42+
* @returns {string} Version string
43+
*/
44+
function getVersion() {
45+
try {
46+
const packageJson = require('./package.json');
47+
return packageJson.version;
48+
} catch (error) {
49+
return 'unknown';
50+
}
51+
}
52+
4053
module.exports = {
4154
runSimulation,
4255
runSim,
4356
getSupportedAlgorithms,
44-
getSupportedTraceTypes
57+
getSupportedTraceTypes,
58+
getVersion
4559
};
4660

4761
// Example usage if run directly
4862
if (require.main === module) {
49-
console.log('libCacheSim Node.js Bindings');
63+
console.log(`libCacheSim Node.js Bindings v${getVersion()}`);
5064
console.log('Supported algorithms:', getSupportedAlgorithms());
5165
console.log('Supported trace types:', getSupportedTraceTypes());
5266

libCacheSim-node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "libcachesim-node",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"main": "index.js",
55
"bin": {
66
"cachesim-js": "./cli.js"

libCacheSim-node/test.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ function runTests() {
5353
assert(typeof libCacheSim.runSim === 'function', 'runSim function exists') &&
5454
assert(typeof libCacheSim.runSimulation === 'function', 'runSimulation function exists') &&
5555
assert(typeof libCacheSim.getSupportedAlgorithms === 'function', 'getSupportedAlgorithms function exists') &&
56-
assert(typeof libCacheSim.getSupportedTraceTypes === 'function', 'getSupportedTraceTypes function exists');
56+
assert(typeof libCacheSim.getSupportedTraceTypes === 'function', 'getSupportedTraceTypes function exists') &&
57+
assert(typeof libCacheSim.getVersion === 'function', 'getVersion function exists');
5758
});
5859

5960
// Test 2: Check supported algorithms
@@ -75,7 +76,15 @@ function runTests() {
7576
assert(traceTypes.includes('csv'), 'Includes CSV');
7677
});
7778

78-
// Test 4: Run default simulation
79+
// Test 4: Check version function
80+
test('Get version', () => {
81+
const version = libCacheSim.getVersion();
82+
return assert(typeof version === 'string', 'Returns a string') &&
83+
assert(version.length > 0, 'Version is not empty') &&
84+
assert(/^\d+\.\d+\.\d+/.test(version), 'Version follows semantic versioning format');
85+
});
86+
87+
// Test 5: Run default simulation
7988
test('Run default simulation', () => {
8089
const result = libCacheSim.runSim();
8190
return assert(typeof result === 'object', 'Returns an object') &&
@@ -88,7 +97,7 @@ function runTests() {
8897
assert(Math.abs(result.hitRatio + result.missRatio - 1.0) < 0.0001, 'Hit ratio + miss ratio ≈ 1.0');
8998
});
9099

91-
// Test 5: Run custom simulation with different algorithms
100+
// Test 6: Run custom simulation with different algorithms
92101
test('Run custom simulations with different algorithms', () => {
93102
const algorithms = ['lru', 'fifo', 's3fifo'];
94103
let allPassed = true;
@@ -110,7 +119,7 @@ function runTests() {
110119
return allPassed;
111120
});
112121

113-
// Test 6: Test different cache sizes
122+
// Test 7: Test different cache sizes
114123
test('Test different cache sizes', () => {
115124
const sizes = ['512kb', '1mb', '2mb'];
116125
let allPassed = true;
@@ -131,7 +140,7 @@ function runTests() {
131140
return allPassed;
132141
});
133142

134-
// Test 7: Error handling - invalid trace file
143+
// Test 8: Error handling - invalid trace file
135144
test('Error handling for invalid trace file', () => {
136145
try {
137146
libCacheSim.runSimulation('/nonexistent/file.vscsi', 'vscsi', 'lru', '1mb');
@@ -141,7 +150,7 @@ function runTests() {
141150
}
142151
});
143152

144-
// Test 8: Error handling - invalid algorithm
153+
// Test 9: Error handling - invalid algorithm
145154
test('Error handling for invalid algorithm', () => {
146155
try {
147156
libCacheSim.runSimulation('../data/cloudPhysicsIO.vscsi', 'vscsi', 'invalid_algo', '1mb');
@@ -151,7 +160,7 @@ function runTests() {
151160
}
152161
});
153162

154-
// Test 9: Error handling - invalid trace type
163+
// Test 10: Error handling - invalid trace type
155164
test('Error handling for invalid trace type', () => {
156165
try {
157166
libCacheSim.runSimulation('../data/cloudPhysicsIO.vscsi', 'invalid_type', 'lru', '1mb');
@@ -161,7 +170,7 @@ function runTests() {
161170
}
162171
});
163172

164-
// Test 10: Performance test - measure execution time
173+
// Test 11: Performance test - measure execution time
165174
test('Performance measurement', () => {
166175
const startTime = process.hrtime.bigint();
167176
const result = libCacheSim.runSim();

scripts/sync_node_version.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Script to synchronize version between libCacheSim main project and Node.js binding.
4+
5+
This script reads the version from version.txt and updates the package.json
6+
in libCacheSim-node to match.
7+
"""
8+
9+
import json
10+
import os
11+
import sys
12+
from pathlib import Path
13+
14+
15+
def get_project_root():
16+
"""Get the project root directory."""
17+
script_dir = Path(__file__).parent
18+
return script_dir.parent
19+
20+
21+
def read_main_version():
22+
"""Read version from version.txt."""
23+
project_root = get_project_root()
24+
version_file = project_root / "version.txt"
25+
26+
if not version_file.exists():
27+
print(f"Error: {version_file} not found", file=sys.stderr)
28+
sys.exit(1)
29+
30+
with open(version_file, 'r') as f:
31+
version = f.read().strip()
32+
33+
if not version:
34+
print("Error: version.txt is empty", file=sys.stderr)
35+
sys.exit(1)
36+
37+
return version
38+
39+
40+
def update_package_json(version):
41+
"""Update package.json with the new version."""
42+
project_root = get_project_root()
43+
package_json_path = project_root / "libCacheSim-node" / "package.json"
44+
45+
if not package_json_path.exists():
46+
print(f"Error: {package_json_path} not found", file=sys.stderr)
47+
sys.exit(1)
48+
49+
# Read current package.json
50+
with open(package_json_path, 'r') as f:
51+
package_data = json.load(f)
52+
53+
current_version = package_data.get('version', 'unknown')
54+
55+
if current_version == version:
56+
print(f"Version already up to date: {version}")
57+
return False
58+
59+
# Update version
60+
package_data['version'] = version
61+
62+
# Write back to file with proper formatting
63+
with open(package_json_path, 'w') as f:
64+
json.dump(package_data, f, indent=2)
65+
f.write('\n') # Add trailing newline
66+
67+
print(f"Updated Node.js binding version: {current_version}{version}")
68+
return True
69+
70+
71+
def main():
72+
"""Main function."""
73+
try:
74+
# Read main project version
75+
main_version = read_main_version()
76+
print(f"Main project version: {main_version}")
77+
78+
# Update Node.js binding version
79+
updated = update_package_json(main_version)
80+
81+
if updated:
82+
print("✓ Node.js binding version synchronized successfully")
83+
else:
84+
print("✓ No changes needed")
85+
86+
except Exception as e:
87+
print(f"Error: {e}", file=sys.stderr)
88+
sys.exit(1)
89+
90+
91+
if __name__ == "__main__":
92+
main()

0 commit comments

Comments
 (0)