diff --git a/ext/lightningcss/lightningcss.c b/ext/lightningcss/lightningcss.c index 1a5944d..99a08e7 100644 --- a/ext/lightningcss/lightningcss.c +++ b/ext/lightningcss/lightningcss.c @@ -85,6 +85,12 @@ static VALUE rb_native_version(VALUE self) { return take_utf8_string(lightningcss_version()); } +static VALUE rb_lightningcss_version(VALUE self) { + (void) self; + + return take_utf8_string(lightningcss_lightningcss_version()); +} + void Init_lightningcss(void) { rb_mLightningCSS = rb_define_module("LightningCSS"); rb_mBackend = rb_define_module_under(rb_mLightningCSS, "Backend"); @@ -98,4 +104,5 @@ void Init_lightningcss(void) { rb_define_singleton_method(rb_mBackend, "transform_style_attribute", rb_transform_style_attribute, 2); rb_define_singleton_method(rb_mBackend, "bundle", rb_bundle, 2); rb_define_singleton_method(rb_mBackend, "version", rb_native_version, 0); + rb_define_singleton_method(rb_mBackend, "lightningcss_version", rb_lightningcss_version, 0); } diff --git a/lib/lightningcss.rb b/lib/lightningcss.rb index 5068d48..286e0ad 100644 --- a/lib/lightningcss.rb +++ b/lib/lightningcss.rb @@ -54,7 +54,7 @@ def self.minify(code, **) end #: () -> String - def self.native_version - Backend.version + def self.lightningcss_version + Backend.lightningcss_version end end diff --git a/lib/lightningcss/backend.rb b/lib/lightningcss/backend.rb index 5d31198..270d909 100644 --- a/lib/lightningcss/backend.rb +++ b/lib/lightningcss/backend.rb @@ -23,6 +23,11 @@ def version unavailable(__method__) end + #: () -> String + def lightningcss_version + unavailable(__method__) + end + private #: (Symbol?) -> bot diff --git a/rust/build.rs b/rust/build.rs index 1ca04d4..6135540 100644 --- a/rust/build.rs +++ b/rust/build.rs @@ -1,4 +1,5 @@ use std::env; +use std::fs; use std::path::PathBuf; fn main() { @@ -12,4 +13,33 @@ fn main() { bindings.write_to_file(&header_path); } + + let lock_path = PathBuf::from(&crate_dir).join("Cargo.lock"); + + println!("cargo:rerun-if-changed={}", lock_path.display()); + + println!( + "cargo:rustc-env=LIGHTNINGCSS_VERSION={}", + locked_version(&lock_path, "lightningcss") + ); +} + +fn locked_version(lock_path: &PathBuf, package: &str) -> String { + let Ok(lock) = fs::read_to_string(lock_path) else { + return "unknown".to_string(); + }; + + let mut lines = lock.lines(); + + while let Some(line) = lines.next() { + if line.trim() != format!("name = \"{package}\"") { + continue; + } + + if let Some(version) = lines.next().and_then(|next| next.trim().strip_prefix("version = ")) { + return version.trim_matches('"').to_string(); + } + } + + "unknown".to_string() } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 3858114..42d148b 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -25,6 +25,7 @@ use crate::options::{TransformOptions, TransformResult}; use crate::scope::Scoper; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); +pub const LIGHTNINGCSS_VERSION: &str = env!("LIGHTNINGCSS_VERSION"); #[repr(C)] pub struct LightningCssResult { @@ -294,6 +295,11 @@ pub unsafe extern "C" fn lightningcss_version() -> *mut c_char { into_c_string(VERSION) } +#[no_mangle] +pub unsafe extern "C" fn lightningcss_lightningcss_version() -> *mut c_char { + into_c_string(LIGHTNINGCSS_VERSION) +} + #[no_mangle] pub unsafe extern "C" fn lightningcss_string_free(value: *mut c_char) { if !value.is_null() { diff --git a/sig/lightningcss.rbs b/sig/lightningcss.rbs index bf7491b..668c769 100644 --- a/sig/lightningcss.rbs +++ b/sig/lightningcss.rbs @@ -27,5 +27,5 @@ module LightningCSS def self.minify: (String, ?filename: String?, ?error_recovery: bool, ?targets: browsers?, ?css_modules: css_modules?, ?scope: String?) -> String # : () -> String - def self.native_version: () -> String + def self.lightningcss_version: () -> String end diff --git a/sig/lightningcss/backend.rbs b/sig/lightningcss/backend.rbs index 83e27ca..7c976bd 100644 --- a/sig/lightningcss/backend.rbs +++ b/sig/lightningcss/backend.rbs @@ -15,6 +15,9 @@ module LightningCSS # : () -> String def version: () -> String + # : () -> String + def lightningcss_version: () -> String + private # : (Symbol?) -> bot diff --git a/test/lightningcss_test.rb b/test/lightningcss_test.rb index baa20f4..e3e7ac7 100644 --- a/test/lightningcss_test.rb +++ b/test/lightningcss_test.rb @@ -8,6 +8,16 @@ class LightningCSSTest < Minitest::Spec end test "the native library was built from the version the gem was" do - assert_equal LightningCSS::VERSION, LightningCSS.native_version + assert_equal LightningCSS::VERSION, LightningCSS::Backend.version + end + + test "reports the version of Lightning CSS it was compiled against" do + assert_equal "1.0.0-alpha.72", LightningCSS.lightningcss_version + end + + test "the version it reports is the one Cargo.lock pins" do + locked = File.read(File.expand_path("../rust/Cargo.lock", __dir__))[/name = "lightningcss"\nversion = "([^"]+)"/, 1] + + assert_equal locked, LightningCSS.lightningcss_version end end