Skip to content

Commit 47c339c

Browse files
committed
(GH-256) sort packages by name
This change adds a new command line option "order-by", allowing the user to specify a few sorting strategies. By default the results will be sorted by name.
1 parent 0c720d6 commit 47c339c

File tree

6 files changed

+93
-7
lines changed

6 files changed

+93
-7
lines changed

src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ $proUninstallOptions = " --from-programs-and-features"
4343
$proPinOptions = " --note=''"
4444

4545
$commandOptions = @{
46-
list = "--lo --id-only --pre --exact --by-id-only --id-starts-with --detailed --approved-only --not-broken --source='' --user= --password= --local-only --prerelease --include-programs --page= --page-size= --order-by-popularity --download-cache-only" + $proListOptions + $allcommands
47-
search = "--pre --exact --by-id-only --id-starts-with --detailed --approved-only --not-broken --source='' --user= --password= --local-only --prerelease --include-programs --page= --page-size= --order-by-popularity --download-cache-only" + $allcommands
46+
list = "--lo --id-only --pre --exact --by-id-only --id-starts-with --detailed --approved-only --not-broken --source='' --user= --password= --local-only --prerelease --include-programs --page= --page-size= --order-by --order-by-popularity --download-cache-only" + $proListOptions + $allcommands
47+
search = "--pre --exact --by-id-only --id-starts-with --detailed --approved-only --not-broken --source='' --user= --password= --local-only --prerelease --include-programs --page= --page-size= --order-by --order-by-popularity --download-cache-only" + $allcommands
4848
info = "--pre --lo --source='' --user= --password= --local-only --prerelease" + $allcommands
4949
install = "-y -whatif -? --pre --version= --params='' --install-arguments='' --override-arguments --ignore-dependencies --source='' --source='windowsfeatures' --source='webpi' --user= --password= --prerelease --forcex86 --not-silent --package-parameters='' --allow-downgrade --force-dependencies --require-checksums --use-package-exit-codes --ignore-package-exit-codes --skip-automation-scripts --allow-multiple-versions --ignore-checksums --allow-empty-checksums --allow-empty-checksums-secure --download-checksum='' --download-checksum-type='' --download-checksum-x64='' --download-checksum-type-x64='' --stop-on-first-package-failure" + $proInstallUpgradeOptions + $allcommands
5050
pin = "--name= --version= -?" + $proPinOptions + $allcommands

src/chocolatey/chocolatey.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
<Compile Include="infrastructure.app\domain\installers\SquirrelInstaller.cs" />
117117
<Compile Include="infrastructure.app\domain\installers\WindowsUpdateInstaller.cs" />
118118
<Compile Include="infrastructure.app\domain\installers\WiseInstaller.cs" />
119+
<Compile Include="infrastructure.app\domain\PackageOrder.cs" />
119120
<Compile Include="infrastructure.app\domain\RegistryHiveType.cs" />
120121
<Compile Include="infrastructure.app\domain\RegistryValueExtensions.cs" />
121122
<Compile Include="infrastructure.app\domain\RegistryValueKindType.cs" />

src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
namespace chocolatey.infrastructure.app.commands
1818
{
19+
using System;
1920
using System.Collections.Generic;
2021
using System.Linq;
2122
using attributes;
2223
using commandline;
2324
using configuration;
25+
using domain;
2426
using infrastructure.commands;
2527
using logging;
2628
using results;
@@ -102,9 +104,28 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
102104
.Add("id-starts-with",
103105
"IdStartsWith - Only return packages where the id starts with the search filter. Available in 0.9.10+.",
104106
option => configuration.ListCommand.IdStartsWith = option != null)
107+
.Add("o=|order=|order-by=",
108+
"OrderBy - Sort by package results by name (default), title, popularity, lastpublished, unsorted. Available in 0.10.12+.",
109+
option =>
110+
{
111+
PackageOrder packageOrder;
112+
if (Enum.TryParse(option, true, out packageOrder)) {
113+
configuration.ListCommand.OrderBy = packageOrder;
114+
}
115+
else
116+
{
117+
this.Log().Warn("Unknown package order {0}, ignoring".format_with(option));
118+
}
119+
})
105120
.Add("order-by-popularity",
106121
"OrderByPopularity - Sort by package results by popularity. Available in 0.9.10+.",
107-
option => configuration.ListCommand.OrderByPopularity = option != null)
122+
option =>
123+
{
124+
if (option != null)
125+
{
126+
configuration.ListCommand.OrderBy = PackageOrder.popularity;
127+
}
128+
})
108129
.Add("approved-only",
109130
"ApprovedOnly - Only return approved packages - this option will filter out results not from the community repository. Available in 0.9.10+.",
110131
option => configuration.ListCommand.ApprovedOnly = option != null)

src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ public sealed class ListCommandConfiguration
403403
public ListCommandConfiguration()
404404
{
405405
PageSize = 25;
406+
OrderBy = PackageOrder.name;
406407
}
407408

408409
// list
@@ -415,7 +416,7 @@ public ListCommandConfiguration()
415416
public bool ByIdOnly { get; set; }
416417
public bool ByTagOnly { get; set; }
417418
public bool IdStartsWith { get; set; }
418-
public bool OrderByPopularity { get; set; }
419+
public PackageOrder OrderBy { get; set; }
419420
public bool ApprovedOnly { get; set; }
420421
public bool DownloadCacheAvailable { get; set; }
421422
public bool NotBroken { get; set; }
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright © 2017 - 2018 Chocolatey Software, Inc
2+
// Copyright © 2011 - 2017 RealDimensions Software, LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
//
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
namespace chocolatey.infrastructure.app.domain
18+
{
19+
public enum PackageOrder
20+
{
21+
/// <summary>
22+
/// Sort by package name, ascending
23+
/// </summary>
24+
name,
25+
26+
/// <summary>
27+
/// Sort by package title, ascending
28+
/// </summary>
29+
title,
30+
31+
/// <summary>
32+
/// Sort by popularity, most popular first
33+
/// </summary>
34+
popularity,
35+
36+
/// <summary>
37+
/// Sort by last published dates, new to old
38+
/// </summary>
39+
lastpublished,
40+
41+
/// <summary>
42+
/// Leave unsorted, i.e. show in the order in which they were retrieved.
43+
/// </summary>
44+
unsorted
45+
}
46+
}

src/chocolatey/infrastructure.app/nuget/NugetList.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace chocolatey.infrastructure.app.nuget
2222

2323
using NuGet;
2424
using configuration;
25+
using chocolatey.infrastructure.app.domain;
2526

2627
// ReSharper disable InconsistentNaming
2728

@@ -137,9 +138,25 @@ private static IQueryable<IPackage> execute_package_search(ChocolateyConfigurati
137138
.AsQueryable();
138139
}
139140

140-
results = configuration.ListCommand.OrderByPopularity ?
141-
results.OrderByDescending(p => p.DownloadCount).ThenBy(p => p.Id)
142-
: results;
141+
142+
switch (configuration.ListCommand.OrderBy)
143+
{
144+
case PackageOrder.name:
145+
results = results.OrderBy(p => p.Id);
146+
break;
147+
148+
case PackageOrder.title:
149+
results = results.OrderBy(p => p.Title).ThenBy(p => p.Id);
150+
break;
151+
152+
case PackageOrder.popularity:
153+
results = results.OrderByDescending(p => p.DownloadCount).ThenBy(p => p.Id);
154+
break;
155+
156+
case PackageOrder.lastpublished:
157+
results = results.OrderByDescending(p => p.Published).ThenBy(p => p.Id);
158+
break;
159+
}
143160

144161
return results;
145162
}

0 commit comments

Comments
 (0)