OA_Main.bas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. '>> main function, called when addin is run
  11. Public Function main()
  12. DoCmd.OpenForm "OpenAccess"
  13. End Function
  14. Public Function make_sources(ByVal include_tables As String, _
  15. Optional ByVal optimizer As Boolean = True, _
  16. Optional ByVal zip As Boolean = True) As Integer
  17. 'exports the source-code of the app
  18. On Error GoTo err
  19. Dim step As String
  20. make_sources = opInterrupted
  21. step = "Initialization"
  22. '*** If '-f' is not in the options: set the optimizer on
  23. If optimizer Then
  24. Dim msg As String
  25. If get_sources_date() > #1/1/1900# Then
  26. msg = msg_list_to_export()
  27. logger "make_sources", "INFO", "Optimizer: ask for confirmation"
  28. If Not Len(msg) > 0 Then
  29. msg = "** O.A. OPTIMIZER **" & vbNewLine & ">> Nothing new to export" & vbNewLine & _
  30. "Only the following will be exported:" & vbNewLine & _
  31. " - included table data" & vbNewLine & _
  32. " - relations" & vbNewLine & vbNewLine & _
  33. "TIP: use 'makesources -f' to force a complete export (could be long)."
  34. Else
  35. msg = "** O.A. OPTIMIZER **" & vbNewLine & _
  36. ">> Following objects will be exported:" & vbNewLine & _
  37. msg & vbNewLine & _
  38. "> DATA: " & vbNewLine & get_include_tables() & vbNewLine & vbNewLine & _
  39. "> RELATIONS"
  40. End If
  41. Call activate_optimizer
  42. Else
  43. ' no sources date recorded, it may be the first export
  44. msg = "FIRST EXPORT: " & vbNewLine & vbNewLine & _
  45. "Everything will be exported, it could be quite long..."
  46. End If
  47. If MsgBox(msg, vbOKCancel + vbExclamation, "Export") = vbCancel Then GoTo cancelOp
  48. logger "make_sources", "INFO", "Activates Optimizer"
  49. End If
  50. If zip Then
  51. ' zip the app file
  52. step = "Zip the app file"
  53. logger "make_sources", "INFO", step
  54. Call zip_app_file
  55. End If
  56. ' run the export
  57. step = "Run VCS Export"
  58. logger "make_sources", "INFO", step
  59. Call ExportAllSource
  60. ' new sources date
  61. step = "Updates sources date"
  62. logger "make_sources", "INFO", step
  63. Call update_sources_date
  64. make_sources = opCompleted
  65. Exit Function
  66. err:
  67. MsgBox "Unknown error - " & err.Description & " (#" & err.number & ")" & vbNewLine & "See the log file for more information", vbCritical, "CRITICAL ERROR"
  68. If err.number <> "60000" Then
  69. logger "make_sources", "ERROR", "Unknown error at: " & step & " - " & err.Description & "(#" & err.number & ")"
  70. End If
  71. Call update_oa_param("sources_date", CStr(old_sources_date))
  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. Dim backup_ok As Boolean
  80. Dim step, msg As String
  81. update_from_sources = opInterrupted
  82. If backup Then
  83. step = "Creates a backup of the app file"
  84. logger "update_from_sources", "INFO", step
  85. backup_ok = make_backup()
  86. If Not backup_ok Then
  87. logger "update_from_sources", "ERROR", "Error: unable to backup the app file, do it manually, then click OK"
  88. MsgBox "Error: unable to backup the app file, do it manually, then click OK", vbExclamation, "Backup"
  89. End If
  90. End If
  91. step = "Check for unexported work"
  92. logger "update_from_sources", "INFO", step
  93. msg = msg_list_to_export()
  94. If Len(msg) > 0 Then
  95. msg = "** IMPORT WARNING **" & vbNewLine & _
  96. UCase(CurrentProject.name) & " is going to be updated " & _
  97. "with the source files. " & vbNewLine & vbNewLine & _
  98. "(!) FOLLOWING NON EXPORTED WORK WILL BE LOST (!): " & vbNewLine & _
  99. msg & vbNewLine & _
  100. "Are you sure you want to continue?"
  101. If MsgBox(msg, vbOKCancel + vbExclamation, "Warning") = vbCancel Then GoTo cancelOp
  102. If MsgBox("Really sure?", vbOKCancel + vbQuestion, "Warning") = vbCancel Then GoTo cancelOp
  103. End If
  104. step = "Run VCS Import"
  105. logger "update_from_sources", "INFO", step
  106. Call ImportAllSource
  107. step = "Cleaning obsolete objects in app"
  108. msg = "Following objects do not exist in the sources, do you want to delete them?" & _
  109. "" & CleanApp(True)
  110. If MsgBox(msg, vbYesNo, "Cleaning" = vbYes) Then
  111. Call CleanApp
  112. End If
  113. ' new sources date to keep the optimizer working
  114. step = "Updates sources date"
  115. logger "update_from_sources", "INFO", step
  116. Call update_sources_date
  117. update_from_sources = opCompleted
  118. Exit Function
  119. err:
  120. MsgBox "Unknown error - " & err.Description & " (#" & err.number & ")" & vbNewLine & "See the log file for more information", vbCritical, "CRITICAL ERROR"
  121. If err.number <> "60000" Then
  122. logger "update_from_sources", "CRITICAL", "Unknown error at: " & step & " - " & err.Description & "(#" & err.number & ")"
  123. End If
  124. Exit Function
  125. cancelOp:
  126. update_from_sources = opCancelled
  127. Exit Function
  128. End Function
  129. Public Function zip_app_file() As Boolean
  130. On Error GoTo UnknownErr
  131. Dim command, shortname As String
  132. zip_app_file = False
  133. shortname = Split(CurrentProject.name, ".")(0)
  134. 'run the shell comand
  135. Call cmd("cd " & CurrentProject.Path & " & " & _
  136. "zip tmp_" & shortname & ".zip " & CurrentProject.name & _
  137. " & exit")
  138. 'remove the old zip file
  139. If dir(CurrentProject.Path & "\" & shortname & ".zip") <> "" Then
  140. Kill CurrentProject.Path & "\" & shortname & ".zip"
  141. End If
  142. 'rename the temporary zip
  143. Call cmd("cd " & CurrentProject.Path & " & " & _
  144. "ren tmp_" & shortname & ".zip" & " " & shortname & ".zip" & _
  145. " & exit")
  146. logger "zip_app_file", "INFO", CurrentProject.Path & "\" & CurrentProject.name & " zipped to " & CurrentProject.Path & "\" & shortname & ".zip"
  147. zip_app_file = True
  148. end_:
  149. Exit Function
  150. UnknownErr:
  151. logger "zip_app_file", "ERROR", "Unable to zip " & CurrentProject.Path & "\" & CurrentProject.name & " - " & err.Description
  152. MsgBox "Unknown error: unable to ZIP the app file, do it manually"
  153. GoTo end_
  154. End Function
  155. Public Function make_backup() As Boolean
  156. On Error GoTo err
  157. make_backup = False
  158. If dir(CurrentProject.Path & "\" & CurrentProject.name & ".old") <> "" Then
  159. Kill CurrentProject.Path & "\" & CurrentProject.name & ".old"
  160. End If
  161. Call cmd("copy " & Chr(34) & CurrentProject.Path & "\" & CurrentProject.name & Chr(34) & _
  162. " " & Chr(34) & CurrentProject.Path & "\" & CurrentProject.name & ".old" & Chr(34))
  163. logger "make_backup", "INFO", CurrentProject.Path & "\" & CurrentProject.name & " copied to " & CurrentProject.Path & "\" & CurrentProject.name & ".old"
  164. make_backup = True
  165. Exit Function
  166. err:
  167. logger "make_backup", "ERROR", "Error during the backup of " & CurrentProject.name & ": " & err.Description
  168. MsgBox "Error during the backup of " & CurrentProject.name & ": " & err.Description & vbNewLine & "Do it manually"
  169. End Function