@@ -19,7 +19,9 @@ internal class AddJsonPropertyPostActionProcessor : PostActionProcessorBase
19
19
private const string ParentPropertyPathArgument = "parentPropertyPath" ;
20
20
private const string NewJsonPropertyNameArgument = "newJsonPropertyName" ;
21
21
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" ;
23
25
24
26
private static readonly JsonSerializerOptions SerializerOptions = new ( )
25
27
{
@@ -87,7 +89,33 @@ protected override bool ProcessInternal(
87
89
return false ;
88
90
}
89
91
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 ) ;
91
119
92
120
if ( jsonFiles . Count == 0 )
93
121
{
@@ -103,13 +131,7 @@ protected override bool ProcessInternal(
103
131
return false ;
104
132
}
105
133
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 ) ;
113
135
environment . Host . FileSystem . WriteAllText ( newJsonFilePath , "{}" ) ;
114
136
jsonFiles = new List < string > { newJsonFilePath } ;
115
137
}
@@ -150,17 +172,19 @@ protected override bool ProcessInternal(
150
172
151
173
private static JsonNode ? AddElementToJson ( IPhysicalFileSystem fileSystem , string targetJsonFile , string ? propertyPath , string propertyPathSeparator , string newJsonPropertyName , string newJsonPropertyValue , IPostAction action )
152
174
{
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 ) ;
154
177
155
178
if ( jsonContent == null )
156
179
{
180
+ Reporter . Error . WriteLine ( string . Format ( LocalizableStrings . PostAction_ModifyJson_Error_NullJson , fileContent ) ) ;
157
181
return null ;
158
182
}
159
183
160
184
if ( ! bool . TryParse ( action . Args . GetValueOrDefault ( AllowPathCreationArgument , "false" ) , out bool createPath ) )
161
185
{
162
186
Reporter . Error . WriteLine ( string . Format ( LocalizableStrings . PostAction_ModifyJson_Error_ArgumentNotBoolean , AllowPathCreationArgument ) ) ;
163
- return false ;
187
+ return null ;
164
188
}
165
189
166
190
JsonNode ? parentProperty = FindJsonNode ( jsonContent , propertyPath , propertyPathSeparator , createPath ) ;
@@ -216,7 +240,10 @@ protected override bool ProcessInternal(
216
240
private static string [ ] FindFilesInCurrentFolderOrParentFolder (
217
241
IPhysicalFileSystem fileSystem ,
218
242
string startPath ,
219
- string matchPattern )
243
+ string matchPattern ,
244
+ SearchOption searchOption ,
245
+ int maxUpLevels ,
246
+ string ? repoRoot )
220
247
{
221
248
string ? directory = fileSystem . DirectoryExists ( startPath ) ? startPath : Path . GetDirectoryName ( startPath ) ;
222
249
@@ -230,17 +257,24 @@ private static string[] FindFilesInCurrentFolderOrParentFolder(
230
257
do
231
258
{
232
259
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 ( ) ;
234
261
235
262
if ( filesInDir . Length > 0 )
236
263
{
237
264
return filesInDir ;
238
265
}
239
266
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
+
240
274
directory = Path . GetPathRoot ( directory ) != directory ? Directory . GetParent ( directory ) ? . FullName : null ;
241
275
numberOfUpLevels ++ ;
242
276
}
243
- while ( directory != null && numberOfUpLevels <= 1 ) ;
277
+ while ( directory != null && numberOfUpLevels <= maxUpLevels ) ;
244
278
245
279
return Array . Empty < string > ( ) ;
246
280
}
0 commit comments