efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Run the string resources T4 files on build

Open AndriySvyryd opened this issue 3 years ago • 7 comments

AndriySvyryd avatar Feb 19 '22 02:02 AndriySvyryd

Does our T4 template run outside of VS already? /cc @bricelam

roji avatar Feb 19 '22 19:02 roji

@roji Our T4 templates currently require VS.

ajcvickers avatar Feb 21 '22 09:02 ajcvickers

I think I already have an MQ issue to clean these up and make them runnable outside of VS. Not sure I’d want them to run on build, but there’s been some work in mono/t4 on an MSBuild task to do this.

bricelam avatar Feb 21 '22 21:02 bricelam

I worked on something like that to run the my own t4 files on build in Visual Studio with an msbuild task that invokes dotnet-t4 from mono/t4 without using their msbuild task (as I am not aware on the status of that task on the ability to use it cross platform unlike with it using dotnet-t4.

But then again, I could argue this: I think that dotnet ef scaffold should be the only places where the templates are used (provided the templates are needed for scaffolding).

AraHaan avatar Sep 18 '22 12:09 AraHaan

@AraHaan this is about string resx that generate string resources and logging code, so internal EF stuff, not scaffolding.

roji avatar Sep 18 '22 13:09 roji

Ah, I see. Then yeah an msbuild task like this would work too:

  <PropertyGroup>
    <T4OutputDirectory>[the folder that efcore outputs them currently]</T4OutputDirectory>
  </PropertyGroup>

  <Target
    BeforeTargets="CoreCompile"
    Name="T4TransformXPlat"
    Condition="[only generate the code files only when those files do not exist]">
    <Message Importance="high" Text="Running t4 text transforms." />
    <Exec
      Command="dotnet t4 -o - -p:RootNamespace=$(RootNamespace) -p:OutputDirectory=&quot;$(T4OutputDirectory)&quot; -p:ProjectName=$(MSBuildProjectName) &lt; &quot;$(MSBuildProjectDirectory)/[the t4 file paths here]&quot;"
      StandardErrorImportance="low"
      StandardOutputImportance="low" />
    <ItemGroup>
      <Compile Include="$(T4OutputDirectory)/[the generated files here]" />
    </ItemGroup>
    <Message Importance="high" Text="Done outputting files." />
  </Target>

This should work for basic templates that write file(s) directly, yes my t4 file takes in RootNamespace, and OutputDirectory so it would know where to save the files it needs to write.

AraHaan avatar Sep 18 '22 13:09 AraHaan

@AraHaan see the note above about our templates currently requiring VS (we plan to remove that requirement).

roji avatar Sep 18 '22 18:09 roji