/
buildserver.cake
109 lines (84 loc) · 3.27 KB
/
buildserver.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
104
105
106
107
108
109
// Customize this file when using a different build server
#l "buildserver-continuaci.cake"
#addin "nuget:?package=MagicChunks&version=2.0.0.119"
using System.Runtime.InteropServices;
[DllImport("kernel32.dll", CharSet=CharSet.Unicode)]
static extern uint GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
uint nSize,
string lpFileName);
Dictionary<string, string> _buildServerVariableCache = null;
//-------------------------------------------------------------
public string GetBuildServerVariable(string variableName, string defaultValue = null)
{
if (_buildServerVariableCache == null)
{
_buildServerVariableCache = new Dictionary<string, string>();
}
var cacheKey = string.Format("{0}__{1}", variableName ?? string.Empty, defaultValue ?? string.Empty);
if (!_buildServerVariableCache.TryGetValue(cacheKey, out string value))
{
value = GetBuildServerVariableForCache(variableName, defaultValue);
_buildServerVariableCache[cacheKey] = value;
}
//else
//{
// Information("Retrieved value for '{0}' from cache", variableName);
//}
return value;
}
//-------------------------------------------------------------
private string GetBuildServerVariableForCache(string variableName, string defaultValue = null)
{
var argumentValue = Argument(variableName, "non-existing");
if (argumentValue != "non-existing")
{
Information("Variable '{0}' is specified via an argument", variableName);
return argumentValue;
}
// Just a forwarder, change this line to use a different build server
var buildServerVariable = GetContinuaCIVariable(variableName, defaultValue);
if (buildServerVariable.Item1)
{
return buildServerVariable.Item2;
}
var overrideFile = "./build.cakeoverrides";
if (System.IO.File.Exists(overrideFile))
{
var sb = new StringBuilder(string.Empty, 128);
var lengthRead = GetPrivateProfileString("General", variableName, null, sb, (uint)sb.Capacity, overrideFile);
if (lengthRead > 0)
{
Information("Variable '{0}' is specified via build.cakeoverrides", variableName);
return sb.ToString();
}
}
if (HasEnvironmentVariable(variableName))
{
Information("Variable '{0}' is specified via an environment variable", variableName);
return EnvironmentVariable(variableName);
}
var parameters = Parameters;
if (parameters.TryGetValue(variableName, out var parameter))
{
Information("Variable '{0}' is specified via the Parameters dictionary", variableName);
if (parameter is null)
{
return null;
}
if (parameter is string)
{
return (string)parameter;
}
if (parameter is Func<string>)
{
return ((Func<string>)parameter).Invoke();
}
throw new Exception(string.Format("Parameter is defined as '{0}', but that type is not supported yet...", parameter.GetType().Name));
}
Information("Variable '{0}' is not specified, returning default value", variableName);
return defaultValue ?? string.Empty;
}