wasm_run_proc_macro/lib.rs
1//! See the crate `wasm-run` for documentation.
2
3mod attr_parser;
4mod main_generator;
5
6use cargo_metadata::MetadataCommand;
7use proc_macro::TokenStream;
8use syn::{parse_macro_input, ItemEnum};
9
10/// Makes an entrypoint to your binary (not WASM).
11///
12/// ```ignore
13/// #[wasm_run::main(
14/// pre_build = my_pre_build_function,
15/// post_build = my_post_build_function,
16/// build_args = my_build_args_struct,
17/// serve_args = my_serve_args_struct,
18/// // ... see below for all the available arguments
19/// )]
20/// enum Cli {}
21/// ```
22///
23/// It requires to be used with `structopt` on an `enum`. Please consult the documentation of
24/// `structopt` if you don't know how to make an `enum` with it.
25///
26/// There are a number of named arguments you can provide to the macro:
27/// - `other_cli_commands`: a function that is called if you have added new commands to the
28/// `enum`;
29/// - `pre_build`: a function that is called when the build has not yet started (you can tweak
30/// the command-line arguments of the build command);
31/// - `post_build`: a function that is called when the build is finished (after the optimization
32/// with `wasm-opt`);
33/// - `frontend_watch`: a function that is called when the watcher is being initialized (allowing
34/// you to add extra things to watch for example);
35/// - `backend_watch`: a function that is called when the watcher is being initialized (allowing
36/// you to add extra things to watch for example);
37/// - `serve`: (only if built with the `serve` feature): a function that is called when the HTTP
38/// serve is getting configured;
39/// - `default_build_path`: a function that is called that provides the default directory path
40/// when the user didn't provide it through the command-line arguments (the default is
41/// `workspace root/build`);
42/// - `build_args`: allow you to override the `build` command when providing a custom argument
43/// (the default is `DefaultBuildArgs`);
44/// - `serve_args`: allow you to override the `serve` command when providing a custom argument
45/// (the default is `DefaultServeArgs`).
46///
47/// You can also change the frontend package that is built by providing its name in the first
48/// positional argument:
49///
50/// ```ignore
51/// #[wasm_run::main("my-frontend-package")]
52/// enum Cli {}
53/// ```
54///
55/// And the backend package in the second positional argument:
56///
57/// ```ignore
58/// #[wasm_run::main("my-frontend-package", "my-backend-package")]
59/// enum Cli {}
60/// ```
61///
62/// # Examples
63///
64/// See the [`examples/`](https://github.com/IMI-eRnD-Be/wasm-run/tree/main/examples/custom-cli-command)
65/// directory.
66#[proc_macro_attribute]
67pub fn main(attr: TokenStream, item: TokenStream) -> TokenStream {
68 let item = parse_macro_input!(item as ItemEnum);
69 let attr = parse_macro_input!(attr with attr_parser::Attr::parse);
70 let metadata = MetadataCommand::new()
71 .exec()
72 .expect("could not get metadata");
73
74 main_generator::generate(item, attr, &metadata)
75 .unwrap_or_else(|err| err.to_compile_error())
76 .into()
77}