File referenced by ProjectItem is not created when new Project created - visual-studio

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

Related

getting variables to work in visual studio templates

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.

Visual Studio .vstemplate files not copying files using $projectname$ parameter

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?

Is there a way to add source files to visual studio project from command-line?

I want to use sublime to edit a visual studio project.
I have a custom build:
{
"cmd": ["c:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild.exe"],
"working_dir": "${project_path:${folder:${file_path}}}/../Project"
}
But if I add new files I also need to include them in the project.
Is there a way to do this from the command line, maybe at compile-time?
I am working with opengl using c++;
I basically set up a project using one of the examples provided on the opengl website.
Then I opened the project folder in sublime text and successfully compiled it using the custom build system.
However, when I add NEW source files to the project (*.h and *.cpp) I get a linking error.
I get the same error when I build in visual studio.
The error disappeared after I had included the files by manually browsing and adding them to the project.
What I wanted was a way to automatically add all the source files in a folder to the project(via command line, or wildcard or smth else).
This way I can easily work on a vs2010 project in sublime, add new source files and build the project.
Or maybe there already is a better workflow for this?
You could try to modify your .vcxproj file to include any .h and .cpp file in your project folder or folders below.
In case of a c++ VS project you can try to alter your .vcxproj file like this:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- rest of project file untouched -->
<!-- start of modified part -->
<ItemGroup>
<ClInclude Include="**\*.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="**\*.cpp" />
</ItemGroup>
<!-- end of modified part -->
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Be aware that adding files to your project from inside VS at later point will replace the modification described above!
As an alternative you could also create an external project file holding the same <ItemGroup /> elements described above and include this project file into your .vcxproj.
I'll add an example of this alternative if you're interested.

Can I use TargetFileName to place a file up a directory within a Visual Studio Template?

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>

VS2010: VSIX installation doesn't deploy item templates inside it

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

Resources