How to set Build Action for generated files from a T4?
Asked Answered
C

1

2

The question is: is there a way to have a setting in the *.tt file so that the generated files are set to a specified Build Action?

The thing is I am generating SQL script using a template. I use this script as pre-deploy script in Database project. Currently, I have to manually set the Build Action to Pre-Deploy every time a file being re-generated - I would like to automate it.

Both files - template and SQL that it generates - are included into project. In Project (*.sqlproj) they are as next tags:

<PreDeploy Include="Migration\PreDeployScript.sql">
   <DependentUpon>Migration\PreDeployScript.tt</DependentUpon>
</PreDeploy>

and

<None Include="Migration\PreDeployScript.tt">
   <Generator>TextTemplatingFileGenerator</Generator>
</None>

When I run Custom Tool to re-generate SQL (I need to do it quite often), then PreDeploy tag is removed and Build tag is put on its place. Like this:

<Build Include="Migration\PreDeployScript.sql">
   <DependentUpon>Migration\PreDeployScript.tt</DependentUpon>
</Build>

It breaks the project and I need to manually change back Build to PreDeploy.

Can specify something in Template file to preserve build action PreDeploy all the time?

Thank you!

Chatav answered 31/3, 2016 at 12:3 Comment(1)
Related to #30039339Aristotelian
P
1

You could use an item group with a wild card. This will make Visual Studio automatically include any file on disk relative to the project which matches the wildcard pattern.

This will only work (that is the files will only show when you load your project) if they exist on disk already.

This pattern will include any files with a g.sql suffix

 <ItemGroup>
    <Build Include="*\*.g.sql" />
 </ItemGroup>

Unfortunately Visual Studio likes to expand these wildcards when you save the project, which effectively removes it. The best way to fix this issue is with indirection.

Create a new MSBuild project file with just the item group in it, then import the new project file with the <Import> element in your SSDT project. The content from the included file will be merged into your primary SSDT project.

Predecease answered 28/5, 2016 at 10:1 Comment(3)
Thanks for your answer but it didn't help. I have updated the question. When I moved out PreDeploy tag to another project file and changed it with using of wild cards. But after re-generation of SQL with TT file it still adds this wrong Build tag, that I mentioned in question.Chatav
Upvoted, as the wildcard did work for me (I used the variation for all .sql files in the project, <Build Include="**\*.sql" />), but with caveats: 1. it did not work for Folder Include=**\* or any variation of that, 2. can add .sql files through VS, but cannot delete them without an error, 3. cannot change any "File Property > Build Action" from VS, or VS will explicitly write out all in the .sqlproj.Cumuliform
I tried to introduce a .targets file with an <Import>, but while it was clearly being imported (intentional errors in .targets would cause a project load error), the <Build Include...> tags seemed to be ignored regardless of path format.Cumuliform

© 2022 - 2024 — McMap. All rights reserved.