chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using OpenTap.Cli;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
 
#pragma warning disable 1591 // TODO: Add XML Comments in this file, then remove this
namespace OpenTap.Package
{
 
    [Display("list", Group: "package", Description: "List locally installed packages and browse the online package repository.")]
    public class PackageListAction : LockingPackageAction
    {
        [CommandLineArgument("repository", Description = CommandLineArgumentRepositoryDescription, ShortName = "r")]
        public string[] Repository { get; set; }
 
        [CommandLineArgument("no-cache", Description = CommandLineArgumentNoCacheDescription)]
        public bool NoCache { get; set; }
 
        [CommandLineArgument("all", Description = "List all versions of <package> even if the OS or the CPU architecture\nare not compatible with the current machine.", ShortName = "a")]
        public bool All { get; set; }
 
        [CommandLineArgument("installed", Description = "Show only installed packages.", ShortName = "i")]
        public bool Installed { get; set; }
 
        [UnnamedCommandLineArgument("package", Description = "The name of the package to list versions for. If omitted, all packages will be listed.")]
        public string Name { get; set; }
 
        [CommandLineArgument("version", Description = CommandLineArgumentVersionDescription)]
        public string Version { get; set; }
 
        [CommandLineArgument("os", Description = CommandLineArgumentOsDescription)]
        public string OS { get; set; }
 
        [CommandLineArgument("architecture", Description = CommandLineArgumentArchitectureDescription)]
        public CpuArchitecture Architecture { get; set; }
 
        public PackageListAction()
        {
            Architecture = ArchitectureHelper.GuessBaseArchitecture;
            OS = null;
        }
 
        protected override int LockedExecute(CancellationToken cancellationToken)
        {
            // OS was explicitly specified. This is interpreted as: Show only packages compatible with that OS. 
            bool checkOs = OS != null;
            OS ??= GuessHostOS();
 
            if (NoCache) PackageManagerSettings.Current.UseLocalPackageCache = false;
            Repository = ExtractRepositoryTokens(Repository, true);
            List<IPackageRepository> repositories = new List<IPackageRepository>();
 
            if (Installed == false)
            {
                repositories = PackageManagerSettings.Current.GetEnabledRepositories(Repository);
            }
 
            Name = AutoCorrectPackageNames.Correct(new[] { Name }, repositories)[0];
 
            if (Target == null)
                Target = FileSystemHelper.GetCurrentInstallationDirectory();
            
            HashSet<PackageDef> installed = new Installation(Target).GetPackages().ToHashSet();
            if (checkOs)
                installed = installed.Where(pkg => pkg.IsOsCompatible(OS)).ToHashSet();
 
            VersionSpecifier versionSpec = VersionSpecifier.Parse("^");
            if (!String.IsNullOrWhiteSpace(Version))
            {
                versionSpec = VersionSpecifier.Parse(Version);
            }
 
            if (string.IsNullOrEmpty(Name))
            {
                var packages = installed.ToList();
                packages.AddRange(PackageRepositoryHelpers.GetPackageNameAndVersionFromAllRepos(repositories, new PackageSpecifier("", versionSpec, Architecture, OS)));
 
                if (Installed)
                    packages = packages.Where(p => installed.Any(i => i.Name == p.Name)).ToList();
 
                PrintReadable(packages, installed);
            }
            else
            {
                IPackageIdentifier package = installed.FirstOrDefault(p => p.Name == Name);
 
                if (Installed)
                {
                    if (package is null)
                    {
                        log.Info($"{Name} is not installed");
                        return (int)ExitCodes.ArgumentError;
                    }
 
                    log.Info(package.Version.ToString());
                    return (int)ExitCodes.Success;
                }
 
 
                List<PackageVersion> versions = null;
 
                if (All)
                {
                    versions = PackageRepositoryHelpers.GetAllVersionsFromAllRepos(repositories, Name).Where(v => v.IsUnlisted == false).Distinct().ToList();
                    var versionsCount = versions.Count;
                    if (versionsCount == 0) // No versions
                    {
                        log.Info($"No versions of '{Name}'.");
                        return (int)ExitCodes.Success;
                    }
 
                    if (Version != null) // Version is specified by user
                        versions = versions.Where(v => versionSpec.IsCompatible(v.Version)).ToList();
                    if(checkOs)
                        versions = versions.Where(v => v.IsOsCompatible(OS)).ToList();
 
                    if (versions.Any() == false && versionsCount > 0)
                    {
                        log.Info($"Package '{Name}' does not exists with version '{Version}'.");
                        log.Info($"Package '{Name}' exists in {versionsCount} other versions, please specify a different version.");
                        return (int)ExitCodes.Success;
                    }
                }
                else
                {
                    var opentap = new Installation(Target).GetOpenTapPackage();
                    versions = PackageRepositoryHelpers.GetAllVersionsFromAllRepos(repositories, Name, opentap).Where(v => v.IsUnlisted == false).Distinct().ToList();
 
                    versions = versions.Where(s => s.IsUnlisted == false && s.IsPlatformCompatible(Architecture, OS)).ToList();
 
                    if (versions.Any() == false) // No compatible versions
                    {
                        versions = PackageRepositoryHelpers.GetAllVersionsFromAllRepos(repositories, Name).Where(v => v.IsUnlisted == false).ToList();
                        if (versions.Any())
                        {
                            log.Warning($"There are no compatible versions of '{Name}'.");
                            log.Info($"There are {versions.Count} incompatible versions available. Use '--all' to show these.");
                        }
                        else
                            log.Warning($"Package '{Name}' could not be found in any repository.");
 
                        return (int)ExitCodes.Success;
                    }
 
 
                    var allVersion = versions;
                    versions = versions.Where(v => versionSpec.IsCompatible(v.Version)).ToList();
                    if (versions.Any() == false) // No versions that are compatible
                    {
                        if (string.IsNullOrEmpty(Version))
                            log.Warning($"There are no released versions of '{Name}'. Showing pre-releases instead.");
                        else
                            log.Warning($"Package '{Name}' does not exists with version '{Version}'.");
 
                        var anyPrereleaseSpecifier = new VersionSpecifier(versionSpec.Major, versionSpec.Minor, versionSpec.Patch, versionSpec.PreRelease, versionSpec.BuildMetadata, VersionMatchBehavior.AnyPrerelease | versionSpec.MatchBehavior);
                        versions = allVersion.Where(v => anyPrereleaseSpecifier.IsCompatible(v.Version)).ToList();
                        if (versions.Any())
                            PrintVersionsReadable(package, versions);
 
                        return (int)ExitCodes.Success;
                    }
                }
                PrintVersionsReadable(package, versions);
            }
            return (int)ExitCodes.Success;
        }
 
        private void PrintVersionsReadable(IPackageIdentifier package, List<PackageVersion> versions)
        {
            var verLen = versions.Select(p => p.Version?.ToString().Length).Max() ?? 0;
            var arcLen = versions.Select(p => p?.Architecture.ToString().Length).Max() ?? 0;
            var osLen = versions.Select(p => p.OS?.Length).Max() ?? 0;
            foreach (var version in versions.OrderBy(x => x.Version))
            {
                // string interpolate + format complex to add padding.
                log.Info(string.Format($"{{0,-{verLen}}} - {{1,-{arcLen}}} - {{2,-{osLen}}} - {{3}}", version.Version, version.Architecture, version.OS ?? "Unknown", package != null && package.Equals(version) ? "installed" : ""));
            }
        }
 
        private void PrintReadable(List<PackageDef> packages, HashSet<PackageDef> installed)
        {
            var packageList = packages.GroupBy(p => p.Name).Select(x => x.OrderByDescending(p => p.Version).First()).OrderBy(x => x.Name).ToList();
 
            if (packageList.Count == 0)
            {
                log.Info("Selected directory has no packages installed.");
                return;
            }
 
            var nameLen = packageList.Select(p => p.Name?.Length).Max() ?? 0;
            var verLen = packageList.Select(p => p.Version?.ToString().Length).Max() ?? 0;
            verLen = Math.Max(verLen, installed.Select(p => p.Version?.ToString().Length).Max() ?? 0);
            
            foreach (var plugin in packageList)
            {
                var installedPackage = installed.FirstOrDefault(p => p.Name == plugin.Name);
                var latestPackage = packages.Where(p => p.Name == plugin.Name).OrderByDescending(p => p.Version).FirstOrDefault();
 
                bool isInstalled = installedPackage != null;
                bool isSystemWide = installedPackage?.IsSystemWide() ?? false;
                bool isValid = installedPackage?.IsValid() ?? false;
                string installedString = "";
                if (isInstalled)
                {
                    installedString = "-";
                    if (isValid)
                        installedString += " installed";
                    if(isSystemWide)
                        installedString += " system-wide";
                    if (!isValid)
                    {
                        if (installedString != "-")
                            installedString += " -";
                        installedString += " needs reinstall";
                    }
                }
 
                // string interpolate + format complex to add padding.
                string logMessage = string.Format($"{{0,-{nameLen}}} - {{1,-{verLen}}} {{2}}", plugin.Name, (installedPackage ?? plugin).Version, installedString);
 
                if (installedPackage != null && installedPackage?.Version?.CompareTo(latestPackage.Version) < 0)
                    logMessage += $" - update available ({latestPackage.Version})";
 
                // assuming that all dlls in the package requires has the same or distinct license requirements. 
                // Duplicates are made if one file requires X|Y and the other X|Z or even Y|X.
                var licensesRequiredStrings = plugin.Files.Select(p => p.LicenseRequired).Where(l => string.IsNullOrWhiteSpace(l) == false).Select(l => LicenseBase.FormatFriendly(l)).Distinct();
 
                var licenses = string.Join(" & ", licensesRequiredStrings);
 
                if (licenses != "")
                    logMessage += " - requires license " + licenses;
 
                log.Info(logMessage);
            }
        }
 
    }
 
}