dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.54k stars 3.13k forks source link

Run the string resources T4 files on build #27475

Open AndriySvyryd opened 2 years ago

roji commented 2 years ago

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

ajcvickers commented 2 years ago

@roji Our T4 templates currently require VS.

bricelam commented 2 years ago

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.

AraHaan commented 1 year ago

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).

roji commented 1 year ago

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

AraHaan commented 1 year ago

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.

roji commented 1 year ago

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