Conditionally exclude project creation in a multi-project Visual Studio project template - visual-studio-2010

I have a multi-project Visual Studio project template. I want to be able to, conditionally, exclude specific projects from the created solution.
I've looked into the ProjectFinishedGenerating method of my IWizard implementation, but I can't seem to figure out how to simply ignore a project.
So, essentially I want to do something like this pseudo-code in my IWizard implementation for projects:
public void ProjectFinishedGenerating(Project project)
{
if(ExcludeAcmeProject && project.Name=="Acme"))
{
project.Cancel();
}
}

I figured it out. I can look at the customParams parameter in the RunStarted method (it contains the path of the .vstemplate file) and throw a WizardCancelledException if I want to skip that specific project.

Related

How to include/exclude source files from the project depending on the build configuration?

I want to include a file in the project only under the 'Debug' build configuration and not in the 'Release' build. How can I do that via the IDE?
I am already able to achieve the above by manually editing the '*.vcxproj ' file.
<ClCompile Include="..\..\..\..\dbg_helper.c" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"/>
I am using Microsoft Visual Studio Express 2013 for Windows Desktop (Version 12.0.21005.1 REL).
If you only want to exclude it from the build and not from the entire project tree, you can do it from UI.
Just edit the file properties.
Now, you can change to the desired configuration, for which you want to edit its properties.
Exclude your file for all configurations and platforms.
And then include it only for the configuration you want it to build.
Unloading the project and edit the file by hand is technically done through the IDE, so I guess that you are looking for way to do this through the Project Properties, which is not possible.
In C# you can decorate your class with ConditionalAttribute like so:
[Conditional("DEBUG")] // or another constant you use in your configurations
public class MyClass {
...
}
And the similar for C++:
#if DEBUG // or another constant you use in your configurations
...
#endif
I would however not recommend using this too much as you can run into problems.

Finding the home directory for a Visual Studio 2010 extension

I am making changes to a Visual Studio wizard that creates a project from a template, and needs to add a reference to an assembly to the project that also lives in the extension directory. So I need to set the <hintpath>.
I have not been able to figure out how a running VS extension can discover its extension directory, which is a directory name like this:
%USERPROFILE%\AppData\Local\Microsoft\VisualStudio\10.0\Extensions\myCompany\myExtension
Using System.Reflection.Assembly.GetExecutingAssembly().CodeBase yields:
"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\myCompany.myExtension\v4.0_1.0.0.0__936015a19c3638eb\myCompany.myExtension.dll"
Unfortunately, not helpful. Using GetCallingAssembly() is no better--it points at another directory in the MSIL_GAC.
Is there a Visual Studio interface that returns this information? I haven't been able to find it.
If that's not possible, is it at least possible to determine if the extension is running in the experimental instance vs. non-experimental? I could use that information to locate the extension directory.
Maybe look at IInstalledExtension.InstallPath. You can get an IInstalledExtension via an IVsExtensionManager.
Unfortunately, the message in the remarks suggests this is not the right way to do things:
Although this API supports the Extension Manager infrastructure, we recommend that you do not use it because it is subject to change.
EDIT: Here's the code:
static IVsExtensionManager GetExtensionManager()
{
return myPackage.GetService(System.typeof(IVsExtensionManager)) as IVsExtensionManager;
}
static IInstalledExtension GetExtension(string identifier)
{
return GetExtensionManager().GetInstalledExtension(identifier);
}
static string GetExtensionDirectory(string identifier)
{
return GetExtension(identifier).InstallPath;
}
The string identifier is whatever you put in the "ID" field of your extension's source.extension.vsixmanifest file. It defaults to the package GUID.
Sorry for digging up an old answered question...
I was looking into a similar problem, and have implemented the Extension Manager, but decided this is just pretty awful, though if you do want to do use it these links will also help:
https://stackoverflow.com/a/24294185
http://blog.ninlabs.com/2011/04/auto-update-visual-studio-extensions/
However, I decided to look into how the CodeGenerator in Asp.Net.Scaffolding accessed template files. Turns out, very easily...
var path = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), "MyPath");
Simples

Project References in Custom VS Project Template

I am working on creating a custom Visual Studio template for multiple project types. I would like to be able to add reference across the projects in the solution. e.g. MVVM solution where View, Model and ViewModels are each in their own projects. I'd like to have the View have reference to the ViewModel project and the ViewModel project to have reference to the Model project.
Only documentation I can find on MSDN calls for strongly named assemblies to be referenced.
http://msdn.microsoft.com/en-us/library/ms185290.aspx
I am able to get partially there, by modifying the .cproj file using some of system parameters, but this is heavily dependent on naming conventions etc. for instance I can have the Model project referenced in the ViewModel project because the concatenation of the $safeprojectname$ and "Model" makes the correct assembly name. But I cant get reference of the VM in the V, nor can I add any references to any testing projects.
<ItemGroup>
<ProjectReference Include="..\$safeprojectname$Model\$safeprojectname$Model.csproj">
<Project>{30C01E8B-96AE-45B4-A7B5-8F7BDCA4BDAB}</Project>
<Name>$safeprojectname$Model</Name>
</ProjectReference>
</ItemGroup>
Does anyone know how I can go about achieving this in the template?
Thanks.
I dont know if this is the correct way to do this, but this is what I did. I ended up opening the config file in a text editor and replacing/adding the appropriate project references as necessary. By using appropriate naming conventions I was able to programatically create the project files, sub folders etc.

VS2010 MultiProject Template

How do I add a specific, NON-PROJECT Folder to a MultiProjectTemplate?
I.e I add a projectCollection to TemplateContent with all of my involved projects as ProjectTempateLink s inside.
How do I add a non-project related folder?
I've tried Folder both inside TemplateContent and inside a Project inside TemplateContent, but it doesn't work.
Looking at the schema http://msdn.microsoft.com/en-us/library/xwkxbww4%28v=VS.100%29.aspx it should be supported? Or is there an issue with using ProjectCollection and another tag inside TemplateContent
Not so much solved, as a workaround made:
I made a blank C# project, included the files necessary.
I then made a single project-template of this, then added it to my multi-project template.
It's silly that It's not possible to add a non-project related folder, but hey...

Visual Studio Solution Template - Link Source Control Projects

My team is creating some standard VS solution templates. We have a well-defined project structure, resources, etc. that we need to use every time we start a new project and this is the perfect solution. The basics work nicely.
However, as well as defining folder structure (etc.) it would be nice to be able to import a number of projects from VSS/TFS. We have a number of shared assemblies that will be used by all projects and it would be awesome to add a reference to these projects when creating a new project via our template. Can anyone tell me if this is possible and, if so, how it can be achieved?
I think there are 3 types of items you might want to templatize (is that a word?).
New Solution
New Project added to a solution
New item added to a project
I'm not sure whether its possible to add existing projects to the solution that is created when a project template is run. http://msdn.microsoft.com/en-us/library/ms185308.aspx shows how to create multiple project templates. You may have to either manually add them to the solution or create a script that modifies the .sln file to do that part.
Adding an assembly reference to either a project or item template is easily doable. The project template is pretty simple since you just need to modify your .vstemplate file for the project template(s). See http://msdn.microsoft.com/en-us/library/ms171405.aspx for reference.
Adding a new assembly reference when you add a new item from a template is a bit harder but can also be done. See http://msdn.microsoft.com/en-us/library/ms185290.aspx for more.
Have fun!

Resources