Skip to content

Commit

Permalink
fix build error caused by package latest detection problem (#21)
Browse files Browse the repository at this point in the history
* fix build error caused by package latest detection problem

* temp change 1

* temp change 2

* hopufully now...
  • Loading branch information
gasparnagy authored Jun 10, 2024
1 parent 8d266cd commit f6848b8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected override void BuildProject()
args.Add($"\"https://api.nuget.org/v3/index.json;{_options.FallbackNuGetPackageSource}\"");
}

var exitCode = ExecDotNet(args.ToArray());
var exitCode = ExecDotNet(args.ToArray()).ExitCode;
if (exitCode != 0)
{
_consoleWriteLine($"dotnet restore exit code: {exitCode}");
Expand All @@ -51,5 +51,5 @@ protected override void BuildProject()
base.BuildProject();
}

protected override int ExecBuild() => ExecDotNet("build", "--no-restore");
protected override int ExecBuild() => ExecDotNet("build", "--no-restore").ExitCode;
}
33 changes: 31 additions & 2 deletions Tests/Reqnroll.SampleProjectGenerator.Core/ProjectGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#nullable disable

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Reqnroll.SampleProjectGenerator;

public abstract class ProjectGenerator : IProjectGenerator
Expand Down Expand Up @@ -319,12 +322,34 @@ private void InstallReqnroll(string packagesFolder, string projectFilePath)
protected void InstallNuGetPackage(ProjectChanger projectChanger, string packagesFolder, string packageName,
string sourcePlatform = "net462", string packageVersion = null, bool dependency = false)
{
packageVersion ??= DetectLatestPackage(packageName);
var package =
projectChanger.InstallNuGetPackage(packagesFolder, packageName, sourcePlatform, packageVersion, dependency);
if (package != null)
InstalledNuGetPackages.Add(package);
}

private readonly Dictionary<string, string> _latestVersionCache = new();

private string DetectLatestPackage(string packageName)
{
if (_latestVersionCache.TryGetValue(packageName, out var version))
return version;

var result = ExecDotNet("package", "search", packageName, "--source", "https://api.nuget.org/v3/index.json", "--exact-match", "--format", "json");
string latestVersion = null;
if (result.ExitCode == 0)
{
var resultJson = JObject.Parse(result.StdOutput);
var lastPackage = (resultJson["searchResult"] as JArray)?.FirstOrDefault()?["packages"]?.LastOrDefault();
latestVersion = lastPackage?["latestVersion"]?.Value<string>() ?? lastPackage?["version"]?.Value<string>();
}

_latestVersionCache[packageName] = latestVersion;

return latestVersion;
}

private void EnsureEmptyFolder(string folder)
{
if (!Directory.Exists(folder))
Expand Down Expand Up @@ -383,17 +408,21 @@ private int ExecGit(params string[] args) => Exec(_options.TargetFolder,
ToolLocator.GetToolPath(ExternalTools.Git, _consoleWriteLine), args);


protected int ExecDotNet(params string[] args) => Exec(_options.TargetFolder,
protected ProcessResult ExecDotNet(params string[] args) => ExecInternal(_options.TargetFolder,
Environment.ExpandEnvironmentVariables(@"%ProgramW6432%\dotnet\dotnet.exe"), args);

protected int Exec(string workingDirectory, string tool, params string[] args)
=> ExecInternal(workingDirectory, tool, args).ExitCode;


protected ProcessResult ExecInternal(string workingDirectory, string tool, string[] args)
{
var arguments = string.Join(" ", args);
_consoleWriteLine($"{tool} {arguments}");

var psi = new ProcessStartInfoEx(workingDirectory, tool, arguments);
var ph = new ProcessHelper();
ProcessResult result = ph.RunProcess(psi, _consoleWriteLine);
return result.ExitCode;
return result;
}
}

0 comments on commit f6848b8

Please sign in to comment.