Skip to content

Commit

Permalink
fix clippy and update CI
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Apr 29, 2024
1 parent a6f9324 commit 4450239
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 69 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ jobs:
audit:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- name: rust audit
uses: actions-rs/audit-check@v1
- uses: actions/checkout@v4
- uses: rustsec/audit-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/change-status-on-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: windows-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: covector status
Expand Down
30 changes: 7 additions & 23 deletions .github/workflows/clippy-fmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,17 @@ jobs:
clippy:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- name: install stable
uses: actions-rs/toolchain@v1
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
profile: minimal
toolchain: stable
override: true
components: clippy
- run: cargo clippy --release --all-targets --all-features -- -D warnings

- uses: actions-rs/cargo@v1
with:
command: clippy
args: --all-targets --all-features -- -D warnings

fmt:
rustfmt:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- name: install stable
uses: actions-rs/toolchain@v1
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
profile: minimal
toolchain: stable
override: true
components: rustfmt

- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
- run: cargo fmt --all -- --check
9 changes: 2 additions & 7 deletions .github/workflows/covector-version-or-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@ jobs:
successfulPublish: ${{ steps.covector.outputs.successfulPublish }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 16
check-latest: true
registry-url: 'https://registry.npmjs.org'
- uses: actions/setup-node@v4

- name: git config
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: windows-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: install stable
uses: actions-rs/toolchain@v1
Expand All @@ -33,4 +33,4 @@ jobs:

- uses: actions-rs/cargo@v1
with:
command: test
command: test --release
5 changes: 4 additions & 1 deletion crates/nsis-fn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ impl Parse for NsisFn {
}
}

/// Generates a wrapper NSIS compliant dll export that calls `nsis_plugin_api::exdll_init`
/// automatically. This macro expects the function to return a `Result<(), nsis_plugin_api::Error>`
/// and will automatically push the error to NSIS stack on failure.
#[proc_macro_attribute]
pub fn nsis_fn(_attr: TokenStream, tokens: TokenStream) -> TokenStream {
let tokens = parse_macro_input!(tokens as NsisFn);
Expand All @@ -23,7 +26,7 @@ pub fn nsis_fn(_attr: TokenStream, tokens: TokenStream) -> TokenStream {
let block = func.block;
let attrs = func.attrs;

let new_ident = Ident::new(&format!("__{}", ident.to_string()), Span::call_site());
let new_ident = Ident::new(&format!("__{}", ident), Span::call_site());

quote! {
#[inline(always)]
Expand Down
49 changes: 42 additions & 7 deletions crates/nsis-plugin-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,21 @@ pub static mut G_STRINGSIZE: c_int = 0;
pub static mut G_VARIABLES: *mut wchar_t = core::ptr::null_mut();
pub static mut G_STACKTOP: *mut *mut stack_t = core::ptr::null_mut();

/// Initis the global variables used by NSIS functions: [`push`], [`pushstr`], [`pushint`], [`pop`], [`popstr`] and [`popint`]
///
/// # Safety
///
/// This function mutates static variables and should only be called in a function
#[inline(always)]
pub unsafe fn exdll_init(string_size: c_int, variables: *mut wchar_t, stacktop: *mut *mut stack_t) {
G_STRINGSIZE = string_size;
G_VARIABLES = variables;
G_STACKTOP = stacktop;
}

pub const ONE: [u16; 2] = [49, 0];
pub const ZERO: [u16; 2] = [48, 0];
pub const NEGATIVE_ONE: [u16; 3] = [45, 49, 0];
pub const ONE: &[u16; 2] = &[49, 0];
pub const ZERO: &[u16; 2] = &[48, 0];
pub const NEGATIVE_ONE: &[u16; 3] = &[45, 49, 0];

#[derive(Debug)]
pub enum Error {
Expand All @@ -70,6 +75,11 @@ impl Error {
}
}

/// Pushes some bytes onto the NSIS stack.
///
/// # Safety
///
/// This function reads static variables and should only be called after [`exdll_init`] is called.
pub unsafe fn push(bytes: &[u16]) -> Result<(), Error> {
if G_STACKTOP.is_null() {
return Err(Error::StackIsNull);
Expand All @@ -84,16 +94,31 @@ pub unsafe fn push(bytes: &[u16]) -> Result<(), Error> {
Ok(())
}

/// Pushes a string onto the NSIS stack.
///
/// # Safety
///
/// This function reads static variables and should only be called after [`exdll_init`] is called.
pub unsafe fn pushstr(str: &str) -> Result<(), Error> {
let bytes = encode_wide(str);
let bytes = encode_utf16(str);
push(&bytes)
}

/// Pushes an integer onto the NSIS stack.
///
/// # Safety
///
/// This function reads static variables and should only be called after [`exdll_init`] is called.
pub unsafe fn pushint(int: i32) -> Result<(), Error> {
let str = int.to_string();
pushstr(&str)
}

/// Pops bytes from NSIS stack.
///
/// # Safety
///
/// This function reads static variables and should only be called after [`exdll_init`] is called.
pub unsafe fn pop() -> Result<Vec<u16>, Error> {
if G_STACKTOP.is_null() || (*G_STACKTOP).is_null() {
return Err(Error::StackIsNull);
Expand All @@ -109,23 +134,33 @@ pub unsafe fn pop() -> Result<Vec<u16>, Error> {
Ok(out)
}

/// Pops a string from NSIS stack.
///
/// # Safety
///
/// This function reads static variables and should only be called after [`exdll_init`] is called.
pub unsafe fn popstr() -> Result<String, Error> {
let bytes = pop()?;
Ok(decode_wide(&bytes))
Ok(decode_utf16_lossy(&bytes))
}

/// Pops an integer from NSIS stack.
///
/// # Safety
///
/// This function reads static variables and should only be called after [`exdll_init`] is called.
pub unsafe fn popint() -> Result<i32, Error> {
let str = popstr()?;
str.parse().map_err(|_| Error::ParseIntError)
}

pub fn encode_wide(str: &str) -> Vec<u16> {
pub fn encode_utf16(str: &str) -> Vec<u16> {
str.encode_utf16()
.chain(iter::once(0))
.collect::<Vec<u16>>()
}

pub fn decode_wide(bytes: &[u16]) -> String {
pub fn decode_utf16_lossy(bytes: &[u16]) -> String {
let bytes = bytes
.iter()
.position(|c| *c == 0)
Expand Down
24 changes: 12 additions & 12 deletions crates/nsis-process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ fn FindProcess() -> Result<(), Error> {
let name = popstr()?;

if !get_processes(&name).is_empty() {
push(&ZERO)
push(ZERO)
} else {
push(&ONE)
push(ONE)
}
}

Expand All @@ -56,15 +56,15 @@ fn FindProcessCurrentUser() -> Result<(), Error> {
.into_iter()
.any(|pid| belongs_to_user(user_sid, pid))
{
push(&ZERO)
push(ZERO)
} else {
push(&ONE)
push(ONE)
}
// Fall back to perMachine checks if we can't get current user id
} else if processes.is_empty() {
push(&ONE)
push(ONE)
} else {
push(&ZERO)
push(ZERO)
}
}

Expand All @@ -80,9 +80,9 @@ fn KillProcess() -> Result<(), Error> {
let processes = get_processes(&name);

if !processes.is_empty() && processes.into_iter().map(kill).all(|b| b) {
push(&ZERO)
push(ZERO)
} else {
push(&ONE)
push(ONE)
}
}

Expand All @@ -98,7 +98,7 @@ fn KillProcessCurrentUser() -> Result<(), Error> {
let processes = get_processes(&name);

if processes.is_empty() {
return push(&ONE);
return push(ONE);
}

let success = if let Some(user_sid) = get_sid(GetCurrentProcessId()) {
Expand All @@ -112,9 +112,9 @@ fn KillProcessCurrentUser() -> Result<(), Error> {
};

if success {
push(&ZERO)
push(ZERO)
} else {
push(&ONE)
push(ONE)
}
}

Expand Down Expand Up @@ -196,7 +196,7 @@ fn get_processes(name: &str) -> Vec<u32> {
if Process32FirstW(handle, &mut process) != 0 {
while Process32NextW(handle, &mut process) != 0 {
if current_pid != process.th32ProcessID
&& decode_wide(&process.szExeFile).to_lowercase() == name.to_lowercase()
&& decode_utf16_lossy(&process.szExeFile).to_lowercase() == name.to_lowercase()
{
processes.push(process.th32ProcessID);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/nsis-semvercompare/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ fn SemverCompare() -> Result<(), Error> {
let v2 = popstr()?;

match compare(&v1, &v2) {
-1 => push(&NEGATIVE_ONE)?,
0 => push(&ZERO)?,
1 => push(&ONE)?,
-1 => push(NEGATIVE_ONE)?,
0 => push(ZERO)?,
1 => push(ONE)?,
_ => unreachable!(),
}

Expand Down
25 changes: 15 additions & 10 deletions crates/nsis-tauri-utils/build.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
fn main() {
write_plugins();
combine_plugins_and_write_to_out_dir();
println!("cargo::rustc-link-arg=/ENTRY:DllMain")
}

fn write_plugins() {
/// Combines the plugins into one file that is included in lib.rs
/// using `include!(concat!(env!("OUT_DIR"), "/combined_libs.rs"));`
///
/// Plugins are combined this way because it saves a few kilobytes in the generated DLL
/// than the making nsis-tauri-utils depend on other plugins and re-export the DLLs
fn combine_plugins_and_write_to_out_dir() {
let out_dir = std::env::var("OUT_DIR").unwrap();
let path = format!("{out_dir}/combined_libs.rs");

let mut file = std::fs::File::options()
.truncate(true)
.write(true)
.create(true)
.open(&path)
.open(path)
.unwrap();

let p1 = include_str!("../nsis-semvercompare/src/lib.rs");
let p2 = include_str!("../nsis-process/src/lib.rs");
for p in [p1, p2] {
let lines = p
for plugin in [
include_str!("../nsis-semvercompare/src/lib.rs"),
include_str!("../nsis-process/src/lib.rs"),
] {
let lines = plugin
.lines()
.filter(|l| {
!(l.contains("#![no_std]")
|| l.contains("use nsis_plugin_api::*;")
|| l.contains("nsis_plugin!();"))
// remove lines that should only be specified once
!(l.contains("#![no_std]") || l.contains("nsis_plugin!();"))
})
.collect::<Vec<&str>>();

Expand Down

0 comments on commit 4450239

Please sign in to comment.