Skip to content

Commit

Permalink
(chocolatey#23) Implement all keyword for install command
Browse files Browse the repository at this point in the history
This adds the ability to install all packages from a source via the all
keyword. It runs a list against the current sources, and sets the
package names from that list of packages.

This functionality is only intended to be used with internal
repositories. Therefore a list of public repositories has been added,
and the current sources are checked against it to ensure only internal
sources are specified.
  • Loading branch information
TheCakeIsNaOH authored and gep13 committed Apr 23, 2024
1 parent 247fdf1 commit 48ede18
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Security.Principal;
using chocolatey.infrastructure.adapters;
using chocolatey.infrastructure.filesystem;
Expand Down Expand Up @@ -101,6 +102,8 @@ public static class ApplicationParameters
public static readonly string ChocolateyCommunityFeedPushSource = "https://push.chocolatey.org/";
public static readonly string ChocolateyCommunityGalleryUrl = "https://community.chocolatey.org/";
public static readonly string ChocolateyCommunityFeedSource = "https://community.chocolatey.org/api/v2/";
public static readonly string NuGetPublicFeedSourceV2 = "https://www.nuget.org/api/v2/";
public static readonly string NuGetPublicFeedSourceV3 = "https://api.nuget.org/v3/index.json";
public static readonly string ChocolateyLicensedFeedSource = "https://licensedpackages.chocolatey.org/api/v2/";
public static readonly string ChocolateyLicensedFeedSourceName = "chocolatey.licensed";
public static readonly string UserAgent = "Chocolatey Command Line";
Expand All @@ -110,6 +113,15 @@ public static class ApplicationParameters
public static readonly string PowerShellModulePathProcessDocuments = _fileSystem.CombinePaths(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "WindowsPowerShell\\Modules");
public static readonly string LocalSystemSidString = "S-1-5-18";
public static readonly SecurityIdentifier LocalSystemSid = new SecurityIdentifier(LocalSystemSidString);
public static readonly List<string> PublicNuGetSources = new List<string>()
{
ChocolateyCommunityFeedSource,
ChocolateyCommunityFeedPushSource,
ChocolateyCommunityFeedPushSourceOld,
NuGetPublicFeedSourceV2,
NuGetPublicFeedSourceV3,
ChocolateyLicensedFeedSource,
};

private static string GetHttpCacheLocation()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ choco install nodejs.install --version 0.10.35
choco install <path/to/nuspec>
choco install <path/to/nupkg>
NOTE: `all` is a special package keyword that will allow you to install
all packages available on a source. This keyword is not available for
public repositories like the Chocolatey Community Repository, and is
intended to be used with internal package sources only.
NOTE: See scripting in the command reference (`choco -?`) for how to
write proper scripts and integrations.
Expand Down
32 changes: 31 additions & 1 deletion src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ public virtual ConcurrentDictionary<string, PackageResult> Install(ChocolateyCon
_fileSystem.EnsureDirectoryExists(ApplicationParameters.PackagesLocation);
var packageResultsToReturn = new ConcurrentDictionary<string, PackageResult>(StringComparer.InvariantCultureIgnoreCase);

//todo: #23 handle all
SetRemotePackageNamesIfAllSpecified(config, () => { });

NuGetVersion version = !string.IsNullOrWhiteSpace(config.Version) ? NuGetVersion.Parse(config.Version) : null;
if (config.Force)
Expand Down Expand Up @@ -2913,6 +2913,36 @@ private IEnumerable<PackageResult> SetLocalPackageNamesIfAllSpecified(Chocolatey
return allPackages;
}

private void SetRemotePackageNamesIfAllSpecified(ChocolateyConfiguration config, Action customAction)
{
if (config.PackageNames.IsEqualTo(ApplicationParameters.AllPackages))
{
foreach (var repositoryUrl in ApplicationParameters.PublicNuGetSources)
{
if (config.Sources.Contains(repositoryUrl.TrimEnd(('/'))))
{
throw new ApplicationException("Installing all packages from {0} is not supported.{1}Only internal NuGet repositories are supported."
.FormatWith(repositoryUrl, Environment.NewLine));
}
}

var isQuiet = config.QuietOutput;
config.QuietOutput = true;
var input = config.Input;
config.Input = string.Empty;
var remotePackageList = List(config).Select(p => p.Name).Distinct().ToList();
config.QuietOutput = isQuiet;
config.Input = input;

config.PackageNames = remotePackageList.Join(ApplicationParameters.PackageNamesSeparator);

if (customAction != null)
{
customAction.Invoke();
}
}
}

#pragma warning disable IDE0022, IDE1006
[Obsolete("This overload is deprecated and will be removed in v3.")]
public void ensure_source_app_installed(ChocolateyConfiguration config, Action<PackageResult, ChocolateyConfiguration> ensureAction)
Expand Down

0 comments on commit 48ede18

Please sign in to comment.