Skip to content

Commit

Permalink
fix: nuget source management fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
2chevskii committed Jan 23, 2024
1 parent 1d72196 commit e7de2d7
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 28 deletions.
9 changes: 9 additions & 0 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
"type": "boolean",
"description": "Disables displaying the NUKE logo"
},
"NugetFeed": {
"type": "string"
},
"Partition": {
"type": "string",
"description": "Partition to use on CI"
Expand Down Expand Up @@ -86,8 +89,11 @@
"CompileTests",
"CopyLibrariesToArtifactsDirectory",
"CreateGitHubRelease",
"DownloadReleaseAssets",
"NugetPush",
"Pack",
"Restore",
"TestAddSource",
"UnitTest"
]
}
Expand All @@ -110,8 +116,11 @@
"CompileTests",
"CopyLibrariesToArtifactsDirectory",
"CreateGitHubRelease",
"DownloadReleaseAssets",
"NugetPush",
"Pack",
"Restore",
"TestAddSource",
"UnitTest"
]
}
Expand Down
4 changes: 0 additions & 4 deletions build/Build.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using Components;
using Nuke.Common;
using Nuke.Common.IO;
using Nuke.Common.ProjectModel;
using Nuke.Common.Tools.GitVersion;

class Build : NukeBuild, IPack, IClean, INugetPush
{
Expand Down
2 changes: 0 additions & 2 deletions build/Components/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.ComponentModel;
using System.Linq;
using Nuke.Common.Tooling;

[TypeConverter(typeof(TypeConverter<Configuration>))]
Expand Down
3 changes: 1 addition & 2 deletions build/Components/ICreateGitHubRelease.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down
87 changes: 87 additions & 0 deletions build/Components/IHazNugetSourceList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using Nuke.Common.Tooling;
using Nuke.Common.Tools.NuGet;
using Serilog;


interface IHazNugetSourceList
{
IEnumerable<NugetSource> NugetSources => NugetSource.GetSources();

bool HasNugetSource(Uri uri) => HasNugetSource(uri.Host);
bool HasNugetSource(string name) => NugetSources.Any(source => source.Name == name);

public struct NugetSource
{
[CanBeNull]
static IEnumerable<NugetSource> _sources;

static readonly Regex NameEnabledRegex = new Regex(
@"\d+\.\s*([a-zA-Z0-9_\.-]+)\s+\[(Enabled|Disabled)\]"
);

public string Name;
public string Uri;
public bool Enabled;

public static IEnumerable<NugetSource> GetSources()
{
if (_sources == null)
_sources = ParseAll(
NuGetTasks
.NuGetSourcesList(settings =>
settings.SetFormat(NuGetSourcesListFormat.Detailed)
)
.Skip(1)
.ToList()
);

return _sources;
}

public static NugetSource Parse(IEnumerable<Output> lines)
{
var firstLine = lines.First().Text;
var nameEnabledMatch = NameEnabledRegex.Match(firstLine);
var name = nameEnabledMatch.Groups[1].Value;

var enabledString = nameEnabledMatch.Groups[2].Value;

Log.Information("Enabled string: {Str}", enabledString);

var isEnabled = ParseEnabled(enabledString);
var uri = lines.Skip(1).First().Text.Trim();

return new NugetSource
{
Name = name,
Enabled = isEnabled,
Uri = uri
};
}

public static IEnumerable<NugetSource> ParseAll(IReadOnlyList<Output> lines)
{
for (int i = 0; i < lines.Count; i += 2)
{
var thisLine = lines[i];
var nextLine = lines[i + 1];
var source = Parse([thisLine, nextLine]);
yield return source;
}
}

static bool ParseEnabled(string str)
{
return str switch
{
"Enabled" or "E" => true,
"Disabled" or "D" => false
};
}
}
}
30 changes: 14 additions & 16 deletions build/Components/INugetPush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,36 @@
using Nuke.Common.Tools.DotNet;
using Nuke.Common.Tools.NuGet;
using Octokit;
using Serilog;
using FileMode = System.IO.FileMode;

namespace Components;

interface INugetPush : INukeBuild, ICreateGitHubRelease
interface INugetPush : INukeBuild, ICreateGitHubRelease, IHazNugetSourceList
{
string NugetApiKey => EnvironmentInfo.GetVariable("NUGET_API_KEY");

[Parameter]
Uri NugetFeed => TryGetValue(() => NugetFeed);

string NugetSourceName => NugetFeed.Host;

Target EnsureHasNugetSource =>
_ =>
_.OnlyWhenDynamic(() => !HasNugetSource(NugetSourceName))
.Executes(
() =>
NuGetTasks.NuGetSourcesAdd(settings =>
settings.SetName(NugetSourceName).SetSource(NugetFeed.ToString())
)
);

Target NugetPush =>
_ =>
_.Requires(() => !string.IsNullOrEmpty(NugetApiKey))
.Requires(() => NugetFeed)
.Executes(() =>
{
try
{
DotNetTasks.DotNetNuGetAddSource(settings =>
settings.SetName(NugetFeed.Host).SetSource(NugetFeed.ToString())
);
}
catch (Exception e)
{
Log.Error("Add source error: {Error}", e.ToString());
}

DotNetTasks.DotNetNuGetPush(settings =>
settings
.SetSource(NugetFeed.Host)
.SetSource(NugetSourceName)
.SetApiKey(NugetApiKey)
.CombineWith(
PackagesDirectory.GlobFiles("*.nupkg"),
Expand Down
1 change: 0 additions & 1 deletion build/Components/IPack.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Nuke.Common;
using Nuke.Common.Tooling;
using Nuke.Common.Tools.DotNet;

interface IPack : ICompile
Expand Down
4 changes: 1 addition & 3 deletions build/Components/ITest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using Extensions;
using Extensions;
using Nuke.Common;
using Nuke.Common.CI.GitHubActions;
using Nuke.Common.IO;
using Nuke.Common.Tooling;
using Nuke.Common.Tools.DotNet;
Expand Down
1 change: 1 addition & 0 deletions build/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<ItemGroup>
<PackageDownload Include="GitVersion.Tool" Version="[6.0.0-beta.4]" />
<PackageDownload Include="NuGet.CommandLine" Version="[6.8.0]" />
</ItemGroup>

</Project>

0 comments on commit e7de2d7

Please sign in to comment.