@@ -85,36 +85,29 @@ private List<AssetsResult> ScanRepo(RepoConfiguration config, AssetsResultSet? p
8585 }
8686
8787 var targetRepoUri = $ "https://{ authString } github.com/{ config . LanguageRepo } .git";
88- var workingDirectory = Path . Combine ( Path . GetTempPath ( ) , Guid . NewGuid ( ) . ToString ( ) ) ;
88+ var workingDirectory = Path . Combine ( WorkingDirectory , config . LanguageRepo . Replace ( "/" , "_" ) ) ;
8989 var results = new List < AssetsResult > ( ) ;
9090
91- try
91+ if ( ! Directory . Exists ( workingDirectory ) )
9292 {
93- if ( ! Directory . Exists ( workingDirectory ) )
94- {
95- Directory . CreateDirectory ( workingDirectory ) ;
96- }
93+ Directory . CreateDirectory ( workingDirectory ) ;
94+ }
9795
98- foreach ( var branch in config . Branches )
99- {
100- var commitsOnBranch = GetBranchCommits ( targetRepoUri , branch , config . ScanStartDate , workingDirectory ) ;
101- var unretrievedCommits = ResolveUnhandledCommits ( commitsOnBranch , previousOutput ) ;
96+ foreach ( var branch in config . Branches )
97+ {
98+ var commitsOnBranch = GetBranchCommits ( targetRepoUri , branch , config . ScanStartDate , workingDirectory ) ;
99+ var unretrievedCommits = ResolveUnhandledCommits ( commitsOnBranch , previousOutput ) ;
102100
103- results . AddRange ( GetAssetsResults ( config . LanguageRepo , unretrievedCommits , workingDirectory ) ) ;
101+ results . AddRange ( GetAssetsResults ( config . LanguageRepo , unretrievedCommits , workingDirectory , config . ScanFolders ) ) ;
104102
105- if ( previousOutput != null )
103+ if ( previousOutput != null )
104+ {
105+ foreach ( var commit in commitsOnBranch . Where ( commit => ! unretrievedCommits . Contains ( commit ) ) )
106106 {
107- foreach ( var commit in commitsOnBranch . Where ( commit => ! unretrievedCommits . Contains ( commit ) ) )
108- {
109- results . AddRange ( previousOutput . ByOriginSHA [ commit ] ) ;
110- }
107+ results . AddRange ( previousOutput . ByOriginSHA [ commit ] ) ;
111108 }
112109 }
113110 }
114- finally
115- {
116- CleanupWorkingDirectory ( workingDirectory ) ;
117- }
118111
119112 return results ;
120113 }
@@ -123,9 +116,10 @@ private List<AssetsResult> ScanRepo(RepoConfiguration config, AssetsResultSet? p
123116 /// Clones a specific branch, then returns all commit shas newer than our targeted date.
124117 /// </summary>
125118 /// <returns>A list of commits (limited to after a startdate) from the targeted branch.</returns>
126- private List < string > GetBranchCommits ( string uri , string branch , DateTime since , string workingDirectory )
119+ private List < string > GetBranchCommits ( string uri , string branch , string since , string workingDirectory )
127120 {
128121 var commitSHAs = new List < string > ( ) ;
122+
129123 try
130124 {
131125 // if git is already initialized, we just need to checkout a specific branch
@@ -141,7 +135,15 @@ private List<string> GetBranchCommits(string uri, string branch, DateTime since,
141135 Cleanup ( workingDirectory ) ;
142136 }
143137
144- var tagResult = handler . Run ( $ "log --since={ since . ToString ( "yyyy-MM-dd" ) } --format=format:%H", workingDirectory ) ;
138+ CommandResult tagResult ;
139+ if ( since == "latest" )
140+ {
141+ tagResult = handler . Run ( $ "log -n 1 --format=format:%H", workingDirectory ) ;
142+ }
143+ else
144+ {
145+ tagResult = handler . Run ( $ "log --since={ since } --format=format:%H", workingDirectory ) ;
146+ }
145147 commitSHAs . AddRange ( tagResult . StdOut . Split ( Environment . NewLine ) . Select ( x => x . Trim ( ) ) . Where ( x => ! string . IsNullOrWhiteSpace ( x ) ) ) ;
146148 }
147149 catch ( GitProcessException gitException )
@@ -207,11 +209,23 @@ public Assets()
207209 /// Find all assets.jsons beneath a targeted folder.
208210 /// </summary>
209211 /// <returns>AssetsResults for each discovered assets.json, populating other metadata as necessary.</returns>
210- private List < AssetsResult > ScanDirectory ( string repo , string commit , string workingDirectory )
212+ private List < AssetsResult > ScanDirectory ( string repo , string commit , string workingDirectory , List < string > scanFolders )
211213 {
212214 Matcher matcher = new ( ) ;
213215 List < AssetsResult > locatedAssets = new List < AssetsResult > ( ) ;
214- matcher . AddIncludePatterns ( new [ ] { "**/assets.json" } ) ;
216+
217+ if ( scanFolders . Count > 0 )
218+ {
219+ foreach ( string folder in scanFolders )
220+ {
221+ matcher . AddIncludePatterns ( new [ ] { Path . Combine ( folder , "**/assets.json" ) } ) ;
222+ }
223+ }
224+ else
225+ {
226+ matcher . AddIncludePatterns ( new [ ] { "**/assets.json" } ) ;
227+ }
228+
215229 IEnumerable < string > assetsJsons = matcher . GetResultsInFullPath ( workingDirectory ) ;
216230
217231 foreach ( var assetsJson in assetsJsons )
@@ -233,14 +247,14 @@ private List<AssetsResult> ScanDirectory(string repo, string commit, string work
233247 /// Walks a set of targeted commits, extracting all available assets.jsons from each.
234248 /// </summary>
235249 /// <returns>A list of AssetsResults reflecting all discovered assets.jsons from each targeted commit.</returns>
236- private List < AssetsResult > GetAssetsResults ( string repo , List < string > commits , string workingDirectory )
250+ private List < AssetsResult > GetAssetsResults ( string repo , List < string > commits , string workingDirectory , List < string > folderGlobs )
237251 {
238252 var allResults = new List < AssetsResult > ( ) ;
239253 foreach ( var commit in commits )
240254 {
241255 handler . Run ( $ "checkout { commit } ", workingDirectory ) ;
242256 Cleanup ( workingDirectory ) ;
243- allResults . AddRange ( ScanDirectory ( repo , commit , workingDirectory ) ) ;
257+ allResults . AddRange ( ScanDirectory ( repo , commit , workingDirectory , folderGlobs ) ) ;
244258 }
245259
246260 return allResults ;
@@ -275,7 +289,7 @@ private void Cleanup(string workingDirectory)
275289 /// This is necessary because certain `.pack` files created by git cannot be deleted without
276290 /// adjusting these permissions.
277291 /// </summary>
278- private void SetPermissionsAndDelete ( string gitfolder )
292+ public static void SetPermissionsAndDelete ( string gitfolder )
279293 {
280294 File . SetAttributes ( gitfolder , FileAttributes . Normal ) ;
281295
@@ -300,7 +314,7 @@ private void SetPermissionsAndDelete(string gitfolder)
300314 /// The .git folder's .pack files can be super finicky to delete from code.
301315 /// This function abstracts the necessary permissions update and cleans that folder for us.
302316 /// </summary>
303- private void CleanupWorkingDirectory ( string workingDirectory )
317+ public static void CleanupGitDirectory ( string workingDirectory )
304318 {
305319 var gitDir = Path . Combine ( workingDirectory , ".git" ) ;
306320
@@ -319,7 +333,11 @@ public void Save(AssetsResultSet newResults)
319333 {
320334 using ( var stream = System . IO . File . OpenWrite ( ResultsFile ) )
321335 {
322- stream . Write ( Encoding . UTF8 . GetBytes ( JsonSerializer . Serialize ( newResults . Results ) ) ) ;
336+ var options = new JsonSerializerOptions
337+ {
338+ WriteIndented = true
339+ } ;
340+ stream . Write ( Encoding . UTF8 . GetBytes ( JsonSerializer . Serialize ( newResults . Results , options : options ) ) ) ;
323341 }
324342 }
325343}
0 commit comments