简体   繁体   中英

TemplateBuilder and SideWaffle. Project template. Change default root namespace

I have successfully created a multiproject template using this guide: https://github.com/ligershark/side-waffle/wiki/How-to-create-a-multi-project-template

The issue im having is the ability to change the default namespace and default assembly name for the generated projects. Like it is explained here: http://reasoncodeexample.com/2013/06/09/creating-visual-studio-project-templates/ where a custom parameter defines f.ex. the default namespace.

Essentially i would like to ensure that our company name is always in the beginning of the namespace...

I cannot make it work with the SideWaffle project template, and im wondering if anyone has an idea as to how one can define the default namespace and assemblyname, while using the SideWaffle project template.

When using SideWaffle (to be more specific, TemplateBuilder , which SideWaffle is built on top of) you should be able to achieve this if you are creating a custom wizard.

I would recommend getting a sample project template with a custom wizard wired up first. Once you know your wizard is being invoked then start moving on.

Try the following

  1. Make your namespace declarations consistent and unique
  2. In your custom wizard create a new parameter which is passed into the templates, lets say $customnamespace$
  3. Use _preprocess.xml to replace the namespace with $customnamespace$ . That way you can keep your standard code so that you can F5/CTRL+F5 but the template version will have the namespace declaration replaced with this.

Then when your wizard is invoked pass the additional $customnamespace$ parameter and it will be replaced. The multi project wizards do this, so the source may be helpful https://github.com/ligershark/template-builder/blob/master/src/TemplateBuilder/ChildWizard.cs .

More info on replacements https://github.com/ligershark/side-waffle/wiki/How-replacements-work .

FYI we are looking to improve the docs so if you could contribute to the wiki that would be great.

I have tracked down what goes on. Since it will be a lengthy post i will make a new answer that explains the problems @ThBlitz

This is not an answer, but a further explanation into the above issues, and i apologize for the length.

So i have setup a IWizard implementation to modify the csproj and as discussed with @Sayed i have tried his solution. Here are my findings.

This is the interresting part of the IWizard implementation

public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            replacementsDictionary.Add("$companynamespace$", "Infomedia");
        }

And the _preprocess.xml

<?xml version="1.0" encoding="utf-8" ?>
<Preprocess>
  <TemplateInfo Path="CSharp\Infomedia"/>
  <Replacements Include="*.*" Exclude="*.vstemplate;*.csproj;*.jpg;*.png;*.ico;_preprocess.xml;_project.vstemplate.xml">
    <add key="ConsoleApp" value="$safeprojectname$"/>
    <add key="RootNamespace" value="$companynamespace$.$safeprojectname$"/>
    <add key="AssemblyName" value="$companynamespace$.$safeprojectname$"/>
    <add key="namespace" value="namespace $companynamespace$.$safeprojectname$"/>
  </Replacements>
</Preprocess>

The namespace part is only to test that the wizard is executed, and indeed it is, because i can observe that change to namespacing in all classes in the template in the experimental VS. (the classes are then invalid, but at least it shows the execution of the wizard).

But it should change the RootNamespace and Assemblyname and to do that i will need to remove the exclusion of *.csproj in the _preprocess.xml

Doing this change invalidates the xml. Example of cleaned _preprocess.xml

<?xml version="1.0" encoding="utf-8" ?>
<Preprocess>
  <TemplateInfo Path="CSharp\Infomedia"/>
  <Replacements Include="*.*" Exclude="*.vstemplate;*.jpg;*.png;*.ico;_preprocess.xml;_project.vstemplate.xml">
    <add key="ConsoleApp" value="$safeprojectname$"/>
    <add key="RootNamespace" value="$companynamespace$.$safeprojectname$"/>
    <add key="AssemblyName" value="$companynamespace$.$safeprojectname$"/>
    <add key="namespace" value="namespace $companynamespace$.$safeprojectname$"/>
  </Replacements>
</Preprocess>

observe the removal of *.csproj

Building the solution will cause compile error:

Name cannot begin with the '$' character, hexadecimal value 0x24. Line 10, position 6.

Ok. so the problem is now the presense of $ and removing them and any whitespaces will allow the solution to build, but of course the replacement will not work, since the replacement values cannot be found.

Modified xml that builds but does not work.

<?xml version="1.0" encoding="utf-8" ?>
<Preprocess>
  <TemplateInfo Path="CSharp\Infomedia"/>
  <Replacements Include="*.*" Exclude="*.vstemplate;*.jpg;*.png;*.ico;_preprocess.xml;_project.vstemplate.xml">
    <add key="ConsoleApp" value="$safeprojectname$"/>
    <add key="RootNamespace" value="companynamespace.safeprojectname"/>
    <add key="AssemblyName" value="companynamespace.safeprojectname"/>
    <add key="namespace" value="companynamespace.safeprojectname"/>
  </Replacements>
</Preprocess>

So @Sayed i really hope you have an idea as to what is going on here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM