@@ -3,11 +3,13 @@ use std::path::{Path, PathBuf};
33use anyhow:: { bail, Context as ResultExt , Result } ;
44use indexmap:: IndexMap ;
55use maplit:: hashmap;
6+ use serde:: Serialize ;
67
78use crate :: config:: { ExternalPlugin , Source , Template } ;
89use crate :: context:: Context ;
910use crate :: lock:: file:: LockedExternalPlugin ;
1011use crate :: lock:: source:: LockedSource ;
12+ use crate :: util:: TEMPLATE_ENGINE ;
1113
1214/// Consume the [`ExternalPlugin`] and convert it to a [`LockedExternalPlugin`].
1315pub fn lock (
@@ -39,10 +41,6 @@ pub fn lock(
3941 apply,
4042 }
4143 } else {
42- // Handlebars instance to do the rendering
43- let mut hbs = handlebars:: Handlebars :: new ( ) ;
44- hbs. set_strict_mode ( true ) ;
45-
4644 // Data to use in template rendering
4745 let mut data = hashmap ! {
4846 "data_dir" => ctx
@@ -54,9 +52,7 @@ pub fn lock(
5452
5553 let source_dir = locked_source. dir ;
5654 let plugin_dir = if let Some ( dir) = dir {
57- let rendered = hbs
58- . render_template ( & dir, & data)
59- . with_context ( s ! ( "failed to render template `{}`" , dir) ) ?;
55+ let rendered = render_template ( & dir, & data) ?;
6056 Some ( source_dir. join ( rendered) )
6157 } else {
6258 None
@@ -73,20 +69,15 @@ pub fn lock(
7369 if let Some ( uses) = & uses {
7470 let patterns = uses
7571 . iter ( )
76- . map ( |u| {
77- hbs. render_template ( u, & data)
78- . with_context ( s ! ( "failed to render template `{}`" , u) )
79- } )
72+ . map ( |u| render_template ( u, & data) )
8073 . collect :: < Result < Vec < _ > > > ( ) ?;
8174 if !match_globs ( dir, & patterns, & mut files) ? {
8275 bail ! ( "failed to find any files matching any of `{:?}`" , patterns) ;
8376 }
8477 // Otherwise we try to figure out which files to use...
8578 } else {
8679 for g in global_matches {
87- let pattern = hbs
88- . render_template ( g, & data)
89- . with_context ( s ! ( "failed to render template `{}`" , g) ) ?;
80+ let pattern = render_template ( g, & data) ?;
9081 if match_globs ( dir, & [ pattern] , & mut files) ? {
9182 break ;
9283 }
@@ -110,6 +101,17 @@ pub fn lock(
110101 } )
111102}
112103
104+ fn render_template < S > ( template : & str , ctx : S ) -> Result < String >
105+ where
106+ S : Serialize ,
107+ {
108+ TEMPLATE_ENGINE
109+ . compile ( template)
110+ . with_context ( s ! ( "failed to compile template `{}`" , template) ) ?
111+ . render ( ctx)
112+ . with_context ( s ! ( "failed to render template `{}`" , template) )
113+ }
114+
113115fn match_globs ( dir : & Path , patterns : & [ String ] , files : & mut Vec < PathBuf > ) -> Result < bool > {
114116 let debug = || {
115117 patterns
0 commit comments