|
| 1 | +#addin "Cake.FileHelpers" |
| 2 | +#addin "Octokit" |
| 3 | +using Octokit; |
| 4 | + |
| 5 | +var configuration = Argument("configuration", "Release"); |
| 6 | +var isRunningOnUnix = IsRunningOnUnix(); |
| 7 | +var isRunningOnWindows = IsRunningOnWindows(); |
| 8 | +var isRunningOnAppVeyor = AppVeyor.IsRunningOnAppVeyor; |
| 9 | +var isPullRequest = AppVeyor.Environment.PullRequest.IsPullRequest; |
| 10 | +var buildNumber = AppVeyor.Environment.Build.Number; |
| 11 | +var releaseNotes = ParseReleaseNotes("./CHANGELOG.md"); |
| 12 | +var version = releaseNotes.Version.ToString(); |
| 13 | +var buildDir = Directory($"./src/{projectName}/bin") + Directory(configuration); |
| 14 | +var buildResultDir = Directory("./bin") + Directory(version); |
| 15 | +var nugetRoot = buildResultDir + Directory("nuget"); |
| 16 | + |
| 17 | +if (target == "PrePublish") |
| 18 | +{ |
| 19 | + version = $"{version}-alpha-{buildNumber}"; |
| 20 | +} |
| 21 | + |
| 22 | +if (!isRunningOnWindows) |
| 23 | +{ |
| 24 | + frameworks.Remove("net46"); |
| 25 | +} |
| 26 | + |
| 27 | +// Initialization |
| 28 | +// ---------------------------------------- |
| 29 | + |
| 30 | +Setup(_ => |
| 31 | +{ |
| 32 | + Information($"Building version {version} of {projectName}."); |
| 33 | + Information("For the publish target the following environment variables need to be set:"); |
| 34 | + Information("- NUGET_API_KEY"); |
| 35 | + Information("- GITHUB_API_TOKEN"); |
| 36 | +}); |
| 37 | + |
| 38 | +// Tasks |
| 39 | +// ---------------------------------------- |
| 40 | + |
| 41 | +Task("Clean") |
| 42 | + .Does(() => |
| 43 | + { |
| 44 | + CleanDirectories(new DirectoryPath[] { buildDir, buildResultDir, nugetRoot }); |
| 45 | + }); |
| 46 | + |
| 47 | +Task("Restore-Packages") |
| 48 | + .IsDependentOn("Clean") |
| 49 | + .Does(() => |
| 50 | + { |
| 51 | + NuGetRestore($"./src/{solutionName}.sln", new NuGetRestoreSettings |
| 52 | + { |
| 53 | + ToolPath = "tools/nuget.exe", |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | +Task("Build") |
| 58 | + .IsDependentOn("Restore-Packages") |
| 59 | + .Does(() => |
| 60 | + { |
| 61 | + ReplaceRegexInFiles("./src/Directory.Build.props", "(?<=<Version>)(.+?)(?=</Version>)", version); |
| 62 | + DotNetCoreBuild($"./src/{solutionName}.sln", new DotNetCoreBuildSettings |
| 63 | + { |
| 64 | + Configuration = configuration, |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | +Task("Run-Unit-Tests") |
| 69 | + .IsDependentOn("Build") |
| 70 | + .Does(() => |
| 71 | + { |
| 72 | + var settings = new DotNetCoreTestSettings |
| 73 | + { |
| 74 | + Configuration = configuration, |
| 75 | + }; |
| 76 | + |
| 77 | + if (isRunningOnAppVeyor) |
| 78 | + { |
| 79 | + settings.TestAdapterPath = Directory("."); |
| 80 | + settings.Logger = "Appveyor"; |
| 81 | + // TODO Finds a way to exclude tests not allowed to run on appveyor |
| 82 | + // Not used in current code |
| 83 | + //settings.Where = "cat != ExcludeFromAppVeyor"; |
| 84 | + } |
| 85 | + |
| 86 | + DotNetCoreTest($"./src/{solutionName}.Tests/", settings); |
| 87 | + }); |
| 88 | + |
| 89 | +Task("Copy-Files") |
| 90 | + .IsDependentOn("Build") |
| 91 | + .Does(() => |
| 92 | + { |
| 93 | + foreach (var item in frameworks) |
| 94 | + { |
| 95 | + var targetDir = nugetRoot + Directory("lib") + Directory(item.Key); |
| 96 | + CreateDirectory(targetDir); |
| 97 | + CopyFiles(new FilePath[] |
| 98 | + { |
| 99 | + buildDir + Directory(item.Value) + File($"{projectName}.dll"), |
| 100 | + buildDir + Directory(item.Value) + File($"{projectName}.xml"), |
| 101 | + }, targetDir); |
| 102 | + } |
| 103 | + |
| 104 | + CopyFiles(new FilePath[] { $"src/{projectName}.nuspec" }, nugetRoot); |
| 105 | + }); |
| 106 | + |
| 107 | +Task("Create-Package") |
| 108 | + .IsDependentOn("Copy-Files") |
| 109 | + .Does(() => |
| 110 | + { |
| 111 | + var nugetExe = GetFiles("./tools/**/nuget.exe").FirstOrDefault() |
| 112 | + ?? (isRunningOnAppVeyor ? GetFiles("C:\\Tools\\NuGet3\\nuget.exe").FirstOrDefault() : null) |
| 113 | + ?? throw new InvalidOperationException("Could not find nuget.exe."); |
| 114 | + |
| 115 | + var nuspec = nugetRoot + File($"{projectName}.nuspec"); |
| 116 | + |
| 117 | + NuGetPack(nuspec, new NuGetPackSettings |
| 118 | + { |
| 119 | + Version = version, |
| 120 | + OutputDirectory = nugetRoot, |
| 121 | + Symbols = false, |
| 122 | + Properties = new Dictionary<String, String> |
| 123 | + { |
| 124 | + { "Configuration", configuration }, |
| 125 | + }, |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | +Task("Publish-Package") |
| 130 | + .IsDependentOn("Create-Package") |
| 131 | + .IsDependentOn("Run-Unit-Tests") |
| 132 | + .Does(() => |
| 133 | + { |
| 134 | + var apiKey = EnvironmentVariable("NUGET_API_KEY"); |
| 135 | + |
| 136 | + if (String.IsNullOrEmpty(apiKey)) |
| 137 | + { |
| 138 | + throw new InvalidOperationException("Could not resolve the NuGet API key."); |
| 139 | + } |
| 140 | + |
| 141 | + foreach (var nupkg in GetFiles(nugetRoot.Path.FullPath + "/*.nupkg")) |
| 142 | + { |
| 143 | + NuGetPush(nupkg, new NuGetPushSettings |
| 144 | + { |
| 145 | + Source = "https://nuget.org/api/v2/package", |
| 146 | + ApiKey = apiKey, |
| 147 | + }); |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | +Task("Publish-Release") |
| 152 | + .IsDependentOn("Publish-Package") |
| 153 | + .IsDependentOn("Run-Unit-Tests") |
| 154 | + .Does(() => |
| 155 | + { |
| 156 | + var githubToken = EnvironmentVariable("GITHUB_API_TOKEN"); |
| 157 | + |
| 158 | + if (String.IsNullOrEmpty(githubToken)) |
| 159 | + { |
| 160 | + throw new InvalidOperationException("Could not resolve GitHub token."); |
| 161 | + } |
| 162 | + |
| 163 | + var github = new GitHubClient(new ProductHeaderValue("AngleSharpCakeBuild")) |
| 164 | + { |
| 165 | + Credentials = new Credentials(githubToken), |
| 166 | + }; |
| 167 | + |
| 168 | + var newRelease = github.Repository.Release; |
| 169 | + newRelease.Create("AngleSharp", projectName, new NewRelease("v" + version) |
| 170 | + { |
| 171 | + Name = version, |
| 172 | + Body = String.Join(Environment.NewLine, releaseNotes.Notes), |
| 173 | + Prerelease = false, |
| 174 | + TargetCommitish = "master", |
| 175 | + }).Wait(); |
| 176 | + }); |
| 177 | + |
| 178 | +Task("Update-AppVeyor-Build-Number") |
| 179 | + .WithCriteria(() => isRunningOnAppVeyor) |
| 180 | + .Does(() => |
| 181 | + { |
| 182 | + var num = AppVeyor.Environment.Build.Number; |
| 183 | + AppVeyor.UpdateBuildVersion($"{version}-{num}"); |
| 184 | + }); |
| 185 | + |
| 186 | +// Targets |
| 187 | +// ---------------------------------------- |
| 188 | + |
| 189 | +Task("Package") |
| 190 | + .IsDependentOn("Run-Unit-Tests") |
| 191 | + .IsDependentOn("Create-Package"); |
| 192 | + |
| 193 | +Task("Default") |
| 194 | + .IsDependentOn("Package"); |
| 195 | + |
| 196 | +Task("Publish") |
| 197 | + .IsDependentOn("Publish-Package") |
| 198 | + .IsDependentOn("Publish-Release"); |
| 199 | + |
| 200 | +Task("PrePublish") |
| 201 | + .IsDependentOn("Publish-Package"); |
| 202 | + |
| 203 | +Task("AppVeyor") |
| 204 | + .IsDependentOn("Run-Unit-Tests") |
| 205 | + .IsDependentOn("Update-AppVeyor-Build-Number"); |
0 commit comments