-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRunCommand.cs
152 lines (126 loc) · 5.57 KB
/
RunCommand.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
147
148
149
150
151
152
// ReSharper disable SuggestBaseTypeForParameter
namespace Atc.CodingRules.Updater.CLI.Commands;
[SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "OK.")]
public class RunCommand : AsyncCommand<RunCommandSettings>
{
private readonly ILogger<RunCommand> logger;
public RunCommand(
ILogger<RunCommand> logger)
=> this.logger = logger;
public override Task<int> ExecuteAsync(
CommandContext context,
RunCommandSettings settings)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(settings);
return ExecuteInternalAsync(settings);
}
[SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "OK.")]
private async Task<int> ExecuteInternalAsync(
RunCommandSettings settings)
{
if (!NetworkInformationHelper.HasHttpConnection())
{
System.Console.WriteLine("This tool requires internet connection!");
return ConsoleExitStatusCodes.Failure;
}
ConsoleHelper.WriteHeader();
var projectPath = new DirectoryInfo(settings.ProjectPath);
var options = await GetOptionsFromFileAndUserArguments(settings, projectPath);
try
{
CodingRulesUpdaterVersionHelper.PrintUpdateInfoIfNeeded(logger);
await ProjectHelper.HandleFiles(
logger,
projectPath,
options);
if (DirectoryBuildPropsHelper.HasFileInsertPlaceholderElement(projectPath, "OrganizationName", "insert organization name here"))
{
var organizationName = settings.OrganizationName is not null && settings.OrganizationName.IsSet
? settings.OrganizationName.Value
: AnsiConsole.Ask<string>("What is the [green]Organization name[/]?");
DirectoryBuildPropsHelper.UpdateFileInsertPlaceholderElement(logger, projectPath, "OrganizationName", "insert organization name here", organizationName);
}
if (DirectoryBuildPropsHelper.HasFileInsertPlaceholderElement(projectPath, "RepositoryName", "insert repository name here"))
{
var repositoryName = settings.RepositoryName is not null && settings.RepositoryName.IsSet
? settings.RepositoryName.Value
: AnsiConsole.Ask<string>("What is the [green]Repository name[/]?");
DirectoryBuildPropsHelper.UpdateFileInsertPlaceholderElement(logger, projectPath, "RepositoryName", "insert repository name here", repositoryName);
}
}
catch (Exception ex)
{
logger.LogError($"{EmojisConstants.Error} {ex.Message}");
return ConsoleExitStatusCodes.Failure;
}
logger.LogInformation($"{EmojisConstants.Success} Done");
return ConsoleExitStatusCodes.Success;
}
private static async Task<OptionsFile> GetOptionsFromFileAndUserArguments(
RunCommandSettings settings,
DirectoryInfo projectPath)
{
var optionsPath = settings.GetOptionsPath();
var options = await OptionsHelper.CreateDefault(projectPath, optionsPath);
options.Mappings.ResolvePaths(projectPath);
var projectTarget = ProjectCommandSettings.GetProjectTarget(settings);
if (projectTarget is not null)
{
options.ProjectTarget = (SupportedProjectTargetType)projectTarget;
}
if (settings.UseLatestMinorNugetVersion.HasValue)
{
options.UseLatestMinorNugetVersion = settings.UseLatestMinorNugetVersion.GetValueOrDefault();
}
if (settings.UseTemporarySuppressions.HasValue)
{
options.UseTemporarySuppressions = settings.UseTemporarySuppressions.GetValueOrDefault();
}
var temporarySuppressionsPath = GetTemporarySuppressionsPath(settings);
if (temporarySuppressionsPath is not null &&
temporarySuppressionsPath.Exists)
{
options.TemporarySuppressionsPath = temporarySuppressionsPath.FullName;
}
if (settings.TemporarySuppressionAsExcel.HasValue)
{
options.TemporarySuppressionAsExcel = settings.TemporarySuppressionAsExcel.GetValueOrDefault();
}
var buildFile = GetBuildFile(settings, projectPath);
if (buildFile is not null)
{
options.BuildFile = buildFile.FullName;
}
return options;
}
private static DirectoryInfo? GetTemporarySuppressionsPath(
RunCommandSettings settings)
{
var temporarySuppressionsPath = string.Empty;
if (settings.TemporarySuppressionsPath is not null && settings.TemporarySuppressionsPath.IsSet)
{
temporarySuppressionsPath = settings.TemporarySuppressionsPath.Value;
}
return !string.IsNullOrEmpty(temporarySuppressionsPath)
? new DirectoryInfo(temporarySuppressionsPath)
: null;
}
private static FileInfo? GetBuildFile(
RunCommandSettings settings,
DirectoryInfo projectPath)
{
var buildFile = string.Empty;
if (settings.BuildFile is not null && settings.BuildFile.IsSet)
{
buildFile = settings.BuildFile.Value;
}
if (!string.IsNullOrEmpty(buildFile))
{
return buildFile.Contains(':', StringComparison.Ordinal)
? new FileInfo(buildFile)
: new FileInfo(Path.Combine(projectPath.FullName, buildFile));
}
return null;
}
}