Building an Application using AjGenesis (Part 6)

In the previous post in this series, I added a lot of stuff to manage the generation of .NET projects and solutions. That files are complicated. In contrast, to generate something alike for Java, I only need to generate the folder structure for package, and maybe add Ant or Maven files. But in .NET, I was to struggle with the structure of solution and project files. The code for this part can be downloaded from AppExampleStep06.zip. You need the compiled binaries AjGenesisTrunkBinaries.zip. The complete AjGenesis project, including the examples for this post series, can be downloaded from http://ajgenesis.codeplex.com.

One of the complicated part was to add all .cs and .vb files inside the project file. I wrote:

for each Entity in Project.Model.Entities
  TransformerManager.Transform("Templates/CSharp/EntityClass.tpl", "${Project.BuildDirectory}/${Project.Name}.Entities/${Entity.Name}.cs", Environment)
  PrjEntities.Includes.Add(CreateFileCs("${Entity.Name}"))
end for

The Includes list in PrjEntities object keeps the information of the files to add in the project file generation. Instead of adding the files in that way, now I add ALL .cs files in the C# project, and all .vb files in the VB.Net project. So, you can add your own custom files, and the project files still will reference them after regeneration of the solution. The key code is inside the Utilities/DotNetUtilities.ajg:

Sub AddVbFilesToProject(project)
  di = new System.IO.DirectoryInfo(project.Directory)
  prefix = ""
  AddVbFiles(project, name, di)
End Sub
Sub AddVbFiles(project, prefix, di)
  for each fi in di.GetFiles("*.vb")
    name = fi.Name.Substring(0, fi.Name.Length - 3)
    project.Includes.Add(CreateFileVb(prefix & name))
  end for
  
  sdirs = di.GetDirectories()
  
  for each sdir in sdirs
    AddVbFiles(project, prefix & sdir.Name & "\", sdir)
  end for
End Sub
Sub AddCsFilesToProject(project)
  di = new System.IO.DirectoryInfo(project.Directory)
  prefix = ""
  AddCsFiles(project, name, di)
End Sub
Sub AddCsFiles(project, prefix, di)
  for each fi in di.GetFiles("*.cs")
    name = fi.Name.Substring(0, fi.Name.Length - 3)
    project.Includes.Add(CreateFileCs(prefix & name))
  end for
  
  sdirs = di.GetDirectories()
  
  for each sdir in sdirs
    AddCsFiles(project, prefix & sdir.Name & "\", sdir)
  end for
End Sub

Another feature I added in this step: in the previous post, each time the generation was launched, it created all the files again. Now, it use a new routine, included in Utilities/TransformUtilities.ajg:

Sub TransformFile(tpl, target, tm, env)
  if System.IO.File.Exists(target) then
    target2 = target & ".tmp"
    tm.Transform(tpl, target2, env)
    content1 = System.IO.File.ReadAllText(target)
    content2 = System.IO.File.ReadAllText(target2)
    if content1 <> content2 then
      System.IO.File.Copy(target2, target, true)
    end if
    System.IO.File.Delete(target2)
  else
    tm.Transform(tpl, target, env)
  end if
End Sub

Now, instead of invoking the predefined object TransformerManager:

  TransformerManager.Transform("Templates/CSharp/EntityClass.tpl", "${Project.BuildDirectory}/${Project.Name}.Entities/${Entity.Name}.cs", Environment)

the task invoke the new routine, that compares the new file with the old one, and replace it only if they are different:

  TransformFile("Templates/CSharp/EntityClass.tpl", "${Project.BuildDirectory}/${Project.Name}.Entities/${Entity.Name}.cs", TransformerManager, Environment)

Next steps, for future posts: include identity to entities in model, generate database creation DLLs, DAOs, and some Java code.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

5 thoughts on “Building an Application using AjGenesis (Part 6)

  1. Pingback: Running AjGenesis using Mono and Ubuntu « Angel “Java” Lopez on Blog

  2. Pingback: Ejecutando AjGenesis usando Mono y Ubuntu - Angel "Java" Lopez

  3. Pingback: AjGenesis: Basis of its Implementation « Angel “Java” Lopez on Blog

  4. Pingback: AjGenesis: Bases de su implementación - Angel "Java" Lopez

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s