KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Importing and exporting project methods
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: December 27, 2017

Here is are two method that will export and import projects methods in your database:

// ----------------------------------------------------------------------
// Name:IMPORT_EXPORT_PROJ_METHODS
// Description: Method will Export and Import project methods
// ----------------------------------------------------------------------
C_TEXT($filename_t;$textCode;$loc;$codePath)
C_TEXT(filename_t;textCode)
C_LONGINT($i;error)
ARRAY TEXT($fileNames_at;0)
ARRAY TEXT($methodNames;0)

$loc:=Get 4D folder(Database folder)+"methods"+Folder separator

CONFIRM("Import or Export methods?";"Import";"Export")

// Importing the Methods
If (OK=1)

   DOCUMENT LIST($loc;$fileNames_at)
   ON ERR CALL("HANDLE_ERR_METHODS")

   For ($i;1;Size of array($fileNames_at))
      If ($fileNames_at{$i}#Current method name)
         $filename_t:=$fileNames_at{$i}
         filename_t:=$fileName_t
         $textCode:=Document to text($loc+$filename_t)
         textCode:=$textCode

         METHOD SET CODE($filename_t;"")

         If (error=1)
          $fileNames_at{$i}:=""
          error:=0
         End If
      End if
   End for

   ON ERR CALL("")

   For ($i;1;Size of array($fileNames_at))
      If (($fileNames_at{$i}#Current method name)&($fileNames_at{$i}#""))
         $filename_t:=$fileNames_at{$i}
         $textCode:=Document to text($loc+$filename_t)
         METHOD SET CODE($filename_t;$textCode)
      End if
   End for

Else // Export the Methods

   METHOD GET NAMES($methodNames)

   If (Test path name($loc)#Is a folder)
      CREATE FOLDER($loc)
   End if

   For ($i;1;Size of array($methodNames))
      If ($methodNames{$i}#Current method name)
         $codePath:=METHOD Get path(Path project method;$methodNames{$i})
         METHOD GET CODE($codePath;$textCode;*) //no tokens
         TEXT TO DOCUMENT($loc+$methodNames{$i};$textCode)
      End if
   End for
End if


Method will handle the ON ERR CALL for methods that cannot be created in the process:

// ----------------------------------------------------------------------
// Name: HANDLE_ERR_METHODS
// Description: Method will handle ON ERR CALL errors on methods that
// can't be created.
// ----------------------------------------------------------------------
C_TEXT($error_text)
ARRAY LONGINT($codesArray;0)
ARRAY TEXT($intCompArray;0)
ARRAY TEXT($textArray;0)

filename_t:="_error_"+filename_t

GET LAST ERROR STACK($codesArray;$intCompArray;$textArray)
$error_text:=""
error:=1

For ($i;1;Size of array($codesArray))
 $error_text:=$error_text+"//Error Code: "+string($codesArray{$i})+" Error Text: "+$textArray{$i}+Char(13)+Char(10)
End for

textCode:=$error_text+textCode
METHOD SET CODE(filename_t;textCode)


1. An example of Exporting methods:

IMPORT_EXPORT_PROJ_METHODS


Click on the "Export" button:



A "methods" folder will be created with the files next to your structure:




2. An example of Importing methods:

Click on the "Import" button:



Methods will be populated from the "methods" folder:




Note: Imported methods that are created before methods that are coded their method will not be tokenized.