-
Notifications
You must be signed in to change notification settings - Fork 9
Extend init and clean commands #31
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e8f15c6
Extend init and clean commands
ikripaka ef7361e
Move errors to src/commands/error
ikripaka 74ea9b7
Add additional files to generate in `init --lib` command
ikripaka bbb1ef6
Mix imports
ikripaka ad241f5
Merge branch 'dev' into feat/clean
Arvolear 3d22888
clean up
Arvolear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| use crate::commands::CleanFlags; | ||
| use crate::commands::error::CommandResult; | ||
| use smplx_build::{ArtifactsResolver, BuildConfig}; | ||
| use std::fmt::Display; | ||
| use std::fs; | ||
| use std::path::{Path, PathBuf}; | ||
| use thiserror::Error; | ||
|
|
||
| pub struct Clean; | ||
|
|
||
| pub struct DeletedItems(Vec<PathBuf>); | ||
|
|
||
| #[derive(Error, Debug)] | ||
| pub enum CleanError { | ||
|
ikripaka marked this conversation as resolved.
Outdated
|
||
| #[error("Failed to resolve out_dir from config, err: '{0}'")] | ||
| ResolveOutDir(String), | ||
|
|
||
| #[error("Failed to remove output directory '{1}': {0}")] | ||
| RemoveOutDir(std::io::Error, PathBuf), | ||
|
|
||
| #[error("Failed to remove file '{1}': {0}")] | ||
| RemoveFile(std::io::Error, PathBuf), | ||
| } | ||
|
|
||
| type CleanResult<T> = Result<T, CleanError>; | ||
|
|
||
| impl Clean { | ||
| pub fn run(config: BuildConfig, additional_flags: CleanFlags, config_path: impl AsRef<Path>) -> CommandResult<()> { | ||
| let deleted_files = Self::delete_files(config, additional_flags, config_path)?; | ||
| println!("Deleted files: {deleted_files}"); | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl Clean { | ||
| fn delete_files( | ||
| config: BuildConfig, | ||
| additional_flags: CleanFlags, | ||
| smplx_toml_path: impl AsRef<Path>, | ||
| ) -> CleanResult<DeletedItems> { | ||
| let mut deleted_items = Vec::with_capacity(2); | ||
|
|
||
| let generated_artifacts = Self::remove_artifacts(config)?; | ||
| if let Some(artifacts_dir) = generated_artifacts { | ||
| deleted_items.push(artifacts_dir); | ||
| } | ||
| if additional_flags.all { | ||
| let simplex_toml_path = Self::remove_file(smplx_toml_path)?; | ||
| if let Some(simplex_toml) = simplex_toml_path { | ||
| deleted_items.push(simplex_toml); | ||
| } | ||
| } | ||
| Ok(DeletedItems(deleted_items)) | ||
| } | ||
|
|
||
| fn remove_artifacts(config: BuildConfig) -> CleanResult<Option<PathBuf>> { | ||
| let output_dir = ArtifactsResolver::resolve_local_dir(&config.out_dir) | ||
| .map_err(|e| CleanError::ResolveOutDir(e.to_string()))?; | ||
| let res = if output_dir.exists() { | ||
| fs::remove_dir_all(&output_dir).map_err(|e| CleanError::RemoveOutDir(e, output_dir.to_path_buf()))?; | ||
| Some(output_dir) | ||
| } else { | ||
| None | ||
| }; | ||
| Ok(res) | ||
| } | ||
|
|
||
| fn remove_file(config_path: impl AsRef<Path>) -> CleanResult<Option<PathBuf>> { | ||
| let config_path = config_path.as_ref().to_path_buf(); | ||
| if config_path.exists() && config_path.is_file() { | ||
| fs::remove_file(&config_path).map_err(|e| CleanError::RemoveFile(e, config_path.to_path_buf()))?; | ||
| Ok(Some(config_path)) | ||
| } else { | ||
| Ok(None) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Display for DeletedItems { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| let paths_len = self.0.len(); | ||
| let mut result = String::from("["); | ||
| for (index, path) in self.0.iter().enumerate() { | ||
| result.push_str(&format!("\n\t{}", path.display())); | ||
| if index < paths_len - 1 { | ||
| result.push(','); | ||
| } | ||
| result.push('\n'); | ||
| } | ||
| result.push(']'); | ||
| write!(f, "{}", result) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.