From c04ba071549620a0fa07a06830af4cfa906135d1 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Sun, 25 May 2025 10:01:13 +0300 Subject: [PATCH] feat: add nsis-string plugin (#43) --- .changes/config.json | 10 ++++++++++ .changes/nsis-string.md | 5 +++++ README.md | 1 + crates/nsis-process/src/lib.rs | 9 ++++----- crates/nsis-string/CHANGELOG.md | 14 ++++++++++++++ crates/nsis-string/Cargo.toml | 16 ++++++++++++++++ crates/nsis-string/build.rs | 5 +++++ crates/nsis-string/src/lib.rs | 24 ++++++++++++++++++++++++ 8 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 .changes/nsis-string.md create mode 100644 crates/nsis-string/CHANGELOG.md create mode 100644 crates/nsis-string/Cargo.toml create mode 100644 crates/nsis-string/build.rs create mode 100644 crates/nsis-string/src/lib.rs diff --git a/.changes/config.json b/.changes/config.json index de57d3a..de7bd52 100644 --- a/.changes/config.json +++ b/.changes/config.json @@ -63,6 +63,16 @@ "name": "${ pkg.pkg }.dll" } ] + }, + "nsis_string": { + "path": "./crates/nsis-string", + "manager": "rust", + "assets": [ + { + "path": "target/i686-pc-windows-msvc/release/${ pkg.pkg }.dll", + "name": "${ pkg.pkg }.dll" + } + ] } } } diff --git a/.changes/nsis-string.md b/.changes/nsis-string.md new file mode 100644 index 0000000..182cc6a --- /dev/null +++ b/.changes/nsis-string.md @@ -0,0 +1,5 @@ +--- +"nsis_string": "minor" +--- + +Initial Release diff --git a/README.md b/README.md index 7538248..1603321 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ A collection of NSIS plugins written in rust. | -------------------------------------------------- | ------------------------------------------------------------------------ | | [nsis-process](./crates/nsis-process/) | Find and Kill processes, and run program as unelevated user | | [nsis-semvercompare](./crates/nsis-semvercompare/) | Compare two semantic versions | +| [nsis-string](./crates/nsis-string/) | String manipulation | | [nsis-tauri-utils](./crates/nsis-tauri-utils/) | A collection of all the above plugins into a single DLL for smaller size | ## License diff --git a/crates/nsis-process/src/lib.rs b/crates/nsis-process/src/lib.rs index 2fa2978..c621f44 100644 --- a/crates/nsis-process/src/lib.rs +++ b/crates/nsis-process/src/lib.rs @@ -93,7 +93,7 @@ fn KillProcess() -> Result<(), Error> { let processes = get_processes(&name); - if !processes.is_empty() && processes.into_iter().map(kill).all(|b| b) { + if !processes.is_empty() && processes.into_iter().all(kill) { push(ZERO) } else { push(ONE) @@ -119,10 +119,9 @@ fn KillProcessCurrentUser() -> Result<(), Error> { processes .into_iter() .filter(|pid| belongs_to_user(user_sid, *pid)) - .map(kill) - .all(|b| b) + .all(kill) } else { - processes.into_iter().map(kill).all(|b| b) + processes.into_iter().all(kill) }; if success { @@ -388,7 +387,7 @@ mod tests { let processes = get_processes("something_that_doesnt_exist.exe"); // TODO: maybe find some way to spawn a dummy process we can kill here? // This will return true on empty iterators so it's basically no-op right now - assert!(processes.into_iter().map(kill).all(|b| b)); + assert!(processes.into_iter().all(kill)); } #[test] diff --git a/crates/nsis-string/CHANGELOG.md b/crates/nsis-string/CHANGELOG.md new file mode 100644 index 0000000..69aa952 --- /dev/null +++ b/crates/nsis-string/CHANGELOG.md @@ -0,0 +1,14 @@ +# Changelog + +## \[0.3.0] + +- [`5423579`](https://www.github.com/tauri-apps/nsis-tauri-utils/commit/5423579860016c4f3074831eda03096ee4854e73)([#26](https://www.github.com/tauri-apps/nsis-tauri-utils/pull/26)) Reduce the DLL size by using `no_std` and without static msvcrt. + +## \[0.2.0] + +- [`33ea4bc`](https://www.github.com/tauri-apps/nsis-tauri-utils/commit/33ea4bcf2a573461ebc5181ef2921d8746005049)([#17](https://www.github.com/tauri-apps/nsis-tauri-utils/pull/17)) Statically link CRT. + +## \[0.1.0] + +- Initial Release. + - [000d632](https://www.github.com/tauri-apps/nsis-tauri-utils/commit/000d6326333f862741f1514de34542316445951e) ci: setup CI/CD and covector ([#2](https://www.github.com/tauri-apps/nsis-tauri-utils/pull/2)) on 2023-01-21 diff --git a/crates/nsis-string/Cargo.toml b/crates/nsis-string/Cargo.toml new file mode 100644 index 0000000..771c621 --- /dev/null +++ b/crates/nsis-string/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "nsis-string" +version = "0.0.0" +authors = { workspace = true } +edition = { workspace = true } +license = { workspace = true } + +[lib] +crate-type = ["cdylib"] + +[features] +test = [] + +[dependencies] +nsis-plugin-api = { workspace = true } +windows-sys = { workspace = true } diff --git a/crates/nsis-string/build.rs b/crates/nsis-string/build.rs new file mode 100644 index 0000000..ce8d102 --- /dev/null +++ b/crates/nsis-string/build.rs @@ -0,0 +1,5 @@ +fn main() { + if std::env::var("CARGO_FEATURE_TEST").as_deref() != Ok("1") { + println!("cargo::rustc-link-arg=/ENTRY:DllMain") + } +} diff --git a/crates/nsis-string/src/lib.rs b/crates/nsis-string/src/lib.rs new file mode 100644 index 0000000..19694df --- /dev/null +++ b/crates/nsis-string/src/lib.rs @@ -0,0 +1,24 @@ +#![no_std] + +use nsis_plugin_api::*; + +nsis_plugin!(); + +/// Replaces all occurrences of a substring in a string with another substring. +/// +/// Returns the modified string. +/// +/// # Safety +/// +/// This function always expects 3 strings on the stack ($string, $search, $replace) and will panic otherwise. +#[nsis_fn] +fn Replace() -> Result<(), Error> { + let string = popstr()?; + let search = popstr()?; + let replace = popstr()?; + + let result = string.replace(&search, &replace); + pushstr(&result)?; + + Ok(()) +}