/
Template.pm6
41 lines (32 loc) · 921 Bytes
/
Template.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#! /usr/bin/env false
use v6.c;
use Template::Mustache;
unit module Dist::Helper::Template;
multi sub template (
Str:D $template,
:%context,
--> Str
) is export {
my Str $absolute = "templates/$template";
my Distribution::Resource $resource = %?RESOURCES{$absolute};
X::AdHoc.new(payload => "Resource '$absolute' does not exist").throw unless %?RESOURCES{$absolute};
Template::Mustache.render($resource.slurp, %context);
}
multi sub template (
Str:D $template,
IO::Path:D $destination,
:%context,
Bool:D :$clobber = False
) is export {
X::AdHoc.new(payload => "$destination already exists").throw if $destination.IO.e && !$clobber;
mkdir $destination.parent.absolute;
spurt($destination, template($template, :%context));
}
multi sub template (
Str:D $template,
Str:D $destination,
:%context,
Bool:D :$clobber = False
) is export {
template($template, $destination.IO, :%context, :$clobber);
}