Skip to content

Commit

Permalink
Add CPM & multiple package manager commands
Browse files Browse the repository at this point in the history
  • Loading branch information
devlead committed Nov 20, 2024
1 parent 0ec9961 commit 3f92daf
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 116 deletions.
12 changes: 11 additions & 1 deletion src/Bootstrap/dist/css/bootstrap-theme.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion src/Bootstrap/less/theme/page-display-package.less
Original file line number Diff line number Diff line change
Expand Up @@ -488,14 +488,25 @@

.copy-button {
display: flex;

button {
display: flex;
gap: 4px;
padding: 2px 8px;
justify-content: center;
align-items: center;
}

.package-manager-command-header {
display: inline;
font-size: 14px;
font-family: @font-family-base;
padding: 0px 8px;
line-height: 75%;
color: var(--neutralForeground1Rest);
justify-content: center;
align-items: center;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/NuGetGallery/Content/gallery/css/bootstrap.min.css

Large diffs are not rendered by default.

45 changes: 26 additions & 19 deletions src/NuGetGallery/Extensions/CakeBuildManagerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;

namespace NuGetGallery
Expand All @@ -13,7 +14,12 @@ public static bool IsCakeExtension(this DisplayPackageViewModel model)
return IsCakeAddin(model) || IsCakeModule(model) || IsCakeRecipe(model);
}

public static string GetCakeInstallPackageCommand(this DisplayPackageViewModel model)
public static PackageManagerViewModel.InstallPackageCommand[] GetCakeInstallPackageCommands(this DisplayPackageViewModel model)
=> model
.EnumerateCakeInstallPackageCommands()
.ToArray();

public static IEnumerable<PackageManagerViewModel.InstallPackageCommand> EnumerateCakeInstallPackageCommands(this DisplayPackageViewModel model)
{
var scheme = model.IsDotnetToolPackageType ? "dotnet" : "nuget";
var reference = $"{scheme}:?package={model.Id}&version={model.Version}";
Expand All @@ -25,31 +31,32 @@ public static string GetCakeInstallPackageCommand(this DisplayPackageViewModel m

if (model.IsDotnetToolPackageType)
{
return $"#tool {reference}";
yield return new PackageManagerViewModel.InstallPackageCommand($"#tool {reference}");
}

if (IsCakeAddin(model))
else if (IsCakeAddin(model))
{
return $"#addin {reference}";
yield return new PackageManagerViewModel.InstallPackageCommand($"#addin {reference}");
}

if (IsCakeModule(model))
else if (IsCakeModule(model))
{
return $"#module {reference}";
yield return new PackageManagerViewModel.InstallPackageCommand($"#module {reference}");
}

if (IsCakeRecipe(model))
else if (IsCakeRecipe(model))
{
return $"#load {reference}";
yield return new PackageManagerViewModel.InstallPackageCommand($"#load {reference}");
}
else
{
yield return new PackageManagerViewModel.InstallPackageCommand(
$"Install {model.Id} as a Cake Addin",
$"#addin {reference}"
);

return string.Join(Environment.NewLine,
$"// Install {model.Id} as a Cake Addin",
$"#addin {reference}",
"",
$"// Install {model.Id} as a Cake Tool",
$"#tool {reference}"
);
yield return new PackageManagerViewModel.InstallPackageCommand(
$"Install {model.Id} as a Cake Tool",
$"#tool {reference}"
);
};
}

private static bool IsCakeAddin(ListPackageItemViewModel model) =>
Expand Down
46 changes: 40 additions & 6 deletions src/NuGetGallery/ViewModels/PackageManagerViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace NuGetGallery
{
/// <summary>
/// The basic model for a client that conforms to the NuGet protocol.
/// </summary>
public class PackageManagerViewModel
{
public PackageManagerViewModel(string name)
public PackageManagerViewModel(
string id,
string name,
params InstallPackageCommand[] installPackageCommands
)
{
Id = id;
Name = name;
CopyLabel = string.Format("Copy the {0} command", name);
int index = 0;
InstallPackageCommands = installPackageCommands.ToDictionary(
_ => string.Format(CultureInfo.InvariantCulture, "{0}-{1:0000}", Id, ++index),
value => value,
StringComparer.OrdinalIgnoreCase
);
}

/// <summary>
/// The package manager's name.
/// </summary>
public string Name { get; set; }
public string Name { get; }

/// <summary>
/// A unique identifier that represents this package manager.
/// </summary>
public string Id { get; set; }
public string Id { get; }

/// <summary>
/// The non-selectable prefix displayed on before the package manager's commands.
Expand All @@ -32,7 +48,7 @@ public PackageManagerViewModel(string name)
/// <summary>
/// One or more strings that represent the command(s) used to install a specific package.
/// </summary>
public string[] InstallPackageCommands { get; set; }
public Dictionary<string, InstallPackageCommand> InstallPackageCommands { get; }

/// <summary>
/// The alert message that contains clarifications about the command/scenario
Expand All @@ -48,6 +64,24 @@ public PackageManagerViewModel(string name)
/// The label for the copy button.
/// </summary>
public string CopyLabel { get; set; }

public class InstallPackageCommand
{
public InstallPackageCommand(string command) : this(string.Empty, command)
{
}

public InstallPackageCommand(string header, string command)
{
HasHeader = !string.IsNullOrWhiteSpace(header);
Header = header;
Command = command;
}

public bool HasHeader { get; }
public string Header { get; }
public string Command { get; }
}
}

public enum AlertLevel
Expand All @@ -56,4 +90,4 @@ public enum AlertLevel
Info,
Warning
}
}
}
15 changes: 12 additions & 3 deletions src/NuGetGallery/ViewModels/ThirdPartyPackageManagerViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace NuGetGallery
Expand All @@ -15,7 +15,16 @@ public class ThirdPartyPackageManagerViewModel : PackageManagerViewModel
/// </summary>
public string ContactUrl { get; set; }

public ThirdPartyPackageManagerViewModel(string name, string contactUrl) : base(name)
public ThirdPartyPackageManagerViewModel(
string id,
string name,
string contactUrl,
params InstallPackageCommand[] installPackageCommands
) : base(
id,
name,
installPackageCommands
)
{
ContactUrl = contactUrl;
AlertLevel = AlertLevel.Warning;
Expand All @@ -24,4 +33,4 @@ public ThirdPartyPackageManagerViewModel(string name, string contactUrl) : base(
+ $"<a href=\"{contactUrl}\" aria-label=\"Contact the maintainers of the {name} client\">maintainers</a> for support.";
}
}
}
}
Loading

0 comments on commit 3f92daf

Please sign in to comment.