From 46f0929fc44705d7da2351bfe3cc14ba723f1047 Mon Sep 17 00:00:00 2001 From: PieterjanDeClippel Date: Wed, 5 Nov 2025 09:06:32 +0100 Subject: [PATCH] Easily publish local packages --- Program.cs | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/Program.cs b/Program.cs index 01d471b..7940bc8 100644 --- a/Program.cs +++ b/Program.cs @@ -1,13 +1,14 @@ // 'Dev' tool can be used to do some development tasks based on the current directory. -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text.RegularExpressions; -using System.Text.Json; using Dev; using Microsoft.VisualStudio.SolutionPersistence.Model; using Microsoft.VisualStudio.SolutionPersistence.Serializer; using Spectre.Console; +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Text.Json; +using System.Text.RegularExpressions; +using System.Xml.Linq; var informationalVersion = ThisAssembly.Info.InformationalVersion.Split('+', 2); AnsiConsole.MarkupLine($"[bold green]{ThisAssembly.Info.Product}[/] v[green]{informationalVersion[0]}[/]+{informationalVersion[1][..7]}"); @@ -379,6 +380,26 @@ npm run build Process.Start(new ProcessStartInfo(slnFile) { UseShellExecute = true }); return true; } + + if (command is "pack") + { + AnsiConsole.MarkupLine($"[green]Packing NuGet packages for[/] {slnFile} [green]...[/]"); + var nugetConfigPath = GetRootNugetConfigPath(); + var nugetConfig = XDocument.Load(nugetConfigPath); + var sourceName = commandArgs[0]; + var nugetSourcePath = nugetConfig.Element("configuration")?.Element("packageSources")?.Elements("add")? + .Select(x => new + { + Name = x.Attribute("key")?.Value, + Source = x.Attribute("value")?.Value + })? + .FirstOrDefault(x => x.Name?.Equals(sourceName, StringComparison.InvariantCultureIgnoreCase) ?? false)? + .Source; + + Process.Start("dotnet", $"pack \"{slnFile}\" -c Release -o \"{nugetSourcePath}\"").WaitForExit(); + AnsiConsole.MarkupLine($"[green]NuGet packages for[/] {slnFile} published to {nugetSourcePath}[green][/]"); + return true; + } } // TODO: Check if we have a .devcontainer folder in the current directory. And if so, open it in Visual Studio Code as a dev container. @@ -538,6 +559,18 @@ static string ReplaceVariables(string command, string? slnFile, string? csprojFi return Directory.GetFiles(searchPath, "*.csproj").FirstOrDefault(); } +static string GetRootNugetConfigPath() +{ + var userConfig = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "NuGet", "nuget.config"); + var linuxMacConfig = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "NuGet", "NuGet.Config"); + + var configPath = File.Exists(userConfig) ? userConfig + : File.Exists(linuxMacConfig) ? linuxMacConfig + : throw new FileNotFoundException("Could not find user NuGet configuration file."); + + return configPath; +} + partial class Program { [GeneratedRegex("(?.*)")]