This repository was archived by the owner on Dec 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathFormatStringTokenResolver.cs
More file actions
70 lines (55 loc) · 2.68 KB
/
Copy pathFormatStringTokenResolver.cs
File metadata and controls
70 lines (55 loc) · 2.68 KB
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
using System;
using System.Text.RegularExpressions;
using LibGit2Sharp;
namespace Stamp.Fody.Internal
{
using Version = System.Version;
internal static class FormatStringTokenResolver
{
private static Regex reEnvironmentToken = new Regex(@"%env\[([^\]]+)]%");
private static Regex reNow = new Regex(@"%now:([^%]+)%");
private static Regex reUtcNow = new Regex(@"%utcnow:([^%]+)%");
private static DateTime now = DateTime.Now;
private static DateTime utcNow = DateTime.UtcNow;
public static string ReplaceTokens(string template, Version version, Repository repo, string changestring)
{
var branch = repo.Head;
template = template.Replace("%version%", version.ToString());
template = template.Replace("%version1%", version.ToString(1));
template = template.Replace("%version2%", version.ToString(2));
template = template.Replace("%version3%", version.ToString(3));
template = template.Replace("%version4%", version.ToString(4));
template = template.Replace("%now%", now.ToShortDateString());
template = template.Replace("%utcnow%", utcNow.ToShortDateString());
template = template.Replace("%githash%", branch.Tip.Sha);
template = template.Replace("%shorthash%", branch.Tip.Sha.Substring(0, 8));
template = template.Replace("%branch%", branch.FriendlyName);
template = template.Replace("%haschanges%", repo.IsClean() ? "" : changestring);
template = template.Replace("%user%", FormatUserName());
template = template.Replace("%machine%", Environment.MachineName);
template = template.Replace("%lasttag%", repo.FindVersionTag());
template = reEnvironmentToken.Replace(template, FormatEnvironmentVariable);
template = reNow.Replace(template, FormatTime);
template = reUtcNow.Replace(template, FormatUtcTime);
return template.Trim();
}
private static string FormatUserName()
{
return string.IsNullOrWhiteSpace(Environment.UserDomainName)
? Environment.UserName
: $@"{Environment.UserDomainName}\{Environment.UserName}";
}
private static string FormatEnvironmentVariable(Match match)
{
return Environment.GetEnvironmentVariable(match.Groups[1].Value);
}
private static string FormatTime(Match match)
{
return now.ToString(match.Groups[1].Value);
}
private static string FormatUtcTime(Match match)
{
return utcNow.ToString(match.Groups[1].Value);
}
}
}