diff --git a/build.cake b/build.cake index 8098515ede53342ae097414b49ae98dcd58f7beb..6181f2be1f08ebcc1f8947fe0d4f00714df46520 100644 --- a/build.cake +++ b/build.cake @@ -2,6 +2,8 @@ #tool nuget:?package=vswhere&version=2.8.4 #tool nuget:?package=GitVersion.CommandLine&version=5.1.3 +#addin nuget:https://api.nuget.org/v3/index.json?package=Cake.Json&version=4.0.0 +#addin nuget:https://api.nuget.org/v3/index.json?package=Newtonsoft.Json&version=11.0.2 #addin nuget:https://api.nuget.org/v3/index.json?package=Cake.FileHelpers&version=3.2.1 using System.Net; @@ -32,11 +34,11 @@ var msBuildPathX64 = (vsLatest == null) ? null : vsLatest.CombineWithFilePath(". Setup(context =>{ nupkgDir = $"{artifactsDir.ToString()}/nupkg"; - Information($"Started at {DateTime.Now}"); + Information("Started at {0}", DateTime.Now); }); Teardown(context =>{ - Information($"Finished at {DateTime.Now}"); + Information("Finished at {0}", DateTime.Now); }); Task("Clean") @@ -57,7 +59,7 @@ Task("Clean") directoriesToClean.Add(artifactsDir); foreach(var dir in directoriesToClean) { - Information($"Cleaning {dir.ToString()}"); + Information("Cleaning {0}", dir.ToString()); if (DirectoryExists(dir)) { DeleteDirectory(dir, settings); CreateDirectory(dir); @@ -99,7 +101,7 @@ Task("GitVersion") } var index = version.IndexOf("-"); semanticVersion = index > 0 ? version.Substring(0, index) : version; - Information($"Version: {version}, SemanticVersion: {semanticVersion}"); + Information("Version: {0}, SemanticVersion: {1}", version, semanticVersion); }); Task("UpdateAssemblyInfo") @@ -124,71 +126,57 @@ Task("UpdateAssemblyInfo") Task("GitlabRelease") .IsDependentOn("GitVersion") .Does(() => { -IEnumerable<string> redirectedStandardOutput; - var exitCodeWithArgument = - StartProcess( - "git", - new ProcessSettings { - Arguments = "describe --tags --abbrev=0", - RedirectStandardOutput = true - }, - out redirectedStandardOutput - ); + var client = new HttpClient(); + client.DefaultRequestHeaders.Add("PRIVATE-TOKEN", gitlabToken); + + // get the latest tag + var result = client.GetAsync($"https://git.rwth-aachen.de/api/v4/projects/{gitlabProjectId}/repository/tags").Result; + if(!result.IsSuccessStatusCode) { + throw new Exception("Tag query failed."); + } - var lastTag = redirectedStandardOutput.LastOrDefault(); - var logParam = ""; - if(exitCodeWithArgument == 0) { - logParam = $"{lastTag}..Head"; + var tagList = result.Content.ReadAsStringAsync().Result; + var jArray = JArray.Parse(tagList); + // null if not tags exists yet + var lastTag = jArray.Select(x => x["name"]).FirstOrDefault(); + + var url = $"https://git.rwth-aachen.de/{gitlabProjectPath}"; + + if(url.EndsWith(".git")) { + url = url.Substring(0, url.Length - ".git".Length); + } + + if(url.EndsWith("/")) { + url = url.Substring(0, url.Length - 1); + } + + var description = ""; + // First line of description + // Gitlab compare url, if something can be compared + if(lastTag == null) { + description = $"# {semanticVersion} ({DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day})\n\n\n"; } else { - lastTag = null; + description = $"# [{semanticVersion}]({url}/compare/{lastTag}...v{semanticVersion}) ({DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day})\n\n\n"; } - string url = null; + // From when will messages be parsed, null results in all messages + var logParam = ""; if(lastTag != null) { - if(String.IsNullOrWhiteSpace(gitlabProjectPath)) { - exitCodeWithArgument = - StartProcess( - "git", - new ProcessSettings { - Arguments = $"config --get remote.origin.url", - RedirectStandardOutput = true - }, - out redirectedStandardOutput - ); - - url = redirectedStandardOutput.LastOrDefault(); - if(url.StartsWith("git@git.rwth-aachen.de:")){ - url = url.Replace("git@git.rwth-aachen.de:", "https://git.rwth-aachen.de/"); - } - } else { - url = $"https://git.rwth-aachen.de/{gitlabProjectPath}"; - } - - if(url.EndsWith(".git")) { - url = url.Substring(0, url.Length - ".git".Length); - } - if(url.EndsWith("/")) { - url = url.Substring(0, url.Length - 1); - } - - url = $"{url}/compare/{lastTag}...v{semanticVersion}"; + logParam = $"{lastTag}..Head"; } - exitCodeWithArgument = + Information(lastTag); + + IEnumerable<string> redirectedStandardOutput; + var exitCodeWithArgument = StartProcess( "git", new ProcessSettings { - Arguments = $"log {logParam} --oneline", + Arguments = $"log {logParam} --pretty=format:HASH%h:%B", RedirectStandardOutput = true }, out redirectedStandardOutput ); - var description = ""; - if(lastTag == null) { - description = $"# {semanticVersion} ({DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day})\n\n\n"; - } else { - description = $"# [{semanticVersion}]({url}) ({DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day})\n\n\n"; - } var prefixList = new Dictionary<string, List<string>>{ {"Fix", new List<string>()}, @@ -201,14 +189,22 @@ IEnumerable<string> redirectedStandardOutput; {"Chore", new List<string>()}, }; + var currentHash = ""; // Output last line of process output. foreach(var line in redirectedStandardOutput) { - var index = line.IndexOf(" "); - var commitHash = line.Substring(0, index); - var commitMessage = line.Substring(index + 1); + var commitMessage = ""; + if(line.StartsWith("HASH")) { + currentHash = line.Substring("HASH".Length); + currentHash = currentHash.Substring(0, currentHash.IndexOf(":")); + commitMessage = line.Substring(currentHash.Length + line.IndexOf(currentHash) + 1); + } else { + commitMessage = line; + } + foreach(var kv in prefixList) { if(commitMessage.StartsWith($"{kv.Key}:")) { - kv.Value.Add($"* {commitMessage.Substring(kv.Key.Length + 1).Trim()} {commitHash}"); + kv.Value.Add($"* {commitMessage.Substring(kv.Key.Length + 1).Trim()} {currentHash}"); + break; } }; } @@ -222,19 +218,27 @@ IEnumerable<string> redirectedStandardOutput; description += "\n"; } } + // correctly escape the json newlines + description = description.Replace("\n", "\\n"); + Information("Description: {0}", description); + throw new Exception(); // create tag - var client = new HttpClient(); client.DefaultRequestHeaders.Add("PRIVATE-TOKEN", gitlabToken); - var result = client.PostAsync($"https://git.rwth-aachen.de/api/v4/projects/{gitlabProjectId}/repository/tags?tag_name=v{semanticVersion}&ref=master", null).Result; + result = client.PostAsync($"https://git.rwth-aachen.de/api/v4/projects/{gitlabProjectId}/repository/tags?tag_name=v{semanticVersion}&ref=master", null).Result; Information("Create tag: {0}", result.Content.ReadAsStringAsync().Result); + if(!result.IsSuccessStatusCode) { + throw new Exception("Tag creation failed."); + } - description = description.Replace("\n", "\\n"); // create release var json = $"{{\"name\": \"v{semanticVersion}\", \"tag_name\": \"v{semanticVersion}\", \"description\": \"{description}\"}}"; var content = new StringContent(json, Encoding.UTF8, "application/json"); result = client.PostAsync($"https://git.rwth-aachen.de/api/v4/projects/{gitlabProjectId}/releases", content).Result; Information("Create release: {0}", result.Content.ReadAsStringAsync().Result); + if(!result.IsSuccessStatusCode) { + throw new Exception("Release creation failed."); + } }); Task("Build") @@ -256,7 +260,7 @@ Task("Build") } // Use MSBuild - Information($"Building {solutionFile}"); + Information("Building {0}", solutionFile); MSBuild(solutionFile, frameworkSettingsWindows); }); @@ -307,7 +311,7 @@ Task("CopyToArtifacts") && !FileExists($"{project.GetDirectory()}/{project.GetFilenameWithoutExtension()}.nuspec") && DirectoryExists(project.GetDirectory())) { - Information($"Copying {project.GetDirectory()}/bin/{configuration}/* to {artifactsDir}"); + Information("Copying {0}/* to {1}", $"{project.GetDirectory()}/bin/{configuration}", artifactsDir); CopyDirectory($"{project.GetDirectory()}/bin/{configuration}/", artifactsDir); } }