OA_Main.bas 7.0 KB

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