Skip to content

Commit

Permalink
Implement engine revert mechanism.
Browse files Browse the repository at this point in the history
The mechanism rolls back all applied actions in case of errors, leaving
the project in the state in which it was found (with the exception of
some apparent permission bit shifting).
  • Loading branch information
msmoiz committed Jun 3, 2022
1 parent be797ae commit 352d918
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Mustafa <mustafa.moiz125@gmail.com>"]
edition = "2018"
name = "renom"
version = "0.1.3"
version = "0.1.4"
description = "A simple program to rename Unreal Engine 4 projects."
keywords = ["gamedev", "ue4", "unreal_engine", "rename"]
categories = [
Expand Down
48 changes: 35 additions & 13 deletions src/director.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,43 @@ impl Director {

Log::header("Application");
let mut engine = Engine::new();
ok_or_quit!(engine.execute(changeset, &backup_dir));
match engine.execute(changeset, &backup_dir) {
Ok(_) => {
Log::header("Cleanup");
match project_type {
ProjectType::Blueprint => {
Log::basic("Nothing to clean up for Blueprint project.");
}
ProjectType::Code => {
Log::basic("Though not strictly necessary, it is a good idea to clean up outdated Saved, Intermediate, and Binaries folders.\nShall we go ahead and do so for you?");
Log::prompt("[Y]es/[N]o");
if Director::request_cleanup() {
ok_or_quit!(Director::cleanup(
&project_root.with_file_name(&final_name)
));
} else {
Log::basic("Cleanup skipped.");
}
}
}

Log::header("Cleanup");
match project_type {
ProjectType::Blueprint => {
Log::basic("Nothing to clean up for Blueprint project.");
Log::header("Success");
Log::basic("Project successfully renamed.");
}
ProjectType::Code => {
Log::basic("Though not strictly necessary, it is a good idea to clean up outdated Saved, Intermediate, and Binaries folders.\nShall we go ahead and do so for you?");
Err(err) => {
Log::error(err);
Log::header("Recovery");
Log::basic("Looks like things did not work out as planned. Would you like to revert the changes made so far?");
Log::prompt("[Y]es/[N]o");
if Director::request_cleanup() {
ok_or_quit!(Director::cleanup(&project_root.with_file_name(&final_name)));
if Director::request_recover() {
ok_or_quit!(engine.revert());
} else {
Log::basic("Cleanup skipped.");
Log::basic("Recovery skipped.");
}
}
}

Log::header("Success");
Log::basic("Project successfully renamed.");
Log::newline();

Log::prompt("Press Enter to exit.");
let _ = stdin().read(&mut [0u8]);
}
Expand Down Expand Up @@ -200,6 +215,13 @@ impl Director {
Ok(files)
}

/// Request recover desired from the user.
fn request_recover() -> bool {
let mut buffer = String::new();
stdin().read_line(&mut buffer).unwrap();
matches!(buffer.trim(), "y" | "Y" | "yes" | "Yes")
}

/// Request cleanup desired from the user.
fn request_cleanup() -> bool {
let mut buffer = String::new();
Expand Down
12 changes: 11 additions & 1 deletion src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Engine {
backup_dir: impl AsRef<Path>,
) -> Result<(), String> {
for change in changeset {
Log::basic(format!("{}", change));
Log::basic(format!("Apply: {}", change));
self.execute_single(change, backup_dir.as_ref())?;
}
Ok(())
Expand All @@ -54,6 +54,16 @@ impl Engine {
Err(err) => Err(err.to_string()),
}
}

/// Revert entire history of actions.
/// Upon error, it will halt execution and return the error.
pub fn revert(&mut self) -> Result<(), String> {
while let Some((change, revert)) = self.history.pop() {
Log::basic(format!("Revert: {}", change));
revert().map_err(|err| err.to_string())?;
}
Ok(())
}
}

fn rename_file(params: &RenameFile) -> io::Result<Revert> {
Expand Down

0 comments on commit 352d918

Please sign in to comment.