Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Fallout.Cli/Commands/AddPackageCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Linq;
using Fallout.Common;
using Fallout.Common.Execution;
using Fallout.Common.IO;
using Fallout.Solutions;
using Fallout.Common.Tooling;
using Fallout.Common.Tools.DotNet;

namespace Fallout.Cli.Commands;

/// <summary>
/// <c>fallout :add-package</c>: adds (or upgrades) a NuGet package reference in the build project.
/// </summary>
public sealed class AddPackageCommand : IFalloutCommand
{
public string Name => "add-package";

public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript)
{
Program.PrintInfo();
Logging.Configure();
Telemetry.AddPackage();
ProjectModelTasks.Initialize();

var packageId = args.ElementAt(0);
var packageVersion =
(EnvironmentInfo.GetNamedArgument<string>("version") ??
args.ElementAtOrDefault(1) ??
NuGetVersionResolver.GetLatestVersion(packageId, includePrereleases: false).GetAwaiter().GetResult() ??
NuGetPackageResolver.GetGlobalInstalledPackage(packageId, version: null, packagesConfigFile: null)?.Version.ToString())
.NotNull("packageVersion != null");

// GetConfiguration / AddOrReplacePackage / BUILD_PROJECT_FILE / PACKAGE_TYPE_* are shared
// helpers still on Program; they move into services in the final #392 collapse PR.
var configuration = Program.GetConfiguration(buildScript, evaluate: true);
var buildProjectFile = configuration[Program.BUILD_PROJECT_FILE];
Host.Information($"Installing {packageId}/{packageVersion} to {buildProjectFile} ...");
Program.AddOrReplacePackage(packageId, packageVersion, Program.PACKAGE_TYPE_DOWNLOAD, buildProjectFile);
DotNetTasks.DotNet($"restore {buildProjectFile}");

var installedPackage = NuGetPackageResolver.GetGlobalInstalledPackage(packageId, packageVersion, packagesConfigFile: null)
.NotNull("installedPackage != null");
var hasToolsDirectory = installedPackage.Directory.GlobDirectories("tools").Any();
if (!hasToolsDirectory)
Program.AddOrReplacePackage(packageId, packageVersion, Program.PACKAGE_TYPE_REFERENCE, buildProjectFile);

Host.Information($"Done installing {packageId}/{packageVersion} to {buildProjectFile}");
return 0;
}
}
33 changes: 2 additions & 31 deletions src/Fallout.Cli/Program.AddPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,8 @@ partial class Program
public const string PACKAGE_TYPE_DOWNLOAD = "PackageDownload";
public const string PACKAGE_TYPE_REFERENCE = "PackageReference";

public static int AddPackage(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript)
{
PrintInfo();
Logging.Configure();
Telemetry.AddPackage();
ProjectModelTasks.Initialize();

var packageId = args.ElementAt(0);
var packageVersion =
(EnvironmentInfo.GetNamedArgument<string>("version") ??
args.ElementAtOrDefault(1) ??
NuGetVersionResolver.GetLatestVersion(packageId, includePrereleases: false).GetAwaiter().GetResult() ??
NuGetPackageResolver.GetGlobalInstalledPackage(packageId, version: null, packagesConfigFile: null)?.Version.ToString())
.NotNull("packageVersion != null");

var configuration = GetConfiguration(buildScript, evaluate: true);
var buildProjectFile = configuration[BUILD_PROJECT_FILE];
Host.Information($"Installing {packageId}/{packageVersion} to {buildProjectFile} ...");
AddOrReplacePackage(packageId, packageVersion, PACKAGE_TYPE_DOWNLOAD, buildProjectFile);
DotNetTasks.DotNet($"restore {buildProjectFile}");

var installedPackage = NuGetPackageResolver.GetGlobalInstalledPackage(packageId, packageVersion, packagesConfigFile: null)
.NotNull("installedPackage != null");
var hasToolsDirectory = installedPackage.Directory.GlobDirectories("tools").Any();
if (!hasToolsDirectory)
AddOrReplacePackage(packageId, packageVersion, PACKAGE_TYPE_REFERENCE, buildProjectFile);

Host.Information($"Done installing {packageId}/{packageVersion} to {buildProjectFile}");
return 0;
}

// Residual after the :add-package command moved to AddPackageCommand: this helper is shared with
// cake and moves into a package service in the #392 collapse PR.
internal static void AddOrReplacePackage(string packageId, string packageVersion, string packageType, string buildProjectFile)
{
var buildProject = ProjectModelTasks.ParseProject(buildProjectFile).NotNull();
Expand Down
2 changes: 1 addition & 1 deletion src/Fallout.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ private static void RegisterCommands(IServiceCollection services)
services.AddSingleton<IFalloutCommand, TriggerCommand>();
services.AddSingleton<IFalloutCommand, CompleteCommand>();
services.AddSingleton<IFalloutCommand, GetConfigurationCommand>();
services.AddSingleton<IFalloutCommand, AddPackageCommand>();

// Legacy handlers still living on Program, adapted until they are extracted into command
// types. Each conversion deletes one line here plus its Program.X.cs partial.
services.AddSingleton<IFalloutCommand>(new DelegateCommand("setup", Setup));
services.AddSingleton<IFalloutCommand>(new DelegateCommand("update", Update));
services.AddSingleton<IFalloutCommand>(new DelegateCommand("add-package", AddPackage));
services.AddSingleton<IFalloutCommand>(new DelegateCommand("cake-convert", CakeConvert));
services.AddSingleton<IFalloutCommand>(new DelegateCommand("cake-clean", CakeClean));
services.AddSingleton<IFalloutCommand>(new DelegateCommand("secrets", Secrets));
Expand Down