dotnet/templating

applyFileRenamesToArgs is not working in Run script Post Action

Opened this issue · 1 comments

Product

dotnet CLI (dotnet new)

Describe The Bug

I'm encountering an issue with the .NET template engine where applyFileRenamesToArgs does not properly substitute the symbol values for ServiceName and ServiceType within the args of a post-action. I want to pass these parameters to a PowerShell script, but the symbols remain as placeholders (ServiceName}, {ServiceType) rather than being replaced with the actual values input by the user.

Expected Behavior: When using applyFileRenamesToArgs, I expect the symbols specified (ServiceName and ServiceType) to be replaced with the actual values provided by the user during template instantiation.
Template instantiation - example: dotnet new microservice -n TypeTest.MyServiceTest --ServiceType TypeTest --ServiceName MyServiceTest
Post-Action run script - example: powershell -ExecutionPolicy Bypass -File ./Create_file.ps1 TypeTest MyServiceTest

Actual Behavior: The placeholders {ServiceName} and {ServiceType} are not replaced with the user-specified values and are passed to the PowerShell script as literal strings instead.
What I have:
Template instantiation - example: dotnet new microservice -n TypeTest.MyServiceTest --ServiceType TypeTest --ServiceName MyServiceTest
Post-Action run script - example: powershell -ExecutionPolicy Bypass -File ./Create_file.ps1 ServiceType ServiceName

To Reproduce

  1. In template.json
    create symbols and postAction run script
 "symbols": {
    "ServiceType": {
      "type": "parameter",
      "description": "e.g., Frame, Application, Service",
      "replaces": "ServiceType",
      "datatype": "text",
      "fileRename": "ServiceType",
      "defaultValue": "ServiceType"
    },
    "ServiceName": {
      "type": "parameter",
      "description": "Name of the project",
      "replaces": "ServiceName",
      "datatype": "text",
      "fileRename": "ServiceName",
      "defaultValue": "ServiceName"
    }
  },
"postActions": [
    {
      "actionId": "3A7C4B45-1F5D-4A30-959A-51B88E82B5D2",
      "description": "Create .bicep",
      "continueOnError": true,
      "applyFileRenamesToArgs": ["ServiceType", "ServiceName"],
      "args": {
        "executable": "powershell",
        "args": "-ExecutionPolicy Bypass -File ../Create_file.ps1 \"${ServiceType}\" \"${ServiceName}\""
      }
    }
  ]

Generate project with standard command with --ServiceName and --ServiceType:

example: dotnet new microservice -n TypeTest.MyServiceTest --ServiceType TypeTest --ServiceName MyServiceTest

  1. There will be command from args from post-Actions with non-changed arguments.

dotnet Info

output .NET SDK: Version: 8.0.303 Commit: 29ab8e3268 Workload version: 8.0.300-manifests.34944930 MSBuild version: 17.10.4+10fbfbf2e

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.303\

.NET workloads installed:
[maui-windows]
Installation Source: VS 17.10.35122.118
Manifest Version: 8.0.61/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.61\WorkloadManifest.json
Install Type: FileBased

[maccatalyst]
Installation Source: VS 17.10.35122.118
Manifest Version: 17.2.8053/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maccatalyst\17.2.8053\WorkloadManifest.json
Install Type: FileBased

[ios]
Installation Source: VS 17.10.35122.118
Manifest Version: 17.2.8053/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.ios\17.2.8053\WorkloadManifest.json
Install Type: FileBased

[android]
Installation Source: VS 17.10.35122.118
Manifest Version: 34.0.95/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.android\34.0.95\WorkloadManifest.json
Install Type: FileBased

[aspire]
Installation Source: VS 17.10.35122.118
Manifest Version: 8.0.0/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.0.0\WorkloadManifest.json
Install Type: FileBased

Host:
Version: 8.0.7
Architecture: x64
Commit: 2aade6beb0

.NET SDKs installed:
6.0.427 [C:\Program Files\dotnet\sdk]
8.0.303 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.35 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.35 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.35 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
D:\source\main5\Sources\global.json

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

Visual Studio Version

I use Visual Studio Code

Additional context

Is my postAction correct?
How should I pass those arguments ?

I also tried to put "applyFileRenamesToArgs": ["ServiceType", "ServiceName"], inside args.. but still it didn't work.

I just learned it the hard way with a lot of trial and error. I feel like the documentation was not clear enough with some examples. If you've to supply the values for this line "args": "-ExecutionPolicy Bypass -File ../Create_file.ps1 \"${ServiceType}\" \"${ServiceName}\"". Then, you'll have to specify the key args in the applyFileRenamesToArgs array. Also, in this case, ${} doesn't do anything here.

The following should work

 "symbols": {
    "ServiceType": {
      "type": "parameter",
      "description": "e.g., Frame, Application, Service",
      "replaces": "ServiceType",
      "datatype": "text",
-     "fileRename": "ServiceType",
+     "fileRename": "_service_type_placeholder_",
      "defaultValue": "ServiceType"
    },
    "ServiceName": {
      "type": "parameter",
      "description": "Name of the project",
      "replaces": "ServiceName",
      "datatype": "text",
-     "fileRename": "ServiceName",
+     "fileRename": "_service_name_placeholder_",
      "defaultValue": "ServiceName"
    }
  },
"postActions": [
    {
      "actionId": "3A7C4B45-1F5D-4A30-959A-51B88E82B5D2",
      "description": "Create .bicep",
      "continueOnError": true,
-    "applyFileRenamesToArgs": ["ServiceType", "ServiceName"],
+    "applyFileRenamesToArgs": ["args"],
      "args": {
        "executable": "powershell",
-       "args": "-ExecutionPolicy Bypass -File ../Create_file.ps1 \"${ServiceType}\" \"${ServiceName}\""
+       "args": "-ExecutionPolicy Bypass -File ../Create_file.ps1 \"_service_type_placeholder_\" \"_service_name_placeholder_\""
      }
    }
  ]