Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing parameters from a Visual Studio Item Template to the generated item

Im writing a Visual Studio Project Item Template and in this case I will only be using a xml file. My issue today is that i would like to pass the name of the file into the completed xml file. ie... when i name the filename for the template it creates the item file and adds it to my project as a xml file. But what i would like to happen is when it is creating the xml file it would pass the filename value into the xml file.

To try to explain it better i have a Item Template now in which the template xml has a line like so:

<!-- Created by ModBuddy on $time$ -->

The above is in the actual VS Item Template, so when I create an Item to add to my project, it actually creates my xml file with the following line in it.

<!-- Created by ModBuddy on 10/26/2014 1:16:16 AM -->

In essence it passes from the Item Template to my xml file the current DateTime at the time it creates the xml file. My question is how do you define and or pass parameters like $time$, and where can i find more information on doing that. I would like to be able to pass the filename of the file from the Visual Studio Item Template (Which i will create myself), into the working xml file, something like $filename$ but i dont know how to do this or where i can learn more on this.

like image 227
roadmaster Avatar asked Mar 16 '23 04:03

roadmaster


1 Answers

After much research I have found that you can create and pass your own custom parameters by using the format

$whatevernameyoulike$ 

through the use of Visual Studio Custom Wizards. Basically you create a new VS Project, Class Library and Implement the IWizard interface from the Assembly:

Microsoft.VisualStudio.TemplateWizardInterface 

You also have to create a Interface/WindowsForm in your Assembly for your Item or Project Template that you will be creating later. You then modify the RunStarted Method in your class that implements IWizard to pass your parameters like so:

public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
    GeneralInfoPage generalInfoPage = new GeneralInfoPage();
    ExtendedInfoPage extendedInfoPage = new ExtendedInfoPage();
    ModWizardMenu modHost = new ModWizardMenu();

    modHost.Pages.Add(generalInfoPage);
    modHost.Pages.Add(extendedInfoPage);

    if (modHost.ShowDialog() != DialogResult.OK)
    {
        throw new WizardCancelledException("The wizard has been canceled by the user.");
    }

    string modDescription = generalInfoPage.ModDescription;
    string toParse = modDescription;
    if (toParse.Length > 127)
        toParse = toParse.Substring(0, 127);

    createUnit = extendedInfoPage.createUnit.Checked;
    createBuilding = extendedInfoPage.createBuilding.Checked;
    createSponsor = extendedInfoPage.createSponsor.Checked;

    replacementsDictionary.Add("$unitName$", extendedInfoPage.unitName);
    replacementsDictionary.Add("$buildingName$", extendedInfoPage.buildingName);
    replacementsDictionary.Add("$sponsorName$", extendedInfoPage.sponsorName);
}

The main part here is the replacementsDictionary, This of course is only part of the solution. Next you have to create a Custom, Project or Item Template to access your newly created Wizard assembly.

Once you have created your VS Project or Item template then you extract the files from the created zip file and modify the .vstemplate file to add the reference to your custom wizard assembly like so:

<WizardExtension>
    <Assembly>rhodesMBWizard, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=56e9113a0fdbb124</Assembly>
    <FullClassName>rhodesMBWizard.InfoManager</FullClassName>
</WizardExtension>

Its important to make sure you have the correct Public Token Key for your Assembly and if your like me and you keep changing class names you will want to make sure that where I have rhodesMBWizard.InfoManager that should be:

yourAssemblyNamespace.classnameThatImplementsIWizard

If you look at your vstemplate file you will see:

<TemplateContent>
    <References />
    <ProjectItem TargetFileName="$fileinputname$.xml" ReplaceParameters="true">BuildingTemplate.xml</ProjectItem>
</TemplateContent>

The line starting with ProjectItem is the line that pulls your named parameters and passes them to the Project or Item Template filename at the end of that line, in my case BuildingTemplate.xml.

So in my BuildingTemplate.xml file I will have a bunch of

$buildingName$ 

entries, the Project or Item template pulls the value of

$buildingName$ 

out of the Replacements Dictionary in the CustomWizard which I created, it then creates for me a working xml file in my Project with all the parameters changed.

Now the main point to doing all of this is when you are creating your Project or Item Template you can receive user input in the form of the User Interface or WindowsForm mentioned earlier, allowing the user to make choices and name some things beforehand that will reduce the amount of work needed in the working xml file.

extra info: http://www.windowsdevcenter.com/pub/a/windows/2007/06/06/developing-visual-studio-project-wizards.html?page=1

like image 164
roadmaster Avatar answered Apr 06 '23 10:04

roadmaster