Repository: cargo-generate/cargo-generate (Rust)

Issue:
Variables set in pre scripts don't carry over to the template
**Describe the bug**
The template acts as if nothing was changed if variables are set in a pre script.
- [x] I'm on the latest rust version (from the [package](https://archlinux.org/packages/extra/x86_64/rust/))
- [x] I'm on the latest cargo-generate version (from the [package](https://archlinux.org/packages/community/x86_64/cargo-generate))

**To Reproduce**
1. Clone [this repo](https://github.com/purpzie/cargo-generate-bug)
2. Look at `cargo-generate.rhai` and `src/lib.rs`
3. Run `cargo-generate --path ./cargo-generate-bug`
4. `src/lib.rs` is empty

**Expected behavior**
`src/lib.rs` should contain "bar"

**Desktop (please complete the following information):**
 - OS: Arch Linux (in Windows subsystem for Linux)
 - Shell: bash
 - rustc --version: `rustc 1.62.1`
 - cargo generate --version: `cargo-generate 0.16.0`



Discussion:

Patch:
diff --git a/src/hooks/mod.rs b/src/hooks/mod.rs
index eb1203c8..cb7ed55e 100644
--- a/src/hooks/mod.rs
+++ b/src/hooks/mod.rs
@@ -38,10 +38,11 @@ pub fn execute_hooks(
     hooks: &[String],
     allow_commands: bool,
     silent: bool,
-) -> Result<()> {
+) -> Result<liquid::Object> {
     let liquid_object = Rc::new(RefCell::new(liquid_object));
-    let engine = create_rhai_engine(dir, liquid_object, allow_commands, silent);
-    evaluate_scripts(dir, hooks, engine)
+    let engine = create_rhai_engine(dir, liquid_object.clone(), allow_commands, silent);
+    evaluate_scripts(dir, hooks, engine)?;
+    Ok(liquid_object.take())
 }
 
 fn evaluate_scripts(template_dir: &Path, scripts: &[String], engine: rhai::Engine) -> Result<()> {
diff --git a/src/lib.rs b/src/lib.rs
index 22571937..73ef9f59 100755
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -568,9 +568,9 @@ fn expand_template(
     )?;
     add_missing_provided_values(&mut liquid_object, source_template.template_values())?;
 
-    execute_hooks(
+    let liquid_object = execute_hooks(
         template_dir,
-        liquid_object.clone(),
+        liquid_object,
         &config.get_pre_hooks(),
         args.allow_commands,
         args.silent,


Test patch:
diff --git a/tests/integration/hooks.rs b/tests/integration/hooks.rs
index e997a75f..2b8522b5 100644
--- a/tests/integration/hooks.rs
+++ b/tests/integration/hooks.rs
@@ -274,3 +274,44 @@ fn it_can_change_case() {
         .stdout(predicates::str::contains("Title Case"))
         .stdout(predicates::str::contains("UpperCamelCase"));
 }
+#[test]
+fn can_change_variables_from_pre_hook() {
+    let template = tmp_dir()
+        .file(
+            "cargo-generate.toml",
+            indoc! {r#"
+            [hooks]
+            pre = ["pre-script.rhai"]
+            "#},
+        )
+        .file(
+            "pre-script.rhai",
+            indoc! {r#"
+                variable::set("foo", "bar");
+            "#},
+        )
+        .file(
+            "PRE-TEST",
+            indoc! {r#"
+                {{foo}};
+            "#},
+        )
+        .init_git()
+        .build();
+
+    let dir = tmp_dir().build();
+
+    binary()
+        .arg("gen")
+        .arg("--git")
+        .arg(template.path())
+        .arg("-n")
+        .arg("script-project")
+        .current_dir(&dir.path())
+        .assert()
+        .success()
+        .stdout(predicates::str::contains("Done!").from_utf8());
+
+    assert!(dir.exists("script-project/PRE-TEST"));
+    assert!(dir.read("script-project/PRE-TEST").contains("bar"));
+}