-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.cake
executable file
·175 lines (150 loc) · 4.21 KB
/
build.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#tool nuget:?package=GitVersion.CommandLine
#tool nuget:?package=gitlink&version=2.4.0
#tool nuget:?package=vswhere
#tool nuget:?package=NUnit.ConsoleRunner
#addin nuget:?package=Cake.Incubator&version=1.5.0
#addin nuget:?package=Cake.Git&version=0.16.0
var sln = new FilePath(“XibFree.sln");
var outputDir = new DirectoryPath("artifacts");
var nuspecDir = new DirectoryPath("nuspec");
var target = Argument("target", "Default");
var isRunningOnAppVeyor = AppVeyor.IsRunningOnAppVeyor;
Task("Clean").Does(() =>
{
CleanDirectories("./**/bin");
CleanDirectories("./**/obj");
CleanDirectories(outputDir.FullPath);
EnsureDirectoryExists(outputDir);
});
GitVersion versionInfo = null;
Task("Version").Does(() => {
versionInfo = GitVersion(new GitVersionSettings {
UpdateAssemblyInfo = true,
OutputType = GitVersionOutput.Json
});
Information("GitVersion -> {0}", versionInfo.Dump());
});
Task("UpdateAppVeyorBuildNumber")
.IsDependentOn("Version")
.WithCriteria(() => isRunningOnAppVeyor)
.Does(() =>
{
AppVeyor.UpdateBuildVersion(versionInfo.FullBuildMetaData);
});
FilePath msBuildPath;
Task("ResolveBuildTools")
.Does(() =>
{
var vsLatest = VSWhereLatest();
msBuildPath = (vsLatest == null)
? null
: vsLatest.CombineWithFilePath("./MSBuild/15.0/Bin/MSBuild.exe");
});
Task("Restore")
.IsDependentOn("ResolveBuildTools")
.Does(() => {
NuGetRestore(sln, new NuGetRestoreSettings {
Verbosity = NuGetVerbosity.Quiet
});
// MSBuild(sln, settings => settings.WithTarget("Restore"));
});
Task("Build")
.IsDependentOn("ResolveBuildTools")
.IsDependentOn("Clean")
.IsDependentOn("UpdateAppVeyorBuildNumber")
.IsDependentOn("Restore")
.Does(() => {
var settings = new MSBuildSettings
{
Configuration = "Release",
ToolPath = msBuildPath,
Verbosity = Verbosity.Minimal
};
settings.Properties.Add("DebugSymbols", new List<string> { "True" });
settings.Properties.Add("DebugType", new List<string> { "Full" });
MSBuild(sln, settings);
});
RunTarget(target);
bool IsMasterOrReleases()
{
if (StringComparer.OrdinalIgnoreCase.Equals(versionInfo.BranchName, "master"))
return true;
if (versionInfo.BranchName.Contains("releases/"))
return true;
return false;
}
bool IsRepository(string repoName)
{
if (isRunningOnAppVeyor)
{
var buildEnvRepoName = AppVeyor.Environment.Repository.Name;
Information("Checking repo name: {0} against build repo name: {1}", repoName, buildEnvRepoName);
return StringComparer.OrdinalIgnoreCase.Equals(repoName, buildEnvRepoName);
}
else
{
try
{
var path = MakeAbsolute(sln).GetDirectory().FullPath;
using (var repo = new LibGit2Sharp.Repository(path))
{
var origin = repo.Network.Remotes.FirstOrDefault(
r => r.Name.ToLowerInvariant() == "origin");
return origin.Url.ToLowerInvariant() ==
"https://github.com/" + repoName.ToLowerInvariant();
}
}
catch(Exception ex)
{
Information("Failed to lookup repository: {0}", ex);
return false;
}
}
}
bool IsTagged()
{
var path = MakeAbsolute(sln).GetDirectory().FullPath;
using (var repo = new LibGit2Sharp.Repository(path))
{
var head = repo.Head;
var headSha = head.Tip.Sha;
var tag = repo.Tags.FirstOrDefault(t => t.Target.Sha == headSha);
if (tag == null)
{
Information("HEAD is not tagged");
return false;
}
Information("HEAD is tagged: {0}", tag.FriendlyName);
return true;
}
}
Tuple<string, string> GetNugetKeyAndSource()
{
var apiKeyKey = string.Empty;
var sourceKey = string.Empty;
if (isRunningOnAppVeyor)
{
apiKeyKey = "NUGET_APIKEY";
sourceKey = "NUGET_SOURCE";
}
else
{
if (StringComparer.OrdinalIgnoreCase.Equals(versionInfo.BranchName, "develop"))
{
apiKeyKey = "NUGET_APIKEY_DEVELOP";
sourceKey = "NUGET_SOURCE_DEVELOP";
}
else if (IsMasterOrReleases())
{
apiKeyKey = "NUGET_APIKEY_MASTER";
sourceKey = "NUGET_SOURCE_MASTER";
}
}
var apiKey = EnvironmentVariable(apiKeyKey);
if (string.IsNullOrEmpty(apiKey))
throw new Exception(string.Format("The {0} environment variable is not defined.", apiKeyKey));
var source = EnvironmentVariable(sourceKey);
if (string.IsNullOrEmpty(source))
throw new Exception(string.Format("The {0} environment variable is not defined.", sourceKey));
return Tuple.Create(apiKey, source);
}