-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
Meta.cs
58 lines (50 loc) · 2.04 KB
/
Meta.cs
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
using System;
using System.Linq;
using System.Reflection;
namespace CKAN
{
public static class Meta
{
/// <summary>
/// Programmatically generate the string "CKAN" from the assembly info attributes,
/// so we don't have to embed that string in many places
/// </summary>
/// <returns>"CKAN"</returns>
public static string GetProductName()
=> Assembly.GetExecutingAssembly()
.GetAssemblyAttribute<AssemblyProductAttribute>()
.Product;
public static string GetVersion(VersionFormat format = VersionFormat.Normal)
{
var version = Assembly
.GetExecutingAssembly()
.GetAssemblyAttribute<AssemblyInformationalVersionAttribute>()
.InformationalVersion;
switch (format)
{
case VersionFormat.Short:
return $"v{version.UpToCharacters(shortDelimiters)}";
break;
case VersionFormat.Normal:
return $"v{version.UpToCharacter('+')}";
break;
case VersionFormat.Full:
return $"v{version}";
break;
default:
throw new ArgumentOutOfRangeException(nameof(format), format, null);
}
}
private static readonly char[] shortDelimiters = new char[] { '-', '+' };
private static string UpToCharacter(this string orig, char what)
=> orig.UpToIndex(orig.IndexOf(what));
private static string UpToCharacters(this string orig, char[] what)
=> orig.UpToIndex(orig.IndexOfAny(what));
private static string UpToIndex(this string orig, int index)
=> index == -1 ? orig
: orig.Substring(0, index);
private static T GetAssemblyAttribute<T>(this Assembly assembly)
=> (T)assembly.GetCustomAttributes(typeof(T), false)
.First();
}
}