macro: add a write attribute to packages

the .pm file now gets written if either 'write=true' or
'file' is set, setting just 'lib' won't trigger a write
anymore

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2022-02-25 15:45:56 +01:00
parent 08c14b8fc5
commit c15e1e1456
2 changed files with 16 additions and 2 deletions

View File

@ -7,6 +7,7 @@ pub struct ModuleAttrs {
pub package_name: String, pub package_name: String,
pub file_name: Option<String>, pub file_name: Option<String>,
pub lib_name: Option<String>, pub lib_name: Option<String>,
pub write: Option<bool>,
} }
fn is_ident_check_dup<T>(path: &syn::Path, var: &Option<T>, what: &'static str) -> bool { fn is_ident_check_dup<T>(path: &syn::Path, var: &Option<T>, what: &'static str) -> bool {
@ -27,6 +28,7 @@ impl TryFrom<AttributeArgs> for ModuleAttrs {
let mut package_name = None; let mut package_name = None;
let mut file_name = None; let mut file_name = None;
let mut lib_name = None; let mut lib_name = None;
let mut write = None;
for arg in args { for arg in args {
match arg { match arg {
@ -45,6 +47,17 @@ impl TryFrom<AttributeArgs> for ModuleAttrs {
error!(path => "unknown argument"); error!(path => "unknown argument");
} }
} }
syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue {
path,
lit: syn::Lit::Bool(litbool),
..
})) => {
if is_ident_check_dup(&path, &write, "write") {
write = Some(litbool.value());
} else {
error!(path => "unknown argument");
}
}
_ => error!(Span::call_site(), "unexpected attribute argument"), _ => error!(Span::call_site(), "unexpected attribute argument"),
} }
} }
@ -56,6 +69,7 @@ impl TryFrom<AttributeArgs> for ModuleAttrs {
package_name, package_name,
file_name, file_name,
lib_name, lib_name,
write,
}) })
} }
} }

View File

@ -68,8 +68,8 @@ pub fn handle_module(attr: AttributeArgs, mut module: syn::ItemMod) -> Result<To
items.push(syn::Item::Verbatim(package.bootstrap_function())); items.push(syn::Item::Verbatim(package.bootstrap_function()));
} }
if package.attrs.file_name.is_some() if package.attrs.write == Some(true)
|| package.attrs.lib_name.is_some() || package.attrs.file_name.is_some()
|| std::env::var("PERLMOD_WRITE_PACKAGES").ok().as_deref() == Some("1") || std::env::var("PERLMOD_WRITE_PACKAGES").ok().as_deref() == Some("1")
{ {
package.write()?; package.write()?;