Repository: axodotdev/cargo-dist
Pull request #355 (2023-08-23 20:38:23)

fix(templates): append trailing newline if needed
Alternate to #354 - append trailing a trailing newline if the rendered template doesn't have one. minijinja always strips trailing newlines; this is likely easier than always remembering to add an extra newline to templates.

Closes #354.


Patch:
diff --git a/cargo-dist/src/backend/templates.rs b/cargo-dist/src/backend/templates.rs
index 44925e52..3f6d7b1c 100644
--- a/cargo-dist/src/backend/templates.rs
+++ b/cargo-dist/src/backend/templates.rs
@@ -195,7 +195,11 @@ impl Templates {
         val: &impl Serialize,
     ) -> DistResult<String> {
         let template = self.envs[file.env].get_template(file.path.as_str())?;
-        let rendered = template.render(val)?;
+        let mut rendered = template.render(val)?;
+        // minijinja strips trailing newlines from templates
+        if !rendered.ends_with('\n') {
+            rendered.push('\n');
+        }
         let cleaned = dos2unix(&rendered).into_owned();
         Ok(cleaned)
     }


Test patch:
diff --git a/cargo-dist/tests/snapshots/akaikatana_basic.snap b/cargo-dist/tests/snapshots/akaikatana_basic.snap
index f5f8dac0..168c96b8 100644
--- a/cargo-dist/tests/snapshots/akaikatana_basic.snap
+++ b/cargo-dist/tests/snapshots/akaikatana_basic.snap
@@ -722,6 +722,7 @@ class AkaikatanaRepack < Formula
     pkgshare.install *leftover_contents unless leftover_contents.empty?
   end
 end
+
 ================ installer.ps1 ================
 # Licensed under the MIT license
 # <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
@@ -1455,3 +1456,4 @@ jobs:
         run: |
           gh release edit ${{ github.ref_name }} --draft=false
 
+
diff --git a/cargo-dist/tests/snapshots/akaikatana_repo_with_dot_git.snap b/cargo-dist/tests/snapshots/akaikatana_repo_with_dot_git.snap
index f5f8dac0..168c96b8 100644
--- a/cargo-dist/tests/snapshots/akaikatana_repo_with_dot_git.snap
+++ b/cargo-dist/tests/snapshots/akaikatana_repo_with_dot_git.snap
@@ -722,6 +722,7 @@ class AkaikatanaRepack < Formula
     pkgshare.install *leftover_contents unless leftover_contents.empty?
   end
 end
+
 ================ installer.ps1 ================
 # Licensed under the MIT license
 # <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
@@ -1455,3 +1456,4 @@ jobs:
         run: |
           gh release edit ${{ github.ref_name }} --draft=false
 
+
diff --git a/cargo-dist/tests/snapshots/axolotlsay_basic.snap b/cargo-dist/tests/snapshots/axolotlsay_basic.snap
index e1d97e09..aeaa3cd1 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_basic.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_basic.snap
@@ -723,6 +723,7 @@ class Axolotlsay < Formula
     pkgshare.install *leftover_contents unless leftover_contents.empty?
   end
 end
+
 ================ installer.ps1 ================
 # Licensed under the MIT license
 # <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
@@ -2322,3 +2323,4 @@ jobs:
         run: |
           gh release edit ${{ github.ref_name }} --draft=false
 
+
diff --git a/cargo-dist/tests/snapshots/install_path_cargo_home.snap b/cargo-dist/tests/snapshots/install_path_cargo_home.snap
index 380de767..1a7eb547 100644
--- a/cargo-dist/tests/snapshots/install_path_cargo_home.snap
+++ b/cargo-dist/tests/snapshots/install_path_cargo_home.snap
@@ -723,6 +723,7 @@ class Axolotlsay < Formula
     pkgshare.install *leftover_contents unless leftover_contents.empty?
   end
 end
+
 ================ installer.ps1 ================
 # Licensed under the MIT license
 # <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
diff --git a/cargo-dist/tests/snapshots/install_path_env_no_subdir.snap b/cargo-dist/tests/snapshots/install_path_env_no_subdir.snap
index 8c9c57a3..41d18006 100644
--- a/cargo-dist/tests/snapshots/install_path_env_no_subdir.snap
+++ b/cargo-dist/tests/snapshots/install_path_env_no_subdir.snap
@@ -706,6 +706,7 @@ class Axolotlsay < Formula
     pkgshare.install *leftover_contents unless leftover_contents.empty?
   end
 end
+
 ================ installer.ps1 ================
 # Licensed under the MIT license
 # <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
diff --git a/cargo-dist/tests/snapshots/install_path_env_subdir.snap b/cargo-dist/tests/snapshots/install_path_env_subdir.snap
index b4b7349f..b52015e9 100644
--- a/cargo-dist/tests/snapshots/install_path_env_subdir.snap
+++ b/cargo-dist/tests/snapshots/install_path_env_subdir.snap
@@ -706,6 +706,7 @@ class Axolotlsay < Formula
     pkgshare.install *leftover_contents unless leftover_contents.empty?
   end
 end
+
 ================ installer.ps1 ================
 # Licensed under the MIT license
 # <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
diff --git a/cargo-dist/tests/snapshots/install_path_env_subdir_space.snap b/cargo-dist/tests/snapshots/install_path_env_subdir_space.snap
index 39c6ea39..b6f2f3d4 100644
--- a/cargo-dist/tests/snapshots/install_path_env_subdir_space.snap
+++ b/cargo-dist/tests/snapshots/install_path_env_subdir_space.snap
@@ -706,6 +706,7 @@ class Axolotlsay < Formula
     pkgshare.install *leftover_contents unless leftover_contents.empty?
   end
 end
+
 ================ installer.ps1 ================
 # Licensed under the MIT license
 # <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
diff --git a/cargo-dist/tests/snapshots/install_path_env_subdir_space_deeper.snap b/cargo-dist/tests/snapshots/install_path_env_subdir_space_deeper.snap
index f4c34c97..fd791841 100644
--- a/cargo-dist/tests/snapshots/install_path_env_subdir_space_deeper.snap
+++ b/cargo-dist/tests/snapshots/install_path_env_subdir_space_deeper.snap
@@ -706,6 +706,7 @@ class Axolotlsay < Formula
     pkgshare.install *leftover_contents unless leftover_contents.empty?
   end
 end
+
 ================ installer.ps1 ================
 # Licensed under the MIT license
 # <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
diff --git a/cargo-dist/tests/snapshots/install_path_home_subdir_deeper.snap b/cargo-dist/tests/snapshots/install_path_home_subdir_deeper.snap
index cd134b32..c16b6a1b 100644
--- a/cargo-dist/tests/snapshots/install_path_home_subdir_deeper.snap
+++ b/cargo-dist/tests/snapshots/install_path_home_subdir_deeper.snap
@@ -706,6 +706,7 @@ class Axolotlsay < Formula
     pkgshare.install *leftover_contents unless leftover_contents.empty?
   end
 end
+
 ================ installer.ps1 ================
 # Licensed under the MIT license
 # <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
diff --git a/cargo-dist/tests/snapshots/install_path_home_subdir_min.snap b/cargo-dist/tests/snapshots/install_path_home_subdir_min.snap
index 69a0af59..e151c9cb 100644
--- a/cargo-dist/tests/snapshots/install_path_home_subdir_min.snap
+++ b/cargo-dist/tests/snapshots/install_path_home_subdir_min.snap
@@ -706,6 +706,7 @@ class Axolotlsay < Formula
     pkgshare.install *leftover_contents unless leftover_contents.empty?
   end
 end
+
 ================ installer.ps1 ================
 # Licensed under the MIT license
 # <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
diff --git a/cargo-dist/tests/snapshots/install_path_home_subdir_space.snap b/cargo-dist/tests/snapshots/install_path_home_subdir_space.snap
index 4fcc3cf5..7aa56454 100644
--- a/cargo-dist/tests/snapshots/install_path_home_subdir_space.snap
+++ b/cargo-dist/tests/snapshots/install_path_home_subdir_space.snap
@@ -706,6 +706,7 @@ class Axolotlsay < Formula
     pkgshare.install *leftover_contents unless leftover_contents.empty?
   end
 end
+
 ================ installer.ps1 ================
 # Licensed under the MIT license
 # <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
diff --git a/cargo-dist/tests/snapshots/install_path_home_subdir_space_deeper.snap b/cargo-dist/tests/snapshots/install_path_home_subdir_space_deeper.snap
index 6a094024..bb1faa21 100644
--- a/cargo-dist/tests/snapshots/install_path_home_subdir_space_deeper.snap
+++ b/cargo-dist/tests/snapshots/install_path_home_subdir_space_deeper.snap
@@ -706,6 +706,7 @@ class Axolotlsay < Formula
     pkgshare.install *leftover_contents unless leftover_contents.empty?
   end
 end
+
 ================ installer.ps1 ================
 # Licensed under the MIT license
 # <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your