mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 11:41:02 +00:00
introduce expander (#4974)
* introduce expander * unify towards expand * bubble up expand * spelling Co-authored-by: Andrei Sandu <54316454+sandreim@users.noreply.github.com> * add newlines Co-authored-by: Andrei Sandu <54316454+sandreim@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
8aff3cb81b
commit
d4176874a1
@@ -20,3 +20,7 @@ pin-project = "1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
trybuild = "1.0.53"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
expand = ["polkadot-overseer-gen-proc-macro/expand"]
|
||||
|
||||
@@ -76,3 +76,12 @@ subsystems must be initialized only once (another compile time check) or be _rep
|
||||
a special setter like method `replace_<subsystem>`.
|
||||
|
||||
A task spawner and subsystem context are required to be defined with `SpawnNamed` and respectively `SubsystemContext` implemented.
|
||||
|
||||
## Debugging
|
||||
|
||||
As always, debugging is notoriously annoying with bugged proc-macros.
|
||||
|
||||
Therefore [`expander`](https://github.com/drahnr/expander) is employed to yield better
|
||||
error messages. Enable with `--feature=polkadot-overseer-gen/expand` or
|
||||
`--feature=polkadot-overseer/expand` from the root of the project or
|
||||
make `"expand"` part of the default feature set.
|
||||
|
||||
@@ -16,12 +16,13 @@ syn = { version = "1.0.86", features = ["full", "extra-traits"] }
|
||||
quote = "1.0.15"
|
||||
proc-macro2 = "1.0.36"
|
||||
proc-macro-crate = "1.1.2"
|
||||
expander = "0.0.4"
|
||||
|
||||
[dev-dependencies]
|
||||
assert_matches = "1.5.0"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
# write the expanded version to a `overlord-expansion.rs`
|
||||
# in the `OUT_DIR` as defined by `cargo`.
|
||||
expansion = []
|
||||
# write the expanded version to a `overlord-expansion.[a-f0-9]{10}.rs`
|
||||
# in the `OUT_DIR` as defined by `cargo` for the `expander` crate.
|
||||
expand = []
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
/// A dummy build script, so `OUT_DIR` is set.
|
||||
fn main() {}
|
||||
@@ -98,36 +98,13 @@ pub(crate) fn impl_overseer_gen(
|
||||
additive.extend(impl_message_wrapper_enum(&info)?);
|
||||
additive.extend(impl_dispatch(&info));
|
||||
|
||||
// Write to a file for expansion, and then use it via `include!()`
|
||||
// in order to obtain better compiler errors when modifying `overlord`.
|
||||
if cfg!(feature = "expansion") {
|
||||
use std::io::Write;
|
||||
let ts = expander::Expander::new("overlord-expansion")
|
||||
.add_comment("Generated overseer code by `#[overlord(..)]`".to_owned())
|
||||
.dry(!cfg!(feature = "expand"))
|
||||
.verbose(false)
|
||||
.fmt(expander::Edition::_2018)
|
||||
.write_to_out_dir(additive)
|
||||
.expect("Expander does not fail due to IO in OUT_DIR. qed");
|
||||
|
||||
let out = env!("OUT_DIR");
|
||||
let out = std::path::PathBuf::from(out);
|
||||
let path = out.join("overlord-expansion.rs");
|
||||
let mut f = std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(&path)
|
||||
.expect("File exists. qed");
|
||||
f.write_all(
|
||||
&mut format!("// {:?} \n{}", std::time::SystemTime::now(), additive).as_bytes(),
|
||||
)
|
||||
.expect("Got permissions to write to file. qed");
|
||||
std::process::Command::new("rustfmt")
|
||||
.arg("--edition=2018")
|
||||
.arg(&path)
|
||||
.current_dir(out)
|
||||
.spawn()
|
||||
.expect("Running rustfmt works. qed");
|
||||
|
||||
let path = path.display().to_string();
|
||||
Ok(quote! {
|
||||
include!( #path );
|
||||
})
|
||||
} else {
|
||||
Ok(additive)
|
||||
}
|
||||
Ok(ts)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user