11using System ;
22using System . IO ;
3+ using System . Linq ;
34using dotnetCampus . Cli ;
45
56namespace PublishFolderCleaner
@@ -13,10 +14,39 @@ static void Main(string[] args)
1314 const string libFolderName = "lib" ;
1415 var publishFolder = options . PublishFolder . Trim ( ) ;
1516 var libFolder = Path . GetFullPath ( Path . Combine ( publishFolder , libFolderName ) ) ;
16- var tempFolder = Path . GetFullPath ( Path . Combine ( publishFolder , @".." , Path . GetRandomFileName ( ) ) ) ;
17- Directory . Move ( publishFolder , tempFolder ) ;
18- Directory . CreateDirectory ( publishFolder ) ;
19- Directory . Move ( tempFolder , libFolder ) ;
17+
18+ Directory . CreateDirectory ( libFolder ) ;
19+
20+ var excludeItems = options . Exclude
21+ . Split ( new [ ] { ';' } , StringSplitOptions . RemoveEmptyEntries )
22+ . Select ( e => e . Trim ( ) )
23+ . Where ( e => ! string . IsNullOrEmpty ( e ) )
24+ . ToList ( ) ;
25+
26+ var entries = Directory . GetFileSystemEntries ( publishFolder ) ;
27+ foreach ( var entry in entries )
28+ {
29+ var name = Path . GetFileName ( entry ) ;
30+ if ( name . Equals ( libFolderName , StringComparison . OrdinalIgnoreCase ) )
31+ {
32+ continue ;
33+ }
34+
35+ if ( ShouldExclude ( name , excludeItems ) )
36+ {
37+ continue ;
38+ }
39+
40+ var dest = Path . Combine ( libFolder , name ) ;
41+ if ( File . Exists ( entry ) )
42+ {
43+ File . Move ( entry , dest ) ;
44+ }
45+ else if ( Directory . Exists ( entry ) )
46+ {
47+ Directory . Move ( entry , dest ) ;
48+ }
49+ }
2050
2151 var appHostFilePath = Path . Combine ( libFolder , options . ApplicationName + ".exe" ) ;
2252 var newAppHostFilePath = Path . Combine ( publishFolder , options . ApplicationName + ".exe" ) ;
@@ -25,5 +55,12 @@ static void Main(string[] args)
2555
2656 AppHostPatcher . Patch ( newAppHostFilePath , Path . Combine ( "lib" , options . ApplicationName + ".dll" ) ) ;
2757 }
58+
59+ static bool ShouldExclude ( string name , System . Collections . Generic . List < string > excludeItems )
60+ {
61+ return excludeItems . Any ( e =>
62+ name . Equals ( e , StringComparison . OrdinalIgnoreCase ) ||
63+ name . Equals ( e . TrimStart ( '.' ) , StringComparison . OrdinalIgnoreCase ) ) ;
64+ }
2865 }
2966}
0 commit comments