forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptServicesBuilderFactory.cs
More file actions
56 lines (49 loc) · 2.37 KB
/
ScriptServicesBuilderFactory.cs
File metadata and controls
56 lines (49 loc) · 2.37 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
using System;
using System.IO;
using ScriptCs.Contracts;
using ScriptCs.Hosting;
using ScriptCs.Logging;
using LogLevel = ScriptCs.Contracts.LogLevel;
namespace ScriptCs
{
public static class ScriptServicesBuilderFactory
{
public static IScriptServicesBuilder Create(Config config, string[] scriptArgs)
{
Guard.AgainstNullArgument("commandArgs", config);
Guard.AgainstNullArgument("scriptArgs", scriptArgs);
IConsole console = new ScriptConsole();
if (!string.IsNullOrWhiteSpace(config.OutputFile))
{
console = new FileConsole(config.OutputFile, console);
}
var configurator = new LoggerConfigurator(config.LogLevel);
configurator.Configure(console, new NoOpLogger());
var logger = configurator.GetLogger();
var initializationServices = new InitializationServices(logger);
initializationServices.GetAppDomainAssemblyResolver().Initialize();
// NOTE (adamralph): this is a hideous assumption about what happens inside the CommandFactory.
// It is a result of the ScriptServicesBuilderFactory also having to know what is going to happen inside the
// Command Factory so that it builds the builder(:-p) correctly in advance.
// This demonstrates the technical debt that exists with the ScriptServicesBuilderFactory and CommandFactory
// in their current form. We have a separate refactoring task raised to address this.
var repl = config.Repl ||
(!config.Clean && config.PackageName == null && !config.Save && config.ScriptName == null);
var scriptServicesBuilder = new ScriptServicesBuilder(console, logger, null, null, initializationServices)
.Cache(config.Cache)
.Debug(config.Debug)
.LogLevel(config.LogLevel)
.ScriptName(config.ScriptName)
.Repl(repl);
return scriptServicesBuilder.LoadModules(Path.GetExtension(config.ScriptName) ?? ".csx", config.Modules);
}
private class NoOpLogger : ILog
{
public bool Log(
Logging.LogLevel logLevel, Func<string> messageFunc, Exception exception, params object[] formatParameters)
{
return false;
}
}
}
}