Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codegen): allow defining additional capabilities, closes #8798 #8802

Merged
merged 12 commits into from Feb 19, 2024

Conversation

lucasfernog
Copy link
Member

Changes the tauri::generate_context proc macro to parse the capabilities attribute:

tauri::generate_context!(
  capabilities = [
    "../capabilities/base.json",
    #[cfg(feature = "beta")]
    "../capabilities/beta.json"
  ]

In this case, the additional capabilities are parsed at runtime (limitation of proc-macro, cannot resolve the #[cfg] attributes :(

Additionally, the tauri-build context codegen method now has a capability method:

fn main() {
  let mut codegen = tauri_build::CodegenContext::new();
  if !cfg!(feature = "custom-protocol") {
    codegen = codegen.dev();
  }

  #[cfg(feature = "admin")]
  codegen = codegen.capability("./capabilities/admin.json");

  tauri_build::try_build(tauri_build::Attributes::new().codegen(codegen))
    .expect("failed to run tauri-build");
}

This API is more powerful since it can resolve the capability at compile time.

@lucasfernog lucasfernog requested a review from a team as a code owner February 6, 2024 20:41
@lucasfernog-crabnebula
Copy link
Contributor

RuntimeAuthority::add_capability implements the base API for #8799

core/tauri/src/ipc/authority.rs Show resolved Hide resolved
@@ -84,6 +85,93 @@ impl RuntimeAuthority {
}
}

#[doc(hidden)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this need to be hidden also? and have this internal name? why not just expose it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not stable yet.. maybe we move it under the unstable feature flag, but I don't like the idea of allowing devs to allow an specific command, they should go through the whole capabilities > permissions folow, this was just added for our examples which can't use a build script so they do not go through our ACL definitions
cc @tweidinger

core/tauri-macros/src/context.rs Outdated Show resolved Hide resolved
core/tauri-utils/src/acl/resolved.rs Outdated Show resolved Hide resolved
core/tauri-utils/src/acl/resolved.rs Outdated Show resolved Hide resolved
@lucasfernog
Copy link
Member Author

Needs #8797

@lucasfernog lucasfernog marked this pull request as draft February 7, 2024 20:56
Base automatically changed from refactor/capabilities-conf to dev February 18, 2024 13:42
@lucasfernog lucasfernog marked this pull request as ready for review February 18, 2024 19:03
@lucasfernog
Copy link
Member Author

I would be ok with removing the option from the generate_context macro too since it leads you into thinking it's resolved at compile time but it isn't (the tauri-build option is much better).

@amrbashir
Copy link
Member

amrbashir commented Feb 19, 2024

In this case, the additional capabilities are parsed at runtime (limitation of proc-macro, cannot resolve the #[cfg] attributes :(

Can't we just provide an option to specify a capability file without #[cfg] and so we can parse this at compile-time? if they want to use #[cfg], they should do so on the whole macro

#[cfg(not(beta))]
let context = tauri::generate_context!(capabilities = ["path/to/capability.json"]);
#[cfg(beta)]
let context = tauri::generate_context!(capabilities = ["path/to/capability-beta.json"]);

@lucasfernog
Copy link
Member Author

Let's roll with that approach for now until Rust supports expanding cfg on the proc macro.

@lucasfernog lucasfernog merged commit 8d16a80 into dev Feb 19, 2024
28 checks passed
@lucasfernog lucasfernog deleted the feat/codegen-context-capabilities branch February 19, 2024 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants