diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.Designer.cs b/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.Designer.cs index ecf9c58147f5..0b1407d3d11b 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.Designer.cs +++ b/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.Designer.cs @@ -1439,6 +1439,17 @@ internal static string PostAction_ModifyJson_Error_NoJsonFile { } } + /// + /// Looks up a localized string similar to The result of parsing the following JSON was 'null': + /// + ///{0}. + /// + internal static string PostAction_ModifyJson_Error_NullJson { + get { + return ResourceManager.GetString("PostAction_ModifyJson_Error_NullJson", resourceCulture); + } + } + /// /// Looks up a localized string similar to Parent property path '{0}' could not be traversed.. /// diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.resx b/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.resx index 01b59dfe8833..d08a680ebae1 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.resx +++ b/src/Cli/Microsoft.TemplateEngine.Cli/LocalizableStrings.resx @@ -953,4 +953,10 @@ The header is followed by the list of parameters and their errors (might be seve Attempting to find json file '{0}' in '{1}' - + + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + + \ No newline at end of file diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs b/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs index 80ca23402914..f517a6b363ba 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs +++ b/src/Cli/Microsoft.TemplateEngine.Cli/PostActionProcessors/AddJsonPropertyPostActionProcessor.cs @@ -19,7 +19,9 @@ internal class AddJsonPropertyPostActionProcessor : PostActionProcessorBase private const string ParentPropertyPathArgument = "parentPropertyPath"; private const string NewJsonPropertyNameArgument = "newJsonPropertyName"; private const string NewJsonPropertyValueArgument = "newJsonPropertyValue"; - private const string DetectRepoRootForFileCreation = "detectRepositoryRootForFileCreation"; + private const string DetectRepoRoot = "detectRepositoryRoot"; + private const string IncludeAllDirectoriesInSearch = "includeAllDirectoriesInSearch"; + private const string IncludeAllParentDirectoriesInSearch = "includeAllParentDirectoriesInSearch"; private static readonly JsonSerializerOptions SerializerOptions = new() { @@ -87,7 +89,33 @@ protected override bool ProcessInternal( return false; } - IReadOnlyList jsonFiles = FindFilesInCurrentFolderOrParentFolder(environment.Host.FileSystem, outputBasePath, jsonFileName); + if (!bool.TryParse(action.Args.GetValueOrDefault(DetectRepoRoot, "false"), out bool detectRepoRoot)) + { + Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, DetectRepoRoot)); + return false; + } + + if (!bool.TryParse(action.Args.GetValueOrDefault(IncludeAllDirectoriesInSearch, "true"), out bool includeAllDirectories)) + { + Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, IncludeAllDirectoriesInSearch)); + return false; + } + + string? repoRoot = detectRepoRoot ? GetRootDirectory(environment.Host.FileSystem, outputBasePath) : null; + + if (!bool.TryParse(action.Args.GetValueOrDefault(IncludeAllParentDirectoriesInSearch, "false"), out bool includeAllParentDirectories)) + { + Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, IncludeAllParentDirectoriesInSearch)); + return false; + } + + IReadOnlyList jsonFiles = FindFilesInCurrentFolderOrParentFolder( + environment.Host.FileSystem, + outputBasePath, + jsonFileName, + includeAllDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly, + includeAllParentDirectories ? int.MaxValue : 1, + repoRoot); if (jsonFiles.Count == 0) { @@ -103,13 +131,7 @@ protected override bool ProcessInternal( return false; } - if (!bool.TryParse(action.Args.GetValueOrDefault(DetectRepoRootForFileCreation, "false"), out bool detectRepoRoot)) - { - Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, DetectRepoRootForFileCreation)); - return false; - } - - string newJsonFilePath = Path.Combine(detectRepoRoot ? GetRootDirectory(environment.Host.FileSystem, outputBasePath) : outputBasePath, jsonFileName); + string newJsonFilePath = Path.Combine(repoRoot ?? outputBasePath, jsonFileName); environment.Host.FileSystem.WriteAllText(newJsonFilePath, "{}"); jsonFiles = new List { newJsonFilePath }; } @@ -150,17 +172,19 @@ protected override bool ProcessInternal( private static JsonNode? AddElementToJson(IPhysicalFileSystem fileSystem, string targetJsonFile, string? propertyPath, string propertyPathSeparator, string newJsonPropertyName, string newJsonPropertyValue, IPostAction action) { - JsonNode? jsonContent = JsonNode.Parse(fileSystem.ReadAllText(targetJsonFile), nodeOptions: null, documentOptions: DeserializerOptions); + var fileContent = fileSystem.ReadAllText(targetJsonFile); + JsonNode? jsonContent = JsonNode.Parse(fileContent, nodeOptions: null, documentOptions: DeserializerOptions); if (jsonContent == null) { + Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_NullJson, fileContent)); return null; } if (!bool.TryParse(action.Args.GetValueOrDefault(AllowPathCreationArgument, "false"), out bool createPath)) { Reporter.Error.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Error_ArgumentNotBoolean, AllowPathCreationArgument)); - return false; + return null; } JsonNode? parentProperty = FindJsonNode(jsonContent, propertyPath, propertyPathSeparator, createPath); @@ -216,7 +240,10 @@ protected override bool ProcessInternal( private static string[] FindFilesInCurrentFolderOrParentFolder( IPhysicalFileSystem fileSystem, string startPath, - string matchPattern) + string matchPattern, + SearchOption searchOption, + int maxUpLevels, + string? repoRoot) { string? directory = fileSystem.DirectoryExists(startPath) ? startPath : Path.GetDirectoryName(startPath); @@ -230,17 +257,24 @@ private static string[] FindFilesInCurrentFolderOrParentFolder( do { Reporter.Verbose.WriteLine(string.Format(LocalizableStrings.PostAction_ModifyJson_Verbose_AttemptingToFindJsonFile, matchPattern, directory)); - string[] filesInDir = fileSystem.EnumerateFileSystemEntries(directory, matchPattern, SearchOption.AllDirectories).ToArray(); + string[] filesInDir = fileSystem.EnumerateFileSystemEntries(directory, matchPattern, searchOption).ToArray(); if (filesInDir.Length > 0) { return filesInDir; } + if (repoRoot is not null && directory == repoRoot) + { + // The post action wants to detect the "repo root". + // We have already processed up to the repo root and didn't find any matching files, so we shouldn't go up any further. + return Array.Empty(); + } + directory = Path.GetPathRoot(directory) != directory ? Directory.GetParent(directory)?.FullName : null; numberOfUpLevels++; } - while (directory != null && numberOfUpLevels <= 1); + while (directory != null && numberOfUpLevels <= maxUpLevels); return Array.Empty(); } diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.cs.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.cs.xlf index 45f6d58a1ba7..ecb19bae6585 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.cs.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.cs.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve V řešení se nepovedlo najít soubor JSON. + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. Cestu k nadřazené vlastnosti „{0}“ nelze projít. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.de.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.de.xlf index eb67ab8bf9bf..0b82cc14850d 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.de.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.de.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve Die JSON-Datei wurde in der Projektmappe nicht gefunden. + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. Der übergeordnete Eigenschaftenpfad "{0}" konnte nicht durchlaufen werden. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.es.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.es.xlf index 748e66f30a00..c76d94288839 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.es.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.es.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve No se encuentra el archivo JSON en la solución + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. No se pudo atravesar la ruta de acceso de la propiedad primaria '{0}'. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.fr.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.fr.xlf index d86722e36e60..3dd088fe92f2 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.fr.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.fr.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve Impossible de trouver le fichier json dans la solution + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. Le chemin de la propriété parente '{0}' n'a pas pu être traversé. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.it.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.it.xlf index 45d8e4f02e4e..5022ad18a8c4 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.it.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.it.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve Non è possibile trovare il file JSON nella soluzione + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. Impossibile attraversare il percorso della proprietà padre '{0}'. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ja.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ja.xlf index 2f9a5c77a259..e27c0eea775e 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ja.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ja.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve ソリューションで JSON ファイルが見つかりません + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. 親プロパティのパス '{0}' を走査できませんでした。 diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ko.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ko.xlf index 11699454506d..28e0b7ef6ea3 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ko.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ko.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve 솔루션에서 json 파일을 찾을 수 없습니다. + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. '{0}' 부모 속성 경로를 트래버스할 수 없습니다. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pl.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pl.xlf index 6e5167124b31..edc3a399520b 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pl.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pl.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve Nie można odnaleźć pliku JSON w rozwiązaniu + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. Nie można przejść przez ścieżkę właściwości nadrzędnej „{0}”. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pt-BR.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pt-BR.xlf index 24f89179cb85..22b848dd30ec 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pt-BR.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.pt-BR.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve Não é possível encontrar o arquivo json na solução + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. O caminho da propriedade pai '{0}' não pôde ser percorrido. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ru.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ru.xlf index 6578a4d5bf14..b41a1a5ac241 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ru.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.ru.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve Не удалось найти файл JSON в решении + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. Не удалось просмотреть путь к родительскому свойству "{0}". diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.tr.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.tr.xlf index a0d71cff2b96..158582a214b2 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.tr.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.tr.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve Çözümde json dosyası bulunamıyor + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. Üst özellik '{0}' yolu geçirilemedi. diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hans.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hans.xlf index f75d8ad68dae..2df9444a65e5 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hans.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hans.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve 在解决方案中找不到 json 文件 + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. 无法遍历父属性路径“{0}”。 diff --git a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hant.xlf b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hant.xlf index 56c64a9d9b91..1c17c1adc32a 100644 --- a/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hant.xlf +++ b/src/Cli/Microsoft.TemplateEngine.Cli/xlf/LocalizableStrings.zh-Hant.xlf @@ -850,6 +850,15 @@ The header is followed by the list of parameters and their errors (might be seve 在解決方案中找不到 JSON 檔案 + + The result of parsing the following JSON was 'null': + +{0} + The result of parsing the following JSON was 'null': + +{0} + {0} is the JSON that is being parsed. + Parent property path '{0}' could not be traversed. 無法周遊父屬性路徑 '{0}'。 diff --git a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-CSharp/.template.config/template.json b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-CSharp/.template.config/template.json index 01977ec20d99..642d2f3b5a19 100644 --- a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-CSharp/.template.config/template.json +++ b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-CSharp/.template.config/template.json @@ -240,7 +240,9 @@ "parentPropertyPath": "test", "newJsonPropertyName": "runner", "newJsonPropertyValue": "Microsoft.Testing.Platform", - "detectRepositoryRootForFileCreation": true + "detectRepositoryRoot": true, + "includeAllDirectoriesInSearch": false, + "includeAllParentDirectoriesInSearch": true }, "continueOnError": true }, diff --git a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-FSharp/.template.config/template.json b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-FSharp/.template.config/template.json index 022ffe70b9cf..ed388b0b28da 100644 --- a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-FSharp/.template.config/template.json +++ b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-FSharp/.template.config/template.json @@ -240,7 +240,9 @@ "parentPropertyPath": "test", "newJsonPropertyName": "runner", "newJsonPropertyValue": "Microsoft.Testing.Platform", - "detectRepositoryRootForFileCreation": true + "detectRepositoryRoot": true, + "includeAllDirectoriesInSearch": false, + "includeAllParentDirectoriesInSearch": true }, "continueOnError": true }, diff --git a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-VisualBasic/.template.config/template.json b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-VisualBasic/.template.config/template.json index cca88e73c3cd..a110969e6973 100644 --- a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-VisualBasic/.template.config/template.json +++ b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/MSTest-VisualBasic/.template.config/template.json @@ -240,7 +240,9 @@ "parentPropertyPath": "test", "newJsonPropertyName": "runner", "newJsonPropertyValue": "Microsoft.Testing.Platform", - "detectRepositoryRootForFileCreation": true + "detectRepositoryRoot": true, + "includeAllDirectoriesInSearch": false, + "includeAllParentDirectoriesInSearch": true }, "continueOnError": true }, diff --git a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/Playwright-MSTest-CSharp/.template.config/template.json b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/Playwright-MSTest-CSharp/.template.config/template.json index b55dff5e27a9..cca0c7262957 100644 --- a/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/Playwright-MSTest-CSharp/.template.config/template.json +++ b/template_feed/Microsoft.DotNet.Common.ProjectTemplates.10.0/content/Playwright-MSTest-CSharp/.template.config/template.json @@ -260,7 +260,9 @@ "parentPropertyPath": "test", "newJsonPropertyName": "runner", "newJsonPropertyValue": "Microsoft.Testing.Platform", - "detectRepositoryRootForFileCreation": true + "detectRepositoryRoot": true, + "includeAllDirectoriesInSearch": false, + "includeAllParentDirectoriesInSearch": true }, "continueOnError": true },