Skip to content

Commit

Permalink
Add a framework for flexible options to be passed on the command line
Browse files Browse the repository at this point in the history
Motivation
----------
We want to support backward compat switches and other customizations,
but they'll need to work for extensions as well so it should be
flexible.

Modifications
-------------
Add name/value pair properties which can be passed on the command line,
similar to MSBuild.
  • Loading branch information
brantburnett committed Feb 1, 2025
1 parent 7c418a4 commit b55794d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/main/Yardarm.CommandLine/CommonCommand.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
using NuGet.Packaging.Core;

namespace Yardarm.CommandLine
{
Expand Down Expand Up @@ -65,5 +59,19 @@ protected virtual void ApplyNuGetSettings(YardarmGenerationSettings settings)
settings.TargetFrameworkMonikers = targetFrameworks.ToImmutableArray();
}
}

protected void ApplyProperties(YardarmGenerationSettings settings)
{
foreach (string property in _options.Properties)
{
string[] parts = property.Split('=', 2);
if (parts.Length != 2)
{
throw new ArgumentException($"Invalid property '{property}'. Properties must be in the format 'NAME=VALUE'.");
}

settings.Properties[parts[0]] = parts[1];
}
}
}
}
3 changes: 3 additions & 0 deletions src/main/Yardarm.CommandLine/CommonOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ public class CommonOptions

[Option("intermediate-dir", HelpText = "Intermediate output directory")]
public string IntermediateOutputPath { get; set; }

[Option('p', "property", Separator = ';', HelpText = "List of property values, separated by semi-colons, in NAME=VALUE format")]
public IEnumerable<string> Properties { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/main/Yardarm.CommandLine/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public async Task<int> ExecuteAsync(CancellationToken cancellationToken)

ApplyNuGetSettings(settings);

ApplyProperties(settings);

List<IntermediateStream> streams = await ApplyFileStreamsAsync(settings);
try
{
Expand Down
2 changes: 2 additions & 0 deletions src/main/Yardarm.CommandLine/RestoreCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public async Task<int> ExecuteAsync(CancellationToken cancellationToken)

ApplyNuGetSettings(settings);

ApplyProperties(settings);

settings
.AddLogging(builder =>
{
Expand Down
5 changes: 5 additions & 0 deletions src/main/Yardarm/YardarmGenerationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ public Stream XmlDocumentationOutput
/// </summary>
public List<string>? ReferencedAssemblies { get; set; }

/// <summary>
/// Properties to alter the behavior of the generation process.
/// </summary>
public Dictionary<string, string> Properties { get; } = [];

public CSharpCompilationOptions CompilationOptions { get; set; } =
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
.WithDeterministic(true)
Expand Down

0 comments on commit b55794d

Please sign in to comment.