Skip to content

Commit 582fe82

Browse files
authored
feat: add a way to get the library version (#652)
1 parent 32e6e61 commit 582fe82

File tree

11 files changed

+113
-89
lines changed

11 files changed

+113
-89
lines changed

.changeset/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,30 @@
1414
- Commit the changed files
1515
- Run `pnpm changeset publish` to publish the packages to npm
1616
- `git push && git push --tags`
17+
18+
# Release Rust Crates
19+
20+
- Run `pnpm release-rust <target-version>` to get the command that can update the version of the rust crates.
21+
22+
```
23+
cargo release version -x --workspace 1.4.1 --exclude loro-rle --exclude loro-delta --exclude loro_fractional_index
24+
```
25+
26+
- Commit the changes
27+
- Replace the `version` command with `publish`
28+
29+
```
30+
cargo release publish -x --workspace --exclude loro-rle --exclude loro-delta --exclude loro_fractional_index
31+
```
32+
33+
- Add git tags by replacing `publish` with `tag`
34+
35+
```
36+
cargo release tag -x --workspace --exclude loro-rle --exclude loro-delta --exclude loro_fractional_index
37+
```
38+
39+
- Push the changes
40+
41+
```
42+
git push && git push --tags
43+
```

crates/loro-ffi/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use loro::{
66
CounterSpan, EventTriggerKind, ExpandType, FractionalIndex, IdLp, IdSpan, JsonChange,
77
JsonFutureOp, JsonFutureOpWrapper, JsonListOp, JsonMapOp, JsonMovableListOp, JsonOp,
88
JsonOpContent, JsonPathError, JsonSchema, JsonTextOp, JsonTreeOp, Lamport, LoroEncodeError,
9-
LoroError, PeerID, StyleConfig, TreeID, UpdateOptions, UpdateTimeoutError, ID,
9+
LoroError, PeerID, StyleConfig, TreeID, UpdateOptions, UpdateTimeoutError, ID, LORO_VERSION,
1010
};
1111
pub use std::cmp::Ordering;
1212
use std::sync::Arc;

crates/loro-internal/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.4.2

crates/loro-internal/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,6 @@ pub struct LoroDocInner {
143143
local_update_subs: SubscriberSetWithQueue<(), LocalUpdateCallback, Vec<u8>>,
144144
peer_id_change_subs: SubscriberSetWithQueue<(), PeerIdUpdateCallback, ID>,
145145
}
146+
147+
/// The version of the loro crate
148+
pub const LORO_VERSION: &str = include_str!("../VERSION");

crates/loro-wasm/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ pub use awareness::AwarenessWasm;
4747

4848
mod convert;
4949

50+
/// Get the version of Loro
51+
#[wasm_bindgen]
52+
pub fn LORO_VERSION() -> String {
53+
loro_internal::LORO_VERSION.to_string()
54+
}
55+
5056
#[wasm_bindgen(start)]
5157
fn run() {
5258
console_error_panic_hook::set_once();

crates/loro/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub use loro_internal::diff::diff_impl::UpdateTimeoutError;
3636
pub use loro_internal::subscription::LocalUpdateCallback;
3737
pub use loro_internal::subscription::PeerIdUpdateCallback;
3838
pub use loro_internal::ChangeMeta;
39+
pub use loro_internal::LORO_VERSION;
3940
pub mod event;
4041
pub use loro_internal::awareness;
4142
pub use loro_internal::change::Timestamp;

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
"test-all": "pnpm test && pnpm test-wasm",
1212
"test-wasm": "cd crates/loro-wasm && pnpm i && pnpm build-dev",
1313
"coverage": "mkdir -p coverage && cargo llvm-cov nextest --features test_utils,jsonpath --lcov > coverage/lcov-nextest.info && cargo llvm-cov report",
14-
"release-wasm": "cd crates/loro-wasm && pnpm i && pnpm build-release",
14+
"release-wasm": "deno run -A ./scripts/sync-loro-version.ts && cd crates/loro-wasm && pnpm i && pnpm build-release",
1515
"check": "cargo clippy --all-features -- -Dwarnings",
1616
"run-fuzz-corpus": "cd crates/fuzz && cargo +nightly fuzz run all -- -max_total_time=1",
1717
"fix": "cargo clippy --fix --features=test_utils",
18-
"vet": "cargo vet"
18+
"vet": "cargo vet",
19+
"release-rust": "deno run -A ./scripts/cargo-release.ts"
1920
},
2021
"keywords": [],
2122
"author": "",

scripts/bump-rust-crates.ts

-86
This file was deleted.

scripts/cargo-release.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env -S deno run --allow-run
22

33
import { defineCommand, runMain } from "npm:citty";
4+
import { runSyncLoroVersion } from "./sync-loro-version.ts";
45

56
async function runCargoRelease(version: string): Promise<string> {
67
const process = new Deno.Command("cargo", {
@@ -62,6 +63,7 @@ const main = defineCommand({
6263
throw new Error("Version must be in format x.y.z (e.g., 1.2.3)");
6364
}
6465

66+
runSyncLoroVersion(version);
6567
const output = await runCargoRelease(version);
6668
console.log("Original output:");
6769
console.log(output);

scripts/deno.lock

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/sync-loro-version.ts

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { compare as semverCompare, parse as semverParse } from "npm:semver";
2+
import { readFileSync, writeFileSync } from "node:fs";
3+
4+
/**
5+
* Syncs the version between package.json and a version file,
6+
* using the higher version number
7+
*/
8+
export function syncLoroVersion(
9+
packageJsonPath: string,
10+
versionFilePath: string,
11+
checkVersion: string = "",
12+
) {
13+
// Read package.json
14+
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
15+
const packageVersion = packageJson.version;
16+
17+
// Read version file
18+
const versionFileContent = readFileSync(versionFilePath, "utf-8");
19+
const versionFileVersion = versionFileContent.trim();
20+
21+
// Parse and compare versions
22+
const parsedPackageVersion = semverParse(packageVersion);
23+
const parsedFileVersion = semverParse(versionFileVersion);
24+
25+
if (!parsedPackageVersion || !parsedFileVersion) {
26+
throw new Error("Invalid version format found");
27+
}
28+
29+
// Compare versions
30+
const comparison = semverCompare(packageVersion, versionFileVersion);
31+
32+
if (comparison > 0) {
33+
// package.json version is higher
34+
writeFileSync(versionFilePath, packageVersion);
35+
console.log(`Updated version file to ${packageVersion}`);
36+
if (checkVersion && checkVersion !== packageVersion) {
37+
throw new Error(`Version mismatch: Expected version ${checkVersion} but found ${packageVersion} in package.json and ${versionFileVersion} in VERSION file`)
38+
}
39+
} else if (comparison < 0) {
40+
// version file version is higher
41+
packageJson.version = versionFileVersion;
42+
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n");
43+
console.log(`Updated package.json to ${versionFileVersion}`);
44+
if (checkVersion && checkVersion !== versionFileVersion) {
45+
throw new Error(`Version mismatch: Expected version ${checkVersion} but found ${packageVersion} in package.json and ${versionFileVersion} in VERSION file`)
46+
}
47+
} else {
48+
console.log("Versions are already in sync");
49+
if (checkVersion && checkVersion !== versionFileVersion) {
50+
throw new Error(`Version mismatch: Expected version ${checkVersion} but found ${packageVersion} in package.json and ${versionFileVersion} in VERSION file`)
51+
}
52+
}
53+
}
54+
55+
export function runSyncLoroVersion(checkVersion: string = "") {
56+
syncLoroVersion(
57+
"./crates/loro-wasm/package.json",
58+
"./crates/loro-internal/VERSION",
59+
checkVersion
60+
);
61+
}
62+
63+
if (import.meta.main) {
64+
runSyncLoroVersion();
65+
}

0 commit comments

Comments
 (0)