-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use user input project name in template.json #1667
Comments
In your template.json, if you define a "sourceName", the value of the sourceName is what is replaced by the value of the --name input. For example, take a look at the CSharp 2.2 console template. The template.json, here: So with that specification in the template.json, the value of the input name will replace In that template, the Program.cs, here: If the input name value is not a valid symbol or namespace name, template processing attempts to modify the value so it's valid. To help template processing correctly do that, we recommend using a sourceName value structured like above - so that it includes periods and numbers. Something like |
Thanks @seancpeters , I've known this, what I wanna know is how to use symbols in template.json, for examples: "fullName": {
"type": "parameter",
"datatype": "text",
"replaces": "ServiceName",
"defaultValue": "'Service.'+$(sourceName)"
} |
Ah ok, for something like this, you should use a "derived" type symbol. #1066 has a good explanation of how they work. For your specific situation, something like this should work:
A simpler method that might work (depending on your situation) is to have the literal "Service." in your template content. For example if your sourceName = "Company.Application1", in the content you could have something like this:
When the file is processed, |
@seancpeters Thanks for your solution 👍 |
Is there any way for a conditional replace? |
@WeihanLi - yes this is possible. There is a way to cause the template processing to turn specific operations on or off for part of a file. Here is an example of causing conditional replacement of the string
This When |
I wanna use the symbol more flexible,like dynamic replace filename and code just like sourceName, is there a document or sample? How do I use |
#1553 might be what you're looking for. If not, could you elaborate on what you're trying to accomplish? |
my samples template.json file is as follows: {
"$schema": "http://json.schemastore.org/template",
"author": "Zhongyi dev team",
"classifications": [ "Web" ],
"name": "MockingJ Service",
"shortName": "mkj",
"tags": {
"language": "C#",
"type":"project"
},
"sourceName": "TServiceName",
"preferNameDirectory": true,
"symbols":{
"includeTest": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "true"
},
"svcType": {
"type": "parameter",
"datatype": "choice",
"defaultValue": "service",
"choices":[
{
"choice": "service",
"description": "service"
},
{
"choice": "business",
"description":"business"
}
]
},
"fullName": {
"type": "derived",
"valueSource": "name",
"valueTransform": "prepend",
"replaces": "FullServiceName"
}
},
"forms": {
"prepend": {
"identifier": "replace",
"pattern": "(?<=^)",
"replacement": "MockingJ.Services."
}
},
"sources":[{
"modifiers": [
{
"condition": "(!includeTest)",
"exclude": [ "MockingJ.Services.TServiceName.Test/**/*"]
},
{
"condition": "(svcType=='business')",
"rename":{
"MockingJ.Services.TServiceName":"MockingJ.Business.TServiceName"
},
"exclude": [ "MockingJ.Services.TServiceName.Business/**/*"]
},
{
"condition": "(svcType=='service')",
"exclude": [ "MockingJ.Business.TServiceName/**/*"]
}
]
}]
} and my files structure like: I wanna to generate file content depend on the choice when Use the When Can I replace the files name and file content in some file at the same time via |
For replacing content within the files, you'll need to use symbol replacements. |
Thanks @seancpeters , is symbol's replace behind the I'm trying to transform the namespace etc with a generator symbols,but not as I expected,I got "fullNameTransform": {
"type": "generated",
"generator": "switch",
"datatype": "string",
"parameters": {
"cases": [
{
"condition": "(svcType!='business')",
"value": "MockingJ.Services.TServiceName"
},
{
"condition": "(svcType=='business')",
"value": "MockingJ.Business.TServiceName"
}
]
},
"replaces": "MockingJ.Services.TServiceName"
} |
I set each *.cs files namespace and *.csproj file reference depend on my symbol #if (svcType=='service')
namespace MockingJ.Services.TServiceName.Web
#else
namespace MockingJ.Business.TServiceName.Web
#endif |
Is there a way to use -o input from the user to replace something? |
Hi did you able to get user input project name, solution name and path? my scenario is I need to get those values and replace other values, I know sourcename will replace but i need to replace other things aswell with those names. |
How to use user input project name in template.json?
I wanna rename some variables by project name, how can I get project name?
I've tried to use
(name)
,but it did not work.The text was updated successfully, but these errors were encountered: