Template based code generation that tracks/merges user changes?
Does a template/code generator already exist that tracks user changes to it's output? I'm not even posative this makes sense - but the space cadet in me thinks something like this could be an interesting kind of change tracking that integrates more tightly to the code than SCM...
Data.xml
<Classes>
<Class>Class1</Class>
</Classes>
Template
<# for(var c in XDocument.Load("Data.xml").Element("Classes").Element开发者_运维百科s("Class")) { #>
class <#=c.Value#> {
public <#=c.Value#>() {
// <InsertPoint>
// </InsertPoint>
}
}
<# } #>
Output
class Class1 {
public Class1() {
// <InsertPoint>
// </InsertPoint>
}
}
User Change
class Class1 {
public Class1() {
// <InsertPoint>
System.Console.WriteLine("I am Class1");
// </InsertPoint>
}
}
--> The template changes to something like:
<# for(var c in XDocument.Load("Data.xml").Element("Classes").Elements("Class")) { #>
class <#=c.Value#> {
public <#=c.Value#>() {
// <UserInsert id="1">
System.Console.WriteLine("I am Class1");
// </UserInsert>
// <InsertPoint>
// </InsertPoint>
}
}
<# } #>
What you have in data.xml is what's normally called a model. Being done in XML, we could call this a "tree model". In other words, you are applying (executing) templates according to a pre-defined model.
Tracking user changes in this case can also be called "round-trip" engineering: a bi-directional change.
The ABSE project (disclaimer: I am the project lead) defines a model-driven code generation approach that is very close to your request: It uses executable templates and generates code by means of a tree model (but it's not XML). But instead of detecting your changes in code, you can add your own code directly in the model so that you can skip the "round-trip" step.
There are tools for diff'ing on xml documents. If you can convert your code into an ast, represented in xml, you could apply these tools on the document, then transform it back to code.
精彩评论