Skip to content

Commit

Permalink
Merge branch 'patriksvensson-feature/GH-2833' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
devlead committed Sep 14, 2020
2 parents 51602fc + 32f44da commit a068ac0
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/Cake.Core.Tests/Fixtures/ScriptAnalyzerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ScriptAnalyzer CreateAnalyzer()

public ScriptAnalyzerResult Analyze(FilePath script)
{
return CreateAnalyzer().Analyze(script);
return CreateAnalyzer().Analyze(script, new ScriptAnalyzerSettings());
}

public void GivenScriptExist(FilePath path, string content)
Expand Down
7 changes: 4 additions & 3 deletions src/Cake.Core.Tests/Unit/Scripting/ScriptRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public void Should_Remove_Directory_From_Script_Path(string path)
// Given
var fixture = new ScriptRunnerFixture(path);
fixture.ScriptAnalyzer = Substitute.For<IScriptAnalyzer>();
fixture.ScriptAnalyzer.Analyze(Arg.Any<FilePath>())
fixture.ScriptAnalyzer.Analyze(Arg.Any<FilePath>(), Arg.Any<ScriptAnalyzerSettings>())
.Returns(new ScriptAnalyzerResult(new ScriptInformation(path), new List<string>()));
var runner = fixture.CreateScriptRunner();

Expand All @@ -259,7 +259,8 @@ public void Should_Remove_Directory_From_Script_Path(string path)

// Then
fixture.ScriptAnalyzer.Received(1).Analyze(
Arg.Is<FilePath>(f => f.FullPath == "build.cake"));
Arg.Is<FilePath>(f => f.FullPath == "build.cake"),
Arg.Any<ScriptAnalyzerSettings>());
}

[Fact]
Expand Down Expand Up @@ -350,7 +351,7 @@ public void Should_Log_All_Analyzer_Errors_And_Throw()
// Given
var fixture = new ScriptRunnerFixture();
fixture.ScriptAnalyzer = Substitute.For<IScriptAnalyzer>();
fixture.ScriptAnalyzer.Analyze(Arg.Any<FilePath>())
fixture.ScriptAnalyzer.Analyze(Arg.Any<FilePath>(), Arg.Any<ScriptAnalyzerSettings>())
.Returns(new ScriptAnalyzerResult(new ScriptInformation(fixture.Script), new List<string>(), new List<ScriptAnalyzerError>
{
new ScriptAnalyzerError("/Working/script1.cake", 2, "Error in script 1"),
Expand Down
3 changes: 2 additions & 1 deletion src/Cake.Core/Scripting/Analysis/IScriptAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public interface IScriptAnalyzer
/// Analyzes the specified script path.
/// </summary>
/// <param name="path">The path to the script to analyze.</param>
/// <param name="settings">The script analyzer settings.</param>
/// <returns>The script analysis result.</returns>
ScriptAnalyzerResult Analyze(FilePath path);
ScriptAnalyzerResult Analyze(FilePath path, ScriptAnalyzerSettings settings);
}
}
47 changes: 42 additions & 5 deletions src/Cake.Core/Scripting/Analysis/ScriptAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
Expand All @@ -23,7 +24,8 @@ public sealed class ScriptAnalyzer : IScriptAnalyzer
private readonly IFileSystem _fileSystem;
private readonly ICakeEnvironment _environment;
private readonly ICakeLog _log;
private readonly LineProcessor[] _lineProcessors;
private readonly LineProcessor[] _defaultProcessors;
private readonly LineProcessor[] _moduleProcessors;

/// <summary>
/// Initializes a new instance of the <see cref="ScriptAnalyzer"/> class.
Expand Down Expand Up @@ -55,7 +57,7 @@ public ScriptAnalyzer(
_environment = environment;
_log = log;

_lineProcessors = new LineProcessor[]
_defaultProcessors = new LineProcessor[]
{
new LoadDirectiveProcessor(providers),
new ReferenceDirectiveProcessor(_fileSystem, _environment),
Expand All @@ -67,14 +69,21 @@ public ScriptAnalyzer(
new DefineDirectiveProcessor(),
new ModuleDirectiveProcessor()
};

_moduleProcessors = new LineProcessor[]
{
new LoadDirectiveProcessor(providers),
new ModuleDirectiveProcessor()
};
}

/// <summary>
/// Analyzes the specified script path.
/// </summary>
/// <param name="path">The path to the script to analyze.</param>
/// <param name="settings">The script analyzer settings.</param>
/// <returns>The script analysis result.</returns>
public ScriptAnalyzerResult Analyze(FilePath path)
public ScriptAnalyzerResult Analyze(FilePath path, ScriptAnalyzerSettings settings)
{
if (path == null)
{
Expand All @@ -84,9 +93,15 @@ public ScriptAnalyzerResult Analyze(FilePath path)
// Make the script path absolute.
path = path.MakeAbsolute(_environment);

// Get the correct callback.
var callback = settings.Mode == ScriptAnalyzerMode.Modules
? ModuleAnalyzeCallback
: (Action<IScriptAnalyzerContext>)AnalyzeCallback;

// Create a new context.
var context = new ScriptAnalyzerContext(
_fileSystem, _environment, _log, AnalyzeCallback, path);
_fileSystem, _environment,
_log, callback, path);

// Analyze the script.
context.Analyze(path);
Expand All @@ -98,6 +113,28 @@ public ScriptAnalyzerResult Analyze(FilePath path)
context.Errors);
}

[SuppressMessage("ReSharper", "ConvertIfStatementToConditionalTernaryExpression")]
private void ModuleAnalyzeCallback(IScriptAnalyzerContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

// Iterate all lines in the script.
var lines = ReadLines(context.Current.Path);
foreach (var line in lines)
{
foreach (var processor in _defaultProcessors)
{
if (processor.Process(context, _environment.ExpandEnvironmentVariables(line), out var _))
{
break;
}
}
}
}

[SuppressMessage("ReSharper", "ConvertIfStatementToConditionalTernaryExpression")]
private void AnalyzeCallback(IScriptAnalyzerContext context)
{
Expand All @@ -117,7 +154,7 @@ private void AnalyzeCallback(IScriptAnalyzerContext context)
{
string replacement = null;

if (!_lineProcessors.Any(p => p.Process(context, _environment.ExpandEnvironmentVariables(line), out replacement)))
if (!_defaultProcessors.Any(p => p.Process(context, _environment.ExpandEnvironmentVariables(line), out replacement)))
{
context.AddScriptLine(line);
}
Expand Down
22 changes: 22 additions & 0 deletions src/Cake.Core/Scripting/Analysis/ScriptAnalyzerMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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.

namespace Cake.Core.Scripting.Analysis
{
/// <summary>
/// Represents the script analyzer mode.
/// </summary>
public enum ScriptAnalyzerMode
{
/// <summary>
/// Analyzes everything.
/// </summary>
Everything = 0,

/// <summary>
/// Analyzes modules.
/// </summary>
Modules = 1,
}
}
17 changes: 17 additions & 0 deletions src/Cake.Core/Scripting/Analysis/ScriptAnalyzerSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

namespace Cake.Core.Scripting.Analysis
{
/// <summary>
/// Represents settings for the script analyzer.
/// </summary>
public sealed class ScriptAnalyzerSettings
{
/// <summary>
/// Gets or sets the analyzer mode.
/// </summary>
public ScriptAnalyzerMode Mode { get; set; } = ScriptAnalyzerMode.Everything;
}
}
2 changes: 1 addition & 1 deletion src/Cake.Core/Scripting/ScriptRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void Run(IScriptHost host, FilePath scriptPath)

// Analyze the script file.
_log.Verbose("Analyzing build script...");
var result = _analyzer.Analyze(scriptPath.GetFilename());
var result = _analyzer.Analyze(scriptPath.GetFilename(), new ScriptAnalyzerSettings());

// Log all errors and throw
if (!result.Succeeded)
Expand Down
30 changes: 0 additions & 30 deletions src/Cake.Tests/Unit/Features/BootstrapFeatureTests.cs

This file was deleted.

5 changes: 3 additions & 2 deletions src/Cake.Tests/Unit/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public async Task Should_Use_Default_Parameters_By_Default()
settings.BuildHostKind == BuildHostKind.Build &&
settings.Debug == false &&
settings.Exclusive == false &&
settings.Script == null &&
settings.Verbosity == Verbosity.Normal));
settings.Script.FullPath == "build.cake" &&
settings.Verbosity == Verbosity.Normal &&
settings.NoBootstrapping == false));
}

[Theory]
Expand Down
2 changes: 1 addition & 1 deletion src/Cake/Cake.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.6.0" />
<PackageReference Include="Autofac" Version="4.9.4" />
<PackageReference Include="Spectre.Cli" Version="0.36.0 " />
<PackageReference Include="Spectre.Cli" Version="0.42.0" />
</ItemGroup>
</Project>
42 changes: 30 additions & 12 deletions src/Cake/Commands/DefaultCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,32 @@ public override int Execute(CommandContext context, DefaultCommandSettings setti
{
return _version.Run();
}
if (settings.ShowInfo)
else if (settings.ShowInfo)
{
return _info.Run();
}
if (settings.Bootstrap)
{
return _bootstrapper.Run(context.Remaining, new BootstrapFeatureSettings
{
Script = settings.Script,
Verbosity = settings.Verbosity
});
}

// Get the build host type.
var host = GetBuildHostKind(settings);

// Run the bootstrapper?
if (!settings.SkipBootstrap || settings.Bootstrap)
{
int bootstrapperResult = PerformBootstrapping(context, settings, host);
if (bootstrapperResult != 0 || settings.Bootstrap)
{
return bootstrapperResult;
}
}

// Run the build feature.
return _builder.Run(context.Remaining, new BuildFeatureSettings(host)
{
Script = settings.Script,
Verbosity = settings.Verbosity,
Exclusive = settings.Exclusive,
Debug = settings.Debug
Debug = settings.Debug,
NoBootstrapping = settings.SkipBootstrap,
});
}

Expand All @@ -73,15 +76,30 @@ private BuildHostKind GetBuildHostKind(DefaultCommandSettings settings)
{
return BuildHostKind.DryRun;
}
if (settings.ShowDescription)
else if (settings.ShowDescription)
{
return BuildHostKind.Description;
}
if (settings.ShowTree)
else if (settings.ShowTree)
{
return BuildHostKind.Tree;
}

return BuildHostKind.Build;
}

private int PerformBootstrapping(CommandContext context, DefaultCommandSettings settings, BuildHostKind host)
{
if (host != BuildHostKind.Build && host != BuildHostKind.DryRun)
{
return 0;
}

return _bootstrapper.Run(context.Remaining, new BootstrapFeatureSettings
{
Script = settings.Script,
Verbosity = settings.Verbosity
});
}
}
}
16 changes: 10 additions & 6 deletions src/Cake/Commands/DefaultCommandSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Cake.Commands
public sealed class DefaultCommandSettings : CommandSettings
{
[CommandArgument(0, "[SCRIPT]")]
[Description("The Cake script")]
[Description("The Cake script. Defaults to [grey]build.cake[/]")]
[TypeConverter(typeof(FilePathConverter))]
[DefaultValue("build.cake")]
public FilePath Script { get; set; }
Expand All @@ -28,18 +28,22 @@ public sealed class DefaultCommandSettings : CommandSettings
[Description("Launches script in debug mode.")]
public bool Debug { get; set; }

[CommandOption("-e|--exclusive")]
[Description("Execute a single task without any dependencies.")]
public bool Exclusive { get; set; }

[CommandOption("--dryrun|--noop|--whatif")]
[Description("Performs a dry run.")]
public bool DryRun { get; set; }

[CommandOption("--exclusive")]
[Description("Execute a single task without any dependencies.")]
public bool Exclusive { get; set; }

[CommandOption("--bootstrap")]
[Description("Download/install modules defined by #module directives")]
[Description("Download/install modules defined by [grey]#module[/] directives, but do not run build.")]
public bool Bootstrap { get; set; }

[CommandOption("--skip-bootstrap")]
[Description("Skips bootstrapping when running build.")]
public bool SkipBootstrap { get; set; }

[CommandOption("--showdescription|--description")]
[Description("Shows description about tasks.")]
public bool ShowDescription { get; set; }
Expand Down
Loading

0 comments on commit a068ac0

Please sign in to comment.