How to access custom project properties from a T4 template? - t4

In VS 2010, is it possible to access a custom project property from a T4 template, e.g. in an Assembly directive?
I can't manage to access such properties, e.g. defined in a <PropertyGroup> in my .csproj file. Accessing predefined VS macros (like $(SolutionDir) or $(ConfigurationName)) works though.
Thanks and best regards,
Oliver

I solved the problem by creating a file containing the Assembly directive as a pre-build step (which can access the environment variables). In all my T4 templates I can then include this auto-generated file which loads the assembly.

Sure you can have the msbuild file set an environment variable which can then be pulled in via T4.
Also Use MSBuild Properties in T4 Templates there's a command-line way to do it.

Related

Access to visual studio variables from Wix

I have Visual Studio 2015 with Wix Extension 0.9.21.62588 and v4 toolset. According to the document these are the project references and variables I can use. In the pre build and post build steps I can access these fine
However I cant seem to access the other available options that a C++ project has access to example $(WindowsSdkDir), $(UniversalCRT_LibraryPath_x86) etc. Is it possible, if not is there a workaround
Usually you can do this by updating the $(DefineConstants) property in the project manually or through visual studio project properties > Build > Define preprocessor variables
You just need to use a semi-colon separated list of variable names-value pairs.
WindowsSdkDir=$(WindowsSdkDir);UniversalCRT_LibraryPath_x86=$(UniversalCRT_LibraryPath_x86)
In this format the $(VarName) references are the MSBuild properties. The name you want to use for your wix variables $(var.Name) can be anything you like.
Then in your wxs wix code you can access these values using $(var.WindowsSdkDir) or $(var.UniversalCRT_LibraryPath_x86)

T4 Visual Studio 2010 include linked T4

Here's one. I have a bunch of T4s in one project / solution. This is a framework with support code and T4 templates.
In a different solution, I want to use this framework, but have the support classes / T4s remain in the original solution.
In the new solution I link to the support code and T4s (add existing / link). Now in the new solution I have a T4 which needs to include the linked T4. It has something like this:
<## template language="C#" debug="false" hostspecific="true"#>
<## include file="..\Models\DALContextGenerator.tt"#>
<## output extension=".cs"#><#
Generate("..\Models\Model1.edmx");
>
In this case, DALContextGenerator.tt is in this solution, but is linked to the real DALContextGenerator.tt in a different solution. WHen I run the T4 I get an error ("Failed to resolve include text"). If I reference the physical location it is fine.
Any ideas?
Thanks
Ray
As far as I know, the T4 engine uses the template file as a root and is unaware of the Visual Studio Solution and Solution items. If you're using a Visual Studio link to a file somewhere else this information is only stored in the project file. The T4 engine looks up the include-path relative to the T4 file. That's why referencing the Visual Studio Link relatively fails. But referencing the include file either with its absolute path or a relative one pointing to the physical file succeeds.
Here are some ideas how to address your problem, but there is no "smooth" solution I can think of:
Use a hard link between the original include file and a file located next to the template file (command line: mklink /H source target)
if you are using a source control system (like svn) you can work with external directories without duplicating your originals
Have a (meta-)T4 template that generates the actual T4 template with the proper paths based on the information you get from Env.DTE Visual Studio Model
Old but relevant question, I think the same as this other thread, where I posted a reply, using expansions $(ProjectDir) and $(SolutionDir): https://stackoverflow.com/a/42785952/1948625

How to add generated code files in VS 2010 as dependent files?

I am automating my project using T4 templates. For this, I have to write some repeated code using T4 template and some hand written code which should not be overwritten by the T4 template. I would like to add the generated code file as the dependent file of the handwritten file. I am following convention of class1.cs for handwritten file and class1.generated.cs for generated file. How can I add this generated file in the project as dependent file of class1.cs?
To do this programmatically you'll have to modify the .csproj file directly. Refer to this question for details:
In Visual Studio (2008) is there a way to have a custom dependent file on another custom file?
If you want to do this via the IDE then use the VSCommonds Add-in for Visual Studio 2010.

Using Intellisense in Visual C++ 2008 Express. Intellisense for one project not available when editing file in another project

I have a multiple vcproj solution going and it seems that no other project's intellisense information is available when editing a file in another project. However when I'm within a file in a project, all intellisense information is available for that project.
Any idea why?
Are you missing a using directive or a reference ?
you need to include the other project's namespace in the file with a using directive in order for the intellisense to show for the other project.

How to NOT include Visual Studio Project folder names in generated namespaces

How do I prevent the name of the Solution Folder in a VS project from being appended to the namespace generated for new items added to the solution folder?
Example: C# project in a VS solution
Default Namespace set in C# project properties: "BigClient.Domain"
If you create a solution folder in this project called "MySpecialStuff" and then add a new class to the 'MySpecialStuff" solution folder, VS creates the new .cs file with a "BigClient.Domain.MySpecialStuff" namespace. I want to find a way to allow the namespace of the newly-added class to retain just the 'project-level' namespace of "BigClient.Domain" instead of VS appending the "MySpecialStuff" solution-folder-name to the namespace.
I seem to recall reading a blog post by someone that this was possible (either via options/settings in VS or a registry setting) but I cannot recall where or how now that I want it :)
My platform is VS 2008 if answer = different for different VS versions.
If you have ReSharper installed, on the settings for the directory (F4), set Namespace Provider to false.
Make a new class template or change Visual Studio's class template. See the MSDN documentation for how to this. In the template file, you quickly see the line that inserts the 'safe' namespace name.

Resources