-
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
Prevent project name modifications #1066
Comments
If you're referring to some way to pass flags to an existing template to get certain replacements to not occur, unless the template author made some sort of provisions for it, then it can't be conditionally controlled without changing the template source. For templates you're creating, there are a number of possibilities. If you want to post details on a specific scenario, we can talk through your options. But I might be able to provide enough general guidelines here for you to accomplish what you're trying to do. Let's use the EmptyWeb-CSharp 2.0 template as an example: https://github.com/dotnet/templating/tree/rel/vs2017/3-Preview3/template_feed/Microsoft.DotNet.Web.ProjectTemplates.2.0/content/EmptyWeb-CSharp In template.json, this line is key to the type of content replacements you're referring to:
sourceName provides the literal string that will get replaced by the value of the In the emptyweb template, the file Program.cs contains the line: Caveat: replacement operations do not care about whitespace or word boundaries, so the namespace couldn't contain the substring Company.WebApplication1, or that part would get replaced. If this isn't sufficient for your scenario, please let me know, and we can figure out a different approach. |
Expanding on what @seancpeters mentioned: If the values that are being replaced shouldn't be driven by the user input at all, the suggestion would be to change sourceName to not have a value that is a substring of that value. If there's a particular occurrence that shouldn't be replaced, but most should be, then using flags to govern the behavior would be the suggestion. For all other cases, there's a detailed breakdown of how name replacements happens in #402. Essentially, there ends up being a map between different transformations of what the user supplied for the name parameter and the string that will get replaced by that transformed value - duplicated source variations are addressed in a last in wins fashion. Generally, it comes down to how "well specified" sourceName is (whether any of the transformations that are applied it it result in duplication of the value of sourceName under a different transformation). For example: Now, if we change to "Foo" for sourceName instead, it becomes a little more well specified, the class name variant is now used (because Foo is a safe class name without modification), but case variations are honored (because Foo != foo). Now, if we throw in a dot, making sourceName "Foo.Bar", it's no longer a safe class name (as the dot prevents that), so we end up with all occurrences of "Foo.Bar" being replaced with the namespace safe transform on the user's name value and "Foo_Bar" being replaced with the safe class name transform applied to the user's value. This is the level of specificity we currently use in the templates in this repo (which is insufficient). The next step would be to make sourceName not a valid namespace, but still agree with for file names - ex. "Foo.Bar Baz". This produces a namespace of Foo.Bar_Baz, so any occurrences of that string in the template would be replaced by the namespace safe version of the user input, while "Foo.Bar Baz" would be replaced with the exact value specified by the user (though it should instead be the safe file name version of it - @seancpeters did we do that as part of the unmerged PR for bringing name and value forms together?) Sorry for any typos/incoherence/tone issues, wrote this up from my phone. |
That's a lot of typing for a phone! |
Here is a comment by @mlorbetske in which he discussed some of this previously #402 (comment) |
@seancpeters @mlorbetske super helpful! Thank you both! |
@mlorbetske I'm running into a slightly different variant of this now... I have the project name as
any ideas? |
@seancpeters any thoughts on how to do about this? |
@dansiegel - we don't currently have a way to support this type of file renames. But we're currently designing a more robust system for file renaming in a lot of ways, including what you're asking for here. I'll keep this thread updated as we progress. |
@dansiegel - This PR #1175 is the first step towards having more control over file renaming in templates. It does enough to satisfy your need in this thread. Once it's merged, you'll be able to setup template configuration such as:
A couple things to note:
|
I should clarify about the |
will this be available in 2.0.0-pre3? |
@dansiegel - This won't be available for 2.0.0-pre3, it'll most likely be in the first 2.1.0 preview. |
@dansiegel - I've just merged the derived symbols code #1194 into dotnet:rel/2.1.0-preview1. This test file contains an example that implements what we'd discussed earlier in this thread: https://github.com/dotnet/templating/pull/1194/files#diff-48edb1d14984859d5e1d2cca3a168db7 This test file is quite similar to the previous, but also defines value forms on the resultant derived value:
configuration is applied to the derived value, creating additional replacements in the same manner that value forms are used on parameter type symbols. |
Currently if you do
dotnet new sometemplate -o Foo.Bar
this will result in certain fields recieving like the namespace of files being renamed toFoo_Bar
. For some replacements, this is acceptable, but for most like the namespace, this is highly undesirable. Is there some way to prevent this?The text was updated successfully, but these errors were encountered: