VCS_Main.bas 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. Option Compare Database
  2. '****
  3. '*
  4. '* Main methods for VCS 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 vcsprompt()
  12. DoCmd.OpenForm "frm_vcs"
  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 = vcs_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 = "** VCS 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 = "** VCS 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_vcs_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 config_git_repo()
  117. 'configure the application GIT repository for VCS use
  118. 'verify that it is a git repository
  119. If Not is_git_repo() Then
  120. MsgBox "Not a git repository, please use 'git init on this directory first"
  121. Exit Function
  122. End If
  123. ' complete the gitignore file
  124. Call complete_gitignore
  125. End Function
  126. Public Function sync()
  127. '[experimental] complete command to synchronize this app with the distant master branch
  128. 'verify that it is a git repository
  129. If Not is_git_repo() Then
  130. MsgBox "Not a git repository, please use 'git init on this directory first"
  131. Exit Function
  132. End If
  133. 'Call make_sources
  134. Call cmd("echo --ADD FILES-- & git add *" & "& timeout 2", vbNormalFocus)
  135. Dim msg As String
  136. msg = InputBox("Commit message:", "VCS")
  137. If Not Len(msg) > 0 Then GoTo err_msg
  138. Call cmd("echo --COMMIT-- & git commit -a -m " & Chr(34) & msg & Chr(34) & "& timeout 2", vbNormalFocus)
  139. Call cmd("echo --PULL-- & git pull origin master & pause", vbNormalFocus)
  140. Call update_from_sources
  141. Call cmd("echo --PUSH-- & git push origin master" & "& timeout 2", vbNormalFocus)
  142. Exit Function
  143. err_msg:
  144. MsgBox "Invalid value", vbExclamation
  145. End Function
  146. Public Function get_include_tables()
  147. get_include_tables = vcs_param("include_tables")
  148. End Function
  149. Public Function vcs_param(ByVal key As String, Optional ByVal default_value As String = "") As String
  150. vcs_param = default_value
  151. On Error GoTo err_vcs_table
  152. vcs_param = DFirst("val", "ztbl_vcs", "[key]='" & key & "'")
  153. err_vcs_table:
  154. End Function
  155. Public Function zip_app_file() As Boolean
  156. On Error GoTo UnknownErr
  157. Dim command, shortname As String
  158. zip_app_file = False
  159. shortname = Split(CurrentProject.name, ".")(0)
  160. 'run the shell comand
  161. Call cmd("cd " & CurrentProject.path & " & " & _
  162. "zip tmp_" & shortname & ".zip " & CurrentProject.name & _
  163. " & exit")
  164. 'remove the old zip file
  165. If dir(CurrentProject.path & "\" & shortname & ".zip") <> "" Then
  166. Kill CurrentProject.path & "\" & shortname & ".zip"
  167. End If
  168. 'rename the temporary zip
  169. Call cmd("cd " & CurrentProject.path & " & " & _
  170. "ren tmp_" & shortname & ".zip" & " " & shortname & ".zip" & _
  171. " & exit")
  172. logger "zip_app_file", "INFO", CurrentProject.path & "\" & CurrentProject.name & " zipped to " & CurrentProject.path & "\" & shortname & ".zip"
  173. zip_app_file = True
  174. end_:
  175. Exit Function
  176. UnknownErr:
  177. logger "zip_app_file", "ERROR", "Unable to zip " & CurrentProject.path & "\" & CurrentProject.name & " - " & err.Description
  178. MsgBox "Unknown error: unable to ZIP the app file, do it manually"
  179. GoTo end_
  180. End Function
  181. Public Function make_backup() As Boolean
  182. On Error GoTo err
  183. make_backup = False
  184. If dir(CurrentProject.path & "\" & CurrentProject.name & ".old") <> "" Then
  185. Kill CurrentProject.path & "\" & CurrentProject.name & ".old"
  186. End If
  187. Call cmd("copy " & Chr(34) & CurrentProject.path & "\" & CurrentProject.name & Chr(34) & _
  188. " " & Chr(34) & CurrentProject.path & "\" & CurrentProject.name & ".old" & Chr(34))
  189. logger "make_backup", "INFO", CurrentProject.path & "\" & CurrentProject.name & " copied to " & CurrentProject.path & "\" & CurrentProject.name & ".old"
  190. make_backup = True
  191. Exit Function
  192. err:
  193. logger "make_backup", "ERROR", "Error during the backup of " & CurrentProject.name & ": " & err.Description
  194. MsgBox "Error during the backup of " & CurrentProject.name & ": " & err.Description & vbNewLine & "Do it manually"
  195. End Function