forked from dotnet/templating
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplateDiscoveryCommand.cs
146 lines (124 loc) · 6.89 KB
/
TemplateDiscoveryCommand.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.CommandLine;
using Microsoft.TemplateSearch.TemplateDiscovery.NuGet;
using Microsoft.TemplateSearch.TemplateDiscovery.PackChecking;
using Microsoft.TemplateSearch.TemplateDiscovery.Results;
using Microsoft.TemplateSearch.TemplateDiscovery.Test;
namespace Microsoft.TemplateSearch.TemplateDiscovery
{
internal class TemplateDiscoveryCommand : Command
{
private const int DefaultPageSize = 100;
private readonly Option<DirectoryInfo> _basePathOption = new("--basePath")
{
Arity = ArgumentArity.ExactlyOne,
Description = "The root dir for output for this run.",
Required = true
};
private readonly Option<bool> _allowPreviewPacksOption = new("--allowPreviewPacks")
{
Description = "Include preview packs in the results (by default, preview packs are ignored and the latest stable pack is used.",
};
private readonly Option<int> _pageSizeOption = new("--pageSize")
{
Description = "(debugging) The chunk size for interactions with the source.",
DefaultValueFactory = (r) => DefaultPageSize,
};
private readonly Option<bool> _onePageOption = new("--onePage")
{
Description = "(debugging) Only process one page of template packs.",
};
private readonly Option<bool> _savePacksOption = new("--savePacks")
{
Description = "Don't delete downloaded candidate packs (by default, they're deleted at the end of a run).",
};
private readonly Option<bool> _noTemplateJsonFilterOption = new("--noTemplateJsonFilter")
{
Description = "Don't prefilter packs that don't contain any template.json files (this filter is applied by default).",
};
private readonly Option<bool> _verboseOption = new("--verbose", "-v")
{
Description = "Verbose output for template processing.",
};
private readonly Option<bool> _testOption = new("--test", "-t")
{
Description = "Run tests on generated metadata files.",
};
private readonly Option<SupportedQueries[]> _queriesOption = new("--queries")
{
Arity = ArgumentArity.OneOrMore,
Description = $"The list of providers to run. Supported providers: {string.Join(",", Enum.GetValues<SupportedQueries>())}.",
AllowMultipleArgumentsPerToken = true,
};
private readonly Option<DirectoryInfo> _packagesPathOption = new Option<DirectoryInfo>("--packagesPath")
{
Description = "Path to pre-downloaded packages. If specified, the packages won't be downloaded from NuGet.org."
}.AcceptExistingOnly();
private readonly Option<bool> _diffOption = new("--diff")
{
Description = "The list of packages will be compared with previous run, and if package version is not changed, the package won't be rescanned.",
DefaultValueFactory = (r) => true,
};
private readonly Option<FileInfo> _diffOverrideCacheOption = new Option<FileInfo>("--diff-override-cache")
{
Description = "Location of current search cache (local path only).",
}.AcceptExistingOnly();
private readonly Option<FileInfo> _diffOverrideNonPackagesOption = new Option<FileInfo>("--diff-override-non-packages")
{
Description = "Location of the list of packages known not to be a valid package (local path only).",
}.AcceptExistingOnly();
public TemplateDiscoveryCommand() : base("template-discovery", "Generates the template package search cache file based on the packages available on NuGet.org.")
{
_basePathOption.AcceptLegalFilePathsOnly();
_queriesOption.AcceptOnlyFromAmong(Enum.GetValues<SupportedQueries>().Select(e => e.ToString()).ToArray());
Options.Add(_basePathOption);
Options.Add(_allowPreviewPacksOption);
Options.Add(_pageSizeOption);
Options.Add(_onePageOption);
Options.Add(_savePacksOption);
Options.Add(_noTemplateJsonFilterOption);
Options.Add(_testOption);
Options.Add(_queriesOption);
Options.Add(_packagesPathOption);
Options.Add(_verboseOption);
Options.Add(_diffOption);
Options.Add(_diffOverrideCacheOption);
Options.Add(_diffOverrideNonPackagesOption);
TreatUnmatchedTokensAsErrors = true;
SetAction(async (ParseResult parseResult, CancellationToken cancellationToken) =>
{
var config = new CommandArgs(parseResult.GetValue(_basePathOption) ?? throw new Exception("Output path is not set"))
{
LocalPackagePath = parseResult.GetValue(_packagesPathOption),
PageSize = parseResult.GetValue(_pageSizeOption),
SaveCandidatePacks = parseResult.GetValue(_savePacksOption),
RunOnlyOnePage = parseResult.GetValue(_onePageOption),
IncludePreviewPacks = parseResult.GetValue(_allowPreviewPacksOption),
DontFilterOnTemplateJson = parseResult.GetValue(_noTemplateJsonFilterOption),
Verbose = parseResult.GetValue(_verboseOption),
TestEnabled = parseResult.GetValue(_testOption),
Queries = parseResult.GetValue(_queriesOption) ?? [],
DiffMode = parseResult.GetValue(_diffOption),
DiffOverrideSearchCacheLocation = parseResult.GetValue(_diffOverrideCacheOption),
DiffOverrideKnownPackagesLocation = parseResult.GetValue(_diffOverrideNonPackagesOption),
};
await ExecuteAsync(config, cancellationToken).ConfigureAwait(false);
return 0;
});
}
private static async Task ExecuteAsync(CommandArgs config, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Verbose.IsEnabled = config.Verbose;
IPackCheckerFactory factory = config.LocalPackagePath == null ? new NuGetPackSourceCheckerFactory() : new TestPackCheckerFactory();
PackSourceChecker packSourceChecker = await factory.CreatePackSourceCheckerAsync(config, cancellationToken).ConfigureAwait(false);
PackSourceCheckResult checkResults = await packSourceChecker.CheckPackagesAsync(cancellationToken).ConfigureAwait(false);
(string metadataPath, string legacyMetadataPath) = PackCheckResultReportWriter.WriteResults(config.OutputPath, checkResults);
if (config.TestEnabled)
{
CacheFileTests.RunTests(metadataPath, legacyMetadataPath);
}
}
}
}