OA_Main.bas 6.9 KB

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