Skip to content

Commit

Permalink
feat:add clap argument parser
Browse files Browse the repository at this point in the history
ops:generate typescript bindings before prod build

this commit introduces the `clap` crate - the command line argument
parser library. with it we can now run the tauri backend with the
`--generate-bindings-only` flag to only output the typescript bindings
and then exit the program. this, we can now use to generate typescript
bindings freshly before every production build. i have also went ahead
and added the generated `src/bindings.ts` file to `.gitignore` so as not
to track it and forcing people to generate it anew on every build.
  • Loading branch information
cedricschwyter committed Jun 30, 2023
1 parent dd64ad8 commit e3bc19d
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 14 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/verify-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.0-dev libappindicator3-dev librsvg2-dev patchelf
- name: generate typescript bindings
run: cd src-tauri && cargo run --release -- --generate-bindings-only
- name: install frontend dependencies
run: yarn install
- uses: tauri-apps/tauri-action@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Tarpaulin
run: cd src-tauri && cargo install cargo-tarpaulin
continue-on-error: true
- name: Run cargo:test
run: cd src-tauri && cargo test --verbose --all-features
- name: Run cargo:fmt
run: cd src-tauri && cargo fmt --all -- --check
- name: Run cargo:clippy
run: cd src-tauri && cargo clippy --all-features -- -D warnings
- name: Run cargo:test
run: cd src-tauri && cargo test --verbose --all-features
- name: Setup Tarpaulin
run: cd src-tauri && cargo install cargo-tarpaulin
continue-on-error: true
- name: Run cargo:tarpaulin
run: cd src-tauri && cargo tarpaulin --verbose --all-features --out Xml --output-dir ./coverage
- name: Codecov
Expand Down Expand Up @@ -172,6 +174,8 @@ jobs:
sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.0-dev libappindicator3-dev librsvg2-dev patchelf
- name: pull latest commit
run: git pull
- name: generate typescript bindings
run: cd src-tauri && cargo run --release -- --generate-bindings-only
- name: install frontend dependencies
run: yarn install
- uses: tauri-apps/tauri-action@v0
Expand Down
103 changes: 103 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
[package]
name = "huehuehue"
version = "0.0.1"
description = "A Tauri App"
authors = ["you"]
description = "A Tauri App to manage your Philips Hue devices from anywhere"
authors = [
"Kyrill Gobber <[email protected]>",
"Soryn Bächli <>",
"Cedric Schwyter <[email protected]>",
]
license = ""
repository = ""
repository = "https://github.com/KyrillGobber/huehuehue"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down Expand Up @@ -36,6 +40,7 @@ serde_derive = "1.0.164"
thiserror = "1.0.40"
specta = "1.0.4"
tauri-specta = { version = "1.0.2", features = ["javascript", "typescript"] }
clap = { version = "4.3.10", features = ["derive"] }

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
1 change: 0 additions & 1 deletion src-tauri/core/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ macro_rules! handlers {
#[macro_export]
macro_rules! bindings {
($path:expr) => {
#[cfg(debug_assertions)]
tauri_specta::ts::export(specta::collect_types![$($handler,)*], $path).unwrap();
};
}
Expand Down
21 changes: 18 additions & 3 deletions src-tauri/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod core;

use clap::Parser;
use futures_util::{pin_mut, stream::StreamExt};
use log::info;
use mdns::RecordKind;
Expand All @@ -12,12 +13,22 @@ const HUE_BRIDGE_SERVICE_NAME: &str = "_hue._tcp.local";
const HUE_BRIDGE_SERVICE_QUERY_INTERVAL_SECONDS: u64 = 3600;
const HUE_BRIDGE_API_BASE_URL: &str = "/clip/v2";

#[derive(Debug, Default)]
pub struct HueHueHueConfig {}
#[derive(Clone, Debug, Parser)]
#[command(author, version, about)]
pub struct HueHueHueConfig {
#[arg(long)]
pub generate_bindings_only: bool,
}

impl Default for HueHueHueConfig {
fn default() -> Self {
HueHueHueConfig::parse()
}
}

#[derive(Debug, Default)]
pub struct HueHueHue {
_config: HueHueHueConfig,
config: HueHueHueConfig,
bridge_ip_addrs: Arc<Mutex<HashSet<IpAddr>>>,
client: Client,
}
Expand All @@ -42,6 +53,10 @@ impl serde::Serialize for HueHueHueError {
}

impl HueHueHue {
pub fn get_config(&self) -> HueHueHueConfig {
self.config.clone()
}

fn get_base_url(&self) -> String {
// TODO: compute the actual base url using the currently selected bridge device
HUE_BRIDGE_API_BASE_URL.to_string()
Expand Down
16 changes: 14 additions & 2 deletions src-tauri/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,28 @@ use log::info;
use tauri::RunEvent;
use tokio::sync::Mutex;

const TS_BINDINGS_FILE: &str = "../src/bindings.ts";

#[tokio::main]
pub async fn main() -> Result<(), HueHueHueError> {
env_logger::init_from_env(
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
);
tauri::async_runtime::set(tokio::runtime::Handle::current());
info!("starting huehuehue...");
let huehuehue: HueHueHue = HueHueHue::default();
info!("run config: {:?}", huehuehue.get_config());
if huehuehue.get_config().generate_bindings_only {
info!("generating bindings to \"{}\"", TS_BINDINGS_FILE);
bindings!(TS_BINDINGS_FILE);
info!(
"successfully generated bindings to \"{}\"",
TS_BINDINGS_FILE
);

return Ok(());
}
info!("starting huehuehue...");
huehuehue.discover()?;
bindings!("../src/bindings.ts");
huehuehue_handlers!(tauri::Builder::default())
.manage(HueHueHueState(Mutex::new(huehuehue)))
.build(tauri::generate_context!())
Expand Down
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bindings.ts

0 comments on commit e3bc19d

Please sign in to comment.