tazel
A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.
Clonegit clone https://git.progmasoft.com/tazel
feat(cli): add tazel license and tazel help --short
- Commit
90bd84024282f48cdd4e9b6b1ec14cad2028f41e- Author
- Leitwolf11 ·
- Parents
cd8892022adc
Changes
CHANGELOG.md | 4 + docs/commands.md | 3 +- src/Progmasoft.Tazel/Cli/CommandLine.cs | 129 ++++++++++++++++++--- src/Progmasoft.Tazel/Cli/Commands.cs | 5 +- src/Progmasoft.Tazel/Cli/Help.cs | 43 ++++++- src/Progmasoft.Tazel/Cli/LicenseText.cs | 87 ++++++++++++++ src/Progmasoft.Tazel/Program.cs | 2 +- .../Progmasoft.Tazel.Tests/LicenseAndHelpTests.cs | 113 ++++++++++++++++++ 8 files changed, 363 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6407ed4..6e2bbbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ ## Unreleased +- `tazel help [command]` now supports `--short` (brief) alongside the medium default and `--long` (full). +- `tazel license [--output|-o text|md|markdown|rst]` prints Tazel's MIT license, the Progmasoft Patent Grant 1.1, and + the licenses of the libraries Tazel ships with to standard output. + ## 0.3.0 - 2026-11-30 - `tazel cache validate [--remote]` checks the action records, test results and compiled project files of `.tazel` diff --git a/docs/commands.md b/docs/commands.md index a82a3ba..6144fe8 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -21,7 +21,8 @@ | `tazel cache repair [--remote]` | removes broken records; with `--remote`, stores again what the remote cache lacks | | `tazel package` | writes `.tazel/package/$group.$module-$version.tazel.tar.xz` and its `.sha256sums` | | `tazel custom <name> [arguments]` | runs a command of the `tazel_custom.txt` of the current directory | -| `tazel version`, `tazel help [command] [--long]` | print the version, or what Tazel or one command does | +| `tazel version`, `tazel help [command] [--short\|--long]` | print the version, or what Tazel or one command does (medium by default, `--short` brief, `--long` full) | +| `tazel license [--output\|-o text\|md\|markdown\|rst]` | prints Tazel's MIT license, the Progmasoft Patent Grant 1.1, and the licenses of shipped libraries to stdout | ## Options diff --git a/src/Progmasoft.Tazel/Cli/CommandLine.cs b/src/Progmasoft.Tazel/Cli/CommandLine.cs index 02ee47f..58a43e5 100644 --- a/src/Progmasoft.Tazel/Cli/CommandLine.cs +++ b/src/Progmasoft.Tazel/Cli/CommandLine.cs @@ -89,7 +89,28 @@ internal sealed record CustomCommandLine(OutputOptions Output, IReadOnlyList<str internal sealed record VersionCommand(OutputOptions Output) : Command(Output); -internal sealed record HelpCommand(OutputOptions Output, string? Topic, bool Long = false) : Command(Output); +/// <summary>How much <c>tazel help</c> prints: <c>--short</c>, medium (default), or <c>--long</c>.</summary> +internal enum HelpLength +{ + Short, + Medium, + Long, +} + +/// <summary>Output format of <c>tazel license</c>.</summary> +internal enum LicenseFormat +{ + Text, + Markdown, + RestructuredText, +} + +internal sealed record LicenseCommand(OutputOptions Output, LicenseFormat Format = LicenseFormat.Text) : Command(Output); + +internal sealed record HelpCommand(OutputOptions Output, string? Topic, HelpLength Length = HelpLength.Medium) : Command(Output) +{ + public bool Long => Length == HelpLength.Long; +} internal sealed record CoverageCommand(OutputOptions Output, IReadOnlyList<string> Labels, BuildFlags Flags) : Command(Output); @@ -145,7 +166,7 @@ internal static class CommandLine /// <summary>The commands, as <c>tazel help</c> lists them.</summary> public static IReadOnlyList<string> Commands { get; } = ["build", "run", "test", "coverage", "query", "cquery", "aquery", "fetch", "vendor", "clean", "package", "custom", - "portal-modules", "cache", "explain", "version", "help", "dotnet", "limited-time", "options", "custom-cmds", "envs", "tool", "ignore"]; + "portal-modules", "cache", "explain", "version", "help", "license", "dotnet", "limited-time", "options", "custom-cmds", "envs", "tool", "ignore"]; private enum Kind { @@ -189,10 +210,11 @@ internal static class CommandLine new("linker-options", Kind.List, null, Building), new("zig-compiler-options", Kind.List, null, Building), new("expunge", Kind.Flag, null, "clean"), + new("short", Kind.Flag, null, "help"), new("long", Kind.Flag, null, "help", "explain"), new("exact", Kind.Flag, null, "portal-modules"), new("algorithm", Kind.Value, ["ed25519", "rsa-4096", "ml-dsa"], "cache"), - new("output", Kind.Value, ["label", "label_kind"], "query", "cquery", "aquery"), + new("output", Kind.Value, null, "query", "cquery", "aquery", "license"), ]; /// <summary>Reads <paramref name="arguments"/>.</summary> @@ -221,6 +243,19 @@ internal static class CommandLine private static Command Make(Scanned scanned) { var command = scanned.Command; + // -o means --output for tazel license and --output-dir for tazel tool. + if (command == "license") + { + var outputOption = Options.First(o => o.Long == "output"); + for (var i = 0; i < scanned.Seen.Count; i++) + { + if (scanned.Seen[i].Written.StartsWith("-o", StringComparison.Ordinal)) + { + scanned.Seen[i] = (outputOption, scanned.Seen[i].Values, scanned.Seen[i].Written); + } + } + } + var unscoped = scanned.Seen.FirstOrDefault(entry => entry.Option.Scope.Length > 0 && !entry.Option.Scope.Contains(command, StringComparer.Ordinal)); if (unscoped.Option is not null) @@ -333,6 +368,7 @@ internal static class CommandLine ? throw new CommandLineException("tazel custom needs the name of a command of tazel_custom.txt") : new CustomCommandLine(output, scanned.Positional), ["version"] = (scanned, output) => Bare(scanned, new VersionCommand(output)), + ["license"] = MakeLicense, // The one word after it is case-sensitive: C#, as the language is written. ["limited-time"] = (scanned, output) => scanned.Positional is ["C#"] ? new MigrateCommand(output) @@ -361,10 +397,46 @@ internal static class CommandLine : new RunCommand(output, positional.FirstOrDefault(), Flags(scanned.Seen), scanned.AfterDashes); } - private static Command MakeQuery(Scanned scanned, OutputOptions output, QueryKind kind) => - scanned.Positional.Count == 0 - ? throw new CommandLineException($"tazel {scanned.Command} needs an expression, such as deps(//app:greeter)") - : new QueryCommand(output, kind, string.Join(' ', scanned.Positional), Flags(scanned.Seen), Last(scanned.Seen, "output") ?? "label"); + private static Command MakeQuery(Scanned scanned, OutputOptions output, QueryKind kind) + { + if (scanned.Positional.Count == 0) + { + throw new CommandLineException($"tazel {scanned.Command} needs an expression, such as deps(//app:greeter)"); + } + + var format = Last(scanned.Seen, "output") ?? "label"; + var allowed = new[] { "label", "label_kind" }; + if (!allowed.Contains(format, StringComparer.Ordinal)) + { + var near = Suggestions.Nearest(format, allowed); + throw new CommandLineException( + $"--output takes {string.Join(", ", allowed)}, not '{format}'" + + (near is null ? "" : $"; did you mean --output {near}?")); + } + + return new QueryCommand(output, kind, string.Join(' ', scanned.Positional), Flags(scanned.Seen), format); + } + + private static Command MakeLicense(Scanned scanned, OutputOptions output) + { + NoPositional(scanned.Command, scanned.Positional); + var raw = Last(scanned.Seen, "output"); + if (raw is null) + { + return new LicenseCommand(output, LicenseFormat.Text); + } + + var allowed = new[] { "text", "md", "markdown", "rst" }; + return raw switch + { + "text" => new LicenseCommand(output, LicenseFormat.Text), + "md" or "markdown" => new LicenseCommand(output, LicenseFormat.Markdown), + "rst" => new LicenseCommand(output, LicenseFormat.RestructuredText), + _ => throw new CommandLineException( + $"--output takes {string.Join(", ", allowed)}, not '{raw}'" + + (Suggestions.Nearest(raw, allowed) is { } near ? $"; did you mean --output {near}?" : "")), + }; + } // --user, --project or --local: one at most; null for none. private static Dsl.Configuration.LayerKind? Scope(Scanned scanned) @@ -496,10 +568,21 @@ internal static class CommandLine return new CacheKeygenCommand(output, algorithm); } - private static Command MakeHelp(Scanned scanned, OutputOptions output) => - scanned.Positional.Count > 1 - ? throw new CommandLineException("tazel help takes one command") - : new HelpCommand(output, scanned.Positional.FirstOrDefault(), scanned.Has("long")); + private static Command MakeHelp(Scanned scanned, OutputOptions output) + { + if (scanned.Positional.Count > 1) + { + throw new CommandLineException("tazel help takes one command"); + } + + if (scanned.Has("short") && scanned.Has("long")) + { + throw new CommandLineException("--short and --long cannot be used together"); + } + + var length = scanned.Has("short") ? HelpLength.Short : scanned.Has("long") ? HelpLength.Long : HelpLength.Medium; + return new HelpCommand(output, scanned.Positional.FirstOrDefault(), length); + } /// <summary>Splits a list at commas; <c>\,</c> is a comma of a value.</summary> public static IReadOnlyList<string> SplitList(string text) @@ -577,10 +660,28 @@ internal static class CommandLine private static void ReadShort( string argument, IReadOnlyList<string> arguments, ref int index, List<(Option, List<string>, string)> seen) { - if (argument == "-o") + if (argument == "-o" || argument.StartsWith("-o", StringComparison.Ordinal)) { - var value = index < arguments.Count ? arguments[index++] : throw new CommandLineException("-o needs a directory"); - seen.Add((Options.First(o => o.Long == "output-dir"), value.Length > 0 ? [value] : throw new CommandLineException("-o has an empty value"), "-o")); + string value; + if (argument == "-o") + { + value = index < arguments.Count ? arguments[index++] : throw new CommandLineException("-o needs a value"); + } + else if (argument[2] is '=' or ':') + { + value = argument[3..]; + } + else + { + value = argument[2..]; + } + + if (value.Length == 0) + { + throw new CommandLineException("-o has an empty value"); + } + + seen.Add((Options.First(o => o.Long == "output-dir"), [value], "-o")); return; } diff --git a/src/Progmasoft.Tazel/Cli/Commands.cs b/src/Progmasoft.Tazel/Cli/Commands.cs index 967e8f2..2557a70 100644 --- a/src/Progmasoft.Tazel/Cli/Commands.cs +++ b/src/Progmasoft.Tazel/Cli/Commands.cs @@ -62,7 +62,10 @@ internal sealed partial class Commands(Reporter reporter, string workingDirector reporter.Plain($"tazel {TazelVersion.ToString(3)} (.NET {Environment.Version}, {Host.Platform.Name()})"); return 0; case HelpCommand help: - reporter.Plain(Help.Text(help.Topic, help.Long)); + reporter.Plain(Help.Text(help.Topic, help.Length)); + return 0; + case LicenseCommand license: + reporter.Plain(LicenseText.Format(license.Format)); return 0; case ExplainCommand explain: Explain(explain); diff --git a/src/Progmasoft.Tazel/Cli/Help.cs b/src/Progmasoft.Tazel/Cli/Help.cs index ac34a50..33a12f1 100644 --- a/src/Progmasoft.Tazel/Cli/Help.cs +++ b/src/Progmasoft.Tazel/Cli/Help.cs @@ -312,6 +312,13 @@ internal static class Help tazel_options.txt comes first and the command line after it. A setting not given is left to .NET. Nothing reaches what Tazel builds or starts, and --jobs is untouched. """), + ["license"] = new("tazel license [--output|-o text|md|markdown|rst]: prints Tazel's license and shipped library notices.", """ + tazel license [--output|-o text|md|markdown|rst] + + Prints Tazel's MIT license, the Progmasoft Patent Grant 1.1, and the licenses of the third-party libraries + Tazel ships with, always to standard output. + --output, -o text|md|markdown|rst output format; default: text + """), }; private const string GlobalOptions = """ @@ -326,17 +333,41 @@ internal static class Help """; /// <summary>The help of <paramref name="topic"/>, or the general help; <paramref name="long"/> says more.</summary> - public static string Text(string? topic, bool @long = false) + public static string Text(string? topic, bool @long) => + Text(topic, @long ? HelpLength.Long : HelpLength.Medium); + + /// <summary>The help of <paramref name="topic"/>, or the general help, in <paramref name="length"/>.</summary> + public static string Text(string? topic, HelpLength length = HelpLength.Medium) { if (topic is not null) { - return Topics.TryGetValue(topic, out var text) - ? (@long ? text.Long : text.Short + $"\nMore: tazel help {topic} --long").TrimEnd() - : throw new CommandLineException( + if (!Topics.TryGetValue(topic, out var text)) + { + throw new CommandLineException( $"'{topic}' is not a command of tazel{(Suggestions.Nearest(topic, Topics.Keys) is { } near ? $"; did you mean {near}?" : "")}"); + } + + return length switch + { + HelpLength.Short => text.Short.TrimEnd(), + HelpLength.Long => text.Long.TrimEnd(), + _ => (text.Short + $"\nMore: tazel help {topic} --long").TrimEnd(), + }; } var commands = string.Join('\n', Topics.Select(entry => $" {entry.Key,-15} {Summary(entry.Value.Short)}")); + if (length == HelpLength.Short) + { + return $""" + tazel <command> [arguments] + + Commands: + {commands} + + tazel help <command> prints one command's help; --long says more. + """.TrimEnd(); + } + var general = $""" Tazel builds C, C++, Objective-C, Objective-C++ and Zig. A module has a MODULE.csx at its root and a BUILD.csx in each package; targets are named by labels such as //app:greeter. @@ -345,9 +376,9 @@ internal static class Help {commands} {GlobalOptions} - tazel help <command> prints the help of one command; --long says more. + tazel help <command> prints the help of one command; --short says less, --long says more. """; - if (!@long) + if (length != HelpLength.Long) { return general.TrimEnd(); } diff --git a/src/Progmasoft.Tazel/Cli/LicenseText.cs b/src/Progmasoft.Tazel/Cli/LicenseText.cs new file mode 100644 index 0000000..d4421ad --- /dev/null +++ b/src/Progmasoft.Tazel/Cli/LicenseText.cs @@ -0,0 +1,87 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: MIT + +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Progmasoft.Tazel.Cli; + +/// <summary>What <c>tazel license</c> writes to standard output: Tazel's MIT license and shipped third-party libraries.</summary> +internal static class LicenseText +{ + private sealed record Library(string Name, string License, string Source); + + private static readonly IReadOnlyList<Library> ShippedLibraries = + [ + new("Spectre.Console", "MIT", "https://github.com/spectreconsole/spectre.console"), + new("Microsoft.CodeAnalysis.CSharp.Scripting (Roslyn)", "MIT", "https://github.com/dotnet/roslyn"), + new("NuGet.Versioning", "Apache-2.0", "https://github.com/NuGet/NuGet.Client"), + new("Lzma.Net", "MIT", "https://github.com/alirezahomai/Lzma.Net"), + new("BouncyCastle.Cryptography", "MIT", "https://github.com/bcgit/bc-csharp"), + ]; + + /// <summary>Formats Tazel's license and shipped third-party notices in <paramref name="format"/>.</summary> + public static string Format(LicenseFormat format) => format switch + { + LicenseFormat.Text => PlainText(), + LicenseFormat.Markdown => Markdown(), + LicenseFormat.RestructuredText => RestructuredText(), + _ => PlainText(), + }; + + private static string PlainText() + { + var libraries = string.Join('\n', ShippedLibraries.Select( + lib => $" - {lib.Name}: {lib.License} ({lib.Source})")); + return $""" + Tazel + License: MIT (https://github.com/Progmasoft/tazel/blob/main/LICENSE.txt) + Patent grant: Progmasoft Patent Grant 1.1 (https://github.com/Progmasoft/tazel/blob/main/PATENTS.md) + Copyright (c) 2026 Progmasoft <support@progmasoft.com> + + Shipped third-party libraries: + {libraries} + """; + } + + private static string Markdown() + { + var rows = string.Join('\n', ShippedLibraries.Select( + lib => $"| {lib.Name} | `{lib.License}` | {lib.Source} |")); + return $""" + # Tazel License + + - **License:** MIT ([LICENSE.txt](https://github.com/Progmasoft/tazel/blob/main/LICENSE.txt)) + - **Patent grant:** Progmasoft Patent Grant 1.1 ([PATENTS.md](https://github.com/Progmasoft/tazel/blob/main/PATENTS.md)) + - **Copyright:** 2026 Progmasoft `<support@progmasoft.com>` + + ## Shipped third-party libraries + + | Library | License | Source | + | --- | --- | --- | + {rows} + """; + } + + private static string RestructuredText() + { + var builder = new StringBuilder(); + builder.AppendLine("Tazel License"); + builder.AppendLine("============="); + builder.AppendLine(); + builder.AppendLine(":License: MIT (https://github.com/Progmasoft/tazel/blob/main/LICENSE.txt)"); + builder.AppendLine(":Patent grant: Progmasoft Patent Grant 1.1 (https://github.com/Progmasoft/tazel/blob/main/PATENTS.md)"); + builder.AppendLine(":Copyright: 2026 Progmasoft <support@progmasoft.com>"); + builder.AppendLine(); + builder.AppendLine("Shipped third-party libraries"); + builder.AppendLine("-----------------------------"); + builder.AppendLine(); + foreach (var lib in ShippedLibraries) + { + builder.Append("- ``").Append(lib.Name).Append("`` — ").Append(lib.License).Append(" (").Append(lib.Source).AppendLine(")"); + } + + return builder.ToString().TrimEnd(); + } +} diff --git a/src/Progmasoft.Tazel/Program.cs b/src/Progmasoft.Tazel/Program.cs index 8551865..36208da 100644 --- a/src/Progmasoft.Tazel/Program.cs +++ b/src/Progmasoft.Tazel/Program.cs @@ -126,7 +126,7 @@ internal static class Program /// </summary> private static void ApplyDotnetSettings(Command command, string directory) { - if (command is HelpCommand or ExplainCommand or VersionCommand or DotnetWhatIsCommand) + if (command is HelpCommand or ExplainCommand or VersionCommand or LicenseCommand or DotnetWhatIsCommand) { return; } diff --git a/tests/Progmasoft.Tazel.Tests/LicenseAndHelpTests.cs b/tests/Progmasoft.Tazel.Tests/LicenseAndHelpTests.cs new file mode 100644 index 0000000..fedb9b6 --- /dev/null +++ b/tests/Progmasoft.Tazel.Tests/LicenseAndHelpTests.cs @@ -0,0 +1,113 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: MIT + +using System; +using System.IO; +using System.Threading.Tasks; +using Progmasoft.Tazel.Cli; +using Xunit; + +namespace Progmasoft.Tazel.Tests; + +public sealed class LicenseAndHelpTests +{ + [Fact] + public void HelpSupportsShortMediumAndLong() + { + var medium = Assert.IsType<HelpCommand>(CommandLine.Parse(["help", "build"])); + Assert.Equal(HelpLength.Medium, medium.Length); + Assert.False(medium.Long); + + var @short = Assert.IsType<HelpCommand>(CommandLine.Parse(["help", "build", "--short"])); + Assert.Equal(HelpLength.Short, @short.Length); + Assert.False(@short.Long); + + var @long = Assert.IsType<HelpCommand>(CommandLine.Parse(["help", "build", "--long"])); + Assert.Equal(HelpLength.Long, @long.Length); + Assert.True(@long.Long); + + var shortText = Help.Text("build", HelpLength.Short); + var mediumText = Help.Text("build", HelpLength.Medium); + var longText = Help.Text("build", HelpLength.Long); + Assert.DoesNotContain("More: tazel help build --long", shortText, StringComparison.Ordinal); + Assert.Contains("More: tazel help build --long", mediumText, StringComparison.Ordinal); + Assert.True(longText.Length > mediumText.Length); + + var generalShort = Help.Text(null, HelpLength.Short); + var generalMedium = Help.Text(null, HelpLength.Medium); + var generalLong = Help.Text(null, HelpLength.Long); + Assert.DoesNotContain("Options of every command:", generalShort, StringComparison.Ordinal); + Assert.Contains("Options of every command:", generalMedium, StringComparison.Ordinal); + Assert.Contains("Exit status:", generalLong, StringComparison.Ordinal); + } + + [Fact] + public void HelpRejectsShortAndLongTogether() + { + var error = Assert.Throws<CommandLineException>(() => CommandLine.Parse(["help", "--short", "--long"])); + Assert.Contains("together", error.Message, StringComparison.Ordinal); + } + + [Theory] + [InlineData(new[] { "license" }, "Text")] + [InlineData(new[] { "license", "--output", "text" }, "Text")] + [InlineData(new[] { "license", "--output=md" }, "Markdown")] + [InlineData(new[] { "license", "--output:markdown" }, "Markdown")] + [InlineData(new[] { "license", "--output", "rst" }, "RestructuredText")] + [InlineData(new[] { "license", "-o", "md" }, "Markdown")] + [InlineData(new[] { "license", "-o=markdown" }, "Markdown")] + [InlineData(new[] { "license", "-o:rst" }, "RestructuredText")] + [InlineData(new[] { "license", "-orst" }, "RestructuredText")] + [InlineData(new[] { "-o", "md", "license" }, "Markdown")] + public void LicenseParsesAllOutputOptionForms(string[] args, string expected) + { + var cmd = Assert.IsType<LicenseCommand>(CommandLine.Parse(args)); + Assert.Equal(Enum.Parse<LicenseFormat>(expected), cmd.Format); + } + + [Fact] + public void LicenseRejectsUnknownFormatAndExtraArguments() + { + var bad = Assert.Throws<CommandLineException>(() => CommandLine.Parse(["license", "--output", "mdd"])); + Assert.Contains("did you mean --output md", bad.Message, StringComparison.Ordinal); + Assert.Throws<CommandLineException>(() => CommandLine.Parse(["license", "extra"])); + Assert.Throws<CommandLineException>(() => CommandLine.Parse(["license", "-o"])); + Assert.Throws<CommandLineException>(() => CommandLine.Parse(["license", "-o="])); + } + + [Fact] + public void LicenseFormatsIncludeMitPatentGrantAndShippedLibraries() + { + var text = LicenseText.Format(LicenseFormat.Text); + Assert.Contains("License: MIT", text, StringComparison.Ordinal); + Assert.Contains("Progmasoft Patent Grant 1.1", text, StringComparison.Ordinal); + Assert.Contains("Spectre.Console", text, StringComparison.Ordinal); + Assert.Contains("NuGet.Versioning", text, StringComparison.Ordinal); + + var md = LicenseText.Format(LicenseFormat.Markdown); + Assert.StartsWith("# Tazel License", md, StringComparison.Ordinal); + Assert.Contains("| Library | License | Source |", md, StringComparison.Ordinal); + + var rst = LicenseText.Format(LicenseFormat.RestructuredText); + Assert.StartsWith("Tazel License\n=============", rst, StringComparison.Ordinal); + Assert.Contains(":License: MIT", rst, StringComparison.Ordinal); + } + + [Fact] + public async Task LicenseRunsWithoutWorkspace() + { + var dir = Path.Combine(Path.GetTempPath(), "tazel-license-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(dir); + try + { + using var output = new StringWriter(); + var status = await Program.RunAsync(["--color", "never", "license", "-o", "md"], dir, output, TestContext.Current.CancellationToken); + Assert.Equal(0, status); + Assert.Contains("# Tazel License", output.ToString(), StringComparison.Ordinal); + } + finally + { + Directory.Delete(dir, recursive: true); + } + } +}