From 9bfed399b799d3706d57dc573cb2aed9f828b9bf Mon Sep 17 00:00:00 2001 From: Manuel Martins Date: Tue, 5 May 2026 14:45:04 +0000 Subject: [PATCH] feat: simplify init command to better manage target MES version --- cmf-cli/Commands/init/InitCommand.cs | 35 ++++---- cmf-cli/Commands/new/HTMLCommand.cs | 8 +- cmf-cli/Commands/new/HelpCommand.cs | 6 +- cmf-cli/Commands/new/IoTCommand.cs | 7 +- core/Objects/ProjectConfig/ProjectConfigV1.cs | 2 +- tests/Specs/Init.cs | 87 +++++++++++++++++-- 6 files changed, 106 insertions(+), 39 deletions(-) diff --git a/cmf-cli/Commands/init/InitCommand.cs b/cmf-cli/Commands/init/InitCommand.cs index 92a8b5361..d10d736e3 100644 --- a/cmf-cli/Commands/init/InitCommand.cs +++ b/cmf-cli/Commands/init/InitCommand.cs @@ -178,15 +178,17 @@ public override void Configure(Command cmd) var nugetVersionOption = new Option("--nugetVersion") { - Description = "NuGet versions to target. This is usually the MES version", - Required = true + Description = "NuGet versions to target. Defaults to --MESVersion when not specified.", + Required = false, + DefaultValueFactory = argResult => { try { return argResult.GetValue(baseVersionOption); } catch { return null; } } }; cmd.Add(nugetVersionOption); var testScenariosNugetVersionOption = new Option("--testScenariosNugetVersion") { - Description = "Test Scenarios Nuget Version", - Required = true + Description = "Test Scenarios NuGet version. Defaults to --MESVersion when not specified.", + Required = false, + DefaultValueFactory = argResult => { try { return argResult.GetValue(baseVersionOption); } catch { return null; } } }; cmd.Add(testScenariosNugetVersionOption); @@ -357,6 +359,9 @@ public override void Configure(Command cmd) internal void Execute(InitArguments x) { using var activity = ExecutionContext.ServiceProvider?.GetService()?.StartExtendedActivity(this.GetType().Name); + + Log.Information($"Starting scaffolding for MES Version: {x.BaseVersion}"); + var args = new List() { // engine options @@ -496,17 +501,21 @@ internal void Execute(InitArguments x) args.AddRange(new [] {"--DevTasksVersion", x.DevTasksVersion ?? ""}); args.AddRange(new [] {"--HTMLStarterVersion", x.HTMLStarterVersion ?? ""}); args.AddRange(new [] {"--yoGeneratorVersion", x.yoGeneratorVersion ?? ""}); - args.AddRange(new [] {"--ngxSchematicsVersion", x.ngxSchematicsVersion ?? ""}); - if (x.nugetVersion != null) - { - args.AddRange(new [] {"--nugetVersion", x.nugetVersion}); - } - if (x.testScenariosNugetVersion != null) + if (string.IsNullOrWhiteSpace(x.ngxSchematicsVersion)) { - args.AddRange(new [] {"--testScenariosNugetVersion", x.testScenariosNugetVersion}); + var mesVer = Version.Parse(x.BaseVersion); + x.ngxSchematicsVersion = $"release-{mesVer.Major}{mesVer.Minor}{mesVer.Build}"; } + Log.Information($"Using ngx-schematics version: {x.ngxSchematicsVersion}"); + + args.AddRange(new [] {"--ngxSchematicsVersion", x.ngxSchematicsVersion}); + + args.AddRange(new [] {"--nugetVersion", x.nugetVersion}); + + args.AddRange(new [] {"--testScenariosNugetVersion", x.testScenariosNugetVersion}); + #region infrastructure if (x.infrastructure != null) @@ -567,10 +576,6 @@ internal void Execute(InitArguments x) throw new CliException("MES Versions under 10 are no longer supported with the newest version of the CLI. Please use cmf-cli 5.8.0 or lower."); } - if (string.IsNullOrWhiteSpace(x.ngxSchematicsVersion)) - { - throw new CliException("--ngxSchematicsVersion is missing, please specify it."); - } #endregion if (x.config != null) diff --git a/cmf-cli/Commands/new/HTMLCommand.cs b/cmf-cli/Commands/new/HTMLCommand.cs index 9ee3ceb78..01e02f831 100644 --- a/cmf-cli/Commands/new/HTMLCommand.cs +++ b/cmf-cli/Commands/new/HTMLCommand.cs @@ -86,10 +86,6 @@ public void Execute(IDirectoryInfo workingDir, string version) public void ExecuteV10(IDirectoryInfo workingDir, string version) { var ngxSchematicsVersion = ExecutionContext.Instance.ProjectConfig.NGXSchematicsVersion; - if (ngxSchematicsVersion == null) - { - throw new CliException("Seems like the repository scaffolding was run on a previous version of MES. Please re-init for versions 10+."); - } var baseLayer = ExecutionContext.Instance.ProjectConfig.BaseLayer ?? CliConstants.DefaultBaseLayer; this.baseWebPackage = baseLayer == BaseLayer.MES @@ -108,7 +104,7 @@ public void ExecuteV10(IDirectoryInfo workingDir, string version) var packageName = base.GeneratePackageName(workingDir)!.Value.Item1; var packageDir = workingDir.GetDirectories(packageName).First(); - var schematicsVersion = ngxSchematicsVersion.ToString() ?? $"@release-{mesVersion.Major}{mesVersion.Minor}{mesVersion.Build}"; + var schematicsVersion = !string.IsNullOrEmpty(ngxSchematicsVersion) ? ngxSchematicsVersion : $"release-{mesVersion.Major}{mesVersion.Minor}{mesVersion.Build}"; //After v11 we use Angular default routing var routing = mesVersion.Major >= 11 ? "true" : "false"; @@ -147,7 +143,7 @@ public void ExecuteV10(IDirectoryInfo workingDir, string version) "add", "--registry", ExecutionContext.Instance.ProjectConfig.NPMRegistry.OriginalString, "--skip-confirmation", $"@criticalmanufacturing/ngx-schematics@{schematicsVersion}", "--eslint", "--application", baseLayer.ToString(), - "--version", $"release-{mesVersion.Major}{mesVersion.Minor}{mesVersion.Build}" + "--version", schematicsVersion ], WorkingDirectory = packageDir, ForceColorOutput = false diff --git a/cmf-cli/Commands/new/HelpCommand.cs b/cmf-cli/Commands/new/HelpCommand.cs index df4f4cf86..f461ecb5d 100644 --- a/cmf-cli/Commands/new/HelpCommand.cs +++ b/cmf-cli/Commands/new/HelpCommand.cs @@ -99,14 +99,10 @@ public void Execute(IDirectoryInfo workingDir, string version) { int majorVersion = ExecutionContext.Instance.ProjectConfig.MESVersion.Major; var ngxSchematicsVersion = ExecutionContext.Instance.ProjectConfig.NGXSchematicsVersion; - if (ngxSchematicsVersion == null) - { - throw new CliException("Seems like the repository scaffolding was run on a previous version of MES. Please re-init for versions 10+."); - } var mesVersion = ExecutionContext.Instance.ProjectConfig.MESVersion; - this.schematicsVersion = ngxSchematicsVersion.ToString() ?? $"@release-{mesVersion.Major}{mesVersion.Minor}{mesVersion.Build}"; + this.schematicsVersion = ngxSchematicsVersion ?? $"release-{mesVersion.Major}{mesVersion.Minor}{mesVersion.Build}"; //Switch between v10 and v11 template switch (majorVersion) diff --git a/cmf-cli/Commands/new/IoTCommand.cs b/cmf-cli/Commands/new/IoTCommand.cs index f96152391..a7d189f96 100644 --- a/cmf-cli/Commands/new/IoTCommand.cs +++ b/cmf-cli/Commands/new/IoTCommand.cs @@ -178,11 +178,6 @@ public void ExecuteV10AngularPackage(IDirectoryInfo workingDir, string version, var ngxSchematicsVersion = ExecutionContext.Instance.ProjectConfig.NGXSchematicsVersion; - if (ngxSchematicsVersion == null) - { - throw new CliException("Seems like the repository scaffolding was run on a previous version of MES. Please re-init for versions 10+."); - } - IDirectoryInfo htmlPackageDir = fileSystem.DirectoryInfo.New(htmlPackageLocation); if (!htmlPackageDir.Exists) throw new CliException(string.Format(CliMessages.SomePackagesNotFound, string.Join(", ", htmlPackageLocation))); @@ -217,7 +212,7 @@ public void ExecuteV10AngularPackage(IDirectoryInfo workingDir, string version, var mesVersion = ExecutionContext.Instance.ProjectConfig.MESVersion; - var schematicsVersion = ngxSchematicsVersion.ToString() ?? $"@release-{mesVersion.Major}{mesVersion.Minor}{mesVersion.Build}"; + var schematicsVersion = ngxSchematicsVersion ?? $"release-{mesVersion.Major}{mesVersion.Minor}{mesVersion.Build}"; Log.Debug($"Creating new IoT Workspace {packageName}"); diff --git a/core/Objects/ProjectConfig/ProjectConfigV1.cs b/core/Objects/ProjectConfig/ProjectConfigV1.cs index 9083edd00..7e0ec5892 100644 --- a/core/Objects/ProjectConfig/ProjectConfigV1.cs +++ b/core/Objects/ProjectConfig/ProjectConfigV1.cs @@ -30,7 +30,7 @@ public class ProjectConfigV1 public SemanticVersion DevTasksVersion { get; set; } public Version HTMLStarterVersion { get; set; } public SemanticVersion YoGeneratorVersion { get; set; } - public SemanticVersion NGXSchematicsVersion { get; set; } + public string NGXSchematicsVersion { get; set; } public Version NugetVersion { get; set; } public Version TestScenariosNugetVersion { get; set; } [Newtonsoft.Json.JsonConverter(typeof(BooleanJsonConverter))] diff --git a/tests/Specs/Init.cs b/tests/Specs/Init.cs index dcfed78d7..31e4d8265 100644 --- a/tests/Specs/Init.cs +++ b/tests/Specs/Init.cs @@ -145,12 +145,87 @@ public void Init_Fail_MissingMandatoryArgumentsAndOptions() TestUtilities.GetParser(cmd).Invoke(Array.Empty(), console); Assert.Contains("Required argument missing for command: 'x'", console.Error.ToString()); - foreach (var optionName in new[] - { - "baseVersion", "nugetVersion", "testScenariosNugetVersion" - }) + Assert.Contains("Option '--baseVersion' is required.", console.Error.ToString()); + Assert.DoesNotContain("Option '--nugetVersion' is required.", console.Error.ToString()); + Assert.DoesNotContain("Option '--testScenariosNugetVersion' is required.", console.Error.ToString()); + } + + [Fact] + public void Init_NugetVersionsDefaultToMESVersion() + { + var tmp = TestUtilities.GetTmpDirectory(); + var projectName = Convert.ToHexString(Guid.NewGuid().ToByteArray()).Substring(0, 8); + var deploymentDir = "\\\\share\\deployment_dir"; + var mesVersion = "11.0.0"; + + var cur = Directory.GetCurrentDirectory(); + try { - Assert.Contains($"Option '--{optionName}' is required.", console.Error.ToString()); + var console = new TestConsole(); + Directory.SetCurrentDirectory(tmp); + + var initCommand = new InitCommand(); + var cmd = new Command("x"); + initCommand.Configure(cmd); + + TestUtilities.GetParser(cmd).Invoke(new[] + { + projectName, + "--infra", TestUtilities.GetFixturePath("init", "infrastructure.json"), + "-c", TestUtilities.GetFixturePath("init", "config.json"), + "--MESVersion", mesVersion, + // --nugetVersion, --testScenariosNugetVersion and --ngxSchematicsVersion are all intentionally omitted + "--deploymentDir", deploymentDir, + }, console); + + console.Error.ToString().Should().NotContain("nugetVersion"); + console.Error.ToString().Should().NotContain("testScenariosNugetVersion"); + console.Error.ToString().Should().NotContain("ngxSchematicsVersion"); + Assert.True(File.Exists("cmfpackage.json"), "root cmfpackage is missing"); + } + finally + { + Directory.SetCurrentDirectory(cur); + Directory.Delete(tmp, true); + } + } + + [Fact] + public void Init_DefaultsNgxSchematicsVersionToDistTag() + { + var tmp = TestUtilities.GetTmpDirectory(); + var projectName = Convert.ToHexString(Guid.NewGuid().ToByteArray()).Substring(0, 8); + var deploymentDir = "\\\\share\\deployment_dir"; + var mesVersion = "11.1.5"; + + var cur = Directory.GetCurrentDirectory(); + try + { + var console = new TestConsole(); + Directory.SetCurrentDirectory(tmp); + + var initCommand = new InitCommand(); + var cmd = new Command("x"); + initCommand.Configure(cmd); + + TestUtilities.GetParser(cmd).Invoke(new[] + { + projectName, + "--infra", TestUtilities.GetFixturePath("init", "infrastructure.json"), + "-c", TestUtilities.GetFixturePath("init", "config.json"), + "--MESVersion", mesVersion, + "--deploymentDir", deploymentDir, + }, console); + + console.Error.ToString().Should().BeEmpty("command should succeed when --ngxSchematicsVersion is omitted"); + Assert.True(File.Exists("cmfpackage.json"), "root cmfpackage is missing"); + File.ReadAllText(Path.Join(tmp, ".project-config.json")) + .Should().Contain(@"""NGXSchematicsVersion"": ""release-1115""", "ngxSchematicsVersion should default to the MES dist-tag"); + } + finally + { + Directory.SetCurrentDirectory(cur); + Directory.Delete(tmp, true); } } @@ -226,7 +301,7 @@ public void Init_Fail_MissingOptionsForGTv10() "--deploymentDir", deploymentDir, }, console); - Assert.Contains("--ngxSchematicsVersion is missing, please specify it.", console.Error.ToString()); + // ngxSchematicsVersion is now auto-resolved from npm when omitted console.Error.ToString().Should().NotContain("DevTasksVersion is required"); console.Error.ToString().Should().NotContain("HTMLStarterVersion is required"); console.Error.ToString().Should().NotContain("yoGeneratorVersion is required");