forked from guudan/Vs-AddMultipleProjectsToSolution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
217 lines (179 loc) · 7.9 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
#tool "nuget:?package=xunit.runner.console&version=2.4.1"
#load "deployment_scripts/deploy_common.cake"
var target = Argument("target", "Default");
public static class EnvironmentVariableNames
{
public const string BuildNumber = "BUILD_BUILDNUMBER";
}
public static class BuildConsts
{
public const string RootDir = "./source";
public static readonly string SolutionFile = $"{RootDir}/Vs.AddMultipleProjectsToSolution.sln";
public const string VsPackageProjectName = "Vs.AddMultipleProjectsToSolution";
public const string VsixManifestFileName = "source.extension.vsixmanifest";
public static string VsixManifestFilePath = $"{RootDir}/{VsPackageProjectName}/{VsixManifestFileName}";
public static string VsPackageAssemblyInfoPath = $"{RootDir}/{VsPackageProjectName}/Properties/AssemblyInfo.cs";
public static string VsixFileName => $"{VsPackageProjectName}.vsix";
public const string VsTestProjectName = "Vs.AddMultipleProjectsToSolution.Tests";
public static string VsTestDllFileName = $"{VsTestProjectName}.dll";
public const string BuildOutputDir = "bin";
public const string DeploymentScriptPath = "deployment_scripts/deploy.cake";
}
public class BuildInfo
{
public int BuildNumber {get;set;}
public string Configuration {get; set;}
public string VsixOutputPath =>
$"{BuildConsts.RootDir}/{BuildConsts.VsPackageProjectName}/bin/{Configuration}/{BuildConsts.VsixFileName}";
public string TestProjectOutputDirPath =>
$"{BuildConsts.RootDir}/{BuildConsts.VsTestProjectName}/bin/{Configuration}";
public string TestProjectDllOutputPath =>
$"{TestProjectOutputDirPath}/{BuildConsts.VsTestDllFileName}";
}
public static void ExecuteDeploymentScript(this ICakeContext context)
{
context.Information("Calling deployment script");
var args = new CakeSettings();
var extensionDir = new DirectoryPath($"{BuildConsts.BuildOutputDir}/Extension");
args.Arguments[DeploymentArgs.DeploymentPackagePath] = context.MakeAbsolute(extensionDir).FullPath;
ReadArgumentsIntoDictionary(context, args.Arguments, DeploymentArgs.ArgumentList);
context.CakeExecuteScript(BuildConsts.DeploymentScriptPath, args);
}
public static void ReadArgumentsIntoDictionary(this ICakeContext context, IDictionary<string, string> arguments, string[] argumentNames)
{
foreach(var arg in argumentNames)
{
if(context.HasArgument(arg))
{
var value = context.Argument<string>(arg);
arguments.Add(arg,value);
}
}
}
Setup<BuildInfo>(ctx =>
{
Information("Running tasks...");
var buildConfiguration = new BuildInfo();
buildConfiguration.Configuration = Argument("configuration", "Release");
var buildNumberString = EnvironmentVariable(EnvironmentVariableNames.BuildNumber);
Verbose($"Parsing build number \"{buildNumberString}\" from environment variable {EnvironmentVariableNames.BuildNumber}.");
buildConfiguration.BuildNumber = string.IsNullOrWhiteSpace(buildNumberString) ? 0 : int.Parse(buildNumberString);
Information($"Build number: {buildConfiguration.BuildNumber}");
return buildConfiguration;
});
Teardown(ctx =>
{
Information("Finished running tasks.");
});
Task("Clean")
.Does(()=>{
CleanDirectory(BuildConsts.BuildOutputDir);
});
Task("UpdatePackageVersion")
.Does<BuildInfo>(config=>{
var versionXPath = "/x:PackageManifest/x:Metadata/x:Identity/@Version";
var vsixNamespace = new KeyValuePair<string, string>("x", "http://schemas.microsoft.com/developer/vsx-schema/2011");
Verbose($"Reading version of the VSIX package from file {BuildConsts.VsixManifestFilePath}.");
var peekSettings = new XmlPeekSettings();
peekSettings.Namespaces.Add(vsixNamespace);
var currentVersionString = XmlPeek(BuildConsts.VsixManifestFilePath, versionXPath, peekSettings);
var currentVersion = Version.Parse(currentVersionString);
Information($"Original version of the VSIX = {currentVersionString}");
var newVersion = new Version(currentVersion.Major, currentVersion.Minor, config.BuildNumber);
Information($"New version of the VSIX = {newVersion}");
Verbose($"Setting version of the VSIX package from file {BuildConsts.VsixManifestFilePath} to {newVersion}.");
var pokeSettings = new XmlPokeSettings();
pokeSettings.Namespaces.Add(vsixNamespace);
XmlPoke(BuildConsts.VsixManifestFilePath, versionXPath, newVersion.ToString(), pokeSettings);
Verbose($"Reading assembly info from ${BuildConsts.VsPackageAssemblyInfoPath} file.");
var currentAssemblyInfo = ParseAssemblyInfo(BuildConsts.VsPackageAssemblyInfoPath);
Verbose($"Setting assembly info in ${BuildConsts.VsPackageAssemblyInfoPath} file.");
var newAssemblyInfo = new AssemblyInfoSettings();
newAssemblyInfo.Title = currentAssemblyInfo.Title;
newAssemblyInfo.Description = currentAssemblyInfo.Description;
newAssemblyInfo.FileVersion = newVersion.ToString();
newAssemblyInfo.Version = newVersion.ToString();
CreateAssemblyInfo(BuildConsts.VsPackageAssemblyInfoPath, newAssemblyInfo);
});
Task("RestoreNuget")
.Does(()=>{
NuGetRestore(BuildConsts.SolutionFile);
});
Task("Build")
.IsDependentOn("Clean")
.IsDependentOn("RestoreNuget")
.IsDependentOn("UpdatePackageVersion")
.Does<BuildInfo>(config=>{
MSBuild(BuildConsts.SolutionFile, msBuildSettings =>{
msBuildSettings.SetConfiguration(config.Configuration);
msBuildSettings.ToolVersion = MSBuildToolVersion.VS2017;
msBuildSettings.SetMSBuildPlatform(MSBuildPlatform.x86);
});
});
Task("Test")
.IsDependentOn("Build")
.Does<BuildInfo>(config=>{
var testSettings = new XUnit2Settings();
testSettings.UseX86 = true;
testSettings.ReportName = "TestResults";
testSettings.XmlReport = true;
testSettings.OutputDirectory = config.TestProjectOutputDirPath;
XUnit2(config.TestProjectDllOutputPath, testSettings);
});
Task("PublishTestResults")
.WithCriteria(TFBuild.IsRunningOnVSTS)
.IsDependentOn("Test")
.Does<BuildInfo>(config=>{
var publishData = new TFBuildPublishTestResultsData();
publishData.Configuration = config.Configuration;
publishData.TestResultsFiles.Add(new FilePath($"{config.TestProjectOutputDirPath}/TestResults.xml"));
publishData.TestRunTitle = "Unit Tests";
publishData.TestRunner = TFTestRunnerType.XUnit;
TFBuild.Commands.PublishTestResults(publishData);
});
Task("CopyBuildOutput")
.IsDependentOn("Build")
.Does<BuildInfo>(config=>{
var extensionDir = $"{BuildConsts.BuildOutputDir}/Extension";
CreateDirectory(extensionDir);
CopyFileToDirectory(config.VsixOutputPath, extensionDir);
CopyFiles(new []{"README.md", "CHANGELOG.md", "CHANGELOG.md", "LICENSE" }, extensionDir);
CopyDirectory("docs", $"{extensionDir}/docs");
});
Task("CopyDeploymentScripts")
.Does<BuildInfo>(config =>{
CopyDirectory("deployment_scripts", BuildConsts.BuildOutputDir);
});
Task("CopyFiles")
.IsDependentOn("CopyBuildOutput")
.IsDependentOn("CopyDeploymentScripts");
Task("PublishBuildArtifacts")
.WithCriteria(TFBuild.IsRunningOnVSTS)
.IsDependentOn("CopyFiles")
.Does<BuildInfo>(config=>{
TFBuild.Commands.UploadArtifactDirectory(BuildConsts.BuildOutputDir, "BuildResult");
});
Task("PublishToVsixGallery")
//.IsDependentOn("CopyFiles")
.Does<BuildInfo>(ctx => {
Context.ExecuteDeploymentScript();
});
Task("PublishToGithub")
//.IsDependentOn("CopyFiles")
.Does<BuildInfo>(ctx =>{
Context.ExecuteDeploymentScript();
});
Task("PublishToVsMarketplace")
//.IsDependentOn("CopyFiles")
.Does<BuildInfo>(ctx =>{
Context.ExecuteDeploymentScript();
});
Task("Default")
.IsDependentOn("Build")
.IsDependentOn("Test")
.IsDependentOn("PublishTestResults")
.IsDependentOn("CopyFiles")
.IsDependentOn("PublishBuildArtifacts")
.Does(() => {
});
RunTarget(target);