Skip to content

Commit

Permalink
Add CommandLineParsed to allow initialization based on arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpotter committed May 31, 2024
1 parent 515854d commit cec73db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/Faithlife.Build/BuildApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,31 @@ public BuildTarget Target(string name)
return target;
}

/// <summary>
/// Adds an action to execute when the command line arguments have been parsed, but before the target(s) are evaluated and executed.
/// </summary>
/// <param name="commandLineParsed">The action to execute when the command line arguments have been parsed.</param>
public void CommandLineParsed(Action<BuildApp> commandLineParsed)
{
ArgumentNullException.ThrowIfNull(commandLineParsed);

m_commandLineParsedActions.Add(commandLineParsed);
}

internal BuildApp(CommandLineApplication app)
{
m_app = app;
m_targets = new List<BuildTarget>();
m_flags = new List<BuildFlag>();
m_options = new List<BuildOption>();
m_commandLineParsedActions = new List<Action<BuildApp>>();
}

internal IReadOnlyList<Action<BuildApp>> CommandLineParsedActions => m_commandLineParsedActions;

private readonly CommandLineApplication m_app;
private readonly List<BuildTarget> m_targets;
private readonly List<BuildFlag> m_flags;
private readonly List<BuildOption> m_options;
private readonly List<Action<BuildApp>> m_commandLineParsedActions;
}
12 changes: 9 additions & 3 deletions src/Faithlife.Build/BuildRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,18 @@ public static async Task<int> ExecuteAsync(string[] args, Action<BuildApp> initi
var helpFlag = buildApp.AddFlag("-h|-?|--help", "Show build help");
var targetsArgument = commandLineApp.Argument("targets", "The targets to build", multipleValues: true);

var bullseyeTargets = new Targets();
foreach (var target in buildApp.Targets)
bullseyeTargets.Add(name: target.Name, description: target.Description, dependsOn: target.Dependencies, action: target.RunAsync);
commandLineApp.OnParsingComplete(_ =>
{
foreach (var commandLineParsedAction in buildApp.CommandLineParsedActions)
commandLineParsedAction(buildApp);
});

commandLineApp.OnExecuteAsync(async _ =>
{
var bullseyeTargets = new Targets();
foreach (var target in buildApp.Targets)
bullseyeTargets.Add(name: target.Name, description: target.Description, dependsOn: target.Dependencies, action: target.RunAsync);

var targetNames = targetsArgument.Values.WhereNotNull().ToList();

if (targetNames.Count == 0 && buildApp.Targets.Any(x => x.Name == c_defaultTarget))
Expand Down

0 comments on commit cec73db

Please sign in to comment.