-
-
Notifications
You must be signed in to change notification settings - Fork 741
/
Copy pathBootstrapFeature.cs
87 lines (73 loc) · 3.15 KB
/
BootstrapFeature.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Linq;
using Autofac;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Core.Scripting;
using Cake.Core.Scripting.Analysis;
using Cake.Infrastructure;
using Spectre.Cli;
namespace Cake.Features.Bootstrapping
{
public interface IBootstrapFeature
{
int Run(IRemainingArguments arguments, BootstrapFeatureSettings settings);
}
public sealed class BootstrapFeature : Feature, IBootstrapFeature
{
private readonly ICakeEnvironment _environment;
public BootstrapFeature(
IFileSystem fileSystem,
ICakeEnvironment environment,
IContainerConfigurator configurator) : base(fileSystem, environment, configurator)
{
_environment = environment;
}
public int Run(IRemainingArguments arguments, BootstrapFeatureSettings settings)
{
// Fix the script path.
settings.Script = settings.Script ?? new FilePath("build.cake");
settings.Script = settings.Script.MakeAbsolute(_environment);
// Read the configuration.
var configuration = ReadConfiguration(arguments, settings.Script.GetDirectory());
// Create the scope where we will perform the bootstrapping.
using (var scope = CreateScope(configuration, arguments))
{
var analyzer = scope.Resolve<IScriptAnalyzer>();
var processor = scope.Resolve<IScriptProcessor>();
// Set log verbosity for log in new scope.
var log = scope.Resolve<ICakeLog>();
log.Verbosity = settings.Verbosity;
// Get the root directory.
var root = settings.Script.GetDirectory();
// Analyze the script.
log.Debug("Looking for modules...");
ScriptAnalyzerResult result = PerformAnalysis(analyzer, root, settings);
if (result.Modules.Count == 0)
{
log.Debug("No modules found to install.");
return 0;
}
// Install modules.
processor.InstallModules(
result.Modules,
configuration.GetModulePath(root, _environment));
}
return 0;
}
private static ScriptAnalyzerResult PerformAnalysis(IScriptAnalyzer analyzer, DirectoryPath root, BootstrapFeatureSettings settings)
{
var result = analyzer.Analyze(settings.Script, new ScriptAnalyzerSettings() { Mode = ScriptAnalyzerMode.Modules });
if (!result.Succeeded)
{
var messages = string.Join("\n", result.Errors.Select(s => $"{root.GetRelativePath(s.File).FullPath}, line #{s.Line}: {s.Message}"));
throw new AggregateException($"Bootstrapping failed for '{settings.Script}'.\n{messages}");
}
return result;
}
}
}