88
99namespace ListingManager
1010{
11+ /// <summary>
12+ /// A utility class providing means to rename listings, namespaces, and corresponding unit tests.
13+ /// </summary>
1114 public static class ListingManager
1215 {
1316 public static IEnumerable < string > GetAllExtraListings ( string pathToStartFrom )
@@ -21,7 +24,7 @@ public static IEnumerable<string> GetAllExtraListings(string pathToStartFrom)
2124 }
2225 }
2326
24- private static bool TryGetListing ( string listingPath , out ListingInformation listingData )
27+ private static bool TryGetListing ( string listingPath , out ListingInformation ? listingData )
2528 {
2629 listingData = null ;
2730
@@ -39,10 +42,19 @@ private static bool TryGetListing(string listingPath, out ListingInformation lis
3942 return true ;
4043 }
4144
45+ /// <summary>
46+ /// Updates the namespace, file names, and corresponding test file of the target listing. This has a cascading
47+ /// effect, resulting in the renaming of subsequent listings in the same chapter.
48+ /// </summary>
49+ /// <param name="pathToChapter">Path to the target chapter</param>
50+ /// <param name="verbose">When true, enables verbose console output</param>
51+ /// <param name="preview">When true, leaves files in place and only print console output</param>
52+ /// <param name="byFolder">Changes a listing's chapter based on the chapter number in the chapter's path</param>
53+ /// <param name="chapterOnly">Changes only the chapter of the listing, leaving the listing number unchanged. Use with <paramref name="byFolder"/></param>
4254 public static void UpdateChapterListingNumbers ( string pathToChapter ,
43- bool verboseMode = false , bool preview = false , bool byFolder = false , bool chapterOnly = false )
55+ bool verbose = false , bool preview = false , bool byFolder = false , bool chapterOnly = false )
4456 {
45- var listingData = new List < ListingInformation > ( ) ;
57+ var listingData = new List < ListingInformation ? > ( ) ;
4658
4759
4860 List < string > allListings = FileManager . GetAllFilesAtPath ( pathToChapter )
@@ -59,8 +71,9 @@ public static void UpdateChapterListingNumbers(string pathToChapter,
5971 string cur = allListings [ i ] ;
6072
6173 var curListingData = listingData [ i ] ;
74+
6275
63- if ( ! chapterOnly && ! byFolder && listingNumber == curListingData . ListingNumber ) { continue ; } //default
76+ if ( curListingData is null || ! chapterOnly && ! byFolder && listingNumber == curListingData . ListingNumber ) { continue ; } //default
6477
6578 string completeListingNumber = listingNumber + "" ; //default
6679 int listingChapterNumber = curListingData . ChapterNumber ; //default
@@ -78,14 +91,14 @@ public static void UpdateChapterListingNumbers(string pathToChapter,
7891
7992 UpdateListingNamespace ( cur , listingChapterNumber ,
8093 completeListingNumber ,
81- curListingData . ListingDescription , verboseMode , preview ) ;
94+ curListingData . ListingDescription , verbose , preview ) ;
8295
8396 if ( GetPathToAccompanyingUnitTest ( cur , out string pathToTest ) )
8497 {
8598 Console . Write ( "Updating test. " ) ;
8699 UpdateListingNamespace ( pathToTest , listingChapterNumber ,
87100 completeListingNumber ,
88- curListingData . ListingDescription , verboseMode , preview ) ;
101+ string . IsNullOrEmpty ( curListingData . ListingDescription ) ? "Tests" : curListingData . ListingDescription + ".Tests" , verbose , preview ) ;
89102 }
90103
91104 }
@@ -96,14 +109,22 @@ public static bool IsExtraListing(string path,
96109 {
97110 Regex fileNameRegex = new Regex ( regexNamespace ) ;
98111
99- string directoryNameFull = Path . GetDirectoryName ( path ) ;
100-
112+ string directoryNameFull = Path . GetDirectoryName ( path ) ?? string . Empty ;
101113 string directoryName = Path . GetFileName ( directoryNameFull ) ;
102114
103115 return fileNameRegex . IsMatch ( path ) && ! directoryName . Contains ( ".Tests" ) ;
104116 }
105117
106- public static void UpdateListingNamespace ( string path , int chapterNumber , string listingNumber ,
118+ /// <summary>
119+ /// Updates the namespace and file name of the listing at <paramref name="path"/>
120+ /// </summary>
121+ /// <param name="path">The path to the target listing</param>
122+ /// <param name="chapterNumber">The chapter the listing belongs to</param>
123+ /// <param name="listingNumber">The updated listing number</param>
124+ /// <param name="listingData">The name of the listing to be included in the namespace/path</param>
125+ /// <param name="verbose">When true, enables verbose console output</param>
126+ /// <param name="preview">When true, leaves files in place and only print console output</param>
127+ private static void UpdateListingNamespace ( string path , int chapterNumber , string listingNumber ,
107128 string listingData , bool verbose = false , bool preview = false )
108129 {
109130 string paddedChapterNumber = chapterNumber . ToString ( "00" ) ;
@@ -127,7 +148,7 @@ public static void UpdateListingNamespace(string path, int chapterNumber, string
127148 string newFileName = string . Format ( newFileNameTemplate ,
128149 paddedChapterNumber ,
129150 paddedListingNumber ,
130- string . IsNullOrWhiteSpace ( listingData ) ? "" : $ ".{ listingData } ") ;
151+ string . IsNullOrWhiteSpace ( listingData ) || string . IsNullOrEmpty ( listingData ) ? "" : $ ".{ listingData } ") ;
131152
132153 if ( verbose )
133154 {
@@ -137,19 +158,19 @@ public static void UpdateListingNamespace(string path, int chapterNumber, string
137158 if ( ! preview ) UpdateNamespaceOfPath ( path , newNamespace , newFileName ) ;
138159 }
139160
140- public static bool UpdateNamespaceOfPath ( string path , string newNamespace , string newFileName = "" )
161+ private static void UpdateNamespaceOfPath ( string path , string newNamespace , string newFileName = "" )
141162 {
142163 if ( Path . GetExtension ( path ) != ".cs" )
143164 {
144- return false ;
165+ return ;
145166 }
146167
147168 // read file into memory
148169 string [ ] allLinesInFile = File . ReadAllLines ( path ) ;
149170
150171 File . Delete ( path ) ;
151172
152- string targetPath = Path . Combine ( Path . GetDirectoryName ( path ) , newFileName ) ?? path ;
173+ string targetPath = Path . Combine ( Path . GetDirectoryName ( path ) ?? string . Empty , newFileName ) ?? path ;
153174
154175 using ( TextWriter textWriter = new StreamWriter ( targetPath , true ) )
155176 {
@@ -165,8 +186,6 @@ public static bool UpdateNamespaceOfPath(string path, string newNamespace, strin
165186 }
166187 }
167188 }
168-
169- return true ;
170189 }
171190
172191 public static bool GetPathToAccompanyingUnitTest ( string listingPath , out string pathToTest )
@@ -200,7 +219,7 @@ public static bool GetPathToAccompanyingUnitTest(string listingPath, out string
200219 return false ;
201220 }
202221
203- public static string GetTestLayout ( string chapterNumber , string listingNumber )
222+ private static string GetTestLayout ( string chapterNumber , string listingNumber )
204223 {
205224 return string . Format ( TestHeaderLayout ,
206225 chapterNumber . PadLeft ( 2 , '0' ) ,
@@ -226,7 +245,7 @@ public void UnitTest1()
226245 }
227246}" ;
228247
229- public static IEnumerable < string > GetAllMissingUnitTests ( string pathToChapter )
248+ private static IEnumerable < string > GetAllMissingUnitTests ( string pathToChapter )
230249 {
231250 foreach ( string file in FileManager . GetAllFilesAtPath ( pathToChapter ) . OrderBy ( x => x ) )
232251 {
@@ -237,7 +256,7 @@ public static IEnumerable<string> GetAllMissingUnitTests(string pathToChapter)
237256 }
238257 }
239258
240- public static ICollection < string > GenerateUnitTests ( string pathToChapter , Func < string , bool > action = null ,
259+ public static ICollection < string > GenerateUnitTests ( string pathToChapter , Func < string , bool > ? action = null ,
241260 bool verbose = false )
242261 {
243262 var toReturn = new List < string > ( ) ;
@@ -267,7 +286,7 @@ public static ICollection<string> GenerateUnitTests(string pathToChapter, Func<s
267286 return toReturn ;
268287 }
269288
270- public static bool GenerateUnitTest ( string pathToTest )
289+ private static bool GenerateUnitTest ( string pathToTest )
271290 {
272291 if ( File . Exists ( pathToTest ) )
273292 {
0 commit comments