Skip to content

Commit

Permalink
Merge pull request #190 from VATSIM-UK/184-skip-compilation
Browse files Browse the repository at this point in the history
feat: runtime options
  • Loading branch information
AndyTWF authored Aug 17, 2022
2 parents ed5c271 + 09ed5f5 commit 96ab3ac
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 25 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ times, then the compiler will attempt to merge the configs together.

### Optional

`--skip-validation` - If set, the compiler will skip the post-parse validation phase of compilation,
`--check-config` - If set, only runs the configuration checking step to ensure that the compiler config is correct.

`--lint` - If set, only runs the configuration check and linting steps.

`--validate` - If set, only runs the configuration check, linting and post-validation steps. Does not output files.

`--skip-validation` - If set, the compiler will skip the post-parse validation phase of compilation.
If running in full compilation mode, will still produce output.

`--strip-comments` - If set, any comments in the input will be removed. If an empty line is leftover, it will be discarded.

Expand Down
11 changes: 7 additions & 4 deletions src/Compiler/Argument/CompilerArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CompilerArguments
public const int EmptyFolderIgnore = 0;
public const int EmptyFolderWarning = 1;
public const int EmptyFolderError = 2;

public const string DefaultBuildVersion = "BUILD_VERSION";

public List<string> ConfigFiles { get; } = new();
Expand All @@ -34,7 +34,7 @@ public override bool Equals(Object obj)
// Different length, so definitely not equal
if (ConfigFiles.Count != compare.ConfigFiles.Count)
{
return false ;
return false;
}

// Check every one is equal.
Expand All @@ -48,7 +48,7 @@ public override bool Equals(Object obj)

return true;
}

public override int GetHashCode()
{
return base.GetHashCode();
Expand All @@ -65,11 +65,14 @@ public override int GetHashCode()

// Replace tokens in the output
public List<ITokenReplacer> TokenReplacers { get; } = new();

// The version being built
public string BuildVersion { get; set; } = DefaultBuildVersion;

// What to do about empty folders
public int EmptyFolderAction { get; set; } = EmptyFolderIgnore;

// What mode should we run in.
public RunMode Mode { get; set; } = RunMode.COMPILE;
}
}
13 changes: 13 additions & 0 deletions src/Compiler/Argument/RunMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace Compiler.Argument
{
[Flags]
public enum RunMode : ushort
{
CHECK_CONFIG = 1,
LINT = 2,
VALIDATE = 4,
COMPILE = 8
}
}
51 changes: 36 additions & 15 deletions src/Compiler/SectorFileCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class SectorFileCompiler
{
private readonly CompilerArguments arguments;
private readonly EventTracker events;
private const int SUCESS_RETURN = 0;
private const int FAILURE_RETURN = 1;

public SectorFileCompiler(CompilerArguments arguments, EventTracker events)
{
Expand All @@ -38,7 +40,7 @@ public int Compile()
if (events.HasFatalError())
{
events.AddEvent(new CompilationFinishedEvent(false));
return 1;
return FAILURE_RETURN;
}

// Parse all the config files
Expand All @@ -49,11 +51,18 @@ public int Compile()
{
events.AddEvent(new CompilationMessage("Loading config files"));
config = ConfigFileLoaderFactory.Make(arguments).LoadConfigFiles(arguments.ConfigFiles, arguments);
} catch (ConfigFileInvalidException e)
}
catch (ConfigFileInvalidException e)
{
events.AddEvent(new CompilationMessage(e.Message));
events.AddEvent(new CompilationFinishedEvent(false));
return 1;
return FAILURE_RETURN;
}

events.AddEvent(new CompilationMessage("Config files loaded successfully"));
if (arguments.Mode == RunMode.CHECK_CONFIG)
{
return SUCESS_RETURN;
}

// Parse all the input files and create elements
Expand All @@ -74,18 +83,18 @@ public int Compile()
{
events.AddEvent(new CompilationMessage(exception.Message));
events.AddEvent(new CompilationFinishedEvent(false));
return 1;
return FAILURE_RETURN;
}

if (events.HasFatalError())
{
events.AddEvent(new CompilationFinishedEvent(false));
return 1;
return FAILURE_RETURN;
}

events.AddEvent(new CompilationMessage("Injecting pre-parse static data"));
RunwayCentrelineInjector.InjectRunwayCentrelineData(sectorElements);

events.AddEvent(new CompilationMessage("Parsing input files"));
foreach (AbstractSectorDataFile dataFile in fileList)
{
Expand All @@ -95,13 +104,18 @@ public int Compile()
if (events.HasFatalError())
{
events.AddEvent(new CompilationFinishedEvent(false));
return 1;
return FAILURE_RETURN;
}

// There's some static data we need to inject to the collection for adjacent airports...
events.AddEvent(new CompilationMessage("Injecting post-parse static data"));
AdjacentAirportsInjector.InjectAdjacentAirportsData(sectorElements);


// We aren't going beyond linting, so stop.
if (arguments.Mode == RunMode.LINT)
{
return SUCESS_RETURN;
}

// Now all the data is loaded, validate that there are no broken references etc.
if (arguments.ValidateOutput)
Expand All @@ -111,14 +125,20 @@ public int Compile()
if (events.HasFatalError())
{
events.AddEvent(new CompilationFinishedEvent(false));
return 1;
return FAILURE_RETURN;
}
}
}
else
{
events.AddEvent(new CompilationMessage("Skipping output validation"));
}

// We aren't going beyond post-validating, so don't compile.
if (arguments.Mode == RunMode.VALIDATE)
{
return SUCESS_RETURN;
}

// Generate the output - all at once
OutputGenerator generator = new OutputGenerator(
sectorElements,
Expand All @@ -127,15 +147,16 @@ public int Compile()
);

var outputTasks = new List<Task>();
foreach(AbstractOutputFile output in arguments.OutputFiles)
foreach (AbstractOutputFile output in arguments.OutputFiles)
{
events.AddEvent(new CompilationMessage($"Generating {output.GetFileDescriptor()} output"));
outputTasks.Add(generator.GenerateOutput(output));
}

Task.WaitAll(outputTasks.ToArray());

events.AddEvent(new CompilationFinishedEvent(true));
return 0;
return SUCESS_RETURN;
}
}
}
4 changes: 2 additions & 2 deletions src/CompilerCli/Argument/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ string[] args

public bool HasCompilerArgument(Type type)
{
return availableCompilerArguments.Select(arg => arg.GetType() == type).Count() != 0;
return availableCompilerArguments.Any(arg => arg.GetType() == type);
}

public bool HasCliArgument(Type type)
{
return availableCompilerArguments.Select(arg => arg.GetType() == type).Count() != 0;
return availableCliArguments.Any(arg => arg.GetType() == type);
}

private int ProcessArguments(string[] args, int startIndex, Action<List<string>> process)
Expand Down
7 changes: 5 additions & 2 deletions src/CompilerCli/Argument/ArgumentParserFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public static ArgumentParser Make()
new SkipValidationCompilerArgument(),
new StripCommentsCompilerArgument(),
new ConfigFileCompilerArgument(),
new BuildVersionCompilerArgument()
new BuildVersionCompilerArgument(),
new CheckConfigCompilerArgument(),
new LintCompilerArgument(),
new ValidateCompilerArgument(),
},
new SortedSet<AbstractCliArgument>
{
Expand All @@ -25,4 +28,4 @@ public static ArgumentParser Make()
);
}
}
}
}
24 changes: 24 additions & 0 deletions src/CompilerCli/Compiler/CheckConfigCompilerArgument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using Compiler.Argument;

namespace CompilerCli.Compiler
{
public class CheckConfigCompilerArgument : AbstractCompilerArgument
{
public override void Parse(List<string> values, CompilerArguments compilerSettings)
{
if (values.Count != 0)
{
throw new ArgumentException("Check config argument does not take any options");
}

compilerSettings.Mode = RunMode.CHECK_CONFIG;
}

public override string GetSpecifier()
{
return "--check-config";
}
}
}
24 changes: 24 additions & 0 deletions src/CompilerCli/Compiler/LintCompilerArgument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using Compiler.Argument;

namespace CompilerCli.Compiler
{
public class LintCompilerArgument : AbstractCompilerArgument
{
public override void Parse(List<string> values, CompilerArguments compilerSettings)
{
if (values.Count != 0)
{
throw new ArgumentException("Lint argument does not take any options");
}

compilerSettings.Mode = RunMode.LINT;
}

public override string GetSpecifier()
{
return "--lint";
}
}
}
24 changes: 24 additions & 0 deletions src/CompilerCli/Compiler/ValidateCompilerArgument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using Compiler.Argument;

namespace CompilerCli.Compiler
{
public class ValidateCompilerArgument : AbstractCompilerArgument
{
public override void Parse(List<string> values, CompilerArguments compilerSettings)
{
if (values.Count != 0)
{
throw new ArgumentException("Validate argument does not take any options");
}

compilerSettings.Mode = RunMode.VALIDATE;
}

public override string GetSpecifier()
{
return "--validate";
}
}
}
5 changes: 4 additions & 1 deletion tests/CompilerCliTest/Argument/ArgumentParserFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class ArgumentParserFactoryTest
[InlineData(typeof(StripCommentsCompilerArgument))]
[InlineData(typeof(ConfigFileCompilerArgument))]
[InlineData(typeof(BuildVersionCompilerArgument))]
[InlineData(typeof(CheckConfigCompilerArgument))]
[InlineData(typeof(LintCompilerArgument))]
[InlineData(typeof(ValidateCompilerArgument))]
public void TestItAddsCompilerArguments(Type type)
{
Assert.True(ArgumentParserFactory.Make().HasCompilerArgument(type));
Expand All @@ -27,4 +30,4 @@ public void TestItAddsCliArguments(Type type)
Assert.True(ArgumentParserFactory.Make().HasCliArgument(type));
}
}
}
}
41 changes: 41 additions & 0 deletions tests/CompilerCliTest/Compiler/CheckConfigCompilerArgumentTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using Compiler.Argument;
using CompilerCli.Compiler;
using Xunit;

namespace CompilerCliTest.Compiler
{
public class CheckConfigCompilerArgumentTest
{
private CompilerArguments arguments;
private CheckConfigCompilerArgument checkConfigCompilerArgument;

public CheckConfigCompilerArgumentTest()
{
arguments = new CompilerArguments();
checkConfigCompilerArgument = new CheckConfigCompilerArgument();
}

[Fact]
public void TestItSetsCompileOnlyAsMode()
{
checkConfigCompilerArgument.Parse(new List<string>(), arguments);
Assert.Equal(RunMode.CHECK_CONFIG, arguments.Mode);
}

[Fact]
public void TestItThrowsExceptionOnValues()
{
Assert.Throws<ArgumentException>(
() => checkConfigCompilerArgument.Parse(new List<string>(new[] { "a"}), arguments)
);
}

[Fact]
public void TestItReturnsASpecifier()
{
Assert.Equal("--check-config", checkConfigCompilerArgument.GetSpecifier());
}
}
}
Loading

0 comments on commit 96ab3ac

Please sign in to comment.