Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba committed Dec 23, 2021
1 parent 898e8de commit cf61f65
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 55 deletions.
7 changes: 4 additions & 3 deletions HACKING.md
Original file line number Diff line number Diff line change
@@ -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
```
70 changes: 49 additions & 21 deletions NuSave.Core/NuSave.cs → NuSave.Core/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public Downloader(
_allowUnlisted = allowUnlisted;
_json = json;
_silent = _json || silent;

_outputDirectory = EnsureOutputDirectory(outputDirectory);
}

public void Download()
Expand Down Expand Up @@ -156,8 +158,54 @@ public void ResolveDependencies(string csprojPath = null)

if (_json)
{
Console.WriteLine(JsonConvert.SerializeObject(GetDependencies()));
Console.WriteLine(JsonConvert.SerializeObject(GetDependencies(), Formatting.Indented));
}
}

/// <summary>
/// Convenience method that can be used in powershell in combination with Out-GridView
/// </summary>
/// <returns></returns>
public List<NuGetPackage> GetDependencies()
{
var list = new List<NuGetPackage>();
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<MsBuildPackageRef> references)
Expand Down Expand Up @@ -261,26 +309,6 @@ private string GetSource()
return _source ?? DefaultSource;
}

/// <summary>
/// Convenience method that can be used in powershell in combination with Out-GridView
/// </summary>
/// <returns></returns>
public List<SimplifiedPackageInfo> GetDependencies()
{
var list = new List<SimplifiedPackageInfo>();
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<PackageMetadataResource>();
Expand Down
12 changes: 0 additions & 12 deletions NuSave.Core/IPackageExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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; }
}
}
2 changes: 1 addition & 1 deletion NuSave.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 46 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
7 changes: 3 additions & 4 deletions NuSave/Program.cs → nusave/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace NuSave.New
namespace NuSave
{
using System;
using System.IO;
using Core;
using Microsoft.Extensions.CommandLineUtils;

Expand Down Expand Up @@ -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(),
Expand Down
File renamed without changes.

0 comments on commit cf61f65

Please sign in to comment.