Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
csq supports running any squirrel version from nuget instead of conta…
Browse files Browse the repository at this point in the history
…ining an embedded version
  • Loading branch information
caesay committed Aug 19, 2022
1 parent 0fe9f8a commit 7dee4ca
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 98 deletions.
14 changes: 4 additions & 10 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,24 @@ msbuild /verbosity:minimal /restore /p:Configuration=Release
# Build single-exe packaged projects and drop into nupkg
Write-Host "Extracting Generated Packages" -ForegroundColor Magenta
Set-Location "$PSScriptRoot\build\Release"
seven x csq*.nupkg -ocsq
seven x Clowd.Squirrel*.nupkg -osquirrel
Remove-Item *.nupkg
Remove-Item Clowd.Squirrel*.nupkg

Write-Host "Publishing SingleFile Projects" -ForegroundColor Magenta
$ToolsDir = "csq\tools\net6.0\any"
$ToolsDir = "squirrel\tools"
dotnet publish -v minimal --no-build -c Release --no-self-contained "$PSScriptRoot\src\Squirrel.CommandLine\Squirrel.CommandLine.csproj" -o "$ToolsDir"
dotnet publish -v minimal --no-build -c Release --self-contained "$PSScriptRoot\src\Update.Windows\Update.Windows.csproj" -o "$ToolsDir"
dotnet publish -v minimal --no-build -c Release --self-contained "$PSScriptRoot\src\Update.OSX\Update.OSX.csproj" -o "$ToolsDir"

Write-Host "Copying Tools" -ForegroundColor Magenta
# First, copy all the tools into the 'csq' package
# Copy all the tools into the 'csq' package
Copy-Item -Path "$PSScriptRoot\vendor\*" -Destination $ToolsDir -Recurse
Copy-Item -Path "Win32\*" -Destination $ToolsDir
Copy-Item -Path "$PSScriptRoot\Squirrel.entitlements" -Destination "$ToolsDir"
Remove-Item "$ToolsDir\*.pdb"
Remove-Item "$ToolsDir\7za.exe"

# Second, copy all the csq files into the 'squirrel' package
New-Item -Path "squirrel" -Name "tools" -ItemType "directory"
Copy-Item -Path "$ToolsDir\*" -Destination "squirrel\tools" -Recurse
Remove-Item "squirrel\tools\*.xml"

Write-Host "Re-assembling Packages" -ForegroundColor Magenta
seven a "csq.$version.nupkg" -tzip -mx9 "$PSScriptRoot\build\Release\csq\*"
seven a "Clowd.Squirrel.$version.nupkg" -tzip -mx9 "$PSScriptRoot\build\Release\squirrel\*"

Write-Host "Done." -ForegroundColor Magenta
2 changes: 2 additions & 0 deletions src/Squirrel.CommandLine/Squirrel.CommandLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
<UseAppHost>false</UseAppHost>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);CA2007</NoWarn>
</PropertyGroup>
Expand Down
75 changes: 75 additions & 0 deletions src/Squirrel.Tool/NugetDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Packaging.Core;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;

namespace Squirrel.Tool
{
public class NugetDownloader
{
private readonly ILogger _logger;
private readonly PackageSource _packageSource;
private readonly SourceRepository _sourceRepository;
private readonly SourceCacheContext _sourceCacheContext;

public NugetDownloader(ILogger logger)
{
_logger = logger;
_packageSource = new PackageSource("https://api.nuget.org/v3/index.json", "NuGet.org");
_sourceRepository = new SourceRepository(_packageSource, Repository.Provider.GetCoreV3());
_sourceCacheContext = new SourceCacheContext();
}

public IPackageSearchMetadata GetPackageMetadata(string packageName, string version)
{
PackageMetadataResource packageMetadataResource = _sourceRepository.GetResource<PackageMetadataResource>();
FindPackageByIdResource packageByIdResource = _sourceRepository.GetResource<FindPackageByIdResource>();
IPackageSearchMetadata package = null;

var prerelease = version?.Equals("pre", StringComparison.InvariantCultureIgnoreCase) == true;
if (version == null || version.Equals("latest", StringComparison.InvariantCultureIgnoreCase) || prerelease) {
// get latest (or prerelease) version
IEnumerable<IPackageSearchMetadata> metadata = packageMetadataResource
.GetMetadataAsync(packageName, true, true, _sourceCacheContext, _logger, CancellationToken.None)
.GetAwaiter().GetResult();
package = metadata
.Where(x => x.IsListed)
.Where(x => prerelease || !x.Identity.Version.IsPrerelease)
.OrderByDescending(x => x.Identity.Version)
.FirstOrDefault();
} else {
// resolve version ranges and wildcards
var versions = packageByIdResource.GetAllVersionsAsync(packageName, _sourceCacheContext, _logger, CancellationToken.None)
.GetAwaiter().GetResult();
var resolved = versions.FindBestMatch(VersionRange.Parse(version), version => version);

// get exact version
var packageIdentity = new PackageIdentity(packageName, resolved);
package = packageMetadataResource
.GetMetadataAsync(packageIdentity, _sourceCacheContext, _logger, CancellationToken.None)
.GetAwaiter().GetResult();
}

if (package == null) {
throw new Exception($"Unable to locate {packageName} {version} on NuGet.org");
}

return package;
}

public void DownloadPackageToStream(IPackageSearchMetadata package, Stream targetStream)
{
FindPackageByIdResource packageByIdResource = _sourceRepository.GetResource<FindPackageByIdResource>();
packageByIdResource
.CopyNupkgToStreamAsync(package.Identity.Id, package.Identity.Version, targetStream, _sourceCacheContext, _logger, CancellationToken.None)
.GetAwaiter().GetResult();
}
}
}
Loading

0 comments on commit 7dee4ca

Please sign in to comment.