Skip to content

Commit

Permalink
vc-build 1.3.0 (#1941)
Browse files Browse the repository at this point in the history
* 1.3.0

* Added QuickRelease Target, added branch deletion
  • Loading branch information
krankenbro committed Jul 22, 2020
1 parent 23e3219 commit 681d603
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 45 deletions.
215 changes: 171 additions & 44 deletions build/Build.cs
Expand Up @@ -17,6 +17,7 @@
using Nuke.Common.IO;
using Nuke.Common.ProjectModel;
using Nuke.Common.Tooling;
using Nuke.Common.Tools.CloudFoundry;
using Nuke.Common.Tools.Coverlet;
using Nuke.Common.Tools.DotNet;
using Nuke.Common.Tools.Git;
Expand Down Expand Up @@ -47,11 +48,11 @@ public Build()
public static int Main()
{
var nukeFile = Directory.GetFiles(Directory.GetCurrentDirectory(), ".nuke");
if(!nukeFile.Any())
if (!nukeFile.Any())
{
Logger.Info("No .nuke file found!");
var solutions = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sln");
if(solutions.Length == 1)
if (solutions.Length == 1)
{
var solutionFileName = Path.GetFileName(solutions.First());
Logger.Info($"Solution found: {solutionFileName}");
Expand Down Expand Up @@ -113,8 +114,11 @@ public static int Main()

[Parameter("Path to Release Notes File")] readonly AbsolutePath ReleaseNotes;

[Parameter("VersionTag for module.manifest")] readonly string CustomVersionSuffix;

[Parameter("VersionTag for module.manifest and Directory.Build.Props")] string CustomVersionPrefix;
[Parameter("VersionSuffix for module.manifest and Directory.Build.Props")] string CustomVersionSuffix;

[Parameter("Release branch")] readonly string ReleaseBranch;

AbsolutePath SourceDirectory => RootDirectory / "src";
AbsolutePath TestsDirectory => RootDirectory / "tests";
[Parameter("Path to Artifacts Directory")] AbsolutePath ArtifactsDirectory = RootDirectory / "artifacts";
Expand All @@ -132,7 +136,7 @@ public static int Main()
AbsolutePath ModuleOutputDirectory => ArtifactsDirectory / ModuleManifest.Id;

AbsolutePath DirectoryBuildPropsPath => Solution.Directory / "Directory.Build.props";

string ZipFileName => IsModule ? $"{ModuleManifest.Id}_{ReleaseVersion}.zip" : $"{WebProject.Solution.Name}.{ReleaseVersion}.zip";
string ZipFilePath => ArtifactsDirectory / ZipFileName;
string GitRepositoryName => GitRepository.Identifier.Split('/')[1];
Expand All @@ -156,21 +160,21 @@ void SonarLogger(OutputType type, string text)
}
}

Target Clean => _ => _
.Before(Restore)
.Executes(() =>
{
SourceDirectory.GlobDirectories("**/bin", "**/obj").ForEach(DeleteDirectory);
if (DirectoryExists(TestsDirectory))
{
TestsDirectory.GlobDirectories("**/bin", "**/obj").ForEach(DeleteDirectory);
}
Target Clean => _ => _
.Before(Restore)
.Executes(() =>
{
SourceDirectory.GlobDirectories("**/bin", "**/obj").ForEach(DeleteDirectory);
if (DirectoryExists(TestsDirectory))
{
TestsDirectory.GlobDirectories("**/bin", "**/obj").ForEach(DeleteDirectory);
}
//if (DirectoryExists(TestsDirectory))
//{
// WebProject.Directory.GlobDirectories("**/node_modules").ForEach(DeleteDirectory);
//}
EnsureCleanDirectory(ArtifactsDirectory);
});
});

Target Restore => _ => _
.Executes(() =>
Expand Down Expand Up @@ -231,7 +235,7 @@ void SonarLogger(OutputType type, string text)
.SetResultsDirectory(OutPath);
var testProjectBinDir = testProjects.First().Directory / "bin" / Configuration;
var testAssemblies = testProjectBinDir.GlobFiles($"**/{Solution.Name}.Tests.dll");
if(testAssemblies.Count() > 0)
if (testAssemblies.Count() > 0)
{
var testAssemblyPath = testAssemblies.First();
Expand Down Expand Up @@ -280,41 +284,163 @@ public class Utf8StringWriter : StringWriter
public override Encoding Encoding => new UTF8Encoding(false);
}


protected override void OnTargetStart(string target)
public void ChangeProjectVersion(string prefix = null, string suffix = null)
{
if(VersionSuffix.IsNullOrEmpty() && !CustomVersionSuffix.IsNullOrEmpty())
//module.manifest
if (IsModule)
{
//module.manifest
if(IsModule)
var manifest = ModuleManifest.Clone();
if(!String.IsNullOrEmpty(prefix))
manifest.Version = prefix;
if(!String.IsNullOrEmpty(suffix))
manifest.VersionTag = suffix;
using (var writer = new Utf8StringWriter())
{
var manifest = ModuleManifest.Clone();
manifest.VersionTag = CustomVersionSuffix;
using (var writer = new Utf8StringWriter())
{
XmlSerializer xml = new XmlSerializer(typeof(ModuleManifest));
xml.Serialize(writer, manifest);
File.WriteAllText(ModuleManifestFile, writer.ToString(), Encoding.UTF8);
}
XmlSerializer xml = new XmlSerializer(typeof(ModuleManifest));
xml.Serialize(writer, manifest);
File.WriteAllText(ModuleManifestFile, writer.ToString(), Encoding.UTF8);
}

//directory.Build.Props
var xmlDoc = new XmlDocument()
{
PreserveWhitespace = true
};
xmlDoc.LoadXml(File.ReadAllText(DirectoryBuildPropsPath));
var suffix = xmlDoc.GetElementsByTagName("VersionSuffix");
suffix[0].InnerText = CustomVersionSuffix;
using(var writer = new Utf8StringWriter())
}

//directory.Build.Props
var xmlDoc = new XmlDocument()
{
PreserveWhitespace = true
};
xmlDoc.LoadXml(File.ReadAllText(DirectoryBuildPropsPath));
if (!String.IsNullOrEmpty(prefix))
{
var prefixNodex = xmlDoc.GetElementsByTagName("VersionPrefix");
prefixNodex[0].InnerText = prefix;
}
if (String.IsNullOrEmpty(VersionSuffix) && !String.IsNullOrEmpty(suffix))
{
var suffixNodes = xmlDoc.GetElementsByTagName("VersionSuffix");
suffixNodes[0].InnerText = suffix;
}
using (var writer = new Utf8StringWriter())
{
xmlDoc.Save(writer);
File.WriteAllText(DirectoryBuildPropsPath, writer.ToString());
}
}

Target ChangeVersion => _ => _
.Requires(() => !CustomVersionPrefix.IsNullOrEmpty() || !CustomVersionSuffix.IsNullOrEmpty())
.Executes(() =>
{
if ((String.IsNullOrEmpty(VersionSuffix) && !CustomVersionSuffix.IsNullOrEmpty()) || !CustomVersionPrefix.IsNullOrEmpty())
{
xmlDoc.Save(writer);
File.WriteAllText(DirectoryBuildPropsPath, writer.ToString());
ChangeProjectVersion(prefix: CustomVersionPrefix, suffix: CustomVersionSuffix);
}
}
base.OnTargetStart(target);
});

Target StartRelease => _ => _
.Executes(() =>
{
var currentDir = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(Solution.Path.Parent);
GitTasks.Git("checkout dev");
var releaseBranchName = $"release/{ReleaseVersion}";
Logger.Info(Directory.GetCurrentDirectory());
GitTasks.Git($"checkout -b {releaseBranchName}");
GitTasks.Git($"push -u origin {releaseBranchName}");
Directory.SetCurrentDirectory(currentDir);
});

Target CompleteRelease => _ => _
.After(StartRelease)
.Executes(() =>
{
//workaround for run from sources
var currentDir = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(Solution.Path.Parent);
var currentBranch = GitTasks.GitCurrentBranch();
//Master
GitTasks.Git("checkout master");
GitTasks.Git($"merge {currentBranch}");
GitTasks.Git("push origin master");
//Dev
GitTasks.Git("checkout dev");
GitTasks.Git($"merge {currentBranch}");
IncrementVersionMinor();
ChangeProjectVersion(prefix: CustomVersionPrefix);
GitTasks.Git("add .");
GitTasks.Git($"commit -m \"{CustomVersionPrefix}\"");
GitTasks.Git($"push origin dev");
//remove release branch
GitTasks.Git($"branch -d {currentBranch}");
GitTasks.Git($"push origin --delete {currentBranch}");
Directory.SetCurrentDirectory(currentDir);
});

Target QuickRelease => _ => _
.DependsOn(StartRelease, CompleteRelease);

Target StartHotfix => _ => _
.Executes(() =>
{
var currentDir = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(Solution.Path.Parent);
GitTasks.Git("checkout master");
IncrementVersionPatch();
var hotfixBranchName = $"hotfix/{CustomVersionPrefix}";
Logger.Info(Directory.GetCurrentDirectory());
GitTasks.Git($"checkout -b {hotfixBranchName}");
ChangeProjectVersion(prefix: CustomVersionPrefix);
GitTasks.Git("add .");
GitTasks.Git($"commit -m \"{CustomVersionPrefix}\"");
GitTasks.Git($"push -u origin {hotfixBranchName}");
Directory.SetCurrentDirectory(currentDir);
});

Target CompleteHotfix => _ => _
.After(StartHotfix)
.Executes(() =>
{
//workaround for run from sources
var currentDir = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(Solution.Path.Parent);
var currentBranch = GitTasks.GitCurrentBranch();
//Master
GitTasks.Git("checkout master");
GitTasks.Git($"merge {currentBranch}");
GitTasks.Git($"tag {VersionPrefix}");
GitTasks.Git("push origin master");
//remove hotfix branch
GitTasks.Git($"branch -d {currentBranch}");
GitTasks.Git($"push origin --delete {currentBranch}");
Directory.SetCurrentDirectory(currentDir);
});

public void IncrementVersionMinor()
{
Version v = new Version(VersionPrefix);
var newPrefix = $"{v.Major}.{v.Minor + 1}.{v.Build}";
CustomVersionPrefix = newPrefix;
}

public void IncrementVersionPatch()
{
Version v = new Version(VersionPrefix);
var newPrefix = $"{v.Major}.{v.Minor}.{v.Build + 1}";
CustomVersionPrefix = newPrefix;
}

Target IncrementMinor => _ => _
.Triggers(ChangeVersion)
.Executes(() =>
{
IncrementVersionMinor();
});

Target IncremenPatch => _ => _
.Triggers(ChangeVersion)
.Executes(() =>
{
IncrementVersionPatch();
});

Target Publish => _ => _
.DependsOn(Compile)
.After(WebPackBuild, Test)
Expand Down Expand Up @@ -625,8 +751,9 @@ void RunGitHubRelease(string args)
ProcessTasks.StartProcess("github-release", args, RootDirectory, customLogger: GitLogger).AssertZeroExitCode();
}
var prereleaseArg = PreRelease ? "--pre-release" : "";
var targetBranchArg = ReleaseBranch.IsNullOrEmpty() ? "" : $"--target \"{ReleaseBranch}\"";
var descriptionArg = File.Exists(ReleaseNotes) ? $"--description \"{File.ReadAllText(ReleaseNotes)}\"" : "";
RunGitHubRelease($@"release --user {GitHubUser} -s {GitHubToken} --repo {GitRepositoryName} --tag {tag} {descriptionArg} {prereleaseArg}"); //-c branch -d description
RunGitHubRelease($@"release --user {GitHubUser} -s {GitHubToken} --repo {GitRepositoryName} {targetBranchArg} --tag {tag} {descriptionArg} {prereleaseArg}"); //-c branch -d description
RunGitHubRelease($@"upload --user {GitHubUser} -s {GitHubToken} --repo {GitRepositoryName} --tag {tag} --name {ZipFileName} --file ""{ZipFilePath}""");
});

Expand Down
8 changes: 8 additions & 0 deletions build/CHANGELOG.md
Expand Up @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.3.0] - 2020-07-20
### Added
- Targets: StartRelease, CompleteRelease, QuickRelease, StartHotfix, CompleteHotfix, IncrementMinor, IncrementPatch, ChangeVersion
### Changed
- OnTargetStart event handler was replaced with ChangeVersion Target
### Fixed
- Hash and Sources of github releases (VP-3628)

## [1.2.0] - 2020-07-07
### Changed
- Opencover is replaced with Coverlet
Expand Down
2 changes: 1 addition & 1 deletion build/Directory.Build.Props
Expand Up @@ -6,7 +6,7 @@
<Copyright>Copyright © VirtoCommerce 2011-2020</Copyright>
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>1.2.0</VersionPrefix>
<VersionPrefix>1.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>

Expand Down

0 comments on commit 681d603

Please sign in to comment.