@@ -26,36 +26,13 @@ public static IEnumerable<string> GetAllExtraListings(string pathToStartFrom)
2626 {
2727 foreach ( string file in FileManager . GetAllFilesAtPath ( pathToStartFrom , true ) )
2828 {
29- if ( IsExtraListing ( file ) )
29+ if ( ListingManagerHelpers . IsExtraListing ( file ) )
3030 {
3131 yield return file ;
3232 }
3333 }
3434 }
3535
36- private static bool TryGetListing ( string listingPath , [ NotNullWhen ( true ) ] out ListingInformation ? listingData )
37- {
38- return TryGetListing ( listingPath , out listingData , false ) ;
39- }
40-
41- private static bool TryGetListing ( string listingPath , [ NotNullWhen ( true ) ] out ListingInformation ? listingData , bool isTest )
42- {
43- listingData = null ;
44-
45- if ( ! ListingInformation . ApprovedFileTypes . Contains ( Path . GetExtension ( listingPath ) ) ) return false ;
46-
47- try
48- {
49- listingData = new ListingInformation ( listingPath , isTest ) ;
50- }
51- catch ( Exception ) // don't care about the type of exception here. If things didn't go perfectly, abort
52- {
53- return false ;
54- }
55-
56- return true ;
57- }
58-
5936 /// <summary>
6037 /// Updates the namespace, file names, and corresponding test file of the target listing. This has a cascading
6138 /// effect, resulting in the renaming of subsequent listings in the same chapter.
@@ -68,7 +45,7 @@ private static bool TryGetListing(string listingPath, [NotNullWhen(true)] out Li
6845 public void UpdateChapterListingNumbers ( DirectoryInfo pathToChapter ,
6946 bool verbose = false , bool preview = false , bool byFolder = false , bool singleDir = false )
7047 {
71- List < ListingInformation > listingData = PopulateListingDataFromPath ( pathToChapter . FullName , singleDir ) ;
48+ List < ListingInformation > listingData = ListingManagerHelpers . PopulateListingDataFromPath ( pathToChapter . FullName , singleDir ) ;
7249 for ( int i = 0 , listingNumber = 1 ; i < listingData . Count ; i ++ , listingNumber ++ )
7350 {
7451 ListingInformation curListingData = listingData [ i ] ?? throw new InvalidOperationException ( $ "Listing data is null for an index of { i } ") ;
@@ -185,81 +162,19 @@ public static bool GetPathToAccompanyingUnitTest(string listingPath, out string
185162 return false ;
186163 }
187164
188- public static List < ListingInformation > PopulateListingDataFromPath ( string pathToChapter , bool singleDir )
189- {
190- List < ListingInformation > listingData = new ( ) ;
191- List < ListingInformation > testListingData = new ( ) ;
192- var listingFiles = FileManager . GetAllFilesAtPath ( pathToChapter )
193- . OrderBy ( x => x ) ;
194- foreach ( string fileName in listingFiles )
195- {
196- if ( TryGetListing ( fileName , out ListingInformation ? data ) )
197- {
198- if ( data . IsTest )
199- {
200- testListingData . Add ( data ) ;
201- }
202- else
203- {
204- listingData . Add ( data ) ;
205- }
206- }
207- }
208-
209- if ( ! singleDir )
210- {
211- var listingTestFiles = FileManager . GetAllFilesAtPath ( $ "{ pathToChapter } .Tests")
212- . OrderBy ( x => x ) ;
213- foreach ( string fileName in listingTestFiles )
214- {
215- if ( TryGetListing ( fileName , out ListingInformation ? data , true ) )
216- {
217- testListingData . Add ( data ) ;
218- }
219- }
220- }
221-
222- foreach ( ListingInformation testListingInformation in testListingData )
223- {
224- ListingInformation listingInformation = listingData . First ( x => x . OriginalListingNumber == testListingInformation . OriginalListingNumber
225- && x . OriginalChapterNumber == testListingInformation . OriginalChapterNumber
226- && x . OriginalListingNumberSuffix == testListingInformation . OriginalListingNumberSuffix ) ;
227-
228- if ( string . Equals ( testListingInformation . Caption , "Tests" , StringComparison . InvariantCultureIgnoreCase ) && listingInformation . Caption != string . Empty )
229- {
230- testListingInformation . Caption = listingInformation . Caption + ".Tests" ;
231- }
232- listingInformation . AssociatedTest = testListingInformation ;
233- }
234-
235- return listingData ;
236- }
237-
238- public static bool IsExtraListing ( string path , string regexNamespace = @".*Listing\d{2}\.\d{2}(A|B|C|D).*\.cs$" )
239- {
240- Regex fileNameRegex = new ( regexNamespace ) ;
241-
242- string directoryNameFull = Path . GetDirectoryName ( path ) ?? string . Empty ;
243- string directoryName = Path . GetFileName ( directoryNameFull ) ;
244-
245- return fileNameRegex . IsMatch ( path ) && ! directoryName . Contains ( ".Tests" ) ;
246- }
247-
248165 [ GeneratedRegex ( "((Listing\\ d{2}\\ .\\ d{2})([A-Z]?)((\\ .Tests)?)).*\\ .cs.tmp$" ) ]
249166 private static partial Regex TemporaryListingTestFile ( ) ;
250167
251- public void UpdateAllChapterListingNumbers ( DirectoryInfo pathToChapter ,
168+ public void UpdateAllChapterListingNumbers ( DirectoryInfo pathToDirectoryOfChapters ,
252169 bool verbose = false , bool preview = false , bool byFolder = false , bool singleDir = false )
253170 {
254- IEnumerable < DirectoryInfo > directoryInfos = Directory . EnumerateDirectories ( pathToChapter . FullName )
171+ IEnumerable < DirectoryInfo > directoryInfos = Directory . EnumerateDirectories ( pathToDirectoryOfChapters . FullName )
255172 . Select ( x => new DirectoryInfo ( x ) )
256- . Where ( x => ChapterDir ( ) . IsMatch ( x . Name ) ) ;
173+ . Where ( x => ListingManagerHelpers . ChapterDir ( ) . IsMatch ( x . Name ) ) ;
257174
258175 foreach ( DirectoryInfo directoryInfo in directoryInfos )
259176 {
260177 UpdateChapterListingNumbers ( directoryInfo , verbose , preview , byFolder , singleDir ) ;
261178 }
262179 }
263- [ GeneratedRegex ( "^Chapter\\ d{2}$" ) ]
264- private static partial Regex ChapterDir ( ) ;
265180}
0 commit comments