refactor(cli-config): modified CrateConfig struct

`out_dir` and `asset_dir` are now methods, because they derive from
`crate_dir` and `dioxus_config`.
This commit is contained in:
Andrew Voynov 2024-01-30 21:03:44 +03:00
parent fa933e35ab
commit 48957b11d8
No known key found for this signature in database
GPG key ID: 1BE92DD685700329
7 changed files with 49 additions and 42 deletions

View file

@ -338,11 +338,9 @@ pub struct WebHttpsConfig {
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrateConfig { pub struct CrateConfig {
pub out_dir: PathBuf,
pub crate_dir: PathBuf, pub crate_dir: PathBuf,
pub workspace_dir: PathBuf, pub workspace_dir: PathBuf,
pub target_dir: PathBuf, pub target_dir: PathBuf,
pub asset_dir: PathBuf,
#[cfg(feature = "cli")] #[cfg(feature = "cli")]
pub manifest: cargo_toml::Manifest<cargo_toml::Value>, pub manifest: cargo_toml::Manifest<cargo_toml::Value>,
pub executable: ExecutableType, pub executable: ExecutableType,
@ -383,12 +381,8 @@ impl CrateConfig {
let workspace_dir = meta.workspace_root; let workspace_dir = meta.workspace_root;
let target_dir = meta.target_directory; let target_dir = meta.target_directory;
let out_dir = crate_dir.join(&dioxus_config.application.out_dir);
let cargo_def = &crate_dir.join("Cargo.toml"); let cargo_def = &crate_dir.join("Cargo.toml");
let asset_dir = crate_dir.join(&dioxus_config.application.asset_dir);
let manifest = cargo_toml::Manifest::from_path(cargo_def).unwrap(); let manifest = cargo_toml::Manifest::from_path(cargo_def).unwrap();
let mut output_filename = String::from("dioxus_app"); let mut output_filename = String::from("dioxus_app");
@ -414,6 +408,7 @@ impl CrateConfig {
let release = false; let release = false;
let hot_reload = false; let hot_reload = false;
let cross_origin_policy = false;
let verbose = false; let verbose = false;
let custom_profile = None; let custom_profile = None;
let features = None; let features = None;
@ -421,26 +416,38 @@ impl CrateConfig {
let cargo_args = vec![]; let cargo_args = vec![];
Ok(Self { Ok(Self {
out_dir,
crate_dir, crate_dir,
workspace_dir, workspace_dir,
target_dir, target_dir,
asset_dir,
#[cfg(feature = "cli")] #[cfg(feature = "cli")]
manifest, manifest,
executable, executable,
release,
dioxus_config, dioxus_config,
release,
hot_reload, hot_reload,
cross_origin_policy: false, cross_origin_policy,
verbose,
custom_profile, custom_profile,
features, features,
verbose,
target, target,
cargo_args, cargo_args,
}) })
} }
/// Compose an asset directory. Represents the typical "public" directory
/// with publicly available resources (configurable in the `Dioxus.toml`).
pub fn asset_dir(&self) -> PathBuf {
self.crate_dir
.join(&self.dioxus_config.application.asset_dir)
}
/// Compose an out directory. Represents the typical "dist" directory that
/// is "distributed" after building an application (configurable in the
/// `Dioxus.toml`).
pub fn out_dir(&self) -> PathBuf {
self.crate_dir.join(&self.dioxus_config.application.out_dir)
}
pub fn as_example(&mut self, example_name: String) -> &mut Self { pub fn as_example(&mut self, example_name: String) -> &mut Self {
self.executable = ExecutableType::Example(example_name); self.executable = ExecutableType::Example(example_name);
self self

View file

@ -13,7 +13,7 @@ pub fn asset_manifest(crate_config: &CrateConfig) -> AssetManifest {
/// Create a head file that contains all of the imports for assets that the user project uses /// Create a head file that contains all of the imports for assets that the user project uses
pub fn create_assets_head(config: &CrateConfig, manifest: &AssetManifest) -> Result<()> { pub fn create_assets_head(config: &CrateConfig, manifest: &AssetManifest) -> Result<()> {
let mut file = File::create(config.out_dir.join("__assets_head.html"))?; let mut file = File::create(config.out_dir().join("__assets_head.html"))?;
file.write_all(manifest.head().as_bytes())?; file.write_all(manifest.head().as_bytes())?;
Ok(()) Ok(())
} }
@ -29,7 +29,7 @@ pub(crate) fn process_assets(config: &CrateConfig, manifest: &AssetManifest) ->
.clone() .clone()
.unwrap_or_default(), .unwrap_or_default(),
); );
let static_asset_output_dir = config.out_dir.join(static_asset_output_dir); let static_asset_output_dir = config.out_dir().join(static_asset_output_dir);
manifest.copy_static_assets_to(static_asset_output_dir)?; manifest.copy_static_assets_to(static_asset_output_dir)?;

View file

@ -39,14 +39,14 @@ pub fn build(config: &CrateConfig, _: bool, skip_assets: bool) -> Result<BuildRe
// [6] Link up the html page to the wasm module // [6] Link up the html page to the wasm module
let CrateConfig { let CrateConfig {
out_dir,
crate_dir, crate_dir,
target_dir, target_dir,
asset_dir,
executable, executable,
dioxus_config, dioxus_config,
.. ..
} = config; } = config;
let out_dir = config.out_dir();
let asset_dir = config.asset_dir();
let _guard = WebAssetConfigDropGuard::new(); let _guard = WebAssetConfigDropGuard::new();
let _manganis_support = ManganisSupportGuard::default(); let _manganis_support = ManganisSupportGuard::default();
@ -251,15 +251,15 @@ pub fn build(config: &CrateConfig, _: bool, skip_assets: bool) -> Result<BuildRe
if path.is_file() { if path.is_file() {
std::fs::copy(&path, out_dir.join(path.file_name().unwrap()))?; std::fs::copy(&path, out_dir.join(path.file_name().unwrap()))?;
} else { } else {
match fs_extra::dir::copy(&path, out_dir, &copy_options) { match fs_extra::dir::copy(&path, &out_dir, &copy_options) {
Ok(_) => {} Ok(_) => {}
Err(_e) => { Err(_e) => {
log::warn!("Error copying dir: {}", _e); log::warn!("Error copying dir: {}", _e);
} }
} }
for ignore in &ignore_files { for ignore in &ignore_files {
let ignore = ignore.strip_prefix(&config.asset_dir).unwrap(); let ignore = ignore.strip_prefix(&config.asset_dir()).unwrap();
let ignore = config.out_dir.join(ignore); let ignore = out_dir.join(ignore);
if ignore.is_file() { if ignore.is_file() {
std::fs::remove_file(ignore)?; std::fs::remove_file(ignore)?;
} }
@ -369,13 +369,13 @@ pub fn build_desktop(
file_name file_name
}; };
if !config.out_dir.is_dir() { if !config.out_dir().is_dir() {
create_dir_all(&config.out_dir)?; create_dir_all(config.out_dir())?;
} }
copy(res_path, config.out_dir.join(target_file))?; copy(res_path, config.out_dir().join(target_file))?;
// this code will copy all public file to the output dir // this code will copy all public file to the output dir
if config.asset_dir.is_dir() { if config.asset_dir().is_dir() {
let copy_options = fs_extra::dir::CopyOptions { let copy_options = fs_extra::dir::CopyOptions {
overwrite: true, overwrite: true,
skip_exist: false, skip_exist: false,
@ -385,20 +385,20 @@ pub fn build_desktop(
depth: 0, depth: 0,
}; };
for entry in std::fs::read_dir(&config.asset_dir)? { for entry in std::fs::read_dir(config.asset_dir())? {
let path = entry?.path(); let path = entry?.path();
if path.is_file() { if path.is_file() {
std::fs::copy(&path, &config.out_dir.join(path.file_name().unwrap()))?; std::fs::copy(&path, &config.out_dir().join(path.file_name().unwrap()))?;
} else { } else {
match fs_extra::dir::copy(&path, &config.out_dir, &copy_options) { match fs_extra::dir::copy(&path, &config.out_dir(), &copy_options) {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
log::warn!("Error copying dir: {}", e); log::warn!("Error copying dir: {}", e);
} }
} }
for ignore in &ignore_files { for ignore in &ignore_files {
let ignore = ignore.strip_prefix(&config.asset_dir).unwrap(); let ignore = ignore.strip_prefix(&config.asset_dir()).unwrap();
let ignore = config.out_dir.join(ignore); let ignore = config.out_dir().join(ignore);
if ignore.is_file() { if ignore.is_file() {
std::fs::remove_file(ignore)?; std::fs::remove_file(ignore)?;
} }
@ -635,7 +635,7 @@ fn build_assets(config: &CrateConfig) -> Result<Vec<PathBuf>> {
if file == "*" { if file == "*" {
// if the sass open auto, we need auto-check the assets dir. // if the sass open auto, we need auto-check the assets dir.
let asset_dir = config.asset_dir.clone(); let asset_dir = config.asset_dir().clone();
if asset_dir.is_dir() { if asset_dir.is_dir() {
for entry in walkdir::WalkDir::new(&asset_dir) for entry in walkdir::WalkDir::new(&asset_dir)
.into_iter() .into_iter()
@ -655,7 +655,7 @@ fn build_assets(config: &CrateConfig) -> Result<Vec<PathBuf>> {
temp.file_stem().unwrap().to_str().unwrap() temp.file_stem().unwrap().to_str().unwrap()
); );
let target_path = config let target_path = config
.out_dir .out_dir()
.join( .join(
temp.strip_prefix(&asset_dir) temp.strip_prefix(&asset_dir)
.unwrap() .unwrap()
@ -685,11 +685,11 @@ fn build_assets(config: &CrateConfig) -> Result<Vec<PathBuf>> {
} else { } else {
file file
}; };
let path = config.asset_dir.join(relative_path); let path = config.asset_dir().join(relative_path);
let out_file = let out_file =
format!("{}.css", path.file_stem().unwrap().to_str().unwrap()); format!("{}.css", path.file_stem().unwrap().to_str().unwrap());
let target_path = config let target_path = config
.out_dir .out_dir()
.join(PathBuf::from(relative_path).parent().unwrap()) .join(PathBuf::from(relative_path).parent().unwrap())
.join(out_file); .join(out_file);
if path.is_file() { if path.is_file() {
@ -719,11 +719,11 @@ fn build_assets(config: &CrateConfig) -> Result<Vec<PathBuf>> {
} else { } else {
path path
}; };
let path = config.asset_dir.join(relative_path); let path = config.asset_dir().join(relative_path);
let out_file = let out_file =
format!("{}.css", path.file_stem().unwrap().to_str().unwrap()); format!("{}.css", path.file_stem().unwrap().to_str().unwrap());
let target_path = config let target_path = config
.out_dir .out_dir()
.join(PathBuf::from(relative_path).parent().unwrap()) .join(PathBuf::from(relative_path).parent().unwrap())
.join(out_file); .join(out_file);
if path.is_file() { if path.is_file() {

View file

@ -87,7 +87,7 @@ impl Bundle {
build_desktop(&crate_config, false, false)?; build_desktop(&crate_config, false, false)?;
// copy the binary to the out dir // copy the binary to the out dir
let package = crate_config.manifest.package.unwrap(); let package = crate_config.manifest.package.as_ref().unwrap();
let mut name: PathBuf = match &crate_config.executable { let mut name: PathBuf = match &crate_config.executable {
ExecutableType::Binary(name) ExecutableType::Binary(name)
@ -149,7 +149,7 @@ impl Bundle {
} }
let mut settings = SettingsBuilder::new() let mut settings = SettingsBuilder::new()
.project_out_directory(crate_config.out_dir) .project_out_directory(crate_config.out_dir())
.package_settings(PackageSettings { .package_settings(PackageSettings {
product_name: crate_config.dioxus_config.application.name.clone(), product_name: crate_config.dioxus_config.application.name.clone(),
version: package.version().to_string(), version: package.version().to_string(),

View file

@ -184,8 +184,8 @@ impl PluginManager {
let args = lua.create_table()?; let args = lua.create_table()?;
args.set("name", crate_config.dioxus_config.application.name.clone())?; args.set("name", crate_config.dioxus_config.application.name.clone())?;
args.set("platform", platform)?; args.set("platform", platform)?;
args.set("out_dir", crate_config.out_dir.to_str().unwrap())?; args.set("out_dir", crate_config.out_dir().to_str().unwrap())?;
args.set("asset_dir", crate_config.asset_dir.to_str().unwrap())?; args.set("asset_dir", crate_config.asset_dir().to_str().unwrap())?;
for i in 1..(manager.len()? as i32 + 1) { for i in 1..(manager.len()? as i32 + 1) {
let info = manager.get::<i32, PluginInfo>(i)?; let info = manager.get::<i32, PluginInfo>(i)?;
@ -208,8 +208,8 @@ impl PluginManager {
let args = lua.create_table()?; let args = lua.create_table()?;
args.set("name", crate_config.dioxus_config.application.name.clone())?; args.set("name", crate_config.dioxus_config.application.name.clone())?;
args.set("platform", platform)?; args.set("platform", platform)?;
args.set("out_dir", crate_config.out_dir.to_str().unwrap())?; args.set("out_dir", crate_config.out_dir().to_str().unwrap())?;
args.set("asset_dir", crate_config.asset_dir.to_str().unwrap())?; args.set("asset_dir", crate_config.asset_dir().to_str().unwrap())?;
for i in 1..(manager.len()? as i32 + 1) { for i in 1..(manager.len()? as i32 + 1) {
let info = manager.get::<i32, PluginInfo>(i)?; let info = manager.get::<i32, PluginInfo>(i)?;

View file

@ -221,7 +221,7 @@ fn start_desktop(config: &CrateConfig, skip_assets: bool) -> Result<(RAIIChild,
ExecutableType::Binary(name) ExecutableType::Binary(name)
| ExecutableType::Lib(name) | ExecutableType::Lib(name)
| ExecutableType::Example(name) => { | ExecutableType::Example(name) => {
let mut file = config.out_dir.join(name); let mut file = config.out_dir().join(name);
if cfg!(windows) { if cfg!(windows) {
file.set_extension("exe"); file.set_extension("exe");
} }

View file

@ -290,7 +290,7 @@ async fn setup_router(
std::fs::read_to_string( std::fs::read_to_string(
file_service_config file_service_config
.crate_dir .crate_dir
.join(file_service_config.out_dir) .join(file_service_config.out_dir())
.join("index.html"), .join("index.html"),
) )
.ok() .ok()
@ -315,7 +315,7 @@ async fn setup_router(
Ok(response) Ok(response)
}, },
) )
.service(ServeDir::new(config.crate_dir.join(&config.out_dir))); .service(ServeDir::new(config.crate_dir.join(config.out_dir())));
// Setup websocket // Setup websocket
let mut router = Router::new().route("/_dioxus/ws", get(ws_handler)); let mut router = Router::new().route("/_dioxus/ws", get(ws_handler));