forked from FacilityApi/FacilityJavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.cake
More file actions
209 lines (179 loc) · 6.38 KB
/
build.cake
File metadata and controls
209 lines (179 loc) · 6.38 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#addin "nuget:?package=Cake.Git&version=0.10.0"
#addin "nuget:?package=Octokit&version=0.23.0"
#tool "nuget:?package=coveralls.io&version=1.3.4"
#tool "nuget:?package=gitlink&version=2.3.0"
#tool "nuget:?package=NUnit.ConsoleRunner&version=3.5.0"
#tool "nuget:?package=OpenCover&version=4.6.519"
#tool "nuget:?package=ReportGenerator&version=2.5.0"
using LibGit2Sharp;
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
var nugetApiKey = Argument("nugetApiKey", "");
var githubApiKey = Argument("githubApiKey", "");
var coverallsApiKey = Argument("coverallsApiKey", "");
var prerelease = Argument("prerelease", "");
var sourceIndex = Argument("sourceIndex", true);
var solutionFileName = "FacilityJavaScript.sln";
var githubOwner = "FacilityApi";
var githubRepo = "FacilityJavaScript";
var githubRawUri = "http://raw.githubusercontent.com";
var nugetSource = "https://www.nuget.org/api/v2/package";
var coverageAssemblies = new[] { "Facility.CodeGen.JavaScript" };
var rootPath = MakeAbsolute(Directory(".")).FullPath;
var gitRepository = LibGit2Sharp.Repository.IsValid(rootPath) ? new LibGit2Sharp.Repository(rootPath) : null;
var githubClient = new Octokit.GitHubClient(new Octokit.ProductHeaderValue("build.cake"));
if (!string.IsNullOrEmpty(githubApiKey))
githubClient.Credentials = new Octokit.Credentials(githubApiKey);
string version = null;
string headSha = null;
Task("Clean")
.Does(() =>
{
CleanDirectories($"src/**/bin");
CleanDirectories($"src/**/obj");
CleanDirectories($"tests/**/bin");
CleanDirectories($"tests/**/obj");
CleanDirectories("release");
});
Task("Build")
.IsDependentOn("Clean")
.Does(() =>
{
NuGetRestore(solutionFileName);
MSBuild(solutionFileName, settings => settings.SetConfiguration(configuration));
});
Task("VerifyCodeGen")
.IsDependentOn("Build")
.Does(() => CodeGen(verify: true));
Task("CodeGen")
.IsDependentOn("Build")
.Does(() => CodeGen(verify: false));
Task("Test")
.IsDependentOn("VerifyCodeGen")
.Does(() => NUnit3($"tests/**/bin/**/*.UnitTests.dll", new NUnit3Settings { NoResults = true }));
Task("SourceIndex")
.IsDependentOn("Test")
.WithCriteria(() => configuration == "Release" && gitRepository != null)
.Does(() =>
{
if (sourceIndex)
{
var dirtyEntry = gitRepository.RetrieveStatus().FirstOrDefault(x => x.State != FileStatus.Unaltered && x.State != FileStatus.Ignored);
if (dirtyEntry != null)
throw new InvalidOperationException($"The git working directory must be clean, but '{dirtyEntry.FilePath}' is dirty.");
headSha = gitRepository.Head.Tip.Sha;
try
{
githubClient.Repository.Commit.GetSha1(githubOwner, githubRepo, headSha).GetAwaiter().GetResult();
}
catch (Octokit.NotFoundException exception)
{
throw new InvalidOperationException($"The current commit '{headSha}' must be pushed to GitHub.", exception);
}
GitLink(MakeAbsolute(Directory(".")).FullPath, new GitLinkSettings
{
RepositoryUrl = $"{githubRawUri}/{githubOwner}/{githubRepo}",
ArgumentCustomization = args => args.Append($"-ignore Bom,BomTest"),
});
}
else
{
Warning("Skipping source index.");
}
version = GetSemVerFromFile(GetFiles($"src/**/bin/**/{coverageAssemblies[0]}.dll").First().ToString());
});
Task("NuGetPackage")
.IsDependentOn("SourceIndex")
.Does(() =>
{
CreateDirectory("release");
foreach (var nuspecPath in GetFiles($"src/**/*.nuspec"))
{
NuGetPack(nuspecPath, new NuGetPackSettings
{
Version = version,
OutputDirectory = "release",
});
}
});
Task("NuGetPublish")
.IsDependentOn("NuGetPackage")
.WithCriteria(() => !string.IsNullOrEmpty(nugetApiKey) && !string.IsNullOrEmpty(githubApiKey))
.Does(() =>
{
foreach (var nupkgPath in GetFiles($"release/*.nupkg"))
{
NuGetPush(nupkgPath, new NuGetPushSettings
{
ApiKey = nugetApiKey,
Source = nugetSource,
});
}
if (headSha != null)
{
var tagName = $"nuget-{version}";
Information($"Creating git tag '{tagName}'...");
githubClient.Git.Reference.Create(githubOwner, githubRepo,
new Octokit.NewReference($"refs/tags/{tagName}", headSha)).GetAwaiter().GetResult();
}
else
{
Warning("Skipping git tag for prerelease.");
}
});
Task("Coverage")
.IsDependentOn("VerifyCodeGen")
.Does(() =>
{
CreateDirectory("release");
if (FileExists("release/coverage.xml"))
DeleteFile("release/coverage.xml");
string filter = string.Concat(coverageAssemblies.Select(x => $@" ""-filter:+[{x}]*"""));
foreach (var testDllPath in GetFiles($"tests/**/bin/**/*.UnitTests.dll"))
{
ExecuteProcess(@"cake\OpenCover\tools\OpenCover.Console.exe",
$@"-register:user -mergeoutput ""-target:cake\NUnit.ConsoleRunner\tools\nunit3-console.exe"" ""-targetargs:{testDllPath} --noresult"" ""-output:release\coverage.xml"" -skipautoprops -returntargetcode" + filter);
}
});
Task("CoverageReport")
.IsDependentOn("Coverage")
.Does(() =>
{
ExecuteProcess(@"cake\ReportGenerator\tools\ReportGenerator.exe", $@"""-reports:release\coverage.xml"" ""-targetdir:release\coverage""");
});
Task("CoveragePublish")
.IsDependentOn("Coverage")
.Does(() =>
{
ExecuteProcess(@"cake\coveralls.io\tools\coveralls.net.exe", $@"--opencover ""release\coverage.xml"" --full-sources --repo-token {coverallsApiKey}");
});
Task("Default")
.IsDependentOn("Test");
string GetSemVerFromFile(string path)
{
var versionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(path);
var semver = $"{versionInfo.FileMajorPart}.{versionInfo.FileMinorPart}.{versionInfo.FileBuildPart}";
if (prerelease.Length != 0)
semver += $"-{prerelease}";
return semver;
}
void CodeGen(bool verify)
{
ExecuteCodeGen(@"example\ExampleApi.fsd example\js --indent 2", verify);
ExecuteCodeGen(@"example\ExampleApi.fsd example\ts --typescript", verify);
}
void ExecuteCodeGen(string args, bool verify)
{
int exitCode = StartProcess($@"src\fsdgenjs\bin\{configuration}\fsdgenjs.exe", args + (verify ? " --verify" : ""));
if (exitCode == 1 && verify)
throw new InvalidOperationException("Generated code doesn't match; use -target=CodeGen to regenerate.");
else if (exitCode != 0)
throw new InvalidOperationException($"Code generation failed with exit code {exitCode}.");
}
void ExecuteProcess(string exePath, string arguments)
{
int exitCode = StartProcess(exePath, arguments);
if (exitCode != 0)
throw new InvalidOperationException($"{exePath} failed with exit code {exitCode}.");
}
RunTarget(target);