Skip to content

Task for pre-processing source files #4018

@natemcmaster

Description

@natemcmaster

I'd like to contribute to MSBuild a task we wrote for aspnet that I think would be generally useful. Is this something MSBuild would accept as a PR?

GenerateFileFromTemplate

Purpose: pre-process a source file using MSBuild variables.
Usage:

<GenerateFileFromTemplate
   TemplateFile="MyFile.cs.in"
   OutputPath="$(IntermediateOutputPath)MyFile.cs"
   Properties="ClassName=Banana;Method=Abc" />

Template file example

// MyFile.cs.in
public class ${ClassName}
{
  public void Method${MethodName}()
  {
     Console.WriteLine("`${NotReplacedDueToBacktick}"");
  }
}

Template syntax:

  • variables in the template file use powershell/bash syntax: ${VarName}.
  • if you need a file with ${...}, you can escape this with a backtick: `${...}

Justification:
WriteLinesToFile is good, but I've frequently run into problems with it when I need to write a file that has \, /, or ; in it.

Current implementation: https://github.com/aspnet/BuildTools/blob/master/modules/BuildTools.Tasks/GenerateFileFromTemplate.cs, https://github.com/aspnet/BuildTools/blob/master/test/BuildTools.Tasks.Tests/GenerateFileFromTemplateTests.cs

cc @jeffkl @livarcocc

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions