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)]
pub struct CrateConfig {
pub out_dir: PathBuf,
pub crate_dir: PathBuf,
pub workspace_dir: PathBuf,
pub target_dir: PathBuf,
pub asset_dir: PathBuf,
#[cfg(feature = "cli")]
pub manifest: cargo_toml::Manifest<cargo_toml::Value>,
pub executable: ExecutableType,
@ -383,12 +381,8 @@ impl CrateConfig {
let workspace_dir = meta.workspace_root;
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 asset_dir = crate_dir.join(&dioxus_config.application.asset_dir);
let manifest = cargo_toml::Manifest::from_path(cargo_def).unwrap();
let mut output_filename = String::from("dioxus_app");
@ -414,6 +408,7 @@ impl CrateConfig {
let release = false;
let hot_reload = false;
let cross_origin_policy = false;
let verbose = false;
let custom_profile = None;
let features = None;
@ -421,26 +416,38 @@ impl CrateConfig {
let cargo_args = vec![];
Ok(Self {
out_dir,
crate_dir,
workspace_dir,
target_dir,
asset_dir,
#[cfg(feature = "cli")]
manifest,
executable,
release,
dioxus_config,
release,
hot_reload,
cross_origin_policy: false,
cross_origin_policy,
verbose,
custom_profile,
features,
verbose,
target,
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 {
self.executable = ExecutableType::Example(example_name);
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
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())?;
Ok(())
}
@ -29,7 +29,7 @@ pub(crate) fn process_assets(config: &CrateConfig, manifest: &AssetManifest) ->
.clone()
.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)?;

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
let CrateConfig {
out_dir,
crate_dir,
target_dir,
asset_dir,
executable,
dioxus_config,
..
} = config;
let out_dir = config.out_dir();
let asset_dir = config.asset_dir();
let _guard = WebAssetConfigDropGuard::new();
let _manganis_support = ManganisSupportGuard::default();
@ -251,15 +251,15 @@ pub fn build(config: &CrateConfig, _: bool, skip_assets: bool) -> Result<BuildRe
if path.is_file() {
std::fs::copy(&path, out_dir.join(path.file_name().unwrap()))?;
} else {
match fs_extra::dir::copy(&path, out_dir, &copy_options) {
match fs_extra::dir::copy(&path, &out_dir, &copy_options) {
Ok(_) => {}
Err(_e) => {
log::warn!("Error copying dir: {}", _e);
}
}
for ignore in &ignore_files {
let ignore = ignore.strip_prefix(&config.asset_dir).unwrap();
let ignore = config.out_dir.join(ignore);
let ignore = ignore.strip_prefix(&config.asset_dir()).unwrap();
let ignore = out_dir.join(ignore);
if ignore.is_file() {
std::fs::remove_file(ignore)?;
}
@ -369,13 +369,13 @@ pub fn build_desktop(
file_name
};
if !config.out_dir.is_dir() {
create_dir_all(&config.out_dir)?;
if !config.out_dir().is_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
if config.asset_dir.is_dir() {
if config.asset_dir().is_dir() {
let copy_options = fs_extra::dir::CopyOptions {
overwrite: true,
skip_exist: false,
@ -385,20 +385,20 @@ pub fn build_desktop(
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();
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 {
match fs_extra::dir::copy(&path, &config.out_dir, &copy_options) {
match fs_extra::dir::copy(&path, &config.out_dir(), &copy_options) {
Ok(_) => {}
Err(e) => {
log::warn!("Error copying dir: {}", e);
}
}
for ignore in &ignore_files {
let ignore = ignore.strip_prefix(&config.asset_dir).unwrap();
let ignore = config.out_dir.join(ignore);
let ignore = ignore.strip_prefix(&config.asset_dir()).unwrap();
let ignore = config.out_dir().join(ignore);
if ignore.is_file() {
std::fs::remove_file(ignore)?;
}
@ -635,7 +635,7 @@ fn build_assets(config: &CrateConfig) -> Result<Vec<PathBuf>> {
if file == "*" {
// 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() {
for entry in walkdir::WalkDir::new(&asset_dir)
.into_iter()
@ -655,7 +655,7 @@ fn build_assets(config: &CrateConfig) -> Result<Vec<PathBuf>> {
temp.file_stem().unwrap().to_str().unwrap()
);
let target_path = config
.out_dir
.out_dir()
.join(
temp.strip_prefix(&asset_dir)
.unwrap()
@ -685,11 +685,11 @@ fn build_assets(config: &CrateConfig) -> Result<Vec<PathBuf>> {
} else {
file
};
let path = config.asset_dir.join(relative_path);
let path = config.asset_dir().join(relative_path);
let out_file =
format!("{}.css", path.file_stem().unwrap().to_str().unwrap());
let target_path = config
.out_dir
.out_dir()
.join(PathBuf::from(relative_path).parent().unwrap())
.join(out_file);
if path.is_file() {
@ -719,11 +719,11 @@ fn build_assets(config: &CrateConfig) -> Result<Vec<PathBuf>> {
} else {
path
};
let path = config.asset_dir.join(relative_path);
let path = config.asset_dir().join(relative_path);
let out_file =
format!("{}.css", path.file_stem().unwrap().to_str().unwrap());
let target_path = config
.out_dir
.out_dir()
.join(PathBuf::from(relative_path).parent().unwrap())
.join(out_file);
if path.is_file() {

View file

@ -87,7 +87,7 @@ impl Bundle {
build_desktop(&crate_config, false, false)?;
// 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 {
ExecutableType::Binary(name)
@ -149,7 +149,7 @@ impl Bundle {
}
let mut settings = SettingsBuilder::new()
.project_out_directory(crate_config.out_dir)
.project_out_directory(crate_config.out_dir())
.package_settings(PackageSettings {
product_name: crate_config.dioxus_config.application.name.clone(),
version: package.version().to_string(),

View file

@ -184,8 +184,8 @@ impl PluginManager {
let args = lua.create_table()?;
args.set("name", crate_config.dioxus_config.application.name.clone())?;
args.set("platform", platform)?;
args.set("out_dir", crate_config.out_dir.to_str().unwrap())?;
args.set("asset_dir", crate_config.asset_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())?;
for i in 1..(manager.len()? as i32 + 1) {
let info = manager.get::<i32, PluginInfo>(i)?;
@ -208,8 +208,8 @@ impl PluginManager {
let args = lua.create_table()?;
args.set("name", crate_config.dioxus_config.application.name.clone())?;
args.set("platform", platform)?;
args.set("out_dir", crate_config.out_dir.to_str().unwrap())?;
args.set("asset_dir", crate_config.asset_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())?;
for i in 1..(manager.len()? as i32 + 1) {
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::Lib(name)
| ExecutableType::Example(name) => {
let mut file = config.out_dir.join(name);
let mut file = config.out_dir().join(name);
if cfg!(windows) {
file.set_extension("exe");
}

View file

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