Skip to content

Commit

Permalink
2.1.0 (#26)
Browse files Browse the repository at this point in the history
* VCI-128: Fix build project search

* VCI-128: Fix module manifest search

* Added help for the specific target

* PT-3772: Fix build of sample projects (#20)

* VCI-156: Add ability to update only platform (#22)

* VCI-162: Fix user input comparing (#21)

* VCI-162: Fix user input comparing

* VCI-162: Make WebProject static

* VCI-162: Fix codesmells

* VCI-157: Backup appsettings.json on update (#23)

* PT-58: Add DefaultProject parameter

Added DefaultProject parameter to abillity to specify startap project name to complile module sample. Default value ".Web".  For sample module  build run vc-build Compile -DefaultProject "SampleModule.Web"

* PT-4042: Add MatchVersions target (#25)

* feat: Add MatchVersions target

* refactor: Decrease complexity

* VCI-166: Update changelog

Co-authored-by: Dimitri Kargapolov <[email protected]>
Co-authored-by: Maksim <[email protected]>
Co-authored-by: Maksim Kopnov <[email protected]>
Co-authored-by: Konstantin Savosteev <[email protected]>
  • Loading branch information
5 people authored Oct 1, 2021
1 parent 66924a8 commit 48a0dc7
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 22 deletions.
19 changes: 12 additions & 7 deletions Build.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -212,6 +213,9 @@ public static int Main(string[] args)
[Parameter("Directory containing modules.json")]
public static string ModulesJsonDirectoryName { get; set; } = "vc-modules";

[Parameter("Defauld (start) project name")]
public static string DefaultProject { get; set; } = ".Web";

// TODO: Convert to a method because GitRepository.FromLocalDirectory() is a heavy method and it should not be used as a property
protected GitRepository GitRepository => GitRepository.FromLocalDirectory(RootDirectory / ".git");

Expand All @@ -220,11 +224,12 @@ public static int Main(string[] args)
protected static AbsolutePath SamplesDirectory => RootDirectory / "samples";

protected AbsolutePath ModulesLocalDirectory => ArtifactsDirectory / ModulesJsonDirectoryName;
protected Project WebProject => Solution?.AllProjects.FirstOrDefault(x => x.Name.EndsWith(".Web") && !x.Path.ToString().Contains("samples") || x.Name.EndsWith("VirtoCommerce.Storefront") || x.Name.EndsWith("_build"));
protected AbsolutePath ModuleManifestFile => WebProject?.Directory / "module.manifest";
protected static Project WebProject => Solution?.AllProjects.FirstOrDefault(x => x.Name.EndsWith(DefaultProject) || x.Name.EndsWith("VirtoCommerce.Storefront") || x.Name.EndsWith("_build"));
protected static AbsolutePath ModuleManifestFile => WebProject?.Directory / "module.manifest";
protected AbsolutePath ModuleIgnoreFile => RootDirectory / "module.ignore";
protected static AbsolutePath WebDirectory => WebProject?.Directory;

protected Microsoft.Build.Evaluation.Project MSBuildProject => WebProject?.GetMSBuildProject();
protected static Microsoft.Build.Evaluation.Project MSBuildProject => WebProject?.GetMSBuildProject();
protected string VersionPrefix => IsTheme ? GetThemeVersion(PackageJsonPath) : MSBuildProject.GetProperty("VersionPrefix")?.EvaluatedValue;
protected string VersionSuffix => MSBuildProject?.GetProperty("VersionSuffix")?.EvaluatedValue;
protected string ReleaseVersion => MSBuildProject?.GetProperty("PackageVersion")?.EvaluatedValue ?? WebProject.GetProperty("Version");
Expand Down Expand Up @@ -267,7 +272,7 @@ private void SonarLogger(OutputType type, string text)
.Before(Restore)
.Executes(() =>
{
var searchPattern = new string[] { "**/bin", "**/obj" };
var searchPattern = new [] { "**/bin", "**/obj" };
if (DirectoryExists(SourceDirectory))
{
SourceDirectory.GlobDirectories(searchPattern).ForEach(DeleteDirectory);
Expand Down Expand Up @@ -503,10 +508,10 @@ private string GetThemeVersion(string packageJsonPath)

if (disableApproval.IsNullOrEmpty() && !Force)
{
Console.Write($"Are you sure you want to release {GitRepository.Identifier}? (Y/N): ");
Console.Write($"Are you sure you want to release {GitRepository.Identifier}? (y/N): ");
var response = Console.ReadLine();

if (response.EqualsInvariant("y"))
if (string.Compare(response, "y", true, CultureInfo.InvariantCulture) != 0)
{
ControlFlow.Fail("Aborted");
}
Expand Down Expand Up @@ -1069,4 +1074,4 @@ private async Task PublishRelease(string owner, string repo, string token, strin
ClearTempBeforeExit = true;
});
}
}
}
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ 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).
## [2.1.0] - 2021-10-01
### Added
- MatchVersions target
- DefaultProject parameter
- Ability to update only platform
- help target
### Changed
- appsettings.json doesn't reset on update
### Fixed
- User input in QuickRelease target
- Building of sample projects

## [2.0.0] - 2021-08-05
### Changed
- vc-build was updated to .Net 5
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Copyright>Copyright © VirtoCommerce 2011-2021</Copyright>
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>2.0.0</VersionPrefix>
<VersionPrefix>2.1.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
<VersionSuffix Condition=" '$(VersionSuffix)' != '' AND '$(BuildNumber)' != '' ">$(VersionSuffix)-$(BuildNumber)</VersionSuffix>
</PropertyGroup>
Expand Down
146 changes: 146 additions & 0 deletions PlatformTools/Build.MatchVerisons.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Nuke.Common;
using Nuke.Common.ProjectModel;
using Nuke.Common.Utilities;
using PlatformTools;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Platform.Core.Modularity;

namespace VirtoCommerce.Build
{
internal partial class Build
{
private Regex _moduleNameRegEx = new Regex(@"(VirtoCommerce.+)Module", RegexOptions.Compiled);

public Target MatchVersions => _ => _
.Executes(() =>
{
var allPackages = new List<PackageItem>();
var allProjects = Solution.GetProjects("*");

foreach (var project in allProjects)
{
var packagesInfo = GetProjectPackages(project);
allPackages.AddRange(packagesInfo);
}

var errors = new List<string>();

var platformErrors = ValdatePlatformVersion(allPackages);
errors.AddRange(platformErrors);

var dependencyVerionErrors = ValidateModuleDependenciesVersions(allPackages);
errors.AddRange(dependencyVerionErrors);

var missedDependenciesErrors = ValidateForMissedDependencies(allPackages);
errors.AddRange(missedDependenciesErrors);

if (errors.Any())
{
ControlFlow.Fail(errors.Join(Environment.NewLine));
}
});

/// <summary>
/// Get list of VirtoCommerce packages (platform and module)
/// </summary>
private IEnumerable<PackageItem> GetProjectPackages(Project project)
{
var msBuildProject = project.GetMSBuildProject();

// find all VirtoCommerce references
return msBuildProject.Items
.Where(x => x.ItemType == "PackageReference"
&& (x.EvaluatedInclude.StartsWith("VirtoCommerce.Platform.") || _moduleNameRegEx.IsMatch(x.EvaluatedInclude)))
.Select(x =>
{
var versionMetadata = x.Metadata.FirstOrDefault(x => x.Name == "Version");
if (versionMetadata == null)
{
return null;
}

var name = x.EvaluatedInclude;

return new PackageItem
{
Name = name,
Version = versionMetadata.EvaluatedValue,
ProjectName = project.Name,
IsPlatformPackage = name.StartsWith("VirtoCommerce.Platform.")
};
})
.Where(x => x != null);
}

/// <summary>
/// Check match between manifest platform version and platform packages
/// </summary>
private IEnumerable<string> ValdatePlatformVersion(IEnumerable<PackageItem> packages)
{
return packages
.Where(package => package.IsPlatformPackage && SemanticVersion.Parse(package.Version) != SemanticVersion.Parse(ModuleManifest.PlatformVersion))
.Select(x =>
$"Mismatched platform dependency version found. Platform version: {ModuleManifest.PlatformVersion}, Platform package name: {x.Name}, platform package version: {x.Version}, project name: {x.ProjectName}");
}

/// <summary>
/// Check dependencies for module packages versions mismatch
/// </summary>
private IEnumerable<string> ValidateModuleDependenciesVersions(IEnumerable<PackageItem> packages)
{
var result = new List<string>();

if (ModuleManifest.Dependencies.IsNullOrEmpty())
{
return result;
}

foreach (var dependency in ModuleManifest.Dependencies)
{
var errors = packages
.Where(package => !package.IsPlatformPackage
&& HasNameMatch(package.Name, dependency.Id)
&& SemanticVersion.Parse(package.Version) != SemanticVersion.Parse(dependency.Version))
.Select(package =>
$"Mismatched dependency version found. Dependency: {dependency.Id}, version: {dependency.Version}, Project package version: {package.Version}, project name: {package.ProjectName}");

result.AddRange(errors);
}

return result;
}

/// <summary>
/// Check project packages for missed dependency in manifest
/// </summary>
private IEnumerable<string> ValidateForMissedDependencies(IEnumerable<PackageItem> packages)
{
var result = new List<string>();

if (ModuleManifest.Dependencies.IsNullOrEmpty())
{
return result;
}

foreach (var packageGroup in packages.Where(x => !x.IsPlatformPackage).GroupBy(x => x.Name))
{
if (!ModuleManifest.Dependencies.Any(dependency => HasNameMatch(packageGroup.Key, dependency.Id)))
{
result.Add($"Dependency in module.manifest is missing. Package name: {packageGroup.Key}");
}
}

return result;
}

private bool HasNameMatch(string packageName, string dependencyName)
{
var match = _moduleNameRegEx.Match(packageName);
return match.Groups.Values.Any(x => x.Value == dependencyName);
}
}
}
49 changes: 37 additions & 12 deletions PlatformTools/Build.PackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nuke.Common;
using Nuke.Common.IO;
Expand All @@ -28,7 +29,7 @@ internal partial class Build
public static bool SkipDependencySolving { get; set; }

[Parameter("Install the platform", Name = "Platform")]
public static bool InstallPlatformParam { get; set; }
public static bool PlatformParameter { get; set; }

public Target Init => _ => _
.Executes(async () =>
Expand All @@ -47,7 +48,7 @@ internal partial class Build
var localModuleCatalog = LocalModuleCatalog.GetCatalog(GetDiscoveryPath(), ProbingPath);
var externalModuleCatalog = ExtModuleCatalog.GetCatalog(GitHubToken, localModuleCatalog, packageManifest.ModuleSources);

if (Module?.Length > 0 && !InstallPlatformParam)
if (Module?.Length > 0 && !PlatformParameter)
{
foreach (var module in ParseModuleParameter(Module))
{
Expand Down Expand Up @@ -88,13 +89,13 @@ internal partial class Build
}
}
}
else if (!InstallPlatformParam && !packageManifest.Modules.Any())
else if (!PlatformParameter && !packageManifest.Modules.Any())
{
Logger.Info("Add group: commerce");
var commerceModules = externalModuleCatalog.Modules.OfType<ManifestModuleInfo>().Where(m => m.Groups.Contains("commerce")).Select(m => new ModuleItem(m.Id, m.Version.ToString()));
packageManifest.Modules.AddRange(commerceModules);
}
else if (InstallPlatformParam)
else if (PlatformParameter)
{
var platformRelease = await GithubManager.GetPlatformRelease(GitHubToken, VersionToInstall);
packageManifest.PlatformVersion = platformRelease.TagName;
Expand Down Expand Up @@ -153,7 +154,26 @@ private async Task InstallPlatformAsync(string platformVersion)
}

await HttpTasks.HttpDownloadFileAsync(platformAssetUrl, platformZip);

// backup appsettings.json if exists
var tempFile = string.Empty;
if (File.Exists(AppsettingsPath))
{
tempFile = Path.GetTempFileName();
FileSystemTasks.MoveFile(AppsettingsPath, tempFile, FileExistsPolicy.Overwrite);
}

CompressionTasks.Uncompress(platformZip, RootDirectory);

// return appsettings.json back
if (!string.IsNullOrEmpty(tempFile))
{
var bakFileName = new StringBuilder("appsettings.")
.Append(DateTime.Now.ToString("MMddyyHHmmss"))
.Append(".bak");
var destinationSettingsPath = !Force ? AppsettingsPath : Path.Join(Path.GetDirectoryName(AppsettingsPath), bakFileName.ToString());
FileSystemTasks.MoveFile(tempFile, destinationSettingsPath, FileExistsPolicy.Overwrite);
}
}
}

Expand Down Expand Up @@ -184,6 +204,7 @@ private bool NeedToInstallPlatform(string version)

public Target InstallModules => _ => _
.After(InstallPlatform)
.OnlyWhenDynamic(() => !PlatformParameter)
.Executes(() =>
{
var packageManifest = PackageManager.FromFile(PackageManifestPath);
Expand Down Expand Up @@ -291,19 +312,23 @@ private bool NeedToInstallPlatform(string version)
var platformRelease = await GithubManager.GetPlatformRelease(GitHubToken, VersionToInstall);
packageManifest.PlatformVersion = platformRelease.TagName;
packageManifest.PlatformAssetUrl = platformRelease.Assets.First().BrowserDownloadUrl;
var localModuleCatalog = LocalModuleCatalog.GetCatalog(GetDiscoveryPath(), ProbingPath);
var externalModuleCatalog = ExtModuleCatalog.GetCatalog(GitHubToken, localModuleCatalog, packageManifest.ModuleSources);

foreach (var module in packageManifest.Modules)
if (!PlatformParameter)
{
var externalModule = externalModuleCatalog.Modules.OfType<ManifestModuleInfo>().Where(m => m.Id == module.Id).FirstOrDefault(m => m.Ref.Contains("github.com"));
var localModuleCatalog = LocalModuleCatalog.GetCatalog(GetDiscoveryPath(), ProbingPath);
var externalModuleCatalog = ExtModuleCatalog.GetCatalog(GitHubToken, localModuleCatalog, packageManifest.ModuleSources);

if (externalModule == null)
foreach (var module in packageManifest.Modules)
{
ControlFlow.Fail($"No module {module.Id} found");
}
var externalModule = externalModuleCatalog.Modules.OfType<ManifestModuleInfo>().Where(m => m.Id == module.Id).FirstOrDefault(m => m.Ref.Contains("github.com"));

if (externalModule == null)
{
ControlFlow.Fail($"No module {module.Id} found");
}

module.Version = externalModule.Version.ToString();
module.Version = externalModule.Version.ToString();
}
}

PackageManager.ToFile(packageManifest);
Expand Down
16 changes: 16 additions & 0 deletions PlatformTools/PackageItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace PlatformTools
{
internal class PackageItem
{
public string Name { get; set; }

public string ProjectName { get; set; }

public string Version { get; set; }

/// <summary>
/// Whether or not the package is a module package or a platfrom package
/// </summary>
public bool IsPlatformPackage { get; set; }
}
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ Target with name 'help' does not exist. Available targets are:
- WebPackBuild
```

To get help for the specific target:
```console

vc-build help NameOfTheTarget

```

## Usage examples
Below you can see how the specific target could be used
Expand Down
3 changes: 1 addition & 2 deletions _build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</ItemGroup>

<ItemGroup>
<NukeSpecificationFiles Include="**\*.json" Exclude="bin\**;obj\**;GrabMigrator\**;global.json;.nuke\parameters.json" />
<NukeSpecificationFiles Include="**\*.json" Exclude="bin\**;obj\**;GrabMigrator\**;global.json;.nuke\parameters.json;.vs\**;Releases\**;" />
<NukeExternalFiles Include="**\*.*.ext" Exclude="bin\**;obj\**" />
<Compile Remove="artifacts\**" />
<Compile Remove="Properties\**" />
Expand All @@ -63,7 +63,6 @@

<!-- Common build related files -->

<None Include="..\.nuke" LinkBase="config" />
<None Include="..\global.json" LinkBase="config" Condition="Exists('..\global.json')" />
<None Include="..\nuget.config" LinkBase="config" Condition="Exists('..\nuget.config')" />
<None Include="..\GitVersion.yml" LinkBase="config" Condition="Exists('..\GitVersion.yml')" />
Expand Down

0 comments on commit 48a0dc7

Please sign in to comment.