OA_Main.bas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 "frm_openaccess"
  13. End Function
  14. Public Function make_sources(Optional ByVal options As String = "") As Integer
  15. 'exports the source-code of the app
  16. On Error GoTo err
  17. Dim step As String
  18. make_sources = opInterrupted
  19. If InStr(options, "-d") Then set_debug_mode
  20. step = "Initialization"
  21. ' backup of the sources date, in case of error
  22. Dim old_sources_date As Date
  23. old_sources_date = oa_param("sources_date", #1/1/1900#)
  24. '*** If '-f' is not in the options: set the optimizer on
  25. If Not InStr(options, "-f") > 0 Then
  26. Dim msg As String
  27. If old_sources_date > #1/1/1900# Then
  28. msg = msg_list_to_export()
  29. logger "make_sources", "INFO", "Optimizer: ask for confirmation"
  30. If Not Len(msg) > 0 Then
  31. msg = "** O.A. OPTIMIZER **" & vbNewLine & ">> Nothing new to export" & vbNewLine & _
  32. "Only the following will be exported:" & vbNewLine & _
  33. " - included table data" & vbNewLine & _
  34. " - relations" & vbNewLine & vbNewLine & _
  35. "TIP: use 'makesources -f' to force a complete export (could be long)."
  36. Else
  37. msg = "** O.A. OPTIMIZER **" & vbNewLine & _
  38. ">> Following objects will be exported:" & vbNewLine & _
  39. msg & vbNewLine & _
  40. "> DATA: " & vbNewLine & get_include_tables() & vbNewLine & vbNewLine & _
  41. "> RELATIONS"
  42. End If
  43. Call activate_optimizer
  44. Else
  45. ' no sources date recorded, it may be the first export
  46. msg = "FIRST EXPORT: " & vbNewLine & vbNewLine & _
  47. "Everything will be exported, it could be quite long..."
  48. End If
  49. If MsgBox(msg, vbOKCancel + vbExclamation, "Export") = vbCancel Then GoTo cancelOp
  50. logger "make_sources", "INFO", "Activates Optimizer"
  51. End If
  52. ' new sources date, before export so that date will be exported with tbl_vsc
  53. step = "Updates sources date"
  54. logger "make_sources", "INFO", step
  55. Call update_sources_date
  56. ' zip the app file
  57. step = "Zip the app file"
  58. logger "make_sources", "INFO", step
  59. Call zip_app_file
  60. ' run the export
  61. step = "Run VCS Export"
  62. logger "make_sources", "INFO", step
  63. Call ExportAllSource
  64. make_sources = opCompleted
  65. Exit Function
  66. err:
  67. Call update_oa_param("sources_date", CStr(old_sources_date))
  68. logger "make_sources", "CRITICAL", "Unknown error at: " & step & " - " & err.Description
  69. Exit Function
  70. cancelOp:
  71. make_sources = opCancelled
  72. Exit Function
  73. End Function
  74. Public Function update_from_sources(Optional ByVal options As String = "") As Integer
  75. 'updates the application from the sources
  76. Dim backup As Boolean
  77. Dim step, msg As String
  78. update_from_sources = opInterrupted
  79. If InStr(options, "-d") Then set_debug_mode
  80. step = "Creates a backup of the app file"
  81. logger "update_from_sources", "INFO", step
  82. backup = make_backup()
  83. If Not backup Then
  84. logger "update_from_sources", "ERROR", "Error: unable to backup the app file, do it manually, then click OK"
  85. MsgBox "Error: unable to backup the app file, do it manually, then click OK", vbExclamation, "Backup"
  86. End If
  87. step = "Check for unexported work"
  88. logger "update_from_sources", "INFO", step
  89. msg = msg_list_to_export()
  90. If Len(msg) > 0 Then
  91. msg = "** IMPORT WARNING **" & vbNewLine & _
  92. UCase(CurrentProject.name) & " is going to be updated " & _
  93. "with the source files. " & vbNewLine & vbNewLine & _
  94. "(!) FOLLOWING NON EXPORTED WORK WILL BE LOST (!): " & vbNewLine & _
  95. msg & vbNewLine & _
  96. "Are you sure you want to continue?"
  97. If MsgBox(msg, vbOKCancel + vbExclamation, "Warning") = vbCancel Then GoTo cancelOp
  98. If MsgBox("Really sure?", vbOKCancel + vbQuestion, "Warning") = vbCancel Then GoTo cancelOp
  99. End If
  100. step = "Run VCS Import"
  101. logger "update_from_sources", "INFO", step
  102. Call ImportAllSource
  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. update_from_sources = opCompleted
  108. Exit Function
  109. err:
  110. logger "update_from_sources", "CRITICAL", "Unknown error at: " & step & " - " & err.Description
  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. 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. MsgBox "Error during the backup of " & CurrentProject.name & ": " & err.Description & vbNewLine & "Do it manually"
  156. End Function