5
5
using dotnetCampus . Cli ;
6
6
using dotnetCampus . Configurations . Core ;
7
7
using Microsoft . Extensions . Logging ;
8
+ using Microsoft . Extensions . Logging . Abstractions ;
8
9
using Microsoft . Extensions . Logging . Console ;
9
10
using Packaging . DebUOS ;
10
11
using Packaging . DebUOS . Contexts . Configurations ;
14
15
15
16
var loggerFactory = LoggerFactory . Create ( builder =>
16
17
{
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 > ( ) ;
22
20
} ) ;
23
21
24
22
var logger = loggerFactory . CreateLogger ( "" ) ;
25
23
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
38
25
{
39
- logger . LogInformation ( $ "开始根据配置创建 UOS 的 deb 包。配置文件:{ options . PackageArgumentFilePath } ") ;
40
- if ( ! File . Exists ( options . PackageArgumentFilePath ) )
26
+ if ( ! string . IsNullOrEmpty ( options . BuildPath ) )
41
27
{
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 ) ;
44
36
}
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 > ( ) ;
45
50
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 ) ;
49
53
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 ( ) ;
52
58
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
+ }
57
77
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." ) ;
61
84
}
62
- else
85
+
86
+ class MSBuildFormatter : ConsoleFormatter
63
87
{
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 )
68
89
{
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 } { message } ") ;
111
+ if ( exception != null )
112
+ {
113
+ textWriter . WriteLine ( exception ) ;
114
+ }
115
+ else
71
116
{
72
- stringBuilder . AppendLine ( $ "- { optionAttribute . ShortName } { ( optionAttribute . LongName ?? string . Empty ) . PadRight ( 10 ) } { optionAttribute . Description } { optionAttribute . LocalizableDescription } " ) ;
117
+ textWriter . WriteLine ( ) ;
73
118
}
74
119
}
75
-
76
- Console . WriteLine ( stringBuilder . ToString ( ) ) ;
77
- }
120
+ }
0 commit comments