Skip to content

Commit

Permalink
Update GetInformationalVersion to find nupkg files.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Feb 8, 2024
1 parent 66689fb commit 28e9842
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.7.4] / 2023-02-08
### Features
- Update `GetInformationalVersion` to find `nupkg` files.
### Added
- Add `NugetVersionInfo` to parse `nupkg` files by name.

## [1.7.3] / 2023-12-18
### Features
- `IPack` with `UnlistNuget` to unlist nuget package.
Expand Down Expand Up @@ -331,6 +337,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- First Release

[vNext]: ../../compare/1.0.0...HEAD
[1.7.4]: ../../compare/1.7.3...1.7.4
[1.7.3]: ../../compare/1.7.2...1.7.3
[1.7.2]: ../../compare/1.7.1...1.7.2
[1.7.1]: ../../compare/1.7.0...1.7.1
Expand Down
37 changes: 34 additions & 3 deletions ricaun.Nuke/Extensions/AssemblyExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,19 @@ public static bool IsVersionPreRelease(this Project project)
/// </summary>
/// <param name="project"></param>
/// <returns></returns>
public static string GetInformationalVersion(this Project project) => project.GetFileVersionInfo()?.ProductVersion.Split('+').First();
/// <remarks>Works with .dll and .nupkg</remarks>
public static string GetInformationalVersion(this Project project)
{
if (project.GetFileVersionInfo() is FileVersionInfo fileVersionInfo)
{
return fileVersionInfo?.ProductVersion.Split('+').First();
}
if (project.GetNugetVersionInfo() is NugetVersionInfo nugetVersionInfo)
{
return nugetVersionInfo?.ProductVersion.Split('+').First();
}
return null;
}

/// <summary>
/// GetCompany => CompanyName
Expand Down Expand Up @@ -131,6 +143,25 @@ public static FileVersionInfo GetFileVersionInfo(this Project project)
return GetFileVersionInfoGreater(project.Directory, $"*{project.Name}*.dll");
}

/// <summary>
/// Get NugetVersionInfo of the first nupkg file found
/// </summary>
/// <param name="project"></param>
/// <returns></returns>
public static NugetVersionInfo GetNugetVersionInfo(this Project project)
{
var sourceDir = project.Directory;
var searchPattern = $"*{project.Name}*.nupkg";
var nupkgFiles = Directory.GetFiles(sourceDir, searchPattern, SearchOption.AllDirectories);
foreach (var nupkgFile in nupkgFiles)
{
var nugetVersionInfo = NugetVersionInfo.Parse(nupkgFile);
if (nugetVersionInfo is NugetVersionInfo) return nugetVersionInfo;
}

return null;
}

/// <summary>
/// GetFileVersionInfoGreater
/// </summary>
Expand Down Expand Up @@ -169,8 +200,8 @@ public static void ShowInformation(this Project project)
Serilog.Log.Information($"Name: {project.Name}");
Serilog.Log.Information($"GetAppId: {project.GetAppId()}");

if (project.GetFileVersionInfo() == null)
Serilog.Log.Warning($"GetFileVersionInfo: {project.Name} not found!");
if (project.GetInformationalVersion() == null)
Serilog.Log.Warning($"GetInformationalVersion: {project.Name} not found!");

Serilog.Log.Information($"GetInformationalVersion: {project.GetInformationalVersion()}");
Serilog.Log.Information($"GetAssemblyName: {project.GetAssemblyName()}");
Expand Down
37 changes: 37 additions & 0 deletions ricaun.Nuke/Extensions/NuGetExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,45 @@
using Nuke.Common.Tools.DotNet;
using Nuke.Common.Tools.NuGet;
using System;
using System.IO;

namespace ricaun.Nuke.Extensions
{
/// <summary>
/// NugetVersionInfo
/// </summary>
public class NugetVersionInfo
{
/// <summary>
/// ProductName
/// </summary>
public string ProductName { get; private set; }
/// <summary>
/// ProductVersion
/// </summary>
public string ProductVersion { get; private set; }

/// <summary>
/// Parse <paramref name="packageFileName"/> and return a new instance of <see cref="NugetVersionInfo"/>
/// </summary>
/// <param name="packageFileName"></param>
/// <returns></returns>
public static NugetVersionInfo Parse(string packageFileName)
{
string packageName;
string packageVersion;
if (NuGetExtension.TryGetPackageNameAndVersion(packageFileName, out packageName, out packageVersion))
{
return new NugetVersionInfo
{
ProductName = packageName,
ProductVersion = packageVersion
};
}
return null;
}
}

/// <summary>
/// NuGetExtension
/// </summary>
Expand All @@ -31,6 +67,7 @@ public static bool TryGetPackageNameAndVersion(string packageFileName, out strin
packageVersion = match.Groups[2].Value;
return true;
}

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion ricaun.Nuke/ricaun.Nuke.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<PropertyGroup>
<PackageId>ricaun.Nuke</PackageId>
<Version>1.7.3</Version>
<Version>1.7.4-alpha</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit 28e9842

Please sign in to comment.