0

I am building a template using the dotnet new template engine.

I have a parameter that looks like this:

"symbols": {
  "rootEntity": {
    "displayName": "Root Entity",
    "type": "parameter",
    "datatype": "string",
    "replaces": "Notebook",
    "defultName": "Notebook",
    "isRequired": false,
    "description": "An example root level data entity"
   },
   ... other parameters here
}

It seems to mostly work, but it only replaces Notebook, not notebook. I could add another parameter for the lowercase version, but that requires users to type it in twice (yuck).

I saw the casing generator, but it does all uppercase or all lowercase (not just the first letter).

Digging more, I found a thing called a form that has the options firstLowerCase and firstUpperCase. But I can't find any examples using forms, only symbols (as shown in my example).

So, I have two possible questions (either will work):

  • How can I lowercase or uppercase the first letter of a symbol variable
  • How can I use forms in a template.

(I would prefer the second question as that enables me to use all the forms, but the first will get me moving forward, so I would be happy with either.)

Vaccano
  • 78,325
  • 149
  • 468
  • 850

1 Answers1

0

About forms

To use a form, it must be declared in the forms section, on the same level as the symbols section. After that, the form can be used where it is expected, for example, in the derived parameters. In the example below, there is a parameter UserName, and a transformative derived parameter that transforms the value of UserName to lowercase:

{
    "author": "Author",
    "name": "My template",
    "identity": "My.Template.Boo",
    "shortName": "mt",
    "symbols": {
        "UserName": {
            "displayName": "User name",
            "type": "parameter",
            "datatype": "string",
            "replaces": "UserName"
        },
        "UserNameLowerCase": {
            "type": "derived",
            "valueSource": "UserName",
            "valueTransform": "lowerCaseForm",
            "replaces": "username"
        }
    },
    "forms": {
        "lowerCaseForm": {
            "identifier": "lowerCase"
        }
    }
}

Lowercase first letter of a parameter

So, you have such a template.json file:

{
    "author": "Author",
    "name": "My template",
    "identity": "My.Template.Boo",
    "shortName": "mt",
    "symbols": {
        "rootEntity": { // parameter from command line
            "displayName": "Root Entity",
            "type": "parameter",
            "datatype": "string",
            "replaces": "Notebook",
            "defultName": "Notebook",
            "isRequired": false,
            "description": "An example root level data entity"
        }
    }
}

And you want to have a source file with the following code:

Upper: Notebook
lower: notebook

And after executing the command dotnet new mt --RootEntity "Hello world" you need a file with the following contents (as far as I understand):

Upper: Hello world
lower: Hello world

I suggest you use the derived parameter and an empty form:

{
    "author": "Author",
    "name": "My template",
    "identity": "My.Template.Boo",
    "shortName": "mt",
    "symbols": {
        "rootEntity": {
            "displayName": "Root Entity",
            "type": "parameter",
            "datatype": "string",
            "replaces": "Notebook",
            "defultName": "Notebook",
            "isRequired": false,
            "description": "An example root level data entity"
        },
        "rootEntityLowerCase": {
            "type": "derived",
            "valueSource": "rootEntity",
            "valueTransform": "notTransform",
            "replaces": "notebook"
        }
    },
    "forms": {
        "notTransform":{    
        }
    }
}

In the forms section, the notTransform form is declared. This form does nothing - it is empty. rootEntityLowerCase is a derived parameter that takes the value of the rootEntity parameter and applies to it the transformation specified by the notTransform form (this form does nothing :)). The value of the rootEntityLowerCase parameter is inserted wherever the notebook text occurs.

WhiteBite
  • 1
  • 1