Angel \”Java\” Lopez on Blog

October 4, 2010

Building an Application using AjGenesis (Part 6)

Filed under: AjGenesis, Code Generation, Open Source Projects — ajlopez @ 9:51 am

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 Comments »

  1. thanks for admin wonderfull information…

    Comment by aşk büyüsü — October 6, 2010 @ 1:25 pm

  2. [...] Then, I downloaded the AjGenesis compiled binaries (from source trunk). And some examples, like AppExampleStep06.zip (described in Building an Application using AjGenesis (Part 6)). [...]

    Pingback by Running AjGenesis using Mono and Ubuntu « Angel “Java” Lopez on Blog — October 9, 2010 @ 8:53 am

  3. [...] (compilados desde el trunk). Y bajé algunos ejemplos, comoe AppExampleStep06.zip (descripto en Building an Application using AjGenesis (Part 6)) (Armando una Aplicación usando AjGenesis (Part [...]

    Pingback by Ejecutando AjGenesis usando Mono y Ubuntu - Angel "Java" Lopez — October 15, 2010 @ 10:01 am

  4. [...] Building an Application with AjGenesis Code Generation with AjGenesis in a Real Project Models for Code Generation in AjGenesis AjBasic: [...]

    Pingback by AjGenesis: Basis of its Implementation « Angel “Java” Lopez on Blog — November 18, 2011 @ 12:07 pm

  5. [...] Building an Application with AjGenesis Code Generation with AjGenesis in a Real Project Models for Code Generation in AjGenesis AjBasic: [...]

    Pingback by AjGenesis: Bases de su implementación - Angel "Java" Lopez — November 19, 2011 @ 5:32 pm


RSS feed for comments on this post. TrackBack URI

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Theme: Shocking Blue Green. Get a free blog at WordPress.com

Follow

Get every new post delivered to your Inbox.

Join 67 other followers

%d bloggers like this: