I am having trouble getting Visual Studio project templates to work correctly.
I have edited the .vstemplate file in order to allow VS to rename project files when a new project is created from the template.
Some of the files are not copied or renamed when the new project is created.
I have a C++ project named Minimal that I have exported as a template.
I am currently NOT using any replaceable parameters in the Minimal template project.
The Minimal.zip folder in the Project Templates directory contains:
__TemplateIcon.ico (created by VS when the project was exported)
Minimal.cpp (my code)
Minmal.rc (a resource file containing a string table with one entry)
Minimal.vcxproj (created by VS at project creation time),
Minimal.vcxproj.filters (created by VS at project creation time)
MyTemplate.vstemplate (an XML file created by VS when the project was exported)
resource.h (created by VS at project creation time).
The MyTemplate.vstemplate file looks like this after editing:
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
<TemplateData>
<Name>Minimal</Name>
<Description><No description available></Description>
<ProjectType>VC</ProjectType>
<ProjectSubType>
</ProjectSubType>
<SortOrder>1000</SortOrder>
<CreateNewFolder>true</CreateNewFolder>
<DefaultName>Minimal</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<LocationField>Enabled</LocationField>
<EnableLocationBrowseButton>true</EnableLocationBrowseButton>
<Icon>__TemplateIcon.ico</Icon>
</TemplateData>
<TemplateContent>
<Project TargetFileName="Minimal.vcxproj" File="Minimal.vcxproj" ReplaceParameters="true">
<ProjectItem ReplaceParameters="false" TargetFileName="$projectname$.vcxproj.filters">Minimal.vcxproj.filters</ProjectItem>
<ProjectItem ReplaceParameters="false" TargetFileName="$projectname$.cpp">Minimal.cpp</ProjectItem>
<ProjectItem ReplaceParameters="false" TargetFileName="resource.h">resource.h</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="$projectname$.rc">Minimal.rc</ProjectItem>
</Project>
</TemplateContent>
</VSTemplate>
I edited the ProjectItem entries to use the $projectname$ parameters.
Two ProjectItems have ReplaceParameters set to "false" and a TargetFileName set to $projectname$.
One ProjectItem has ReplaceParameters set to "false" and TargetFileName unedited.
One ProjectItem has ReplaceParameters set to "true" and TargetFileName set to $projectname$.
I have a project named Test that I created from the Minimal template.
I would expect the Test project folder to contain one file for each ProjectItem, and the files that use the $projectname$ parameter to be renamed as Test.something.
The Test project folder actually contains this:
Test.sln (created by VS)
resource.h (not renamed, as expected)
Test.vcxproj (renamed, as expected)
Test.vcxproj.filters (renamed, as expected)
Test.vcxoproj.user (created by VS).
The Test project folder does not contain any .cpp or .rc file.
I have also tried not using $projectname$ for the .cpp file and the .rc file, and just used their original names, and they get copied into the new project.
Does anyone know what I need to do to make it work correctly with renamed files?
Related
I'm trying to make a simple proof of concept type visual studio template, but I can't seem to get the built in variables to work like they should. I'm trying to follow the documentation here https://learn.microsoft.com/en-us/visualstudio/ide/template-parameters?view=vs-2019 but it's not working.
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
<TemplateData>
<Name>Readers</Name>
<Description><No description available></Description>
<ProjectType>CSharp</ProjectType>
<ProjectSubType>
</ProjectSubType>
<SortOrder>1000</SortOrder>
<CreateNewFolder>true</CreateNewFolder>
<DefaultName>Readers</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<LocationField>Enabled</LocationField>
<EnableLocationBrowseButton>true</EnableLocationBrowseButton>
<Icon>__TemplateIcon.ico</Icon>
</TemplateData>
<TemplateContent>
<Project TargetFileName="Readers.csproj" File="Readers.csproj" ReplaceParameters="true">
<ProjectItem ReplaceParameters="true" TargetFileName="App.config">App.config</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="DBAccess.cs">DBAccess.cs</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$.cs">MainReader.cs</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="packages.config">packages.config</ProjectItem>
<Folder Name="Properties" TargetFolderName="Properties">
<ProjectItem ReplaceParameters="true" TargetFileName="AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="Resources.resx">Resources.resx</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="Resources.Designer.cs">Resources.Designer.cs</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="Settings.settings">Settings.settings</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="Settings.Designer.cs">Settings.Designer.cs</ProjectItem>
</Folder>
<ProjectItem ReplaceParameters="true" TargetFileName="ReaderBase.cs">ReaderBase.cs</ProjectItem>
</Project>
</TemplateContent>
</VSTemplate>
This is what I came up with following that example. This is basically the same template file that was generated when I did Project -> Export Template in visual studio except for the line.
<ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$.cs">MainReader.cs</ProjectItem>
which should rename the MainReader.cs file to the name of the project when I make a new one using this template. It does not, whenever I make a new project with this template that file keeps the name MainReader.cs. I've tried adding the ext_ extension to the name, I've tried using the variable inside the brackets instead of outside and both. There has to be something else I'm missing or maybe this just isn't a thing any more. The process I've been using to get the template in is using the export template wizard, extracting that zip file, changing the .vstemplate file, putting the whole thing back in a zip fil, and then finally replacing the zip file in exported templates folder for visual studio. There may be another place I need to put the revised zip file but I'm not sure.
So I finally managed to get this to work and it ended up being multiple issues.
Firstly I was actually saving my new template to the wrong place. The exported templates folder is not where visual studio looks for user created templates, this is confusing because it will automatically add any template you export to it's list so you may see this folder full of templates that show up when you're making a new project and think this is where it's pulling it's templates list from. the actual location for user created templates is noted here. https://learn.microsoft.com/en-us/visualstudio/ide/how-to-locate-and-organize-project-and-item-templates?view=vs-2019 in the user templates section. You can also change this location under tools -> options -> projects and solutions -> locations. The correct procedure is to export the template, extract the zip file somewhere you can work on it, make your changes to the vstemplate file and the proj file (I'll get to that in a bit), compress all of that into a zip file, and then place that file in the user templates directory.
Another thing I noticed, and this is specifically when setting the file name to a variable, is that you also have to edit the .csproj file (or any other project file I'm assuming) and change the file name in there to match what is on the .vstemplate file. In my case in order for this to work
<ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$.cs">MainReader.cs</ProjectItem>
my csproj file had to include this
<Compile Include="$safeprojectname$.cs" />
replacing the old line that listed MainReader.cs. With both of those taken care of I was able to see the file name changed like I expected it to be.
Trying to create my first Project Template for a simple VS2015 C# project. But when I try to create a Project with it, one of the files - post_build.bat - is missing.
The content of the project template ZIP file is:
AssemblyInfo.cs
Class1.cs
ImsProject.ico
ImsProject.vstemplate
post_build.bat
ProjectTemplate.csproj
The ImsProject.vstemplate file contains the following TemplateContent:
<TemplateContent>
<Project File="ProjectTemplate.csproj" ReplaceParameters="true">
<ProjectItem ReplaceParameters="true" TargetFileName="Properties\AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
<ProjectItem ReplaceParameters="true" OpenInEditor="true">Class1.cs</ProjectItem>
<ProjectItem>post_build.bat</ProjectItem>
</Project>
</TemplateContent>
Seems simple, but I must be doing something wrong.
Stoopid me!
I was thinking the post_build.bat file was not present was because when I created a new project it did not appear in the Solution Explorer in Visual Studio.
However ... it was present in the folder containing the new project.
The problem was I hadn't created a entry in an ItemGroup in the ProjectTemplate.csproj file.
Adding
<ItemGroup>
<None Include="post_build.bat" />
</ItemGroup>
solved the problem
I have a .cs file with a base class that I want to use from within many projects. I've first placed this cs file above the project folder (along side the .sln). Then, within the project, I've changed the
<Compile Include="BaseClass.cs">
to
<Compile Include="..\BaseClass.cs">
This works, and the file icon then has a little shortcut arrow.
The problem comes when I want to export the project as a project template. The BaseClass does not get included in the template because it is outside the project folder. I've tried adding the BaseClass.cs to the template zip and adding
<ProjectItem ReplaceParameters="true" TargetFileName="..\BaseClass.cs">BaseClass.cs</ProjectItem>
to the MyTemplate.vstemplate, but I get the error:
A target file name within the VSTemplate file is invalid, it contains a fully qualified path
If I remove the ..\, it includes the file directly.
If I leave the <ProjectItem> out of the .vstemplate file, and just put <Compile Include="..\BaseClass.cs"> in the .csproj file in the template, it looks for the file in Users/me/AppData/Local....
How can I get the template to reference a file outside the project directory?
This is a known issue which has not been solved by Microsoft : https://connect.microsoft.com/VisualStudio/feedback/details/565068/project-template-relative-file-path-does-not-work
You should be doing the mapping of the folders in the VSTemplate and not in the project. The VS would not do any manipulation but in the template.
An example would be something like this. The below would copy the file from the "InnerFolder" to the root.
<Folder Name="InnerFolder" TargetFolderName="." >
<ProjectItem ReplaceParameters="true" TargetFileName="Web.config">Web.config</ProjectItem>
</Folder>
I have created a multi-project template in VS 2010 and the folder structure created is:
Service1
MyService.Service1.CommunicationLayer
-->MyService.Service1.CommunicationLayer.csproj
MyService.Service1.DataAccessLayer
-->MyService.Service1.DataAccessLayer.csproj
MyService.Service1.ServiceLayer
-->MyService.Service1.ServiceLayer.csproj
But I need the structure as follows:
Service1
Communication
-->MyService.Service1.CommunicationLayer.csproj
DataAccess
-->MyService.Service1.DataAccessLayer.csproj
Services
-->MyService.Service1.ServiceLayer.csproj
Also, I need my SolutionFolder name to be set to Service1 which will be the name of project user provides.
Here is the .vstemplate file I am using:
`<!--Outer .vstemplate file -->
<VSTemplate Version="2.0.0" Type="ProjectGroup"
xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>DataAccessService</Name>
<Description>Data Access Service Template</Description>
<ProjectType>CSharp</ProjectType>
<Icon>__TemplateIcon.ico</Icon>
<CreateNewFolder>true</CreateNewFolder>
<ProvideDefaultName >true</ProvideDefaultName>
<DefaultName>DALService</DefaultName>
</TemplateData>
<TemplateContent>
<ProjectCollection>
<ProjectTemplateLink ProjectName="MyService.$safeprojectname$.CommunicationLayer">Communication\Communication.vstemplate</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="MyService.$safeprojectname$.DataAccessLayer">DataAccess\DataAccess.vstemplate</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="MyService.$safeprojectname$.ServiceLayer">Services\Services.vstemplate</ProjectTemplateLink>
</ProjectCollection>
</TemplateContent>
</VSTemplate>`
Any help would be appreciated.
Ok. So here is the solution I found:
I used TemplateWizard to customize my folder structure.
Create the projects with default project name and containing folder.
Create SolutionFolder using TemplateWizard.
Remove projects from Solution.
Rename the containing folder.
Add the project to SolutionFolder which I created in Step 2.
Hope this helps someone with similar issue.
Given demo solution with two VSX-projects:
1. Add New Project -> Extensibility -> Item Template - "ItemTemplate1"
2. Add New Project -> Extensibility -> VSIX Project - "VSIXProject1"
I did no changes in "ItemTemplate1", so it constains default item template (ItemTemplate1.vstemplate):
<VSTemplate Version="3.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>ItemTemplate1</Name>
<Description><No description available></Description>
<Icon>ItemTemplate1.ico</Icon>
<TemplateID>e298765c-97b8-4f4c-9b7b-a6b368f914df</TemplateID>
<ProjectType>CSharp</ProjectType>
<RequiredFrameworkVersion>2.0</RequiredFrameworkVersion>
<NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp>
<DefaultName>Class.cs</DefaultName>
</TemplateData>
<TemplateContent>
<References>
<Reference>
<Assembly>System</Assembly>
</Reference>
</References>
<ProjectItem ReplaceParameters="true">Class.cs</ProjectItem>
</TemplateContent>
</VSTemplate>
In VSIXProject1 I only edited source.extension.vsixmanifest adding reference to ItemTemplates1 project. After this vsixmanifest contains only one content description:
<Content>
<ItemTemplate>ItemTemplates</ItemTemplate>
</Content>
Then built it. In bin/debug I got VSIXProject1.vsix inside which I can see my item template in ItemTemplates\CSharp\1033\ItemTemplate1.zip file.
Everything looks great!
Except the fact it doesn't work. I run VSIXProject1.vsix, vsix installed (I can see it in the extension manager) but no any templates was copied to
"C:\Users\{UserName}\Documents\Visual Studio 2010\Templates\ItemTemplates" !
Templates that are installed through a VSIX don't get installed to the Templates folder in the user Documents folder. They get installed under %LocalAppData%\Microsoft\VisualStudio\10.0\Extensions\<YourExtensionFolder> with all the other content for that extension. (A VSIX install is basically just unzipping the VSIX to a folder.)
You should still be able to see the template when you try to add a new item to a C# project.
please check the website below.It has help to solve my issue.
http://silverlightdude.wordpress.com/2010/06/07/installing-the-prism-template-pack-for-visual-studio-2010/#comment-4