OA_Main.bas 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. Option Compare Database
  2. '****
  3. '*
  4. '* Main methods for OpenAccess add-in
  5. '*
  6. '****
  7. Public Const opInterrupted = 10
  8. Public Const opCancelled = 11
  9. Public Const opCompleted = 12
  10. Dim debug_mode As Boolean
  11. Public Sub activate_debug_mode()
  12. debug_mode = True
  13. End Sub
  14. '>> main function, called when addin is run
  15. Public Function main()
  16. DoCmd.OpenForm "OpenAccess"
  17. End Function
  18. Public Function make_sources(Optional ByVal optimizer As Boolean = True, _
  19. Optional ByVal zip As Boolean = True) As Integer
  20. 'exports the source-code of the app
  21. If Not debug_mode Then On Error GoTo err
  22. Dim step As String
  23. make_sources = opInterrupted
  24. step = "Initialization"
  25. If optimizer Then
  26. Call activate_optimizer
  27. End If
  28. 'Save is needed to correctly list objects to export
  29. SaveProject
  30. If Not prompt_for_export_confirmation Then
  31. GoTo cancelOp
  32. End If
  33. If zip Then
  34. ' zip the app file
  35. step = "Zip the app file"
  36. logger "make_sources", "INFO", step
  37. Call zip_app_file
  38. End If
  39. ' run the export
  40. step = "Run VCS Export"
  41. logger "make_sources", "INFO", step
  42. Call ExportAllSource
  43. ' new sources date
  44. step = "Updates sources date"
  45. logger "make_sources", "INFO", step
  46. Call update_sources_date
  47. ' cleans the obsolete files (see CleanDirs in optimizer)
  48. msg = CleanDirs(True)
  49. If Len(msg) > 0 Then
  50. msg = "Following objects do not exist anymore, do you want to delete treir source files?" & vbNewLine & _
  51. "" & msg
  52. If OA_MsgBox(msg, vbYesNo, "Cleaning") = vbYes Then
  53. Call CleanDirs
  54. End If
  55. End If
  56. make_sources = opCompleted
  57. Exit Function
  58. err:
  59. OA_MsgBox "Unknown error - " & err.Description & " (#" & err.Number & ")" & vbNewLine & "See the log file for more information", vbCritical, "CRITICAL ERROR"
  60. If err.Number <> "60000" Then
  61. logger "make_sources", "ERROR", "Unknown error at: " & step & " - " & err.Description & "(#" & err.Number & ")"
  62. End If
  63. Call update_oa_param("sources_date", CStr(old_sources_date))
  64. Exit Function
  65. cancelOp:
  66. make_sources = opCancelled
  67. Exit Function
  68. End Function
  69. Public Function update_from_sources(Optional ByVal backup As Boolean) As Integer
  70. 'updates the application from the sources
  71. If Not debug_mode Then On Error GoTo err
  72. Dim backup_ok As Boolean
  73. Dim step, msg As String
  74. update_from_sources = opInterrupted
  75. If backup Then
  76. step = "Creates a backup of the app file"
  77. logger "update_from_sources", "INFO", step
  78. backup_ok = make_backup()
  79. If Not backup_ok Then
  80. logger "update_from_sources", "ERROR", "Error: unable to backup the app file, do it manually, then click OK"
  81. OA_MsgBox "Error: unable to backup the app file, do it manually, then click OK", vbExclamation, "Backup"
  82. End If
  83. End If
  84. step = "Prompt for confirmation"
  85. If Not prompt_for_import_confirmation Then
  86. GoTo cancelOp
  87. End If
  88. step = "Run VCS Import"
  89. logger "update_from_sources", "INFO", step
  90. Call ImportAllSource
  91. step = "Cleaning obsolete objects in app"
  92. msg = CleanApp(True)
  93. If Len(msg) > 0 Then
  94. msg = "Following objects do not exist in the sources, do you want to delete them?" & vbNewLine & _
  95. "" & msg
  96. If OA_MsgBox(msg, vbYesNo, "Cleaning") = vbYes Then
  97. Call CleanApp
  98. End If
  99. End If
  100. ' new sources date to keep the optimizer working
  101. step = "Updates sources date"
  102. logger "update_from_sources", "INFO", step
  103. Call update_sources_date
  104. update_from_sources = opCompleted
  105. Exit Function
  106. err:
  107. OA_MsgBox "Unknown error - " & err.Description & " (#" & err.Number & ")" & vbNewLine & "See the log file for more information", vbCritical, "CRITICAL ERROR"
  108. If err.Number <> "60000" Then
  109. logger "update_from_sources", "CRITICAL", "Unknown error at: " & step & " - " & err.Description & "(#" & err.Number & ")"
  110. End If
  111. Exit Function
  112. cancelOp:
  113. update_from_sources = opCancelled
  114. Exit Function
  115. End Function
  116. Public Function zip_app_file() As Boolean
  117. On Error GoTo UnknownErr
  118. Dim command, shortname As String
  119. zip_app_file = False
  120. shortname = Split(CurrentProject.name, ".")(0)
  121. 'run the shell comand
  122. Call cmd("cd " & CurrentProject.path & " & " & _
  123. "zip tmp_" & shortname & ".zip " & CurrentProject.name & _
  124. " & exit")
  125. 'remove the old zip file
  126. If dir(CurrentProject.path & "\" & shortname & ".zip") <> "" Then
  127. Kill CurrentProject.path & "\" & shortname & ".zip"
  128. End If
  129. 'rename the temporary zip
  130. Call cmd("cd " & CurrentProject.path & " & " & _
  131. "ren tmp_" & shortname & ".zip" & " " & shortname & ".zip" & _
  132. " & exit")
  133. logger "zip_app_file", "INFO", CurrentProject.path & "\" & CurrentProject.name & " zipped to " & CurrentProject.path & "\" & shortname & ".zip"
  134. zip_app_file = True
  135. end_:
  136. Exit Function
  137. UnknownErr:
  138. logger "zip_app_file", "ERROR", "Unable to zip " & CurrentProject.path & "\" & CurrentProject.name & " - " & err.Description
  139. OA_MsgBox "Unknown error: unable to ZIP the app file, do it manually"
  140. GoTo end_
  141. End Function
  142. Public Function make_backup() As Boolean
  143. On Error GoTo err
  144. make_backup = False
  145. If dir(CurrentProject.path & "\" & CurrentProject.name & ".old") <> "" Then
  146. Kill CurrentProject.path & "\" & CurrentProject.name & ".old"
  147. End If
  148. Call cmd("copy " & Chr(34) & CurrentProject.path & "\" & CurrentProject.name & Chr(34) & _
  149. " " & Chr(34) & CurrentProject.path & "\" & CurrentProject.name & ".old" & Chr(34))
  150. logger "make_backup", "INFO", CurrentProject.path & "\" & CurrentProject.name & " copied to " & CurrentProject.path & "\" & CurrentProject.name & ".old"
  151. make_backup = True
  152. Exit Function
  153. err:
  154. logger "make_backup", "ERROR", "Error during the backup of " & CurrentProject.name & ": " & err.Description
  155. OA_MsgBox "Error during the backup of " & CurrentProject.name & ": " & err.Description & vbNewLine & "Do it manually"
  156. End Function
  157. Public Function silent_export()
  158. 'used for tests
  159. OA_Msg.activate_SilentMode
  160. OA_Log.set_debug_mode
  161. Dim result As Variant
  162. result = make_sources(optimizer:=False, zip:=True)
  163. logger "silent_export", "INFO", "make_sources returned " & result
  164. End Function
  165. Public Function silent_import()
  166. 'used for tests
  167. OA_Msg.activate_SilentMode
  168. OA_Log.set_debug_mode
  169. Dim result As Variant
  170. result = update_from_sources(backup:=True)
  171. logger "silent_export", "INFO", "update_from_sources returned " & result
  172. End Function