forked from rabwent11/storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.cake
284 lines (246 loc) · 10.1 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System.IO.Compression;
using System.Runtime.InteropServices;
var target = Argument<string>("Target", "Default");
var configuration = Argument<string>("Configuration", "Release");
bool publishWithoutBuild = Argument<bool>("PublishWithoutBuild", false);
string nugetPrereleaseTextPart = Argument<string>("PrereleaseText", "alpha");
string targetFramework = Argument<string>("TargetFramework", "net461");
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
Information("OS Description: " + System.Runtime.InteropServices.RuntimeInformation.OSDescription);
string pythonCommand = isWindows ? "python" : "python3";
string shellCommand = isWindows ? "cmd" : "bash";
var artifactsDirectory = Directory("./artifacts");
var testResultDir = "./temp/";
string vsBuildOutputDirectory = System.IO.Path.Combine(".", "src", "Cmdty.Storage.Excel", "bin", configuration, targetFramework);
var isRunningOnBuildServer = !BuildSystem.IsLocalBuild;
var msBuildSettings = new DotNetCoreMSBuildSettings();
// Maps text used in prerelease part in NuGet package to PyPI package
var prereleaseVersionTextMapping = new Dictionary<string, string>
{
{"alpha", "a"},
{"beta", "b"},
{"rc", "rc"}
};
string pythonPrereleaseTextPart = prereleaseVersionTextMapping[nugetPrereleaseTextPart];
msBuildSettings.WithProperty("PythonPreReleaseTextPart", pythonPrereleaseTextPart);
if (HasArgument("PrereleaseNumber"))
{
msBuildSettings.WithProperty("PrereleaseNumber", Argument<string>("PrereleaseNumber"));
msBuildSettings.WithProperty("VersionSuffix", nugetPrereleaseTextPart + Argument<string>("PrereleaseNumber"));
}
if (HasArgument("VersionPrefix"))
{
msBuildSettings.WithProperty("VersionPrefix", Argument<string>("VersionPrefix"));
}
if (HasArgument("PythonVersion"))
{
msBuildSettings.WithProperty("PythonVersion", Argument<string>("PythonVersion"));
}
Task("Clean-Artifacts")
.Does(() =>
{
CleanDirectory(artifactsDirectory);
});
Task("Build")
.IsDependentOn("Clean-Artifacts") // Necessary as msbuild tasks in Cmdty.Storage.Excel.csproj copy the add-ins into the artifacts directory
.Does(() =>
{
var dotNetCoreSettings = new DotNetCoreBuildSettings()
{
Configuration = configuration,
MSBuildSettings = msBuildSettings
};
string solutionName = isWindows ? "Cmdty.Storage.sln" : "Cmdty.Storage.XPlat.sln";
DotNetCoreBuild(solutionName, dotNetCoreSettings);
});
Task("Test-C#")
.IsDependentOn("Build")
.Does(() =>
{
Information("Cleaning test output directory");
CleanDirectory(testResultDir);
var projects = GetFiles("./tests/**/*.Test.csproj");
foreach(var project in projects)
{
Information("Testing project " + project);
DotNetCoreTest(
project.ToString(),
new DotNetCoreTestSettings()
{
ArgumentCustomization = args=>args.Append($"/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura"),
Logger = "trx",
ResultsDirectory = testResultDir,
Configuration = configuration,
NoBuild = true
});
}
});
string vEnvPath = System.IO.Path.Combine("src", "Cmdty.Storage.Python", "storage-venv");
string venvActiveFolderAndFile = isWindows ? System.IO.Path.Combine("Scripts", "activate.bat") :
System.IO.Path.Combine("bin", "activate");
string vEnvActivatePath = System.IO.Path.Combine(vEnvPath, venvActiveFolderAndFile);
Task("Create-VirtualEnv")
.Does(() =>
{
if (System.IO.File.Exists(vEnvActivatePath))
{
Information("storage-venv Virtual Environment already exists, so no need to create.");
}
else
{
Information("Creating storage-venv Virtual Environment.");
StartProcessThrowOnError(pythonCommand, "-m venv " + vEnvPath);
}
});
Task("Install-VirtualEnvDependencies")
.IsDependentOn("Create-VirtualEnv")
.Does(() =>
{
RunCommandInVirtualEnv(pythonCommand + " -m pip install --upgrade pip", vEnvActivatePath);
RunCommandInVirtualEnv("pip install pytest", vEnvActivatePath);
RunCommandInVirtualEnv("pip install -r src/Cmdty.Storage.Python/requirements.txt", vEnvActivatePath);
RunCommandInVirtualEnv("pip install -e src/Cmdty.Storage.Python", vEnvActivatePath);
});
var testPythonTask = Task("Test-Python")
.IsDependentOn("Install-VirtualEnvDependencies")
.IsDependentOn("Test-C#")
.Does(() =>
{
RunCommandInVirtualEnv(pythonCommand + " -m pytest src/Cmdty.Storage.Python/tests --junitxml=junit/test-results.xml", vEnvActivatePath);
});
Task("Build-Samples")
.Does(() =>
{
var dotNetCoreSettings = new DotNetCoreBuildSettings()
{
Configuration = configuration,
};
DotNetCoreBuild("samples/csharp/Cmdty.Storage.Samples.sln", dotNetCoreSettings);
});
Task("Pack-NuGet")
.IsDependentOn("Build-Samples")
.IsDependentOn("Test-C#")
.Does(() =>
{
var dotNetPackSettings = new DotNetCorePackSettings()
{
Configuration = configuration,
OutputDirectory = artifactsDirectory,
NoRestore = true,
NoBuild = true,
MSBuildSettings = msBuildSettings
};
DotNetCorePack("src/Cmdty.Storage/Cmdty.Storage.csproj", dotNetPackSettings);
});
Task("Pack-Python")
.IsDependentOn("Test-Python")
.IsDependentOn("Build")
.Does(setupContext =>
{
CleanDirectory("src/Cmdty.Storage.Python/build");
CleanDirectory("src/Cmdty.Storage.Python/dist");
var originalWorkingDir = setupContext.Environment.WorkingDirectory;
string pythonProjDir = System.IO.Path.Combine(originalWorkingDir.ToString(), "src", "Cmdty.Storage.Python");
setupContext.Environment.WorkingDirectory = pythonProjDir;
try
{
StartProcessThrowOnError(pythonCommand, "setup.py", "bdist_wheel");
}
finally
{
setupContext.Environment.WorkingDirectory = originalWorkingDir;
}
Information("Python package created");
CopyFiles("src/Cmdty.Storage.Python/dist/*", artifactsDirectory);
Information("Python package file copied to /artifacts directory");
});
Task("Pack-Excel")
.IsDependentOn("Test-C#")
.Does(setupContext =>
{
Information("Creating x86 Excel add-in zip file.");
CreateAddInZipFile("x86", artifactsDirectory.ToString(), vsBuildOutputDirectory);
Information("x86 Excel add-in zip file has been created.");
Information("Creating x64 Excel add-in zip file.");
CreateAddInZipFile("x64", artifactsDirectory.ToString(), vsBuildOutputDirectory);
Information("x64 Excel add-in zip file has been created.");
});
private void CreateAddInZipFile(string platform, string artifactsDirectory, string vsBuildOutputDirectory)
{
string folderToZip = System.IO.Path.Combine(vsBuildOutputDirectory, platform);
CopyFiles(System.IO.Path.Combine(vsBuildOutputDirectory, "*.dll"), folderToZip);
CopyFiles(System.IO.Path.Combine(vsBuildOutputDirectory, "*.pdb"), folderToZip);
string addInName = "Cmdty.Storage-" + platform;
CopyFiles(System.IO.Path.Combine(vsBuildOutputDirectory, addInName + ".xll"), folderToZip);
CopyFiles(System.IO.Path.Combine(vsBuildOutputDirectory, addInName + ".dna"), folderToZip);
string addInZipFilePath = System.IO.Path.Combine(artifactsDirectory, addInName + ".zip");
ZipFile.CreateFromDirectory(folderToZip, addInZipFilePath);
}
private string GetEnvironmentVariable(string envVariableName)
{
string envVariableValue = EnvironmentVariable(envVariableName);
if (string.IsNullOrEmpty(envVariableValue))
throw new ApplicationException($"Environment variable '{envVariableName}' has not been set.");
return envVariableValue;
}
private void StartProcessThrowOnError(string applicationName, params string[] processArgs)
{
var argsBuilder = new ProcessArgumentBuilder();
foreach(string processArg in processArgs)
{
argsBuilder.Append(processArg);
}
int exitCode = StartProcess(applicationName, new ProcessSettings {Arguments = argsBuilder});
if (exitCode != 0)
throw new ApplicationException($"Starting {applicationName} in new process returned non-zero exit code of {exitCode}");
}
private void RunCommandInVirtualEnv(string command, string vEnvActivatePath)
{
Information("Running command in venv: " + command);
string fullCommand = isWindows ? $"/k {vEnvActivatePath} & {command} & deactivate & exit" :
$"-c {vEnvActivatePath} && {command} && deactivate && exit";
Information($"Command to execute: {shellCommand} " + fullCommand);
StartProcessThrowOnError(shellCommand, fullCommand);
}
var publishNuGetTask = Task("Publish-NuGet")
.Does(() =>
{
string nugetApiKey = GetEnvironmentVariable("NUGET_API_KEY");
var nupkgPath = GetFiles(artifactsDirectory.ToString() + "/*.nupkg").Single();
NuGetPush(nupkgPath, new NuGetPushSettings
{
ApiKey = nugetApiKey,
Source = "https://api.nuget.org/v3/index.json"
});
});
var publishTestPyPiTask = Task("Publish-TestPyPI")
.Does(() =>
{
string testPyPiPassword = GetEnvironmentVariable("TEST_PYPI_PASSWORD");
StartProcessThrowOnError(pythonCommand, "-m twine upload --repository-url https://test.pypi.org/legacy/ src/Cmdty.Storage.Python/dist/*",
"--username fowja", "--password " + testPyPiPassword);
});
var publishPyPiTask = Task("Publish-PyPI")
.Does(() =>
{
string pyPiPassword = GetEnvironmentVariable("PYPI_PASSWORD");
StartProcessThrowOnError(pythonCommand, "-m twine upload src/Cmdty.Storage.Python/dist/*",
"--username __token__", "--password " + pyPiPassword);
});
if (!publishWithoutBuild)
{
publishTestPyPiTask.IsDependentOn("Pack-Python");
publishPyPiTask.IsDependentOn("Pack-Python");
publishNuGetTask.IsDependentOn("Pack-NuGet");
}
else
{
Information("Publishing without first building as PublishWithoutBuild variable set to true.");
}
var defaultTask = Task("Default");
defaultTask
.IsDependentOn("Pack-NuGet")
.IsDependentOn("Pack-Python");
if (isWindows)
defaultTask.IsDependentOn("Pack-Excel");
RunTarget(target);