4
4
using System . Text . Encodings . Web ;
5
5
using System . Text . Json ;
6
6
using System . Text . Json . Nodes ;
7
+
7
8
using Microsoft . DotNet . Cli . Utils ;
8
9
using Microsoft . TemplateEngine . Abstractions ;
9
10
using Microsoft . TemplateEngine . Abstractions . PhysicalFileSystem ;
@@ -12,6 +13,8 @@ namespace Microsoft.TemplateEngine.Cli.PostActionProcessors
12
13
{
13
14
internal class AddJsonPropertyPostActionProcessor : PostActionProcessorBase
14
15
{
16
+ private const string AllowFileCreationArgument = "allowFileCreation" ;
17
+ private const string AllowPathCreationArgument = "allowPathCreation" ;
15
18
private const string JsonFileNameArgument = "jsonFileName" ;
16
19
private const string ParentPropertyPathArgument = "parentPropertyPath" ;
17
20
private const string NewJsonPropertyNameArgument = "newJsonPropertyName" ;
@@ -46,12 +49,25 @@ protected override bool ProcessInternal(
46
49
return false ;
47
50
}
48
51
49
- IReadOnlyList < string > jsonFiles = FindFilesInCurrentProjectOrSolutionFolder ( environment . Host . FileSystem , outputBasePath , matchPattern : jsonFileName , maxAllowedAboveDirectories : 1 ) ;
52
+ IReadOnlyList < string > jsonFiles = FindFilesInCurrentFolderOrParentFolder ( environment . Host . FileSystem , outputBasePath , jsonFileName ) ;
50
53
51
54
if ( jsonFiles . Count == 0 )
52
55
{
53
- Reporter . Error . WriteLine ( LocalizableStrings . PostAction_ModifyJson_Error_NoJsonFile ) ;
54
- return false ;
56
+ if ( ! bool . TryParse ( action . Args . GetValueOrDefault ( AllowFileCreationArgument , "false" ) , out bool createFile ) )
57
+ {
58
+ Reporter . Error . WriteLine ( string . Format ( LocalizableStrings . PostAction_ModifyJson_Error_ArgumentNotBoolean , AllowFileCreationArgument ) ) ;
59
+ return false ;
60
+ }
61
+
62
+ if ( ! createFile )
63
+ {
64
+ Reporter . Error . WriteLine ( LocalizableStrings . PostAction_ModifyJson_Error_NoJsonFile ) ;
65
+ return false ;
66
+ }
67
+
68
+ string newJsonFilePath = Path . Combine ( outputBasePath , jsonFileName ) ;
69
+ environment . Host . FileSystem . WriteAllText ( newJsonFilePath , "{}" ) ;
70
+ jsonFiles = new List < string > { newJsonFilePath } ;
55
71
}
56
72
57
73
if ( jsonFiles . Count > 1 )
@@ -73,7 +89,8 @@ protected override bool ProcessInternal(
73
89
newJsonElementProperties ! . ParentProperty ,
74
90
":" ,
75
91
newJsonElementProperties . NewJsonPropertyName ,
76
- newJsonElementProperties . NewJsonPropertyValue ) ;
92
+ newJsonElementProperties . NewJsonPropertyValue ,
93
+ action ) ;
77
94
78
95
if ( newJsonContent == null )
79
96
{
@@ -87,7 +104,7 @@ protected override bool ProcessInternal(
87
104
return true ;
88
105
}
89
106
90
- private static JsonNode ? AddElementToJson ( IPhysicalFileSystem fileSystem , string targetJsonFile , string ? propertyPath , string propertyPathSeparator , string newJsonPropertyName , string newJsonPropertyValue )
107
+ private static JsonNode ? AddElementToJson ( IPhysicalFileSystem fileSystem , string targetJsonFile , string ? propertyPath , string propertyPathSeparator , string newJsonPropertyName , string newJsonPropertyValue , IPostAction action )
91
108
{
92
109
JsonNode ? jsonContent = JsonNode . Parse ( fileSystem . ReadAllText ( targetJsonFile ) , nodeOptions : null , documentOptions : DeserializerOptions ) ;
93
110
@@ -96,7 +113,13 @@ protected override bool ProcessInternal(
96
113
return null ;
97
114
}
98
115
99
- JsonNode ? parentProperty = FindJsonNode ( jsonContent , propertyPath , propertyPathSeparator ) ;
116
+ if ( ! bool . TryParse ( action . Args . GetValueOrDefault ( AllowPathCreationArgument , "false" ) , out bool createPath ) )
117
+ {
118
+ Reporter . Error . WriteLine ( string . Format ( LocalizableStrings . PostAction_ModifyJson_Error_ArgumentNotBoolean , AllowPathCreationArgument ) ) ;
119
+ return false ;
120
+ }
121
+
122
+ JsonNode ? parentProperty = FindJsonNode ( jsonContent , propertyPath , propertyPathSeparator , createPath ) ;
100
123
101
124
if ( parentProperty == null )
102
125
{
@@ -116,7 +139,7 @@ protected override bool ProcessInternal(
116
139
return jsonContent ;
117
140
}
118
141
119
- private static JsonNode ? FindJsonNode ( JsonNode content , string ? nodePath , string pathSeparator )
142
+ private static JsonNode ? FindJsonNode ( JsonNode content , string ? nodePath , string pathSeparator , bool createPath )
120
143
{
121
144
if ( nodePath == null )
122
145
{
@@ -134,18 +157,22 @@ protected override bool ProcessInternal(
134
157
return null ;
135
158
}
136
159
137
- node = node [ property ] ;
160
+ JsonNode ? childNode = node [ property ] ;
161
+ if ( childNode is null && createPath )
162
+ {
163
+ node [ property ] = childNode = new JsonObject ( ) ;
164
+ }
165
+
166
+ node = childNode ;
138
167
}
139
168
140
169
return node ;
141
170
}
142
171
143
- private static IReadOnlyList < string > FindFilesInCurrentProjectOrSolutionFolder (
172
+ private static string [ ] FindFilesInCurrentFolderOrParentFolder (
144
173
IPhysicalFileSystem fileSystem ,
145
174
string startPath ,
146
- string matchPattern ,
147
- Func < string , bool > ? secondaryFilter = null ,
148
- int maxAllowedAboveDirectories = 250 )
175
+ string matchPattern )
149
176
{
150
177
string ? directory = fileSystem . DirectoryExists ( startPath ) ? startPath : Path . GetDirectoryName ( startPath ) ;
151
178
@@ -158,22 +185,20 @@ private static IReadOnlyList<string> FindFilesInCurrentProjectOrSolutionFolder(
158
185
159
186
do
160
187
{
161
- List < string > filesInDir = fileSystem . EnumerateFileSystemEntries ( directory , matchPattern , SearchOption . AllDirectories ) . ToList ( ) ;
162
- List < string > matches = new ( ) ;
163
-
164
- matches = secondaryFilter == null ? filesInDir : filesInDir . Where ( x => secondaryFilter ( x ) ) . ToList ( ) ;
188
+ Reporter . Verbose . WriteLine ( string . Format ( LocalizableStrings . PostAction_ModifyJson_Verbose_AttemptingToFindJsonFile , matchPattern , directory ) ) ;
189
+ string [ ] filesInDir = fileSystem . EnumerateFileSystemEntries ( directory , matchPattern , SearchOption . AllDirectories ) . ToArray ( ) ;
165
190
166
- if ( matches . Count > 0 )
191
+ if ( filesInDir . Length > 0 )
167
192
{
168
- return matches ;
193
+ return filesInDir ;
169
194
}
170
195
171
196
directory = Path . GetPathRoot ( directory ) != directory ? Directory . GetParent ( directory ) ? . FullName : null ;
172
197
numberOfUpLevels ++ ;
173
198
}
174
- while ( directory != null && numberOfUpLevels <= maxAllowedAboveDirectories ) ;
199
+ while ( directory != null && numberOfUpLevels <= 1 ) ;
175
200
176
- return new List < string > ( ) ;
201
+ return Array . Empty < string > ( ) ;
177
202
}
178
203
179
204
private class JsonContentParameters
0 commit comments