From df2992c886417a6e794658c97185afe7d2acb3b1 Mon Sep 17 00:00:00 2001 From: Victor Bushong Date: Tue, 14 May 2024 09:28:02 -0500 Subject: [PATCH 1/3] Add file name suffix option. Allows appending a custom suffix to generated file names before the extension. For example, if the suffix is `.g`, the generated client file name would be `moduleName.g.js` (or `moduleName.g.ts` when using TypeScript). --- .../JavaScriptGenerator.cs | 12 +++++++++--- .../JavaScriptGeneratorSettings.cs | 5 +++++ src/fsdgenjs/FsdGenJavaScriptApp.cs | 3 +++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs b/src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs index 5c7e023..9eaf4df 100644 --- a/src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs +++ b/src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs @@ -46,6 +46,11 @@ public static int GenerateJavaScript(JavaScriptGeneratorSettings settings) => /// public bool DisableESLint { get; set; } + /// + /// Suffix to append to generated file names before the extension. + /// + public string? FileNameSuffix { get; set; } + /// /// Generates the JavaScript/TypeScript output. /// @@ -58,9 +63,9 @@ public override CodeGenOutput GenerateOutput(ServiceInfo service) var moduleName = ModuleName ?? service.Name; var capModuleName = CodeGenUtility.Capitalize(moduleName); - var typesFileName = CodeGenUtility.Uncapitalize(moduleName) + "Types" + (TypeScript ? ".ts" : ".js"); - var clientFileName = CodeGenUtility.Uncapitalize(moduleName) + (TypeScript ? ".ts" : ".js"); - var serverFileName = CodeGenUtility.Uncapitalize(moduleName) + "Server" + (TypeScript ? ".ts" : ".js"); + var typesFileName = CodeGenUtility.Uncapitalize(moduleName) + "Types" + (FileNameSuffix ?? "") + (TypeScript ? ".ts" : ".js"); + var clientFileName = CodeGenUtility.Uncapitalize(moduleName) + (FileNameSuffix ?? "") + (TypeScript ? ".ts" : ".js"); + var serverFileName = CodeGenUtility.Uncapitalize(moduleName) + "Server" + (FileNameSuffix ?? "") + (TypeScript ? ".ts" : ".js"); var errors = new List(); var namedTexts = new List(); @@ -529,6 +534,7 @@ public override void ApplySettings(FileGeneratorSettings settings) Express = ourSettings.Express; Fastify = ourSettings.Fastify; DisableESLint = ourSettings.DisableESLint; + FileNameSuffix = ourSettings.FileNameSuffix; } /// diff --git a/src/Facility.CodeGen.JavaScript/JavaScriptGeneratorSettings.cs b/src/Facility.CodeGen.JavaScript/JavaScriptGeneratorSettings.cs index dbf0b2e..6261b86 100644 --- a/src/Facility.CodeGen.JavaScript/JavaScriptGeneratorSettings.cs +++ b/src/Facility.CodeGen.JavaScript/JavaScriptGeneratorSettings.cs @@ -34,5 +34,10 @@ public sealed class JavaScriptGeneratorSettings : FileGeneratorSettings /// True to disable ESLint via code comment. /// public bool DisableESLint { get; set; } + + /// + /// Suffix to append to generated file names before the extension. + /// + public string? FileNameSuffix { get; set; } } } diff --git a/src/fsdgenjs/FsdGenJavaScriptApp.cs b/src/fsdgenjs/FsdGenJavaScriptApp.cs index 45437d7..c209e65 100644 --- a/src/fsdgenjs/FsdGenJavaScriptApp.cs +++ b/src/fsdgenjs/FsdGenJavaScriptApp.cs @@ -26,6 +26,8 @@ public sealed class FsdGenJavaScriptApp : CodeGeneratorApp " Generates a Fastify plugin. When specified, only the server plugin is generated, not the client.", " --disable-eslint", " Disables ESLint via code comment.", + " --file-name-suffix", + " Suffix to append to generated file names before the file extension.", }; protected override CodeGenerator CreateGenerator() => new JavaScriptGenerator(); @@ -38,6 +40,7 @@ protected override FileGeneratorSettings CreateSettings(ArgsReader args) => Express = args.ReadFlag("express"), Fastify = args.ReadFlag("fastify"), DisableESLint = args.ReadFlag("disable-eslint"), + FileNameSuffix = args.ReadOption("file-name-suffix"), }; } } From 0fe6b2b8f76612ef8ef797a3d290293bdb134697 Mon Sep 17 00:00:00 2001 From: Victor Bushong Date: Tue, 14 May 2024 09:59:09 -0500 Subject: [PATCH 2/3] Add unit test. --- .../JavaScriptGeneratorTests.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Facility.CodeGen.JavaScript.UnitTests/JavaScriptGeneratorTests.cs b/tests/Facility.CodeGen.JavaScript.UnitTests/JavaScriptGeneratorTests.cs index e9fa96e..de6012e 100644 --- a/tests/Facility.CodeGen.JavaScript.UnitTests/JavaScriptGeneratorTests.cs +++ b/tests/Facility.CodeGen.JavaScript.UnitTests/JavaScriptGeneratorTests.cs @@ -277,6 +277,30 @@ export enum ExampleApiErrors { Assert.That(typesFile.Text, Contains.Substring(expectedErrorSet)); } + [TestCase("", true)] + [TestCase("", false)] + [TestCase("suffix", true)] + [TestCase("suffix", false)] + [TestCase(".g", true)] + [TestCase(".g", false)] + public void GenerateWithCustomFileNameSuffix(string suffix, bool isTypeScript) + { + const string definition = "service TestApi { }"; + var parser = new FsdParser(); + var service = parser.ParseDefinition(new ServiceDefinitionText("TestApi.fsd", definition)); + var generator = new JavaScriptGenerator { GeneratorName = "JavaScriptGeneratorTests", TypeScript = isTypeScript, Express = true, FileNameSuffix = suffix }; + var result = generator.GenerateOutput(service); + Assert.IsNotNull(result); + + Assert.AreEqual(isTypeScript ? 3 : 2, result.Files.Count); + var fullSuffix = suffix + (isTypeScript ? ".ts" : ".js"); + Assert.NotNull(result.Files.SingleOrDefault(f => f.Name == $"testApi{fullSuffix}")); + Assert.NotNull(result.Files.SingleOrDefault(f => f.Name == $"testApiServer{fullSuffix}")); + + if (isTypeScript) + Assert.NotNull(result.Files.SingleOrDefault(f => f.Name == $"testApiTypes{fullSuffix}")); + } + private void ThrowsServiceDefinitionException(string definition, string message) { var parser = new FsdParser(); From dc2b47c331bbf5d7b92dca1d389cc5adfcbff95a Mon Sep 17 00:00:00 2001 From: Victor Bushong Date: Fri, 17 May 2024 15:46:02 -0500 Subject: [PATCH 3/3] Update tests --- .../JavaScriptGeneratorTests.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/Facility.CodeGen.JavaScript.UnitTests/JavaScriptGeneratorTests.cs b/tests/Facility.CodeGen.JavaScript.UnitTests/JavaScriptGeneratorTests.cs index d67665d..18ca755 100644 --- a/tests/Facility.CodeGen.JavaScript.UnitTests/JavaScriptGeneratorTests.cs +++ b/tests/Facility.CodeGen.JavaScript.UnitTests/JavaScriptGeneratorTests.cs @@ -1,5 +1,6 @@ using System.Reflection; using Facility.Definition; +using Facility.Definition.CodeGen; using Facility.Definition.Fsd; using FluentAssertions; using NUnit.Framework; @@ -290,15 +291,16 @@ public void GenerateWithCustomFileNameSuffix(string suffix, bool isTypeScript) var service = parser.ParseDefinition(new ServiceDefinitionText("TestApi.fsd", definition)); var generator = new JavaScriptGenerator { GeneratorName = "JavaScriptGeneratorTests", TypeScript = isTypeScript, Express = true, FileNameSuffix = suffix }; var result = generator.GenerateOutput(service); - Assert.IsNotNull(result); + Assert.That(result, Is.Not.Null); - Assert.AreEqual(isTypeScript ? 3 : 2, result.Files.Count); + Assert.That(result.Files, Has.Count.EqualTo(isTypeScript ? 3 : 2)); var fullSuffix = suffix + (isTypeScript ? ".ts" : ".js"); - Assert.NotNull(result.Files.SingleOrDefault(f => f.Name == $"testApi{fullSuffix}")); - Assert.NotNull(result.Files.SingleOrDefault(f => f.Name == $"testApiServer{fullSuffix}")); + Assert.That(result.Files, Has.One.Matches(f => f.Name == $"testApi{fullSuffix}")); + Assert.That(result.Files, Has.One.Matches(f => f.Name == $"testApi{fullSuffix}")); + Assert.That(result.Files, Has.One.Matches(f => f.Name == $"testApiServer{fullSuffix}")); if (isTypeScript) - Assert.NotNull(result.Files.SingleOrDefault(f => f.Name == $"testApiTypes{fullSuffix}")); + Assert.That(result.Files.SingleOrDefault(f => f.Name == $"testApiTypes{fullSuffix}"), Is.Not.Null); } private void ThrowsServiceDefinitionException(string definition, string message)