-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtemplates-tasks.cake
103 lines (79 loc) · 3.01 KB
/
templates-tasks.cake
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#l "templates-variables.cake"
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.IO;
//-------------------------------------------------------------
public class TemplatesProcessor : ProcessorBase
{
public TemplatesProcessor(BuildContext buildContext)
: base(buildContext)
{
var templatesRelativePath = "./deployment/templates";
if (CakeContext.DirectoryExists(templatesRelativePath))
{
var currentDirectoryPath = System.IO.Directory.GetCurrentDirectory();
var templateAbsolutePath = System.IO.Path.Combine(currentDirectoryPath, templatesRelativePath);
var files = System.IO.Directory.GetFiles(templateAbsolutePath, "*.*", System.IO.SearchOption.AllDirectories);
CakeContext.Information($"Found '{files.Count()}' template files");
foreach (var file in files)
{
BuildContext.Templates.Items.Add(file.Substring(templateAbsolutePath.Length + 1));
}
}
}
public override bool HasItems()
{
return BuildContext.Templates.Items.Count > 0;
}
public override async Task PrepareAsync()
{
}
public override async Task UpdateInfoAsync()
{
if (!HasItems())
{
return;
}
var variableRegex = new Regex(@"\$\{([^}]+)\}", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
foreach (var template in BuildContext.Templates.Items)
{
CakeContext.Information($"Updating template file '{template}'");
var templateSourceFile = $"./deployment/templates/{template}";
var content = CakeContext.FileReadText(templateSourceFile);
var matches = variableRegex.Matches(content);
foreach (var match in matches.Cast<Match>())
{
var variableName = match.Groups[1].Value;
CakeContext.Information($"Found usage of variable '{variableName}'");
if (!BuildContext.Variables.TryGetValue(variableName, out var replacement))
{
CakeContext.Error($"Could not find value for variable '{variableName}'");
continue;
}
content = content.Replace($"${{{variableName}}}", replacement);
}
CakeContext.FileWriteText($"{template}", content);
}
}
public override async Task BuildAsync()
{
// Run templates every time
await UpdateInfoAsync();
}
public override async Task PackageAsync()
{
// Run templates every time
await UpdateInfoAsync();
}
public override async Task DeployAsync()
{
if (!HasItems())
{
return;
}
}
public override async Task FinalizeAsync()
{
}
}