Hotreloading of for/if/body, formatted strings, literals, component props, nested rsx, light CLI rewrite, cli TUI (#2258)
Hotreload the contents of for loops, if chains, component bodies, props, attributes, and any literals discovered in rsx! Add a TUI renderer to the CLI. Improve the CLI build system to be async and parallel. Refactor RSX to allow partial expansion of expressions. Merge autofmt implementations for consistency. Merge the representation of elements and components under the hood. Add a diagnostics system for rsx for improved error messages. Drop interprocess and move to websockets for communication between the CLI and the server. Assign IDs to nodes and attributes in a stable way to be used in non compiler contexts. Add hotreloading to any body of component/for loop/if chain/etc. --------- Co-authored-by: Evan Almloff <evanalmloff@gmail.com> Co-authored-by: Liam Mitchell <liamkarlmitchell@gmail.com>
This commit is contained in:
181 files changed
+12755
-10806
No files matched your search
@@ -1,667 +1,145 @@
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::ToTokens;
|
||||
use syn::{Expr, File, Item, Macro, Stmt, TraitItem};
|
||||
//! Compare two files and find any rsx calls that have changed
|
||||
//!
|
||||
//! This is used to determine if a hotreload is needed.
|
||||
//! We use a special syn visitor to find all the rsx! calls in the file and then compare them to see
|
||||
//! if they are the same. This visitor will actually remove the rsx! calls and replace them with a
|
||||
//! dummy rsx! call. The final file type is thus mutated in place, leaving the original file idents
|
||||
//! in place. We then compare the two files to see if they are the same. We're able to differentiate
|
||||
//! between rust code changes and rsx code changes with much less code than the previous manual diff
|
||||
//! approach.
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum DiffResult {
|
||||
/// Non-rsx was changed in the file
|
||||
CodeChanged(NotreloadableReason),
|
||||
|
||||
/// Rsx was changed in the file
|
||||
///
|
||||
/// Contains a list of macro invocations that were changed
|
||||
RsxChanged { rsx_calls: Vec<ChangedRsx> },
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum NotreloadableReason {
|
||||
RootMismatch,
|
||||
|
||||
RsxMismatch,
|
||||
}
|
||||
use syn::visit_mut::VisitMut;
|
||||
use syn::{File, Macro};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ChangedRsx {
|
||||
/// The macro that was changed
|
||||
/// The old macro - the original RSX from the original file
|
||||
pub old: Macro,
|
||||
|
||||
/// The new tokens for the macro
|
||||
pub new: TokenStream,
|
||||
/// The new macro
|
||||
pub new: Macro,
|
||||
}
|
||||
|
||||
/// Find any rsx calls in the given file and return a list of all the rsx calls that have changed.
|
||||
pub fn diff_rsx(new: &File, old: &File) -> DiffResult {
|
||||
let mut rsx_calls = Vec::new();
|
||||
///
|
||||
/// Takes in the two files, clones them, removes the rsx! contents and prunes any doc comments.
|
||||
/// Then it compares the two files to see if they are different - if they are, the code changed.
|
||||
/// Otherwise, the code is the same and we can move on to handling the changed rsx
|
||||
///
|
||||
/// Returns `None` if the files are the same and `Some` if they are different
|
||||
/// If there are no rsx! calls in the files, the vec will be empty.
|
||||
pub fn diff_rsx(new: &File, old: &File) -> Option<Vec<ChangedRsx>> {
|
||||
// Make a clone of these files in place so we don't have to worry about mutating the original
|
||||
let mut old = old.clone();
|
||||
let mut new = new.clone();
|
||||
|
||||
if new.items.len() != old.items.len() {
|
||||
tracing::trace!(
|
||||
"found not hot reload-able change {:#?} != {:#?}",
|
||||
new.items
|
||||
.iter()
|
||||
.map(|i| i.to_token_stream().to_string())
|
||||
.collect::<Vec<_>>(),
|
||||
old.items
|
||||
.iter()
|
||||
.map(|i| i.to_token_stream().to_string())
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
return DiffResult::CodeChanged(NotreloadableReason::RootMismatch);
|
||||
// Collect all the rsx! macros from the old file - modifying the files in place
|
||||
let old_macros = collect_from_file(&mut old);
|
||||
let new_macros = collect_from_file(&mut new);
|
||||
|
||||
// If the number of rsx! macros is different, then it's not hotreloadable
|
||||
if old_macros.len() != new_macros.len() {
|
||||
return None;
|
||||
}
|
||||
|
||||
for (new, old) in new.items.iter().zip(old.items.iter()) {
|
||||
if find_rsx_item(new, old, &mut rsx_calls) {
|
||||
tracing::trace!(
|
||||
"found not hot reload-able change {:#?} != {:#?}",
|
||||
new.to_token_stream().to_string(),
|
||||
old.to_token_stream().to_string()
|
||||
);
|
||||
|
||||
return DiffResult::CodeChanged(NotreloadableReason::RsxMismatch);
|
||||
}
|
||||
// If the files are not the same, then it's not hotreloadable
|
||||
if old != new {
|
||||
return None;
|
||||
}
|
||||
|
||||
tracing::trace!("found hot reload-able changes {:#?}", rsx_calls);
|
||||
DiffResult::RsxChanged { rsx_calls }
|
||||
Some(
|
||||
old_macros
|
||||
.into_iter()
|
||||
.zip(new_macros)
|
||||
.map(|(old, new)| ChangedRsx { old, new })
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
fn find_rsx_item(new: &Item, old: &Item, rsx_calls: &mut Vec<ChangedRsx>) -> bool {
|
||||
match (new, old) {
|
||||
(Item::Const(new_item), Item::Const(old_item)) => {
|
||||
find_rsx_expr(&new_item.expr, &old_item.expr, rsx_calls)
|
||||
|| new_item.attrs != old_item.attrs
|
||||
|| new_item.vis != old_item.vis
|
||||
|| new_item.const_token != old_item.const_token
|
||||
|| new_item.ident != old_item.ident
|
||||
|| new_item.colon_token != old_item.colon_token
|
||||
|| new_item.ty != old_item.ty
|
||||
|| new_item.eq_token != old_item.eq_token
|
||||
|| new_item.semi_token != old_item.semi_token
|
||||
}
|
||||
(Item::Enum(new_item), Item::Enum(old_item)) => {
|
||||
if new_item.variants.len() != old_item.variants.len() {
|
||||
return true;
|
||||
}
|
||||
for (new_variant, old_variant) in new_item.variants.iter().zip(old_item.variants.iter())
|
||||
{
|
||||
match (&new_variant.discriminant, &old_variant.discriminant) {
|
||||
(Some((new_eq, new_expr)), Some((old_eq, old_expr))) => {
|
||||
if find_rsx_expr(new_expr, old_expr, rsx_calls) || new_eq != old_eq {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
(None, None) => (),
|
||||
_ => return true,
|
||||
}
|
||||
if new_variant.attrs != old_variant.attrs
|
||||
|| new_variant.ident != old_variant.ident
|
||||
|| new_variant.fields != old_variant.fields
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
new_item.attrs != old_item.attrs
|
||||
|| new_item.vis != old_item.vis
|
||||
|| new_item.enum_token != old_item.enum_token
|
||||
|| new_item.ident != old_item.ident
|
||||
|| new_item.generics != old_item.generics
|
||||
|| new_item.brace_token != old_item.brace_token
|
||||
}
|
||||
(Item::ExternCrate(new_item), Item::ExternCrate(old_item)) => old_item != new_item,
|
||||
(Item::Fn(new_item), Item::Fn(old_item)) => {
|
||||
find_rsx_block(&new_item.block, &old_item.block, rsx_calls)
|
||||
|| new_item.attrs != old_item.attrs
|
||||
|| new_item.vis != old_item.vis
|
||||
|| new_item.sig != old_item.sig
|
||||
}
|
||||
(Item::ForeignMod(new_item), Item::ForeignMod(old_item)) => old_item != new_item,
|
||||
(Item::Impl(new_item), Item::Impl(old_item)) => {
|
||||
if new_item.items.len() != old_item.items.len() {
|
||||
return true;
|
||||
}
|
||||
for (new_item, old_item) in new_item.items.iter().zip(old_item.items.iter()) {
|
||||
if match (new_item, old_item) {
|
||||
(syn::ImplItem::Const(new_item), syn::ImplItem::Const(old_item)) => {
|
||||
find_rsx_expr(&new_item.expr, &old_item.expr, rsx_calls)
|
||||
}
|
||||
(syn::ImplItem::Fn(new_item), syn::ImplItem::Fn(old_item)) => {
|
||||
find_rsx_block(&new_item.block, &old_item.block, rsx_calls)
|
||||
}
|
||||
(syn::ImplItem::Type(new_item), syn::ImplItem::Type(old_item)) => {
|
||||
old_item != new_item
|
||||
}
|
||||
(syn::ImplItem::Macro(new_item), syn::ImplItem::Macro(old_item)) => {
|
||||
old_item != new_item
|
||||
}
|
||||
(syn::ImplItem::Verbatim(stream), syn::ImplItem::Verbatim(stream2)) => {
|
||||
stream.to_string() != stream2.to_string()
|
||||
}
|
||||
_ => true,
|
||||
} {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
new_item.attrs != old_item.attrs
|
||||
|| new_item.defaultness != old_item.defaultness
|
||||
|| new_item.unsafety != old_item.unsafety
|
||||
|| new_item.impl_token != old_item.impl_token
|
||||
|| new_item.generics != old_item.generics
|
||||
|| new_item.trait_ != old_item.trait_
|
||||
|| new_item.self_ty != old_item.self_ty
|
||||
|| new_item.brace_token != old_item.brace_token
|
||||
}
|
||||
(Item::Macro(new_item), Item::Macro(old_item)) => {
|
||||
find_rsx_macro(&new_item.mac, &old_item.mac, rsx_calls)
|
||||
|| new_item.attrs != old_item.attrs
|
||||
|| new_item.semi_token != old_item.semi_token
|
||||
|| new_item.ident != old_item.ident
|
||||
}
|
||||
(Item::Mod(new_item), Item::Mod(old_item)) => {
|
||||
match (&new_item.content, &old_item.content) {
|
||||
(Some((_, new_items)), Some((_, old_items))) => {
|
||||
if new_items.len() != old_items.len() {
|
||||
return true;
|
||||
}
|
||||
for (new_item, old_item) in new_items.iter().zip(old_items.iter()) {
|
||||
if find_rsx_item(new_item, old_item, rsx_calls) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
new_item.attrs != old_item.attrs
|
||||
|| new_item.vis != old_item.vis
|
||||
|| new_item.mod_token != old_item.mod_token
|
||||
|| new_item.ident != old_item.ident
|
||||
|| new_item.semi != old_item.semi
|
||||
}
|
||||
(None, None) => {
|
||||
new_item.attrs != old_item.attrs
|
||||
|| new_item.vis != old_item.vis
|
||||
|| new_item.mod_token != old_item.mod_token
|
||||
|| new_item.ident != old_item.ident
|
||||
|| new_item.semi != old_item.semi
|
||||
}
|
||||
_ => true,
|
||||
pub fn collect_from_file(file: &mut File) -> Vec<Macro> {
|
||||
struct MacroCollector(Vec<Macro>);
|
||||
impl VisitMut for MacroCollector {
|
||||
/// Take out the rsx! macros, leaving a default in their place
|
||||
fn visit_macro_mut(&mut self, dest: &mut syn::Macro) {
|
||||
let name = &dest.path.segments.last().map(|i| i.ident.to_string());
|
||||
if let Some("rsx" | "render") = name.as_deref() {
|
||||
let mut default: syn::Macro = syn::parse_quote! { rsx! {} };
|
||||
std::mem::swap(dest, &mut default);
|
||||
self.0.push(default)
|
||||
}
|
||||
}
|
||||
(Item::Static(new_item), Item::Static(old_item)) => {
|
||||
find_rsx_expr(&new_item.expr, &old_item.expr, rsx_calls)
|
||||
|| new_item.attrs != old_item.attrs
|
||||
|| new_item.vis != old_item.vis
|
||||
|| new_item.static_token != old_item.static_token
|
||||
|| new_item.mutability != old_item.mutability
|
||||
|| new_item.ident != old_item.ident
|
||||
|| new_item.colon_token != old_item.colon_token
|
||||
|| new_item.ty != old_item.ty
|
||||
|| new_item.eq_token != old_item.eq_token
|
||||
|| new_item.semi_token != old_item.semi_token
|
||||
}
|
||||
(Item::Struct(new_item), Item::Struct(old_item)) => old_item != new_item,
|
||||
(Item::Trait(new_item), Item::Trait(old_item)) => {
|
||||
find_rsx_trait(new_item, old_item, rsx_calls)
|
||||
}
|
||||
(Item::TraitAlias(new_item), Item::TraitAlias(old_item)) => old_item != new_item,
|
||||
(Item::Type(new_item), Item::Type(old_item)) => old_item != new_item,
|
||||
(Item::Union(new_item), Item::Union(old_item)) => old_item != new_item,
|
||||
(Item::Use(new_item), Item::Use(old_item)) => old_item != new_item,
|
||||
(Item::Verbatim(_), Item::Verbatim(_)) => false,
|
||||
|
||||
_ => true,
|
||||
/// Ignore doc comments by swapping them out with a default
|
||||
fn visit_attribute_mut(&mut self, i: &mut syn::Attribute) {
|
||||
if i.path().is_ident("doc") {
|
||||
*i = syn::parse_quote! { #[doc = ""] };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut macros = MacroCollector(vec![]);
|
||||
macros.visit_file_mut(file);
|
||||
macros.0
|
||||
}
|
||||
|
||||
fn find_rsx_trait(
|
||||
new_item: &syn::ItemTrait,
|
||||
old_item: &syn::ItemTrait,
|
||||
rsx_calls: &mut Vec<ChangedRsx>,
|
||||
) -> bool {
|
||||
if new_item.items.len() != old_item.items.len() {
|
||||
return true;
|
||||
}
|
||||
for (new_item, old_item) in new_item.items.iter().zip(old_item.items.iter()) {
|
||||
if match (new_item, old_item) {
|
||||
(TraitItem::Const(new_item), TraitItem::Const(old_item)) => {
|
||||
if let (Some((_, new_expr)), Some((_, old_expr))) =
|
||||
(&new_item.default, &old_item.default)
|
||||
{
|
||||
find_rsx_expr(new_expr, old_expr, rsx_calls)
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
(TraitItem::Fn(new_item), TraitItem::Fn(old_item)) => {
|
||||
match (&new_item.default, &old_item.default) {
|
||||
(Some(new_block), Some(old_block)) => {
|
||||
find_rsx_block(new_block, old_block, rsx_calls)
|
||||
}
|
||||
(None, None) => false,
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
(TraitItem::Type(new_item), TraitItem::Type(old_item)) => old_item != new_item,
|
||||
(TraitItem::Macro(new_item), TraitItem::Macro(old_item)) => old_item != new_item,
|
||||
(TraitItem::Verbatim(stream), TraitItem::Verbatim(stream2)) => {
|
||||
stream.to_string() != stream2.to_string()
|
||||
}
|
||||
_ => true,
|
||||
} {
|
||||
return true;
|
||||
#[test]
|
||||
fn changing_files() {
|
||||
let old = r#"
|
||||
use dioxus::prelude::*;
|
||||
|
||||
/// some comment
|
||||
pub fn CoolChild() -> Element {
|
||||
let a = 123;
|
||||
|
||||
rsx! {
|
||||
div {
|
||||
{some_expr()}
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
new_item.attrs != old_item.attrs
|
||||
|| new_item.vis != old_item.vis
|
||||
|| new_item.unsafety != old_item.unsafety
|
||||
|| new_item.auto_token != old_item.auto_token
|
||||
|| new_item.ident != old_item.ident
|
||||
|| new_item.generics != old_item.generics
|
||||
|| new_item.colon_token != old_item.colon_token
|
||||
|| new_item.supertraits != old_item.supertraits
|
||||
|| new_item.brace_token != old_item.brace_token
|
||||
}
|
||||
let new = r#"
|
||||
use dioxus::prelude::*;
|
||||
|
||||
fn find_rsx_block(
|
||||
new_block: &syn::Block,
|
||||
old_block: &syn::Block,
|
||||
rsx_calls: &mut Vec<ChangedRsx>,
|
||||
) -> bool {
|
||||
if new_block.stmts.len() != old_block.stmts.len() {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (new_stmt, old_stmt) in new_block.stmts.iter().zip(old_block.stmts.iter()) {
|
||||
if find_rsx_stmt(new_stmt, old_stmt, rsx_calls) {
|
||||
return true;
|
||||
/// some comment
|
||||
pub fn CoolChild() -> Element {
|
||||
rsx! {
|
||||
div {
|
||||
{some_expr()}
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
new_block.brace_token != old_block.brace_token
|
||||
}
|
||||
let same = r#"
|
||||
use dioxus::prelude::*;
|
||||
|
||||
fn find_rsx_stmt(new_stmt: &Stmt, old_stmt: &Stmt, rsx_calls: &mut Vec<ChangedRsx>) -> bool {
|
||||
match (new_stmt, old_stmt) {
|
||||
(Stmt::Local(new_local), Stmt::Local(old_local)) => {
|
||||
(match (&new_local.init, &old_local.init) {
|
||||
(Some(new_local), Some(old_local)) => {
|
||||
find_rsx_expr(&new_local.expr, &old_local.expr, rsx_calls)
|
||||
|| new_local.diverge != old_local.diverge
|
||||
}
|
||||
(None, None) => false,
|
||||
_ => true,
|
||||
} || new_local.attrs != old_local.attrs
|
||||
|| new_local.let_token != old_local.let_token
|
||||
|| new_local.pat != old_local.pat
|
||||
|| new_local.semi_token != old_local.semi_token)
|
||||
}
|
||||
(Stmt::Item(new_item), Stmt::Item(old_item)) => {
|
||||
find_rsx_item(new_item, old_item, rsx_calls)
|
||||
}
|
||||
(Stmt::Expr(new_expr, _), Stmt::Expr(old_expr, _)) => {
|
||||
find_rsx_expr(new_expr, old_expr, rsx_calls)
|
||||
}
|
||||
(Stmt::Macro(new_macro), Stmt::Macro(old_macro)) => {
|
||||
find_rsx_macro(&new_macro.mac, &old_macro.mac, rsx_calls)
|
||||
|| new_macro.attrs != old_macro.attrs
|
||||
|| new_macro.semi_token != old_macro.semi_token
|
||||
}
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
/// some comment!!!!!
|
||||
pub fn CoolChild() -> Element {
|
||||
let a = 123;
|
||||
|
||||
fn find_rsx_expr(new_expr: &Expr, old_expr: &Expr, rsx_calls: &mut Vec<ChangedRsx>) -> bool {
|
||||
match (new_expr, old_expr) {
|
||||
(Expr::Array(new_expr), Expr::Array(old_expr)) => {
|
||||
if new_expr.elems.len() != old_expr.elems.len() {
|
||||
return true;
|
||||
}
|
||||
for (new_el, old_el) in new_expr.elems.iter().zip(old_expr.elems.iter()) {
|
||||
if find_rsx_expr(new_el, old_el, rsx_calls) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
new_expr.attrs != old_expr.attrs || new_expr.bracket_token != old_expr.bracket_token
|
||||
}
|
||||
(Expr::Assign(new_expr), Expr::Assign(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.left, &old_expr.left, rsx_calls)
|
||||
|| find_rsx_expr(&new_expr.right, &old_expr.right, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.eq_token != old_expr.eq_token
|
||||
}
|
||||
(Expr::Async(new_expr), Expr::Async(old_expr)) => {
|
||||
find_rsx_block(&new_expr.block, &old_expr.block, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.async_token != old_expr.async_token
|
||||
|| new_expr.capture != old_expr.capture
|
||||
}
|
||||
(Expr::Await(new_expr), Expr::Await(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.base, &old_expr.base, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.dot_token != old_expr.dot_token
|
||||
|| new_expr.await_token != old_expr.await_token
|
||||
}
|
||||
(Expr::Binary(new_expr), Expr::Binary(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.left, &old_expr.left, rsx_calls)
|
||||
|| find_rsx_expr(&new_expr.right, &old_expr.right, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.op != old_expr.op
|
||||
}
|
||||
(Expr::Block(new_expr), Expr::Block(old_expr)) => {
|
||||
find_rsx_block(&new_expr.block, &old_expr.block, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.label != old_expr.label
|
||||
}
|
||||
(Expr::Break(new_expr), Expr::Break(old_expr)) => match (&new_expr.expr, &old_expr.expr) {
|
||||
(Some(new_inner), Some(old_inner)) => {
|
||||
find_rsx_expr(new_inner, old_inner, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.break_token != old_expr.break_token
|
||||
|| new_expr.label != old_expr.label
|
||||
}
|
||||
(None, None) => {
|
||||
new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.break_token != old_expr.break_token
|
||||
|| new_expr.label != old_expr.label
|
||||
}
|
||||
_ => true,
|
||||
},
|
||||
(Expr::Call(new_expr), Expr::Call(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.func, &old_expr.func, rsx_calls);
|
||||
if new_expr.args.len() != old_expr.args.len() {
|
||||
return true;
|
||||
}
|
||||
for (new_arg, old_arg) in new_expr.args.iter().zip(old_expr.args.iter()) {
|
||||
if find_rsx_expr(new_arg, old_arg, rsx_calls) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
new_expr.attrs != old_expr.attrs || new_expr.paren_token != old_expr.paren_token
|
||||
}
|
||||
(Expr::Cast(new_expr), Expr::Cast(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.expr, &old_expr.expr, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.as_token != old_expr.as_token
|
||||
|| new_expr.ty != old_expr.ty
|
||||
}
|
||||
(Expr::Closure(new_expr), Expr::Closure(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.body, &old_expr.body, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.movability != old_expr.movability
|
||||
|| new_expr.asyncness != old_expr.asyncness
|
||||
|| new_expr.capture != old_expr.capture
|
||||
|| new_expr.or1_token != old_expr.or1_token
|
||||
|| new_expr.inputs != old_expr.inputs
|
||||
|| new_expr.or2_token != old_expr.or2_token
|
||||
|| new_expr.output != old_expr.output
|
||||
}
|
||||
(Expr::Const(new_expr), Expr::Const(old_expr)) => {
|
||||
find_rsx_block(&new_expr.block, &old_expr.block, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.const_token != old_expr.const_token
|
||||
}
|
||||
(Expr::Continue(new_expr), Expr::Continue(old_expr)) => old_expr != new_expr,
|
||||
(Expr::Field(new_expr), Expr::Field(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.base, &old_expr.base, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.dot_token != old_expr.dot_token
|
||||
|| new_expr.member != old_expr.member
|
||||
}
|
||||
(Expr::ForLoop(new_expr), Expr::ForLoop(old_expr)) => {
|
||||
find_rsx_block(&new_expr.body, &old_expr.body, rsx_calls)
|
||||
|| find_rsx_expr(&new_expr.expr, &old_expr.expr, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.label != old_expr.label
|
||||
|| new_expr.for_token != old_expr.for_token
|
||||
|| new_expr.pat != old_expr.pat
|
||||
|| new_expr.in_token != old_expr.in_token
|
||||
}
|
||||
(Expr::Group(new_expr), Expr::Group(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.expr, &old_expr.expr, rsx_calls)
|
||||
}
|
||||
(Expr::If(new_expr), Expr::If(old_expr)) => {
|
||||
if find_rsx_expr(&new_expr.cond, &old_expr.cond, rsx_calls)
|
||||
|| find_rsx_block(&new_expr.then_branch, &old_expr.then_branch, rsx_calls)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
match (&new_expr.else_branch, &old_expr.else_branch) {
|
||||
(Some((new_tok, new_else)), Some((old_tok, old_else))) => {
|
||||
find_rsx_expr(new_else, old_else, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.if_token != old_expr.if_token
|
||||
|| new_expr.cond != old_expr.cond
|
||||
|| new_tok != old_tok
|
||||
}
|
||||
(None, None) => {
|
||||
new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.if_token != old_expr.if_token
|
||||
|| new_expr.cond != old_expr.cond
|
||||
}
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
(Expr::Index(new_expr), Expr::Index(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.expr, &old_expr.expr, rsx_calls)
|
||||
|| find_rsx_expr(&new_expr.index, &old_expr.index, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.bracket_token != old_expr.bracket_token
|
||||
}
|
||||
(Expr::Infer(new_expr), Expr::Infer(old_expr)) => new_expr != old_expr,
|
||||
(Expr::Let(new_expr), Expr::Let(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.expr, &old_expr.expr, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.let_token != old_expr.let_token
|
||||
|| new_expr.pat != old_expr.pat
|
||||
|| new_expr.eq_token != old_expr.eq_token
|
||||
rsx! {
|
||||
div {
|
||||
{some_expr()}
|
||||
}
|
||||
(Expr::Lit(new_expr), Expr::Lit(old_expr)) => old_expr != new_expr,
|
||||
(Expr::Loop(new_expr), Expr::Loop(old_expr)) => {
|
||||
find_rsx_block(&new_expr.body, &old_expr.body, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.label != old_expr.label
|
||||
|| new_expr.loop_token != old_expr.loop_token
|
||||
}
|
||||
(Expr::Macro(new_expr), Expr::Macro(old_expr)) => {
|
||||
find_rsx_macro(&new_expr.mac, &old_expr.mac, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
}
|
||||
(Expr::Match(new_expr), Expr::Match(old_expr)) => {
|
||||
if find_rsx_expr(&new_expr.expr, &old_expr.expr, rsx_calls) {
|
||||
return true;
|
||||
}
|
||||
for (new_arm, old_arm) in new_expr.arms.iter().zip(old_expr.arms.iter()) {
|
||||
match (&new_arm.guard, &old_arm.guard) {
|
||||
(Some((new_tok, new_expr)), Some((old_tok, old_expr))) => {
|
||||
if find_rsx_expr(new_expr, old_expr, rsx_calls) || new_tok != old_tok {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
(None, None) => (),
|
||||
_ => return true,
|
||||
}
|
||||
if find_rsx_expr(&new_arm.body, &old_arm.body, rsx_calls)
|
||||
|| new_arm.attrs != old_arm.attrs
|
||||
|| new_arm.pat != old_arm.pat
|
||||
|| new_arm.fat_arrow_token != old_arm.fat_arrow_token
|
||||
|| new_arm.comma != old_arm.comma
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.match_token != old_expr.match_token
|
||||
|| new_expr.brace_token != old_expr.brace_token
|
||||
}
|
||||
(Expr::MethodCall(new_expr), Expr::MethodCall(old_expr)) => {
|
||||
if find_rsx_expr(&new_expr.receiver, &old_expr.receiver, rsx_calls) {
|
||||
return true;
|
||||
}
|
||||
for (new_arg, old_arg) in new_expr.args.iter().zip(old_expr.args.iter()) {
|
||||
if find_rsx_expr(new_arg, old_arg, rsx_calls) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.dot_token != old_expr.dot_token
|
||||
|| new_expr.method != old_expr.method
|
||||
|| new_expr.turbofish != old_expr.turbofish
|
||||
|| new_expr.paren_token != old_expr.paren_token
|
||||
}
|
||||
(Expr::Paren(new_expr), Expr::Paren(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.expr, &old_expr.expr, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.paren_token != old_expr.paren_token
|
||||
}
|
||||
(Expr::Path(new_expr), Expr::Path(old_expr)) => old_expr != new_expr,
|
||||
(Expr::Range(new_expr), Expr::Range(old_expr)) => {
|
||||
match (&new_expr.start, &old_expr.start) {
|
||||
(Some(new_expr), Some(old_expr)) => {
|
||||
if find_rsx_expr(new_expr, old_expr, rsx_calls) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
(None, None) => (),
|
||||
_ => return true,
|
||||
}
|
||||
match (&new_expr.end, &old_expr.end) {
|
||||
(Some(new_inner), Some(old_inner)) => {
|
||||
find_rsx_expr(new_inner, old_inner, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.limits != old_expr.limits
|
||||
}
|
||||
(None, None) => {
|
||||
new_expr.attrs != old_expr.attrs || new_expr.limits != old_expr.limits
|
||||
}
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
(Expr::Reference(new_expr), Expr::Reference(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.expr, &old_expr.expr, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.and_token != old_expr.and_token
|
||||
|| new_expr.mutability != old_expr.mutability
|
||||
}
|
||||
(Expr::Repeat(new_expr), Expr::Repeat(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.expr, &old_expr.expr, rsx_calls)
|
||||
|| find_rsx_expr(&new_expr.len, &old_expr.len, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.bracket_token != old_expr.bracket_token
|
||||
|| new_expr.semi_token != old_expr.semi_token
|
||||
}
|
||||
(Expr::Return(new_expr), Expr::Return(old_expr)) => {
|
||||
match (&new_expr.expr, &old_expr.expr) {
|
||||
(Some(new_inner), Some(old_inner)) => {
|
||||
find_rsx_expr(new_inner, old_inner, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.return_token != old_expr.return_token
|
||||
}
|
||||
(None, None) => {
|
||||
new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.return_token != old_expr.return_token
|
||||
}
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
(Expr::Struct(new_expr), Expr::Struct(old_expr)) => {
|
||||
match (&new_expr.rest, &old_expr.rest) {
|
||||
(Some(new_expr), Some(old_expr)) => {
|
||||
if find_rsx_expr(new_expr, old_expr, rsx_calls) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
(None, None) => (),
|
||||
_ => return true,
|
||||
}
|
||||
for (new_field, old_field) in new_expr.fields.iter().zip(old_expr.fields.iter()) {
|
||||
if find_rsx_expr(&new_field.expr, &old_field.expr, rsx_calls)
|
||||
|| new_field.attrs != old_field.attrs
|
||||
|| new_field.member != old_field.member
|
||||
|| new_field.colon_token != old_field.colon_token
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.path != old_expr.path
|
||||
|| new_expr.brace_token != old_expr.brace_token
|
||||
|| new_expr.dot2_token != old_expr.dot2_token
|
||||
}
|
||||
(Expr::Try(new_expr), Expr::Try(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.expr, &old_expr.expr, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.question_token != old_expr.question_token
|
||||
}
|
||||
(Expr::TryBlock(new_expr), Expr::TryBlock(old_expr)) => {
|
||||
find_rsx_block(&new_expr.block, &old_expr.block, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.try_token != old_expr.try_token
|
||||
}
|
||||
(Expr::Tuple(new_expr), Expr::Tuple(old_expr)) => {
|
||||
for (new_el, old_el) in new_expr.elems.iter().zip(old_expr.elems.iter()) {
|
||||
if find_rsx_expr(new_el, old_el, rsx_calls) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
new_expr.attrs != old_expr.attrs || new_expr.paren_token != old_expr.paren_token
|
||||
}
|
||||
(Expr::Unary(new_expr), Expr::Unary(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.expr, &old_expr.expr, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.op != old_expr.op
|
||||
}
|
||||
(Expr::Unsafe(new_expr), Expr::Unsafe(old_expr)) => {
|
||||
find_rsx_block(&new_expr.block, &old_expr.block, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.unsafe_token != old_expr.unsafe_token
|
||||
}
|
||||
(Expr::While(new_expr), Expr::While(old_expr)) => {
|
||||
find_rsx_expr(&new_expr.cond, &old_expr.cond, rsx_calls)
|
||||
|| find_rsx_block(&new_expr.body, &old_expr.body, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.label != old_expr.label
|
||||
|| new_expr.while_token != old_expr.while_token
|
||||
}
|
||||
(Expr::Yield(new_expr), Expr::Yield(old_expr)) => match (&new_expr.expr, &old_expr.expr) {
|
||||
(Some(new_inner), Some(old_inner)) => {
|
||||
find_rsx_expr(new_inner, old_inner, rsx_calls)
|
||||
|| new_expr.attrs != old_expr.attrs
|
||||
|| new_expr.yield_token != old_expr.yield_token
|
||||
}
|
||||
(None, None) => {
|
||||
new_expr.attrs != old_expr.attrs || new_expr.yield_token != old_expr.yield_token
|
||||
}
|
||||
_ => true,
|
||||
},
|
||||
(Expr::Verbatim(stream), Expr::Verbatim(stream2)) => {
|
||||
stream.to_string() != stream2.to_string()
|
||||
}
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
fn find_rsx_macro(
|
||||
new_mac: &syn::Macro,
|
||||
old_mac: &syn::Macro,
|
||||
rsx_calls: &mut Vec<ChangedRsx>,
|
||||
) -> bool {
|
||||
if matches!(
|
||||
new_mac
|
||||
.path
|
||||
.get_ident()
|
||||
.map(|ident| ident.to_string())
|
||||
.as_deref(),
|
||||
Some("rsx" | "render")
|
||||
) && matches!(
|
||||
old_mac
|
||||
.path
|
||||
.get_ident()
|
||||
.map(|ident| ident.to_string())
|
||||
.as_deref(),
|
||||
Some("rsx" | "render")
|
||||
) {
|
||||
rsx_calls.push(ChangedRsx {
|
||||
old: old_mac.clone(),
|
||||
new: new_mac.tokens.clone(),
|
||||
});
|
||||
false
|
||||
} else {
|
||||
new_mac != old_mac
|
||||
}
|
||||
let old = syn::parse_file(old).unwrap();
|
||||
let new = syn::parse_file(new).unwrap();
|
||||
let same = syn::parse_file(same).unwrap();
|
||||
|
||||
assert!(
|
||||
diff_rsx(&old, &new).is_none(),
|
||||
"Files with different expressions should not be hotreloadable"
|
||||
);
|
||||
|
||||
assert!(
|
||||
diff_rsx(&new, &new).is_some(),
|
||||
"The same file should be reloadable with itself"
|
||||
);
|
||||
|
||||
assert!(
|
||||
diff_rsx(&old, &same).is_some(),
|
||||
"Files with changed comments should be hotreloadable"
|
||||
);
|
||||
}
|
||||
@@ -1,399 +0,0 @@
|
||||
use super::{
|
||||
hot_reload_diff::{diff_rsx, DiffResult},
|
||||
ChangedRsx,
|
||||
};
|
||||
use crate::{CallBody, HotReloadingContext};
|
||||
use dioxus_core::{
|
||||
prelude::{TemplateAttribute, TemplateNode},
|
||||
Template,
|
||||
};
|
||||
use krates::cm::MetadataCommand;
|
||||
use krates::Cmd;
|
||||
pub use proc_macro2::TokenStream;
|
||||
pub use std::collections::HashMap;
|
||||
pub use std::sync::Mutex;
|
||||
pub use std::time::SystemTime;
|
||||
use std::{collections::HashSet, ffi::OsStr, marker::PhantomData, path::PathBuf};
|
||||
pub use std::{fs, io, path::Path};
|
||||
pub use std::{fs::File, io::Read};
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
pub enum UpdateResult {
|
||||
UpdatedRsx(Vec<Template>),
|
||||
|
||||
NeedsRebuild,
|
||||
}
|
||||
|
||||
/// The result of building a FileMap
|
||||
pub struct FileMapBuildResult<Ctx: HotReloadingContext> {
|
||||
/// The FileMap that was built
|
||||
pub map: FileMap<Ctx>,
|
||||
|
||||
/// Any errors that occurred while building the FileMap that were not fatal
|
||||
pub errors: Vec<io::Error>,
|
||||
}
|
||||
|
||||
pub struct FileMap<Ctx: HotReloadingContext> {
|
||||
pub map: HashMap<PathBuf, CachedSynFile>,
|
||||
|
||||
in_workspace: HashMap<PathBuf, Option<PathBuf>>,
|
||||
|
||||
phantom: PhantomData<Ctx>,
|
||||
}
|
||||
|
||||
/// A cached file that has been parsed
|
||||
///
|
||||
/// We store the templates found in this file
|
||||
pub struct CachedSynFile {
|
||||
pub raw: String,
|
||||
pub path: PathBuf,
|
||||
pub templates: HashMap<&'static str, Template>,
|
||||
pub tracked_assets: HashSet<PathBuf>,
|
||||
}
|
||||
|
||||
impl<Ctx: HotReloadingContext> FileMap<Ctx> {
|
||||
/// Create a new FileMap from a crate directory
|
||||
///
|
||||
/// TODO: this should be created with a gitignore filter
|
||||
pub fn create(path: PathBuf) -> io::Result<FileMapBuildResult<Ctx>> {
|
||||
Self::create_with_filter(path, |p| {
|
||||
// skip some stuff we know is large by default
|
||||
p.file_name() == Some(OsStr::new("target"))
|
||||
|| p.file_name() == Some(OsStr::new("node_modules"))
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a new FileMap from a crate directory
|
||||
pub fn create_with_filter(
|
||||
crate_dir: PathBuf,
|
||||
mut filter: impl FnMut(&Path) -> bool,
|
||||
) -> io::Result<FileMapBuildResult<Ctx>> {
|
||||
let FileMapSearchResult { map, errors } = find_rs_files(crate_dir.clone(), &mut filter);
|
||||
|
||||
let mut map = Self {
|
||||
map,
|
||||
in_workspace: HashMap::new(),
|
||||
phantom: PhantomData,
|
||||
};
|
||||
|
||||
map.load_assets(crate_dir.as_path());
|
||||
|
||||
Ok(FileMapBuildResult { errors, map })
|
||||
}
|
||||
|
||||
/// Start watching assets for changes
|
||||
///
|
||||
/// This just diffs every file against itself and populates the tracked assets as it goes
|
||||
pub fn load_assets(&mut self, crate_dir: &Path) {
|
||||
let keys = self.map.keys().cloned().collect::<Vec<_>>();
|
||||
for file in keys {
|
||||
_ = self.update_rsx(file.as_path(), crate_dir);
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to update the rsx in a file
|
||||
pub fn update_rsx(
|
||||
&mut self,
|
||||
file_path: &Path,
|
||||
crate_dir: &Path,
|
||||
) -> Result<UpdateResult, HotreloadError> {
|
||||
let mut file = File::open(file_path)?;
|
||||
let mut src = String::new();
|
||||
file.read_to_string(&mut src)?;
|
||||
|
||||
// If we can't parse the contents we want to pass it off to the build system to tell the user that there's a syntax error
|
||||
let syntax = syn::parse_file(&src).map_err(|_err| HotreloadError::Parse)?;
|
||||
|
||||
let in_workspace = self.child_in_workspace(crate_dir)?;
|
||||
|
||||
// Get the cached file if it exists, otherwise try to create it
|
||||
let Some(old_cached) = self.map.get_mut(file_path) else {
|
||||
// if this is a new file, rebuild the project
|
||||
let FileMapBuildResult { map, mut errors } =
|
||||
FileMap::<Ctx>::create(crate_dir.to_path_buf())?;
|
||||
|
||||
if let Some(err) = errors.pop() {
|
||||
return Err(HotreloadError::Failure(err));
|
||||
}
|
||||
|
||||
// merge the new map into the old map
|
||||
self.map.extend(map.map);
|
||||
|
||||
return Ok(UpdateResult::NeedsRebuild);
|
||||
};
|
||||
|
||||
// If the cached file is not a valid rsx file, rebuild the project, forcing errors
|
||||
// TODO: in theory the error is simply in the RsxCallbody. We could attempt to parse it using partial expansion
|
||||
// And collect out its errors instead of giving up to a full rebuild
|
||||
let old = syn::parse_file(&old_cached.raw).map_err(|_e| HotreloadError::Parse)?;
|
||||
|
||||
let instances = match diff_rsx(&syntax, &old) {
|
||||
// If the changes were just some rsx, we can just update the template
|
||||
//
|
||||
// However... if the changes involved code in the rsx itself, this should actually be a CodeChanged
|
||||
DiffResult::RsxChanged {
|
||||
rsx_calls: instances,
|
||||
} => instances,
|
||||
|
||||
// If the changes were some code, we should insert the file into the map and rebuild
|
||||
// todo: not sure we even need to put the cached file into the map, but whatever
|
||||
DiffResult::CodeChanged(_) => {
|
||||
let cached_file = CachedSynFile {
|
||||
raw: src.clone(),
|
||||
path: file_path.to_path_buf(),
|
||||
templates: HashMap::new(),
|
||||
tracked_assets: HashSet::new(),
|
||||
};
|
||||
|
||||
self.map.insert(file_path.to_path_buf(), cached_file);
|
||||
return Ok(UpdateResult::NeedsRebuild);
|
||||
}
|
||||
};
|
||||
|
||||
let mut messages: Vec<Template> = Vec::new();
|
||||
|
||||
for calls in instances.into_iter() {
|
||||
let ChangedRsx { old, new } = calls;
|
||||
|
||||
let old_start = old.span().start();
|
||||
|
||||
let old_parsed = syn::parse2::<CallBody>(old.tokens);
|
||||
let new_parsed = syn::parse2::<CallBody>(new);
|
||||
let (Ok(old_call_body), Ok(new_call_body)) = (old_parsed, new_parsed) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// if the file!() macro is invoked in a workspace, the path is relative to the workspace root, otherwise it's relative to the crate root
|
||||
// we need to check if the file is in a workspace or not and strip the prefix accordingly
|
||||
let prefix = match in_workspace {
|
||||
Some(ref workspace) => workspace,
|
||||
_ => crate_dir,
|
||||
};
|
||||
|
||||
let Ok(file) = file_path.strip_prefix(prefix) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// We leak the template since templates are a compiletime value
|
||||
// This is not ideal, but also not a huge deal for hot reloading
|
||||
// TODO: we could consider arena allocating the templates and dropping them when the connection is closed
|
||||
let leaked_location = Box::leak(template_location(old_start, file).into_boxed_str());
|
||||
|
||||
// Retuns Some(template) if the template is hotreloadable
|
||||
// dynamic changes are not hot reloadable and force a rebuild
|
||||
let hotreloadable_template =
|
||||
new_call_body.update_template::<Ctx>(Some(old_call_body), leaked_location);
|
||||
|
||||
// if the template is not hotreloadable, we need to do a full rebuild
|
||||
let Some(template) = hotreloadable_template else {
|
||||
return Ok(UpdateResult::NeedsRebuild);
|
||||
};
|
||||
|
||||
// dioxus cannot handle empty templates...
|
||||
// todo: I think it can? or we just skip them nowa
|
||||
if template.roots.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// if the template is the same, don't send it
|
||||
if let Some(old_template) = old_cached.templates.get(template.name) {
|
||||
if old_template == &template {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// update the cached file
|
||||
old_cached.templates.insert(template.name, template);
|
||||
|
||||
// Track any new assets
|
||||
old_cached
|
||||
.tracked_assets
|
||||
.extend(Self::populate_assets(template));
|
||||
|
||||
messages.push(template);
|
||||
}
|
||||
|
||||
Ok(UpdateResult::UpdatedRsx(messages))
|
||||
}
|
||||
|
||||
fn populate_assets(template: Template) -> HashSet<PathBuf> {
|
||||
fn collect_assetlike_attrs(node: &TemplateNode, asset_urls: &mut HashSet<PathBuf>) {
|
||||
if let TemplateNode::Element {
|
||||
attrs, children, ..
|
||||
} = node
|
||||
{
|
||||
for attr in attrs.iter() {
|
||||
if let TemplateAttribute::Static { name, value, .. } = attr {
|
||||
if *name == "src" || *name == "href" {
|
||||
asset_urls.insert(PathBuf::from(*value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for child in children.iter() {
|
||||
collect_assetlike_attrs(child, asset_urls);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut asset_urls = HashSet::new();
|
||||
|
||||
for node in template.roots {
|
||||
collect_assetlike_attrs(node, &mut asset_urls);
|
||||
}
|
||||
|
||||
asset_urls
|
||||
}
|
||||
|
||||
/// add the template to an existing file in the filemap if it exists
|
||||
/// create a new file if it doesn't exist
|
||||
pub fn insert(&mut self, path: PathBuf, template: Template) {
|
||||
let tracked_assets = Self::populate_assets(template);
|
||||
|
||||
if self.map.contains_key(&path) {
|
||||
let entry = self.map.get_mut(&path).unwrap();
|
||||
entry.tracked_assets.extend(tracked_assets);
|
||||
entry.templates.insert(template.name, template);
|
||||
} else {
|
||||
self.map.insert(
|
||||
path.clone(),
|
||||
CachedSynFile {
|
||||
raw: String::new(),
|
||||
path,
|
||||
tracked_assets,
|
||||
templates: HashMap::from([(template.name, template)]),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tracked_assets(&self) -> HashSet<PathBuf> {
|
||||
self.map
|
||||
.values()
|
||||
.flat_map(|file| file.tracked_assets.iter().cloned())
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn is_tracking_asset(&self, path: &PathBuf) -> Option<&CachedSynFile> {
|
||||
self.map
|
||||
.values()
|
||||
.find(|file| file.tracked_assets.contains(path))
|
||||
}
|
||||
|
||||
fn child_in_workspace(&mut self, crate_dir: &Path) -> io::Result<Option<PathBuf>> {
|
||||
if let Some(in_workspace) = self.in_workspace.get(crate_dir) {
|
||||
return Ok(in_workspace.clone());
|
||||
}
|
||||
|
||||
let mut cmd = Cmd::new();
|
||||
let manafest_path = crate_dir.join("Cargo.toml");
|
||||
cmd.manifest_path(&manafest_path);
|
||||
let cmd: MetadataCommand = cmd.into();
|
||||
let metadata = cmd
|
||||
.exec()
|
||||
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
|
||||
|
||||
let in_workspace = metadata.workspace_root != crate_dir;
|
||||
let workspace_path = in_workspace.then(|| metadata.workspace_root.into());
|
||||
self.in_workspace
|
||||
.insert(crate_dir.to_path_buf(), workspace_path.clone());
|
||||
Ok(workspace_path)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn template_location(old_start: proc_macro2::LineColumn, file: &Path) -> String {
|
||||
let line = old_start.line;
|
||||
let column = old_start.column + 1;
|
||||
|
||||
// Always ensure the path components are separated by `/`.
|
||||
let path = file
|
||||
.components()
|
||||
.map(|c| c.as_os_str().to_string_lossy())
|
||||
.collect::<Vec<_>>()
|
||||
.join("/");
|
||||
|
||||
path
|
||||
+ ":"
|
||||
+ &line.to_string()
|
||||
+ ":"
|
||||
+ &column.to_string()
|
||||
// the byte index doesn't matter, but dioxus needs it
|
||||
+ ":0"
|
||||
}
|
||||
|
||||
struct FileMapSearchResult {
|
||||
map: HashMap<PathBuf, CachedSynFile>,
|
||||
errors: Vec<io::Error>,
|
||||
}
|
||||
|
||||
// todo: we could just steal the mod logic from rustc itself
|
||||
fn find_rs_files(root: PathBuf, filter: &mut impl FnMut(&Path) -> bool) -> FileMapSearchResult {
|
||||
let mut files = HashMap::new();
|
||||
let mut errors = Vec::new();
|
||||
|
||||
if root.is_dir() {
|
||||
let read_dir = match fs::read_dir(root) {
|
||||
Ok(read_dir) => read_dir,
|
||||
Err(err) => {
|
||||
errors.push(err);
|
||||
return FileMapSearchResult { map: files, errors };
|
||||
}
|
||||
};
|
||||
for entry in read_dir.flatten() {
|
||||
let path = entry.path();
|
||||
if !filter(&path) {
|
||||
let FileMapSearchResult {
|
||||
map,
|
||||
errors: child_errors,
|
||||
} = find_rs_files(path, filter);
|
||||
errors.extend(child_errors);
|
||||
files.extend(map);
|
||||
}
|
||||
}
|
||||
} else if root.extension().and_then(|s| s.to_str()) == Some("rs") {
|
||||
if let Ok(mut file) = File::open(root.clone()) {
|
||||
let mut src = String::new();
|
||||
match file.read_to_string(&mut src) {
|
||||
Ok(_) => {
|
||||
let cached_file = CachedSynFile {
|
||||
raw: src.clone(),
|
||||
path: root.clone(),
|
||||
templates: HashMap::new(),
|
||||
tracked_assets: HashSet::new(),
|
||||
};
|
||||
|
||||
// track assets while we're here
|
||||
files.insert(root, cached_file);
|
||||
}
|
||||
Err(err) => {
|
||||
errors.push(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FileMapSearchResult { map: files, errors }
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum HotreloadError {
|
||||
Failure(io::Error),
|
||||
Parse,
|
||||
NoPreviousBuild,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for HotreloadError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Failure(err) => write!(f, "Failed to parse file: {}", err),
|
||||
Self::Parse => write!(f, "Failed to parse file"),
|
||||
Self::NoPreviousBuild => write!(f, "No previous build found"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for HotreloadError {
|
||||
fn from(err: io::Error) -> Self {
|
||||
HotreloadError::Failure(err)
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,3 @@ pub use hot_reload_diff::*;
|
||||
mod hot_reloading_context;
|
||||
#[cfg(feature = "hot_reload_traits")]
|
||||
pub use hot_reloading_context::*;
|
||||
|
||||
#[cfg(feature = "hot_reload")]
|
||||
mod hot_reloading_file_map;
|
||||
#[cfg(feature = "hot_reload")]
|
||||
pub use hot_reloading_file_map::*;
|
||||
Reference in new issue
Block a user