55using dotnetCampus . Cli ;
66using dotnetCampus . Configurations . Core ;
77using Microsoft . Extensions . Logging ;
8+ using Microsoft . Extensions . Logging . Abstractions ;
89using Microsoft . Extensions . Logging . Console ;
910using Packaging . DebUOS ;
1011using Packaging . DebUOS . Contexts . Configurations ;
1415
1516var loggerFactory = LoggerFactory . Create ( builder =>
1617{
17- builder . AddSimpleConsole ( simpleConsoleFormatterOptions =>
18- {
19- simpleConsoleFormatterOptions . ColorBehavior = LoggerColorBehavior . Disabled ;
20- simpleConsoleFormatterOptions . SingleLine = true ;
21- } ) ;
18+ builder . AddConsole ( loggerOptions => loggerOptions . FormatterName = MSBuildFormatter . FormatterName ) ;
19+ builder . AddConsoleFormatter < MSBuildFormatter , ConsoleFormatterOptions > ( ) ;
2220} ) ;
2321
2422var logger = loggerFactory . CreateLogger ( "" ) ;
2523
26- if ( ! string . IsNullOrEmpty ( options . BuildPath ) )
27- {
28- var packingFolder = new DirectoryInfo ( options . BuildPath ) ;
29- var outputPath = options . OutputPath ?? Path . Join ( packingFolder . FullName , $ "{ packingFolder . Name } .deb") ;
30- var outputDebFile = new FileInfo ( outputPath ) ;
31-
32- var debUosPackageCreator = new DebUOSPackageCreator ( logger ) ;
33- //var packingFolder = new DirectoryInfo(@"C:\lindexi\Work\");
34- //var outputDebFile = new FileInfo(@"C:\lindexi\Work\Downloader.deb");
35- debUosPackageCreator . PackageDeb ( packingFolder , outputDebFile ) ;
36- }
37- else if ( ! string . IsNullOrEmpty ( options . PackageArgumentFilePath ) )
24+ try
3825{
39- logger . LogInformation ( $ "开始根据配置创建 UOS 的 deb 包。配置文件:{ options . PackageArgumentFilePath } ") ;
40- if ( ! File . Exists ( options . PackageArgumentFilePath ) )
26+ if ( ! string . IsNullOrEmpty ( options . BuildPath ) )
4127 {
42- logger . LogError ( $ "配置文件 '{ options . PackageArgumentFilePath } ' 不存在") ;
43- return ;
28+ var packingFolder = new DirectoryInfo ( options . BuildPath ) ;
29+ var outputPath = options . OutputPath ?? Path . Join ( packingFolder . FullName , $ "{ packingFolder . Name } .deb") ;
30+ var outputDebFile = new FileInfo ( outputPath ) ;
31+
32+ var debUosPackageCreator = new DebUOSPackageCreator ( logger ) ;
33+ //var packingFolder = new DirectoryInfo(@"C:\lindexi\Work\");
34+ //var outputDebFile = new FileInfo(@"C:\lindexi\Work\Downloader.deb");
35+ debUosPackageCreator . PackageDeb ( packingFolder , outputDebFile ) ;
4436 }
37+ else if ( ! string . IsNullOrEmpty ( options . PackageArgumentFilePath ) )
38+ {
39+ logger . LogInformation ( $ "开始根据配置创建 UOS 的 deb 包。配置文件:{ options . PackageArgumentFilePath } ") ;
40+ if ( ! File . Exists ( options . PackageArgumentFilePath ) )
41+ {
42+ logger . LogError ( $ "配置文件 '{ options . PackageArgumentFilePath } ' 不存在") ;
43+ return ;
44+ }
45+
46+ var fileConfigurationRepo =
47+ ConfigurationFactory . FromFile ( options . PackageArgumentFilePath , RepoSyncingBehavior . Static ) ;
48+ var appConfigurator = fileConfigurationRepo . CreateAppConfigurator ( ) ;
49+ var configuration = appConfigurator . Of < DebUOSConfiguration > ( ) ;
4550
46- var fileConfigurationRepo = ConfigurationFactory . FromFile ( options . PackageArgumentFilePath , RepoSyncingBehavior . Static ) ;
47- var appConfigurator = fileConfigurationRepo . CreateAppConfigurator ( ) ;
48- var configuration = appConfigurator . Of < DebUOSConfiguration > ( ) ;
51+ var fileStructCreator = new DebUOSPackageFileStructCreator ( logger ) ;
52+ fileStructCreator . CreatePackagingFolder ( configuration ) ;
4953
50- var fileStructCreator = new DebUOSPackageFileStructCreator ( logger ) ;
51- fileStructCreator . CreatePackagingFolder ( configuration ) ;
54+ var packingFolder = new DirectoryInfo ( configuration . PackingFolder ! ) ;
55+ var outputDebFile = new FileInfo ( configuration . DebUOSOutputFilePath ! ) ;
56+ var workingFolder = new DirectoryInfo ( configuration . WorkingFolder ! ) ;
57+ var excludePackingDebFileExtensionsPredicate = configuration . ToExcludePackingDebFileExtensionsPredicate ( ) ;
5258
53- var packingFolder = new DirectoryInfo ( configuration . PackingFolder ! ) ;
54- var outputDebFile = new FileInfo ( configuration . DebUOSOutputFilePath ! ) ;
55- var workingFolder = new DirectoryInfo ( configuration . WorkingFolder ! ) ;
56- var excludePackingDebFileExtensionsPredicate = configuration . ToExcludePackingDebFileExtensionsPredicate ( ) ;
59+ var debUosPackageCreator = new DebUOSPackageCreator ( logger ) ;
60+ debUosPackageCreator . PackageDeb ( packingFolder , outputDebFile , workingFolder ,
61+ optFileCanIncludePredicate : entry => /*取反,因为配置是不包括*/ ! excludePackingDebFileExtensionsPredicate ( entry ) ) ;
62+ }
63+ else
64+ {
65+ // Show Help
66+ var stringBuilder = new StringBuilder ( )
67+ . AppendLine ( $ "用法:[options] [arguments]") ;
68+ foreach ( var propertyInfo in typeof ( Options ) . GetProperties ( ) )
69+ {
70+ var optionAttribute = propertyInfo . GetCustomAttribute < OptionAttribute > ( ) ;
71+ if ( optionAttribute != null )
72+ {
73+ stringBuilder . AppendLine (
74+ $ "-{ optionAttribute . ShortName } { ( optionAttribute . LongName ?? string . Empty ) . PadRight ( 10 ) } { optionAttribute . Description } { optionAttribute . LocalizableDescription } ") ;
75+ }
76+ }
5777
58- var debUosPackageCreator = new DebUOSPackageCreator ( logger ) ;
59- debUosPackageCreator . PackageDeb ( packingFolder , outputDebFile , workingFolder ,
60- optFileCanIncludePredicate : entry => /*取反,因为配置是不包括*/ ! excludePackingDebFileExtensionsPredicate ( entry ) ) ;
78+ Console . WriteLine ( stringBuilder . ToString ( ) ) ;
79+ }
80+ }
81+ catch ( Exception e )
82+ {
83+ logger . LogError ( e , "Fail." ) ;
6184}
62- else
85+
86+ class MSBuildFormatter : ConsoleFormatter
6387{
64- // Show Help
65- var stringBuilder = new StringBuilder ( )
66- . AppendLine ( $ "用法:[options] [arguments]") ;
67- foreach ( var propertyInfo in typeof ( Options ) . GetProperties ( ) )
88+ public MSBuildFormatter ( ) : base ( FormatterName )
6889 {
69- var optionAttribute = propertyInfo . GetCustomAttribute < OptionAttribute > ( ) ;
70- if ( optionAttribute != null )
90+ }
91+
92+ public const string FormatterName = "MSBuild" ;
93+
94+ public override void Write < TState > ( in LogEntry < TState > logEntry , IExternalScopeProvider ? scopeProvider , TextWriter textWriter )
95+ {
96+ var logLevel = logEntry . LogLevel ;
97+ //var eventId = logEntry.EventId.Id;
98+ var message = logEntry . Formatter ( logEntry . State , logEntry . Exception ) ;
99+ var exception = logEntry . Exception ;
100+ var logLevelString = logLevel switch
101+ {
102+ LogLevel . Trace => "debug: " ,
103+ LogLevel . Debug => "debug: " ,
104+ LogLevel . Information => "info: " ,
105+ LogLevel . Warning => "warning: " ,
106+ LogLevel . Error => "error: " ,
107+ LogLevel . Critical => "error: " ,
108+ _ => "" ,
109+ } ;
110+ textWriter . Write ( $ "{ logLevelString } [DebUOS] { message } ") ;
111+ if ( exception != null )
112+ {
113+ textWriter . WriteLine ( exception ) ;
114+ }
115+ else
71116 {
72- stringBuilder . AppendLine ( $ "- { optionAttribute . ShortName } { ( optionAttribute . LongName ?? string . Empty ) . PadRight ( 10 ) } { optionAttribute . Description } { optionAttribute . LocalizableDescription } " ) ;
117+ textWriter . WriteLine ( ) ;
73118 }
74119 }
75-
76- Console . WriteLine ( stringBuilder . ToString ( ) ) ;
77- }
120+ }
0 commit comments