Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dotnet-t4: Use -c without specifying class name #129

Open
bricelam opened this issue May 3, 2022 · 4 comments
Open

dotnet-t4: Use -c without specifying class name #129

bricelam opened this issue May 3, 2022 · 4 comments
Labels

Comments

@bricelam
Copy link

bricelam commented May 3, 2022

I hate the redundancy of specifying commands like this to preprocess templates:

t4 -c MyTemplate MyTemplate.tt

I'd love to see a new option that infers the class name from the input file so I can just specify something like this:

t4 --preprocess MyTemplate.tt
@bricelam
Copy link
Author

Hmm, looks like you'd also need a way to specify the namespace.

Maybe this issue isn't really worth it in the end.

@mhutch
Copy link
Member

mhutch commented Oct 4, 2022

This seems reasonable to me - even if it's no more concise, eliminating redundancy helps reduce user error.

Maybe just t4 -n SomeNamespace SomeClassname.tt ?

@mhutch mhutch added the feature label Oct 4, 2022
@zha999
Copy link

zha999 commented Jan 11, 2023

@bricelam
If you are add command in project config file (e.g. csproj), you may try the method of https://github.com/nogic1008/T4Sample.

<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet t4 %(TextTemplate.Identity) -c $(RootNameSpace).%(TextTemplate.Filename) -o %(TextTemplate.Filename).Generated.cs" />

@trysetnull
Copy link

We have a similar use-case; we want to achieve similar functionality to the legacy custom tool built into Visual Studio.
Here, the class name and namespace is resolved automatically.

In the end we ended up with creating a new ItemGroup (T4Template) in our csproj files and essentially doing:

<ItemGroup>
    <T4TemplateWithMetadata Include="@(T4Template)">
      <OutputFile>%(RelativeDir)%(Filename).g.cs</OutputFile>
      <Namespace>$([System.String]::Copy('$(RootNamespace).%(RelativeDir)%(Filename)').Replace('\', '.').Replace('/', '.'))</Namespace>
    </T4TemplateWithMetadata>
</ItemGroup>

Then we can call the dotnet tool t4, like so:

<Exec Command="dotnet tool run t4 --out=&quot;%(T4TemplateWithMetadata.OutputFile)&quot; -I=&quot;%(T4TemplateWithMetadata.Identity)&quot; --class=&quot;%(T4TemplateWithMetadata.Namespace)&quot; &quot;%(T4TemplateWithMetadata.FullPath)&quot;"
          ContinueOnError="false"
          StandardOutputImportance="normal"
          StandardErrorImportance="high" />

This works but feels incredibly clunky, would be great if the T4.BuildTools would support this out-of-the-box.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants