Author Topic: How to search and edit all the CTF code templates with a real code editor  (Read 4335 times)

dandrus

  • EA Novice
  • *
  • Posts: 9
  • Karma: +1/-0
    • View Profile
I expect to be making considerable changes to the CTF base templates as well as integrating my own custom templates.  I'm tempted to methodically cut and paste each template into a separate text file so I can work on them using Eclipse, but I wanted to make sure there isn't a better way.  This will require me to keep track of each stereotype of each template and do a lot of cut-and-paste between Eclipse and the Code Template Editor to keep things in sync.  But without a way to even search for a string over all the templates, the Code Template Editor is insufficient.
« Last Edit: January 07, 2017, 05:02:08 am by dandrus »

Helmut Ortmann

  • EA User
  • **
  • Posts: 970
  • Karma: +42/-1
    • View Profile
Re: Search for a string (grep) over all CTF base templates?
« Reply #1 on: December 22, 2016, 05:19:40 pm »
Hi,

EA stores the changed CTF templates in the table 't_template' of the repository. For example the *.eap file or the database you use. By the way: The *.eap file is an Access database.

With the following SQL you can search for the templates you have changed for 'Macro' in EA (#WC# expands to the DB specific wildcard):

Code: [Select]
select * from t_template
            where template like '#WC#Macro#WC#'

With other tools, you can export the whole Table to file like a *.csv. You can search on the internet for the best way to do it with your database.  Maybe Access or Excel are appropriate.

Maybe there are other ways like:
- Export as referenced Data (*.xml file)
- Make an MDG (*.xml file)


Regards,

Helmut
Coaching, Training, Workshop (Addins: hoTools, Search&Replace, LineStyle)

dandrus

  • EA Novice
  • *
  • Posts: 9
  • Karma: +1/-0
    • View Profile
Re: Search for a string (grep) over all CTF base templates?
« Reply #2 on: December 25, 2016, 06:36:29 am »
Also, after doing a Project Transfer to the EABase_JET4.eap I was able to open the project in Access 2003.  The t_template table has the text of each modified template.  It may work to have a Visual Basic script in Access that updates the templates from my development text files...

dandrus

  • EA Novice
  • *
  • Posts: 9
  • Karma: +1/-0
    • View Profile
Re: Search for a string (grep) over all CTF base templates?
« Reply #3 on: January 07, 2017, 04:58:21 am »
FWIW, here are the visual basic scripts I'm using to allow for external editing of the code templates.  By checking the "use JET 4.0" box in the general preferences of EA and converting the project to be based on the EABase_JET4.eap, you can then open the .eap file with Access 2013.  If you save each script below into a separate file you can import them into Access 2013 by going to Database Tools -> Visual Basic ->  right click on the project "db2"-> Import File... 

You also need to create two macros (Create->Macro), each with one action
export_templates macro: RunCode
                                         Function Name: export_templates()
import_templates macro: RunCode import_templates()
                                         Function Name: import_templates()

The only other trick is that you need to initially touch each template because by default the t_templates table is empty.  EA only puts the template into the table if it is modified somehow.  You only need to do this once, however, then do Configure->Transfer->ExportReferenceData (in EA13) which will save an XML that has your touched versions.  You can later import this into any other project at the start to force all the templates into modified state.

One limitation of these scripts is that they assume the templates for only one language have been modified in EA, and only one stereotype of the template is modified.  Extension is needed if you want to save/restore using separate subdirectories for each language and each stereotype.  These are already other fields in record set the scripts are already getting from the t_template table so this should be straightforward.  Another limitation is that to add new templates you first need to create it in the code template editor.

Attribute VB_Name = "ExportTemplates"
Option Compare Database

Type template_t
  filename As String
  template As String
End Type

Sub write_template_to_file(t As template_t)
  Dim fso As Object
  Dim strPath As String
  strPath = "C:\path\to\templates\" & t.filename & ".tpl"
  Set fso = CreateObject("Scripting.FileSystemObject")
  Dim oFile As Object
  Set oFile = fso.CreateTextFile(strPath)
  oFile.Write t.template
  oFile.Close
  Set fso = Nothing
  Set oFile = Nothing
End Sub

Public Function export_templates()
  Dim rs As Recordset
  Dim t As template_t
  Set rs = CurrentDb.OpenRecordset("t_template", dbOpenTable)
  Do While Not rs.EOF
    t.filename = rs(2)
    If Not IsNull(rs(5)) Then
      t.template = rs(5)
    Else
      t.template = ""
    End If
    write_template_to_file t
    rs.MoveNext
  Loop
  rs.Close
  Set rs = Nothing
End Function


--

Attribute VB_Name = "ImportTemplates"
Option Compare Database

Sub read_template_from_file(ByRef t As template_t)
  Dim fileNo As Integer
  Dim strPath As String
  strPath = "C:\path\to\templates\" & t.filename & ".tpl"
  fileNo = FreeFile 'Get first free file number
  Open strPath For Input As #fileNo
  t.template = Input$(LOF(fileNo), fileNo)
  Close #fileNo
End Sub

Public Function import_templates()
  Dim rs As Recordset
  Dim t As template_t
  Set rs = CurrentDb.OpenRecordset("t_template", dbOpenTable)
  Do While Not rs.EOF
    t.filename = rs(2)
    read_template_from_file t
    rs.Edit
    rs(5) = t.template
    rs.Update
    rs.MoveNext
  Loop
  rs.Close
  Set rs = Nothing
End Function
« Last Edit: January 07, 2017, 05:12:13 am by dandrus »