diff --git a/HACKING.md b/HACKING.md index f9c3d42..2b28f7e 100644 --- a/HACKING.md +++ b/HACKING.md @@ -1,7 +1,8 @@ ### Generate executable macOS: ```shell -dotnet publish -c Release -r win-x64 -p:PublishSingleFile=true -p:PublishReadyToRun=true --self-contained false -dotnet publish -c Release -r osx-x64 -p:PublishSingleFile=true -p:PublishReadyToRun=true --self-contained false -dotnet publish -c Release -r linux-x64 -p:PublishSingleFile=true -p:PublishReadyToRun=true --self-contained false +cd ./nusave +dotnet publish -c Release -r win-x64 -p:PublishSingleFile=true -p:PublishReadyToRun=true -p:PublishTrimmed=true --self-contained true +dotnet publish -c Release -r osx-x64 -p:PublishSingleFile=true -p:PublishReadyToRun=true -p:PublishTrimmed=true --self-contained true +dotnet publish -c Release -r linux-x64 -p:PublishSingleFile=true -p:PublishReadyToRun=true -p:PublishTrimmed=true --self-contained true ``` \ No newline at end of file diff --git a/NuSave.Core/NuSave.cs b/NuSave.Core/Downloader.cs similarity index 90% rename from NuSave.Core/NuSave.cs rename to NuSave.Core/Downloader.cs index 84cdab5..01b0884 100644 --- a/NuSave.Core/NuSave.cs +++ b/NuSave.Core/Downloader.cs @@ -44,6 +44,8 @@ public Downloader( _allowUnlisted = allowUnlisted; _json = json; _silent = _json || silent; + + _outputDirectory = EnsureOutputDirectory(outputDirectory); } public void Download() @@ -156,8 +158,54 @@ public void ResolveDependencies(string csprojPath = null) if (_json) { - Console.WriteLine(JsonConvert.SerializeObject(GetDependencies())); + Console.WriteLine(JsonConvert.SerializeObject(GetDependencies(), Formatting.Indented)); + } + } + + /// + /// Convenience method that can be used in powershell in combination with Out-GridView + /// + /// + public List GetDependencies() + { + var list = new List(); + foreach (var p in _toDownload) + { + list.Add(new NuGetPackage + { + Id = p.Identity.Id, + Version = p.Identity.Version.ToString(), + Authors = string.Join(" ", p.Authors) + }); } + + return list; + } + + private string EnsureOutputDirectory(string value) + { + string outputDirectory = value; + + if (!string.IsNullOrWhiteSpace(outputDirectory) && Directory.Exists(outputDirectory)) + { + return outputDirectory; + } + + if (string.IsNullOrWhiteSpace(outputDirectory)) + { + outputDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nusave"); + } + + Directory.CreateDirectory(outputDirectory); + + if (!_silent) + { + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine($"Using output directory: {outputDirectory}"); + Console.ResetColor(); + } + + return outputDirectory; } private void ResolveDependencies(IEnumerable references) @@ -261,26 +309,6 @@ private string GetSource() return _source ?? DefaultSource; } - /// - /// Convenience method that can be used in powershell in combination with Out-GridView - /// - /// - public List GetDependencies() - { - var list = new List(); - foreach (var p in _toDownload) - { - list.Add(new SimplifiedPackageInfo - { - Id = p.Identity.Id, - Version = p.Identity.Version.ToString(), - Authors = string.Join(" ", p.Authors) - }); - } - - return list; - } - private IPackageSearchMetadata FindPackage(string id, SemanticVersion version, bool includePrerelease, bool includeUnlisted) { PackageMetadataResource resource = SourceRepository.GetResource(); diff --git a/NuSave.Core/IPackageExtensions.cs b/NuSave.Core/IPackageExtensions.cs deleted file mode 100644 index a053111..0000000 --- a/NuSave.Core/IPackageExtensions.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace NuSave.Core -{ - using NuGet.Protocol.Core.Types; - - public static class IPackageExtensions - { - public static string GetFileName(this IPackageSearchMetadata package) - { - return $"{package.Identity.Id}.{package.Identity.Version}.nupkg".ToLower(); - } - } -} \ No newline at end of file diff --git a/NuSave.Core/SimplifiedPackageInfo.cs b/NuSave.Core/NuGetPackage.cs similarity index 52% rename from NuSave.Core/SimplifiedPackageInfo.cs rename to NuSave.Core/NuGetPackage.cs index 401ff3f..4ce2a19 100644 --- a/NuSave.Core/SimplifiedPackageInfo.cs +++ b/NuSave.Core/NuGetPackage.cs @@ -1,11 +1,16 @@ namespace NuSave.Core { - public class SimplifiedPackageInfo + using Newtonsoft.Json; + + public class NuGetPackage { + [JsonProperty("id")] public string Id { get; set; } + [JsonProperty("version")] public string Version { get; set; } + [JsonProperty("authors")] public string Authors { get; set; } } } \ No newline at end of file diff --git a/NuSave.sln b/NuSave.sln index fc2700f..9224e37 100644 --- a/NuSave.sln +++ b/NuSave.sln @@ -5,7 +5,7 @@ VisualStudioVersion = 15.0.25928.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NuSave.Core", "NuSave.Core\NuSave.Core.csproj", "{FE24E5C3-507C-4198-93B5-A012AF0408BD}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NuSave", "NuSave\NuSave.csproj", "{8C7D84C7-3345-4567-8745-17C85EB93277}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nusave", "nusave\nusave.csproj", "{8C7D84C7-3345-4567-8745-17C85EB93277}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Things", "Things", "{24ADCE77-9560-40CB-BE68-D5DE0B1A8351}" ProjectSection(SolutionItems) = preProject diff --git a/README.md b/README.md index 9a9e7bf..071a8a9 100644 --- a/README.md +++ b/README.md @@ -20,25 +20,58 @@ Don't forget to add the location of `nusave.exe` or `nusave` to the `$PATH`. .NET 5 is needed to build and run nusave. -## More +## Download nuget packages from a `.csproj` file -### Download nuget packages from a .csproj MSBuild project - -```powershell +```shell nusave -msbuildProject "/path/to/project.csproj" -outputDirectory "/path/to/output/dir" ``` -### Pipe the JSON result to PowerShell's `Out-GridView` +## JSON output -`nusave` is able to output the dependency list without downloading it, and formatting the output as JSON, that way you can pipe the content to another program that will use this information to do other tasks, this can be the case for build scripts. The following command will pipe the content to PowerShell's `Out-GridView` : - -```powershell -nusave -id "Newtonsoft.Json" -version "12.0.3" -noDownload -json | ConvertFrom-Json | Out-GridView +`nusave` is able to output the dependency list as JSON without downloading it: +```shell +./nusave -id "System.Collections" -version "4.3.0" -noDownload -json +``` +Result: +```json +[ + { + "id": "System.Collections", + "version": "4.3.0", + "authors": "Microsoft" + }, + { + "id": "Microsoft.NETCore.Platforms", + "version": "1.1.0", + "authors": "Microsoft" + }, + { + "id": "Microsoft.NETCore.Platforms", + "version": "1.1.0", + "authors": "Microsoft" + }, + { + "id": "Microsoft.NETCore.Targets", + "version": "1.1.0", + "authors": "Microsoft" + }, + { + "id": "Microsoft.NETCore.Targets", + "version": "1.1.0", + "authors": "Microsoft" + }, + { + "id": "System.Runtime", + "version": "4.3.0", + "authors": "Microsoft" + }, + { + "id": "System.Runtime", + "version": "4.3.0", + "authors": "Microsoft" + } +] ``` - -The result: - -![outgridview](https://raw.githubusercontent.com/anass-b/nusave/master/readme/outgridview.png) Check `nusave -help` for more command line options. diff --git a/NuSave/Program.cs b/nusave/Program.cs similarity index 93% rename from NuSave/Program.cs rename to nusave/Program.cs index 812f8f5..4b7f9c2 100644 --- a/NuSave/Program.cs +++ b/nusave/Program.cs @@ -1,6 +1,7 @@ -namespace NuSave.New +namespace NuSave { using System; + using System.IO; using Core; using Microsoft.Extensions.CommandLineUtils; @@ -36,11 +37,9 @@ private static void Main(string[] args) app.OnExecute(() => { - string outputDirectoryStr = noDownload.HasValue() ? null : outputDirectory.Value(); - var downloader = new Downloader( source: source.Value(), - outputDirectory: outputDirectoryStr, + outputDirectory: outputDirectory.Value(), id: packageId.Value(), version: packageVersion.Value(), allowPreRelease: allowPreRelease.HasValue(), diff --git a/NuSave/NuSave.csproj b/nusave/nusave.csproj similarity index 100% rename from NuSave/NuSave.csproj rename to nusave/nusave.csproj