-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProgram.cs
More file actions
128 lines (112 loc) · 4.76 KB
/
Program.cs
File metadata and controls
128 lines (112 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// See https://aka.ms/new-console-template for more information
using System.Reflection;
using System.Text;
using DotNetCampus.Cli;
using DotNetCampus.Cli.Compiler;
using dotnetCampus.Configurations.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging.Console;
using Packaging.DebUOS;
using Packaging.DebUOS.Contexts.Configurations;
using Packaging.DebUOS.Tool;
var options = CommandLine.Parse(args).As<Options>();
if (options.ForceUtf8ConsoleOutput is true)
{
Console.OutputEncoding = Encoding.UTF8;
}
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole(loggerOptions => loggerOptions.FormatterName = MSBuildFormatter.FormatterName);
builder.AddConsoleFormatter<MSBuildFormatter, ConsoleFormatterOptions>();
});
var logger = loggerFactory.CreateLogger("");
try
{
if (!string.IsNullOrEmpty(options.BuildPath))
{
var packingFolder = new DirectoryInfo(options.BuildPath);
var outputPath = options.OutputPath ?? Path.Join(packingFolder.FullName, $"{packingFolder.Name}.deb");
var outputDebFile = new FileInfo(outputPath);
var debUosPackageCreator = new DebUOSPackageCreator(logger);
//var packingFolder = new DirectoryInfo(@"C:\lindexi\Work\");
//var outputDebFile = new FileInfo(@"C:\lindexi\Work\Downloader.deb");
debUosPackageCreator.PackageDeb(packingFolder, outputDebFile);
}
else if (!string.IsNullOrEmpty(options.PackageArgumentFilePath))
{
logger.LogInformation($"开始根据配置创建 UOS 的 deb 包。配置文件:{options.PackageArgumentFilePath}");
if (!File.Exists(options.PackageArgumentFilePath))
{
logger.LogError($"配置文件 '{options.PackageArgumentFilePath}' 不存在");
return;
}
var fileConfigurationRepo =
ConfigurationFactory.FromFile(options.PackageArgumentFilePath, RepoSyncingBehavior.Static);
var appConfigurator = fileConfigurationRepo.CreateAppConfigurator();
var configuration = appConfigurator.Of<DebUOSConfiguration>();
var fileStructCreator = new DebUOSPackageFileStructCreator(logger);
fileStructCreator.CreatePackagingFolder(configuration);
var packingFolder = new DirectoryInfo(configuration.PackingFolder!);
var outputDebFile = new FileInfo(configuration.DebUOSOutputFilePath!);
var workingFolder = new DirectoryInfo(configuration.WorkingFolder!);
var excludePackingDebFileExtensionsPredicate = configuration.ToExcludePackingDebFileExtensionsPredicate();
var debUosPackageCreator = new DebUOSPackageCreator(logger);
debUosPackageCreator.PackageDeb(packingFolder, outputDebFile, workingFolder,
optFileCanIncludePredicate: entry => /*取反,因为配置是不包括*/ !excludePackingDebFileExtensionsPredicate(entry));
}
else
{
// Show Help
var stringBuilder = new StringBuilder()
.AppendLine($"用法:[options] [arguments]");
foreach (var propertyInfo in typeof(Options).GetProperties())
{
var optionAttribute = propertyInfo.GetCustomAttribute<OptionAttribute>();
if (optionAttribute != null)
{
stringBuilder.AppendLine(
$"-{optionAttribute.ShortNames?.FirstOrDefault()} {(optionAttribute.LongNames?.FirstOrDefault() ?? string.Empty).PadRight(10)} {optionAttribute.Description} {optionAttribute.LocalizableDescription}");
}
}
stringBuilder.AppendLine($"OriginCommandline: {Environment.CommandLine}");
Console.WriteLine(stringBuilder.ToString());
}
}
catch (Exception e)
{
logger.LogError(e, "Fail.");
}
class MSBuildFormatter : ConsoleFormatter
{
public MSBuildFormatter() : base(FormatterName)
{
}
public const string FormatterName = "MSBuild";
public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter)
{
var logLevel = logEntry.LogLevel;
//var eventId = logEntry.EventId.Id;
var message = logEntry.Formatter(logEntry.State, logEntry.Exception);
var exception = logEntry.Exception;
var logLevelString = logLevel switch
{
LogLevel.Trace => "debug: ",
LogLevel.Debug => "debug: ",
LogLevel.Information => "info: ",
LogLevel.Warning => "warning: ",
LogLevel.Error => "error: ",
LogLevel.Critical => "error: ",
_ => "",
};
textWriter.Write($"{logLevelString}[DebUOS] {message}");
if (exception != null)
{
textWriter.WriteLine(exception);
}
else
{
textWriter.WriteLine();
}
}
}