2

I am creating my first template for a .NET solution. For the most part it works, but I have a string "MyService" at some places in the Program.cs that I would like to replace with "NewService" when I create a solution with this template.

{
  "$schema": "http://json.schemastore.org/template",
  "author": "ich",
  "classifications": [ "Solution", "Service" ],
  "identity": "Service.Template",
  "name": "Service Template",
  "shortName": "my-service",
  "sourceName": "Services.MyService",
  "preferNameDirectory": true,
  "placeholderFilename": ".keep",
  "symbols": {
    "Company": {
      "type": "parameter",
      "datatype": "string",
      "defaultValue": "not",
      "description": "not saying"
    },
    "ServiceName": {
      "type": "parameter",
      "datatype": "string",
      "defaultValue": "MyService",
      "description": "Kurzname des Service"
    }
  },
  "sources": [
    {
      "modifiers": [
        {
          "condition": "true",
          "replaces": [
            {
              "replace": "MyService",
              "with": "{ServiceName}",
              "files": "**/*.cs"
            }
          ]
        }
      ]
    }
  ],
  "tags": {
    "language": "C#",
    "type": "solution"
  }
}

After installing the template, I use it with this command:

dotnet new my-service -n TestService --ServiceName=NewService

At the places I have to replace a string, I tried using:

c.SwaggerEndpoint("v1/swagger.json", "MyService Service v1");
c.SwaggerEndpoint("v1/swagger.json", "{ServiceName} Service v1");
c.SwaggerEndpoint("v1/swagger.json", "{{ServiceName}} Service v1");
c.SwaggerEndpoint("v1/swagger.json", "__ServiceName__ Service v1");

What am I doing wrong?

Thank you for your help.

0

1 Answer 1

5

As far as I remember/understand, you can just add replaces to the symbol and remove the sources handling:

"ServiceName": {
  "type": "parameter",
  "datatype": "string",
  "defaultValue": "MyService",
  "description": "Kurzname des Service",
  "replaces": "MyService" // <- here
}

And in the template code:

Console.WriteLine("MyService Service v1");

enter image description here

Also probably it would be better to use a more meaningful replace marker. For example:

"replaces": "{TemplateServiceName}"
Console.WriteLine("{TemplateServiceName} Service v1");

Full template.json used:

{
  "$schema": "http://json.schemastore.org/template",
  "author": "sest",
  "classifications": [ "Common", "Web" ],
  "identity": "ExampleTemplate.AsyncProject",
  "name": "Example templates: async project",
  "shortName": "sesttemplate",
  "sourceName":"WebAppTemplate",
  "tags": {
    "language": "C#",
    "type": "project"
  },
  "symbols": {
    "ServiceName": {
      "type": "parameter",
      "datatype": "string",
      "defaultValue": "MyService",
      "description": "Kurzname des Service",
      "replaces": "{TemplateServiceName}"
    }
  }
}

See also:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.