Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Improve metassr-utils #11

Merged
merged 10 commits into from
Sep 21, 2024
25 changes: 19 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ edition = "2021"
authors = ["Mohamed Emad ([email protected])"]
description = "Another SSR framework but built with MetaCall!"


[dependencies]
anyhow = "1.0.82"
chrono = "0.4.38"
Expand All @@ -32,6 +31,7 @@ tower-layer = "0.3.3"
tower-service = "0.3.3"
metassr-create = { path = "crates/metassr-create" }
metassr-bundler = { path = "crates/metassr-bundler" }
metassr-fs-analyzer = { path = "crates/metassr-fs-analyzer" }

[workspace]
members = [
Expand All @@ -41,7 +41,9 @@ members = [
"crates/metassr-utils",
"crates/html-generator",
"metassr-cli",
"crates/metassr-create", "crates/metassr-bundler",
"crates/metassr-create",
"crates/metassr-bundler",
"crates/metassr-fs-analyzer",
]

[[bin]]
Expand Down
1 change: 1 addition & 0 deletions crates/metassr-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ html-generator = { path = "../html-generator" }
lazy_static = "1.5.0"
serde = { version = "1.0.207", features = ["derive"] }
metassr-bundler = { path = "../metassr-bundler" }
metassr-fs-analyzer = { path = "../metassr-fs-analyzer" }
9 changes: 6 additions & 3 deletions crates/metassr-build/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ use anyhow::{anyhow, Result};
use hydrator::Hydrator;

use metassr_bundler::WebBundler;
use metassr_utils::{
cache_dir::CacheDir, src_analyzer::special_entries, src_analyzer::SourceDir, traits::AnalyzeDir,
use metassr_fs_analyzer::{
src_dir::{special_entries, SourceDir},
DirectoryAnalyzer,
};
use metassr_utils::cache_dir::CacheDir;

use std::{
collections::HashMap,
ffi::OsStr,
Expand Down Expand Up @@ -34,7 +37,7 @@ impl ClientBuilder {
return Err(anyhow!("src directory not found."));
}
if !dist_path.exists() {
fs::create_dir(dist_path.clone())?;
fs::create_dir(&dist_path)?;
}
Ok(Self {
src_path,
Expand Down
15 changes: 8 additions & 7 deletions crates/metassr-build/src/server/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::{anyhow, Result};
use metassr_utils::{
cache_dir::CacheDir,
dist_analyzer::{DistDirContainer, PageEntry},
};

use metassr_fs_analyzer::dist_dir::{DistDirContainer, PageEntry};
use metassr_utils::cache_dir::CacheDir;

use serde::{Deserialize, Serialize};
use serde_json::to_string_pretty;
use std::{
Expand Down Expand Up @@ -119,12 +119,13 @@ impl ManifestGenerator {
}
}
pub fn generate<H: AsRef<OsStr> + ?Sized>(&self, head: &H) -> Result<Manifest> {
let global = GlobalEntry::new(head, &self.cache.dir_path())?;
let cache_path = self.cache.path();
let global = GlobalEntry::new(head, cache_path)?;
let mut manifest = Manifest::new(global);

for (path, &id) in self.targets.iter() {
let route = match path
.strip_prefix(self.cache.dir_path().join("pages"))?
.strip_prefix(cache_path.join("pages"))?
.parent()
.unwrap()
{
Expand Down
11 changes: 6 additions & 5 deletions crates/metassr-build/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ use crate::traits::Build;
use manifest::ManifestGenerator;

use metassr_bundler::WebBundler;
use metassr_utils::{
cache_dir::CacheDir,
dist_analyzer::DistDir,
src_analyzer::{special_entries, SourceDir},
traits::AnalyzeDir,
use metassr_fs_analyzer::{
dist_dir::DistDir,
src_dir::{special_entries, SourceDir},
DirectoryAnalyzer,
};
use metassr_utils::cache_dir::CacheDir;

use pages_generator::PagesGenerator;
use renderer::head::HeadRenderer;

Expand Down
10 changes: 5 additions & 5 deletions crates/metassr-build/src/server/pages_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use std::{
};

use anyhow::{anyhow, Result};
use metassr_utils::{
cache_dir::CacheDir,
dist_analyzer::{DistDir, DistDirContainer},
traits::AnalyzeDir,
use metassr_fs_analyzer::{
dist_dir::{DistDir, DistDirContainer},
DirectoryAnalyzer,
};
use metassr_utils::cache_dir::CacheDir;

use crate::traits::Exec;

Expand All @@ -34,7 +34,7 @@ impl PagesGenerator {
) -> Result<Self> {
let dist = DistDir::new(dist_path)?.analyze()?;
let head = HeadRenderer::new(&head_path, cache_dir.clone()).render(true)?;
let cache = cache_dir.dir_path();
let cache = cache_dir.path().to_path_buf();

let output = MultiRenderExec::new(targets.ready_for_exec())?.exec()?;

Expand Down
4 changes: 2 additions & 2 deletions crates/metassr-build/src/server/renderer/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl HeadRenderer {

let _ = loaders::from_single_file(
"node",
format!("{}/head.js", self.cache_dir.dir_path().display()),
format!("{}/head.js", self.cache_dir.path().display()),
);
guard.make_true()
}
Expand All @@ -48,7 +48,7 @@ impl HeadRenderer {
}

fn bundle(&mut self) -> Result<()> {
if let Err(e) = WebBundler::new(&self.bundling_target()?, &self.cache_dir.dir_path()).exec()
if let Err(e) = WebBundler::new(&self.bundling_target()?, &self.cache_dir.path()).exec()
{
return Err(anyhow!("Cannot bundling head: {e}"));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/metassr-build/src/server/renderer/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use html_generator::{
html_props::HtmlProps,
template::HtmlTemplate,
};
use metassr_utils::dist_analyzer::PageEntry;
use metassr_fs_analyzer::dist_dir::PageEntry;

pub struct HtmlRenderer<'a> {
head: String,
Expand Down
4 changes: 3 additions & 1 deletion crates/metassr-build/src/server/renderer/page.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::ffi::OsStr;

use anyhow::Result;
use metassr_utils::{cache_dir::CacheDir, dist_analyzer::PageEntry};

use metassr_fs_analyzer::dist_dir::PageEntry;
use metassr_utils::cache_dir::CacheDir;

use crate::{
server::{manifest::Manifest, render_exec::RenderExec},
Expand Down
4 changes: 3 additions & 1 deletion crates/metassr-build/src/server/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::{
};

use anyhow::Result;
use metassr_utils::{cache_dir::CacheDir, src_analyzer::PagesEntriesType};

use metassr_fs_analyzer::src_dir::PagesEntriesType;
use metassr_utils::cache_dir::CacheDir;

use crate::{traits::Generate, utils::setup_page_path};

Expand Down
4 changes: 2 additions & 2 deletions crates/metassr-bundler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tracing::error;

lazy_static! {
/// A detector for if the bundling script `./bundle.js` is loaded or not. It is used to solve multiple loading script error in metacall.
static ref IS_BUNDLING_SCRIPT_LOADED: Mutex<CheckerState> = Mutex::new(CheckerState::new());
static ref IS_BUNDLING_SCRIPT_LOADED: Mutex<CheckerState> = Mutex::new(CheckerState::default());

/// A simple checker to check if the bundling function is done or not. It is used to block the program until bundling done.
static ref IS_COMPLIATION_WAIT: Arc<CompilationWait> = Arc::new(CompilationWait::default());
Expand All @@ -30,7 +30,7 @@ struct CompilationWait {
impl Default for CompilationWait {
fn default() -> Self {
Self {
checker: Mutex::new(CheckerState::with(false)),
checker: Mutex::new(CheckerState::default()),
cond: Condvar::new(),
}
}
Expand Down
13 changes: 13 additions & 0 deletions crates/metassr-fs-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "metassr-fs-analyzer"
description = "This module provides directory analyzers for MetaSSR web applications."
version = "0.0.1-alpha"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.89"
metassr-utils = { version = "0.0.1-alpha", path = "../metassr-utils" }
serde = "1.0.210"
walkdir = "2.5.0"
Loading
Loading