Skip to content

Commit b62cacf

Browse files
authored
Misc Json PostAction fixes (#50977)
1 parent 9798e3b commit b62cacf

File tree

20 files changed

+195
-19
lines changed

20 files changed

+195
-19
lines changed

src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.Designer.cs

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.resx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,4 +953,10 @@ The header is followed by the list of parameters and their errors (might be seve
953953
<data name="PostAction_ModifyJson_Verbose_AttemptingToFindJsonFile" xml:space="preserve">
954954
<value>Attempting to find json file '{0}' in '{1}'</value>
955955
</data>
956-
</root>
956+
<data name="PostAction_ModifyJson_Error_NullJson" xml:space="preserve">
957+
<value>The result of parsing the following JSON was 'null':
958+
959+
{0}</value>
960+
<comment>{0} is the JSON that is being parsed.</comment>
961+
</data>
962+
</root>

src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ internal class AddJsonPropertyPostActionProcessor : PostActionProcessorBase
1919
private const string ParentPropertyPathArgument = "parentPropertyPath";
2020
private const string NewJsonPropertyNameArgument = "newJsonPropertyName";
2121
private const string NewJsonPropertyValueArgument = "newJsonPropertyValue";
22-
private const string DetectRepoRootForFileCreation = "detectRepositoryRootForFileCreation";
22+
private const string DetectRepoRoot = "detectRepositoryRoot";
23+
private const string IncludeAllDirectoriesInSearch = "includeAllDirectoriesInSearch";
24+
private const string IncludeAllParentDirectoriesInSearch = "includeAllParentDirectoriesInSearch";
2325

2426
private static readonly JsonSerializerOptions SerializerOptions = new()
2527
{
@@ -87,7 +89,33 @@ protected override bool ProcessInternal(
8789
return false;
8890
}
8991

90-
IReadOnlyList<string> jsonFiles = FindFilesInCurrentFolderOrParentFolder(environment.Host.FileSystem, outputBasePath, jsonFileName);
92+
if (!bool.TryParse(action.Args.GetValueOrDefault(DetectRepoRoot, "false"), out bool detectRepoRoot))
93+
{
94+
Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, DetectRepoRoot));
95+
return false;
96+
}
97+
98+
if (!bool.TryParse(action.Args.GetValueOrDefault(IncludeAllDirectoriesInSearch, "true"), out bool includeAllDirectories))
99+
{
100+
Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, IncludeAllDirectoriesInSearch));
101+
return false;
102+
}
103+
104+
string? repoRoot = detectRepoRoot ? GetRootDirectory(environment.Host.FileSystem, outputBasePath) : null;
105+
106+
if (!bool.TryParse(action.Args.GetValueOrDefault(IncludeAllParentDirectoriesInSearch, "false"), out bool includeAllParentDirectories))
107+
{
108+
Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, IncludeAllParentDirectoriesInSearch));
109+
return false;
110+
}
111+
112+
IReadOnlyList<string> jsonFiles = FindFilesInCurrentFolderOrParentFolder(
113+
environment.Host.FileSystem,
114+
outputBasePath,
115+
jsonFileName,
116+
includeAllDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly,
117+
includeAllParentDirectories ? int.MaxValue : 1,
118+
repoRoot);
91119

92120
if (jsonFiles.Count == 0)
93121
{
@@ -103,13 +131,7 @@ protected override bool ProcessInternal(
103131
return false;
104132
}
105133

106-
if (!bool.TryParse(action.Args.GetValueOrDefault(DetectRepoRootForFileCreation, "false"), out bool detectRepoRoot))
107-
{
108-
Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, DetectRepoRootForFileCreation));
109-
return false;
110-
}
111-
112-
string newJsonFilePath = Path.Combine(detectRepoRoot ? GetRootDirectory(environment.Host.FileSystem, outputBasePath) : outputBasePath, jsonFileName);
134+
string newJsonFilePath = Path.Combine(repoRoot ?? outputBasePath, jsonFileName);
113135
environment.Host.FileSystem.WriteAllText(newJsonFilePath, "{}");
114136
jsonFiles = new List<string> { newJsonFilePath };
115137
}
@@ -150,17 +172,19 @@ protected override bool ProcessInternal(
150172

151173
private static JsonNode? AddElementToJson(IPhysicalFileSystem fileSystem, string targetJsonFile, string? propertyPath, string propertyPathSeparator, string newJsonPropertyName, string newJsonPropertyValue, IPostAction action)
152174
{
153-
JsonNode? jsonContent = JsonNode.Parse(fileSystem.ReadAllText(targetJsonFile), nodeOptions: null, documentOptions: DeserializerOptions);
175+
var fileContent = fileSystem.ReadAllText(targetJsonFile);
176+
JsonNode? jsonContent = JsonNode.Parse(fileContent, nodeOptions: null, documentOptions: DeserializerOptions);
154177

155178
if (jsonContent == null)
156179
{
180+
Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_NullJson, fileContent));
157181
return null;
158182
}
159183

160184
if (!bool.TryParse(action.Args.GetValueOrDefault(AllowPathCreationArgument, "false"), out bool createPath))
161185
{
162186
Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, AllowPathCreationArgument));
163-
return false;
187+
return null;
164188
}
165189

166190
JsonNode? parentProperty = FindJsonNode(jsonContent, propertyPath, propertyPathSeparator, createPath);
@@ -216,7 +240,10 @@ protected override bool ProcessInternal(
216240
private static string[] FindFilesInCurrentFolderOrParentFolder(
217241
IPhysicalFileSystem fileSystem,
218242
string startPath,
219-
string matchPattern)
243+
string matchPattern,
244+
SearchOption searchOption,
245+
int maxUpLevels,
246+
string? repoRoot)
220247
{
221248
string? directory = fileSystem.DirectoryExists(startPath) ? startPath : Path.GetDirectoryName(startPath);
222249

@@ -230,17 +257,24 @@ private static string[] FindFilesInCurrentFolderOrParentFolder(
230257
do
231258
{
232259
Reporter.Verbose.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Verbose_AttemptingToFindJsonFile, matchPattern, directory));
233-
string[] filesInDir = fileSystem.EnumerateFileSystemEntries(directory, matchPattern, SearchOption.AllDirectories).ToArray();
260+
string[] filesInDir = fileSystem.EnumerateFileSystemEntries(directory, matchPattern, searchOption).ToArray();
234261

235262
if (filesInDir.Length > 0)
236263
{
237264
return filesInDir;
238265
}
239266

267+
if (repoRoot is not null && directory == repoRoot)
268+
{
269+
// The post action wants to detect the "repo root".
270+
// We have already processed up to the repo root and didn't find any matching files, so we shouldn't go up any further.
271+
return Array.Empty<string>();
272+
}
273+
240274
directory = Path.GetPathRoot(directory) != directory ? Directory.GetParent(directory)?.FullName : null;
241275
numberOfUpLevels++;
242276
}
243-
while (directory != null && numberOfUpLevels <= 1);
277+
while (directory != null && numberOfUpLevels <= maxUpLevels);
244278

245279
return Array.Empty<string>();
246280
}

src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.cs.xlf

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.de.xlf

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.es.xlf

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.fr.xlf

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.it.xlf

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ja.xlf

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ko.xlf

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)