Deprecate relative asset paths, better warnings for asset!() (#3214)

* Deprecate relative asset paths

* fix paths, better warnings in asset parser
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2024-11-13 19:52:23 -05:00
1 parent 8a2922c663
commit ba4389567d
13 files changed
+95 -36

No files matched your search

+27 -11
View File
@@ -1,6 +1,6 @@
use manganis_core::ResourceAsset;
use manganis_core::{AssetParseError, ResourceAsset};
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens};
use quote::{quote, ToTokens, TokenStreamExt};
use syn::{
parse::{Parse, ParseStream},
LitStr,
@@ -8,7 +8,7 @@ use syn::{
pub struct AssetParser {
/// The asset itself
asset: ResourceAsset,
asset: Result<ResourceAsset, AssetParseError>,
/// The source of the trailing options
options: TokenStream2,
@@ -19,13 +19,13 @@ impl Parse for AssetParser {
//
// This gives you the Asset type - it's generic and basically unrefined
// ```
// asset!("myfile.png")
// asset!("/assets/myfile.png")
// ```
//
// To narrow the type, use a method call to get the refined type
// ```
// asset!(
// "myfile.png",
// "/assets/myfile.png",
// asset::image()
// .format(ImageType::Jpg)
// .size(512, 512)
@@ -36,7 +36,7 @@ impl Parse for AssetParser {
fn parse(input: ParseStream) -> syn::Result<Self> {
// And then parse the options
let src = input.parse::<LitStr>()?;
let asset = ResourceAsset::parse_any(&src.value()).unwrap();
let asset = ResourceAsset::parse_any(&src.value());
let options = input.parse()?;
Ok(Self { asset, options })
@@ -60,23 +60,39 @@ impl ToTokens for AssetParser {
// a limitation from rust itself. We technically could support them but not without some hoops
// to jump through
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let asset = match self.asset.as_ref() {
Ok(asset) => asset,
Err(err) => {
let err = err.to_string();
tokens.append_all(quote! { compile_error!(#err) });
return;
}
};
// 1. the link section itself
let link_section = crate::generate_link_section(&self.asset);
let link_section = crate::generate_link_section(&asset);
// 2. original
let input = self.asset.input.display().to_string();
let input = asset.input.display().to_string();
// 3. resolved on the user's system
let local = self.asset.absolute.display().to_string();
let local = asset.absolute.display().to_string();
// 4. bundled
let bundled = self.asset.bundled.to_string();
let bundled = asset.bundled.to_string();
// 5. source tokens
let option_source = &self.options;
// generate the asset::new method to deprecate the `./assets/blah.css` syntax
let method = if asset.input.is_relative() {
quote::quote! { new_relative }
} else {
quote::quote! { new }
};
tokens.extend(quote! {
Asset::new(
Asset::#method(
{
#link_section
manganis::Asset {