Skip to content

Commit

Permalink
refactor: Use embed-resource crate to compile resources (#9)
Browse files Browse the repository at this point in the history
* refactor: Use embed-resource for compilation

* fix: use rc path instead of output. use rc path in compile call

* let's try canonicalize to get absolute icon paths

* reorder imports

* update readme

* clean up example
  • Loading branch information
FabianLars authored May 4, 2023
1 parent db468ec commit 3aa8411
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 411 deletions.
5 changes: 5 additions & 0 deletions .changes/compile-for.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"winres": patch
---

Added `compile_for` function to select which binaries to apply the resource to.
5 changes: 5 additions & 0 deletions .changes/use-embed-resource.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"winres": patch
---

Use https://github.com/nabijaczleweli/rust-embed-resource to compile the resource for better cross-platform compilation support.
7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ license = "MIT"
repository = "https://github.com/tauri-apps/winres"
documentation = "https://docs.rs/tauri-winres/"

[lib]
path = "lib.rs"

[dependencies]
toml = "0.5"
version_check = "0.9"
toml = "0.7"
embed-resource = "2.1"

[dev-dependencies]
winapi = { version = "0.3", features = [ "winnt" ] }
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# tauri-winres

A simple library to facilitate adding [Resources](<https://en.wikipedia.org/wiki/Resource_(Windows)>) (metainformation and icons) to [Portable Executables](https://en.wikipedia.org/wiki/Portable_Executable) (Windows executables and dynamic libraries).
A simple library to facilitate adding [Resources](<https://en.wikipedia.org/wiki/Resource_(Windows)>) (metainformation and icons) to [Portable Executables](https://en.wikipedia.org/wiki/Portable_Executable).

Note: `tauri-winres` is a fork of [winres](https://github.com/mxre/winres) which no longer works on Rust 1.61 or higher and has been [left unmaintained](https://github.com/mxre/winres/issues/40).
Note: `tauri-winres` is a fork of [winres](https://github.com/mxre/winres) which no longer works on Rust 1.61 or higher and has been [left unmaintained](https://github.com/mxre/winres/issues/40). This fork completely replaced the resource compiler implementation with the awesome [embed-resource](https://github.com/nabijaczleweli/rust-embed-resource) crate for better cross-platform compilation support. This fork was primarily updated and modified for use in [Tauri](https://github.com/tauri-apps/tauri). For a more general-purpose-like fork, which currently sticks closer to upstream, we suggest to also take a look at [winresource](https://github.com/BenjaminRi/winresource).

[Documentation](https://docs.rs/tauri-winres/)

## Toolkit

Before we begin you need to have the appropriate tools installed.

- `rc.exe` from the [Windows SDK]
- `windres.exe` and `ar.exe` from [minGW64]
- `rc.exe` from the [Windows SDK]
- `windres.exe` and `ar.exe` from [minGW64]

[windows sdk]: https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk
[mingw64]: http://mingw-w64.org

If you are using Rust with the MSVC ABI you will need the Windows SDK for the GNU ABI you'll need minGW64.
If you are using Rust with the MSVC ABI you will need the Windows SDK and for the GNU ABI you'll need minGW64.

Windows SDK can be found in the registry, minGW64 has to be in the path.
The Windows SDK can generally be found in the registry, but minGW64 must be in the $PATH environment.

## Using tauri-winres

Expand Down Expand Up @@ -49,7 +49,7 @@ fn main() {

That's it. The file `test.ico` should be located in the same directory as `build.rs`. Metainformation (like program version and description) is taken from `Cargo.toml`'s `[package]` section.

Note that support for using this crate on non windows platforms is experimental. It is recommended to only use `tauri-winres` on Windows hosts, by using `cfg` as a directive to avoid building `tauri-winres` on unix platforms alltogether.
Note that support for using this crate on non-Windows platforms is experimental. It is recommended to only use `tauri-winres` on Windows hosts, by using `cfg` as a directive to avoid building `tauri-winres` on unix platforms alltogether.

```toml
[package]
Expand Down Expand Up @@ -99,6 +99,6 @@ See [MSDN] for more details on the version info section of executables/libraries

## About this project

The [original author](https://github.com/mxre) and maintainers use this crate for their personal projects and although is has been tested in that context, we have no idea if the behaviour is the same everywhere.
The [original author](https://github.com/mxre) and maintainers use this crate for their personal projects and although it has been tested in that context, we have no idea if the behaviour is the same everywhere.

To be brief, we are very much reliant on your bug reports and feature suggestions to make this crate better.
8 changes: 0 additions & 8 deletions example/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ fn main() {
// as calling rc.exe might be slow
if std::env::var("PROFILE").unwrap() == "release" {
let mut res = tauri_winres::WindowsResource::new();
if cfg!(unix) {
// paths for X64 on archlinux
res.set_toolkit_path("/usr/x86_64-w64-mingw32/bin");
// ar tool for mingw in toolkit path
res.set_ar_path("ar");
// windres tool
res.set_windres_path("/usr/bin/x86_64-w64-mingw32-windres");
}

res.set_icon("icon.ico")
// can't use winapi crate constants for cross compiling
Expand Down
62 changes: 62 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::{
collections::HashMap,
env,
fs::File,
io::{self, Read},
path::Path,
};

pub(crate) fn parse_cargo_toml(props: &mut HashMap<String, String>) -> io::Result<()> {
let cargo = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("Cargo.toml");
let mut f = File::open(cargo)?;
let mut cargo_toml = String::new();
f.read_to_string(&mut cargo_toml)?;
if let Ok(ml) = cargo_toml.parse::<toml::Value>() {
if let Some(pkg) = ml.get("package") {
if let Some(pkg) = pkg.get("metadata") {
if let Some(pkg) = pkg.get("tauri-winres") {
if let Some(pkg) = pkg.as_table() {
for (k, v) in pkg {
// println!("{} {}", k ,v);
if let Some(v) = v.as_str() {
props.insert(k.clone(), v.to_string());
} else {
println!("package.metadata.tauri-winres.{} is not a string", k);
}
}
} else {
println!("package.metadata.tauri-winres is not a table");
}
} else {
println!("package.metadata.tauri-winres does not exist");
}
} else {
println!("package.metadata does not exist");
}
} else {
println!("package does not exist");
}
} else {
println!("TOML parsing error")
}
Ok(())
}

pub(crate) fn escape_string(string: &str) -> String {
let mut escaped = String::new();
for chr in string.chars() {
// In quoted RC strings, double-quotes are escaped by using two
// consecutive double-quotes. Other characters are escaped in the
// usual C way using backslashes.
match chr {
'"' => escaped.push_str("\"\""),
'\'' => escaped.push_str("\\'"),
'\\' => escaped.push_str("\\\\"),
'\n' => escaped.push_str("\\n"),
'\t' => escaped.push_str("\\t"),
'\r' => escaped.push_str("\\r"),
_ => escaped.push(chr),
};
}
escaped
}
Loading

0 comments on commit 3aa8411

Please sign in to comment.