From 0d06e096c5514e1d3032a31c6102b1984ea27df1 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Thu, 20 Apr 2023 08:52:54 +0200 Subject: [PATCH] Make _VERSION global consistently formatted and add runtime version to it --- CHANGELOG.md | 10 +++++++ packages/lib/src/lua/create.rs | 20 ++++++++++++++ packages/lib/src/tests.rs | 1 + tests/globals/version.luau | 50 ++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 tests/globals/version.luau diff --git a/CHANGELOG.md b/CHANGELOG.md index f404bf2..c3dd5f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Changed + +- The `_VERSION` global now follows a consistent format `Lune x.y.z+luau` to allow libraries to check against it for version requirements. + + Examples: + + - `Lune 0.0.0+0` + - `Lune 1.0.0+500` + - `Lune 0.11.22+9999` + ### Fixed - Fixed using instances as keys in tables diff --git a/packages/lib/src/lua/create.rs b/packages/lib/src/lua/create.rs index 5c97488..c618adc 100644 --- a/packages/lib/src/lua/create.rs +++ b/packages/lib/src/lua/create.rs @@ -119,6 +119,26 @@ pub fn create() -> LuaResult<&'static Lua> { .set_environment(dbg_trace_env)? .into_function()?; lua.set_named_registry_value("dbg.trace", dbg_trace_fn)?; + // Modify the _VERSION global to also contain the current version of Lune + let luau_version_full = globals + .get::<_, LuaString>("_VERSION") + .expect("Missing _VERSION global"); + let luau_version = luau_version_full + .to_str()? + .strip_prefix("Luau 0.") + .expect("_VERSION global is formatted incorrectly") + .trim(); + if luau_version.is_empty() { + panic!("_VERSION global is missing version number") + } + globals.set( + "_VERSION", + lua.create_string(&format!( + "Lune {lune}+{luau}", + lune = env!("CARGO_PKG_VERSION"), + luau = luau_version, + ))?, + )?; // All done Ok(lua) } diff --git a/packages/lib/src/tests.rs b/packages/lib/src/tests.rs index 3a24bc3..846b88c 100644 --- a/packages/lib/src/tests.rs +++ b/packages/lib/src/tests.rs @@ -75,6 +75,7 @@ create_tests! { global_pcall: "globals/pcall", global_type: "globals/type", global_typeof: "globals/typeof", + global_version: "globals/version", serde_json_decode: "serde/json/decode", serde_json_encode: "serde/json/encode", serde_toml_decode: "serde/toml/decode", diff --git a/tests/globals/version.luau b/tests/globals/version.luau new file mode 100644 index 0000000..4641f4e --- /dev/null +++ b/tests/globals/version.luau @@ -0,0 +1,50 @@ +-- _VERSION global should follow the following format: +--[[ + _VERSION global must have the following format: + + Lune LUNE_MAJOR.LUNE_MINOR.LUNE_PATCH+LUAU_VERSION + + Examples: + + Lune 0.0.0+0 + Lune 1.0.0+500 + Lune 0.11.22+9999 +]] + +assert(_VERSION ~= nil, "_VERSION global is missing") +assert(type(_VERSION) == "string", "_VERSION global must be a string") + +assert(string.sub(_VERSION, 1, 5) == "Lune ", "_VERSION global must start with 'Lune '") + +local withoutPrefix = string.sub(_VERSION, 6) +local versions = string.split(withoutPrefix, "+") +assert(versions[1] ~= nil, "_VERSION global does not contain lune version") +assert(versions[2] ~= nil, "_VERSION global does not contain luau version") + +local luneVersion = string.split(versions[1], ".") +assert(luneVersion[1] ~= nil, "_VERSION global is missing lune major version") +assert(luneVersion[2] ~= nil, "_VERSION global is missing lune minor version") +assert(luneVersion[3] ~= nil, "_VERSION global is missing lune patch version") + +assert( + string.find(versions[2], ".", 1, true) == nil, + "_VERSION global contains more than one semver partial for luau version" +) + +assert(tonumber(luneVersion[1]) ~= nil, "_VERSION global lune major version is not a number") +assert(tonumber(luneVersion[2]) ~= nil, "_VERSION global lune minor version is not a number") +assert(tonumber(luneVersion[3]) ~= nil, "_VERSION global lune patch version is not a number") +assert(tonumber(versions[2]) ~= nil, "_VERSION global luau version is not a number") + +local rebuilt = string.format( + "Lune %d.%d.%d+%d", + tonumber(luneVersion[1]) :: number, + tonumber(luneVersion[2]) :: number, + tonumber(luneVersion[3]) :: number, + tonumber(versions[2]) :: number +) + +print("_VERSION:", _VERSION) +print("Rebuilt:", rebuilt) + +assert(rebuilt == _VERSION)