OA_Main.bas 7.4 KB

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