Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi file item template in multiple projects

Can I make an item template that will insert multiple files into different projects under the same solution?

I am NOT looking to add files under different folders in the same project.

This is what I am looking for:

Solution

  • Project 1

    • Template item 1
  • Project 2

    • Template item 2
like image 762
Eccentrikit Avatar asked Nov 09 '22 20:11

Eccentrikit


1 Answers

Ok based on VS 2017 I created one VSIX project one Library Project and one Item Template Project

1- In Item template project I create both item template files one and two. I also added wizardExtension info in vstemplate file like this

    <TemplateContent>
    <ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$Service.cs">Service.cs</ProjectItem>
    <ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$DTO.cs">DTO.cs</ProjectItem>
    <ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$Message.cs">Message.cs</ProjectItem>
  </TemplateContent>
  <WizardExtension>
    <Assembly>WizardImplementation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</Assembly>
    <FullClassName>WizardImplementation.WizardImplementation</FullClassName>
  </WizardExtension>

WizardImplementation is my class library project.

2- I added TemplateWizardInterface Nuget Package to the Class Library Project and implement IWizard in a class. In ProjectItemFinishedGenerating I am moving the second template item like a file to the second project location folder and add it to project programmatically using Microsoft.Build.Evaluation.ProjectCollection in Microsoft.Build dll.

public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
            if (projectItem.FileNames[0].Contains("DTO") || projectItem.FileNames[0].Contains("Message"))
            {
                string newPath = Path.GetFullPath(Path.Combine(projectItem.FileNames[0], @"..\Project2\"));
                if (!Directory.Exists(newPath))
                {
                    MessageBox.Show($"Message path does not exist. \r\n {newPath}");
                }
                else
                {
                    var newFullPath = Path.Combine(newPath, projectItem.Name);
                    File.Move(projectItem.FileNames[0], newFullPath);
                    var p = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.GetLoadedProjects(PathInformation.MessageProjectPath).FirstOrDefault();
                    if (p == null)
                        p = new Microsoft.Build.Evaluation.Project(PathInformation.MessageProjectPath);

                    var res = p.AddItem("Compile", newFullPath);
                    p.Save();
                    if(res == null)
                    {
                        MessageBox.Show("Nothing added to project");
                    }
                    else
                    {
                        MessageBox.Show($"{res.Count()} item added to project");
                    }
                }
            }
        }

for more information about IWizard look at this link https://msdn.microsoft.com/en-us/library/ms185301.aspx

3- In VSIX project I added the Class Library I created in second step to the Assets by type of Assembly into vsixmanifest. Also I added the item template project as a File and give the zip file created by VS after build of Item Project in bin.

like image 163
Moein Avatar answered Dec 06 '22 16:12

Moein