Skip to content

Commit

Permalink
Merge pull request #118 from jeremytammik/dev
Browse files Browse the repository at this point in the history
Release by GitAction
  • Loading branch information
Nice3point authored Nov 13, 2021
2 parents 885a95f + 5f3be14 commit ffcb4eb
Show file tree
Hide file tree
Showing 16 changed files with 333 additions and 1,654 deletions.
1,437 changes: 8 additions & 1,429 deletions .editorconfig

Large diffs are not rendered by default.

25 changes: 0 additions & 25 deletions .github/workflows/Compile.yml

This file was deleted.

29 changes: 0 additions & 29 deletions .github/workflows/CreatePackage.yml

This file was deleted.

19 changes: 19 additions & 0 deletions .github/workflows/NukePipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: NukePipeline.yml

on:
push:
branches:
- '**'
pull_request:
branches:
- '!master'

jobs:
windows-latest:
name: windows-latest
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Run Nuke Build
run: ./Build/Build.cmd --GitHubToken ${{ secrets.GITHUB_TOKEN }}
8 changes: 7 additions & 1 deletion Build/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# noinspection EditorConfigKeyCorrectness
[*.cs]
dotnet_style_qualification_for_field = false:warning
dotnet_style_qualification_for_property = false:warning
Expand All @@ -8,4 +9,9 @@ dotnet_style_require_accessibility_modifiers = never:warning
csharp_style_expression_bodied_methods = true:silent
csharp_style_expression_bodied_properties = true:warning
csharp_style_expression_bodied_indexers = true:warning
csharp_style_expression_bodied_accessors = true:warning
csharp_style_expression_bodied_accessors = true:warning

dotnet_naming_rule.private_instance_fields_rule.style = upper_camel_case_style

resharper_check_namespace_highlighting = none
resharper_class_never_instantiated_global_highlighting = none
147 changes: 147 additions & 0 deletions Build/Build.GitHubRelease.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Nuke.Common;
using Nuke.Common.Git;
using Nuke.Common.IO;
using Nuke.Common.Tools.GitHub;
using Nuke.Common.Tools.GitVersion;
using Octokit;

partial class Build
{
[GitVersion(NoFetch = true)] readonly GitVersion GitVersion;
readonly Regex VersionRegex = new(@"(\d+\.)+\d+", RegexOptions.Compiled);
[Parameter] string GitHubToken { get; set; }
AbsolutePath ChangeLogPath => RootDirectory / "Doc" / "Changelog.md";

Target PublishGitHubRelease => _ => _
.TriggeredBy(CreateInstaller)
.Requires(() => GitHubToken)
.Requires(() => GitRepository)
.Requires(() => GitVersion)
.OnlyWhenStatic(() => GitRepository.IsOnMasterBranch())
.OnlyWhenStatic(() => IsServerBuild)
.Executes(() =>
{
GitHubTasks.GitHubClient = new GitHubClient(new ProductHeaderValue(Solution.Name))
{
Credentials = new Credentials(GitHubToken)
};

var gitHubName = GitRepository.GetGitHubName();
var gitHubOwner = GitRepository.GetGitHubOwner();
var msiFiles = Directory.GetFiles(ArtifactsDirectory, "*.msi");
var version = GetMsiVersion(msiFiles);

CheckTags(gitHubOwner, gitHubName, version);
Logger.Normal($"Detected tag: {version}");

var newRelease = new NewRelease(version)
{
Name = version,
Body = CreateChangelog(version),
Draft = true,
TargetCommitish = GitVersion.Sha
};

var draft = CreatedDraft(gitHubOwner, gitHubName, newRelease);
UploadMsiFiles(draft, msiFiles);
ReleaseDraft(gitHubOwner, gitHubName, draft);
});

string CreateChangelog(string version)
{
if (!File.Exists(ChangeLogPath))
{
Logger.Warn($"Can't find changelog file: {ChangeLogPath}");
return string.Empty;
}

Logger.Normal($"Detected Changelog: {ChangeLogPath}");
var logBuilder = new StringBuilder();
foreach (var line in File.ReadLines(ChangeLogPath))
{
if (logBuilder.Length > 0)
{
if (line.StartsWith("-")) break;
logBuilder.AppendLine(line);
continue;
}

var match = VersionRegex.Match(line);
if (!match.Success) continue;
if (match.Value.Equals(version))
{
var truncatedLine = Regex.Replace(line, $"^.*{version} ?", string.Empty);
logBuilder.AppendLine(truncatedLine);
}
}

var log = logBuilder.ToString();
if (string.IsNullOrEmpty(log)) Logger.Warn($"There is no version entry in the changelog: {version}");
return log;
}

static void CheckTags(string gitHubOwner, string gitHubName, string version)
{
var gitHubTags = GitHubTasks.GitHubClient.Repository
.GetAllTags(gitHubOwner, gitHubName)
.Result;

if (gitHubTags.Select(tag => tag.Name).Contains(version)) throw new ArgumentException($"The repository already contains a Release with the tag: {version}");
}

string GetMsiVersion(IEnumerable<string> msiFiles)
{
var stringVersion = string.Empty;
var doubleVersion = 0d;
foreach (var msiFile in msiFiles)
{
var fileInfo = new FileInfo(msiFile);
var match = VersionRegex.Match(fileInfo.Name);
if (!match.Success) continue;
var version = match.Value;
var parsedValue = double.Parse(version.Replace(".", ""));
if (parsedValue > doubleVersion)
{
doubleVersion = parsedValue;
stringVersion = version;
}
}

if (stringVersion.Equals(string.Empty)) throw new ArgumentException("The version number of the MSI files was not found.");

return stringVersion;
}

static void UploadMsiFiles(Release createdRelease, IEnumerable<string> msiFiles)
{
foreach (var file in msiFiles)
{
var releaseAssetUpload = new ReleaseAssetUpload
{
ContentType = "application/x-binary",
FileName = Path.GetFileName(file),
RawData = File.OpenRead(file)
};
var _ = GitHubTasks.GitHubClient.Repository.Release.UploadAsset(createdRelease, releaseAssetUpload).Result;
Logger.Normal($"Added MSI file: {file}");
}
}

static Release CreatedDraft(string gitHubOwner, string gitHubName, NewRelease newRelease) =>
GitHubTasks.GitHubClient.Repository.Release
.Create(gitHubOwner, gitHubName, newRelease)
.Result;

static void ReleaseDraft(string gitHubOwner, string gitHubName, Release draft)
{
var _ = GitHubTasks.GitHubClient.Repository.Release
.Edit(gitHubOwner, gitHubName, draft.Id, new ReleaseUpdate {Draft = false})
.Result;
}
}
3 changes: 2 additions & 1 deletion Build/Build.Installer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Nuke.Common;
using Nuke.Common.Git;

partial class Build
{
Target CreateInstaller => _ => _
.TriggeredBy(Compile)
.Produces(ArtifactsDirectory / "*.msi")
.OnlyWhenStatic(() => IsLocalBuild || GitRepository.IsOnMasterBranch())
.Executes(() =>
{
var installerProject = BuilderExtensions.GetProject(Solution, InstallerProject);
Expand Down
6 changes: 4 additions & 2 deletions Build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using Nuke.Common;
using Nuke.Common.Git;
using Nuke.Common.IO;
using Nuke.Common.ProjectModel;
using Nuke.Common.Tooling;
Expand All @@ -11,12 +12,13 @@
using static Nuke.Common.Tools.MSBuild.MSBuildTasks;

/// <summary>
/// Documentation:
/// https://github.com/Nice3point/RevitTemplates/wiki
/// Documentation:
/// https://github.com/Nice3point/RevitTemplates/wiki
/// </summary>
partial class Build : NukeBuild
{
readonly AbsolutePath ArtifactsDirectory = RootDirectory / ArtifactsFolder;
[GitRepository] readonly GitRepository GitRepository;

[Solution] public readonly Solution Solution;
string ProcessToolPath;
Expand Down
7 changes: 5 additions & 2 deletions Build/Build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
<Platforms>AnyCPU</Platforms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Nuke.Common" Version="5.3.0" />
<PackageReference Include="vswhere" Version="2.8.4" />
<PackageReference Include="Nuke.Common" Version="5.3.0"/>
<PackageReference Include="vswhere" Version="2.8.4"/>
</ItemGroup>
<ItemGroup>
<PackageDownload Include="GitVersion.Tool" Version="[5.7.0]"/>
</ItemGroup>
</Project>
28 changes: 0 additions & 28 deletions Build/Build.csproj.DotSettings

This file was deleted.

Loading

0 comments on commit ffcb4eb

Please sign in to comment.