VCS_Main.bas 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. step = "Initialization"
  20. ' backup of the sources date, in case of error
  21. Dim old_sources_date As Date
  22. old_sources_date = vcs_param("sources_date", #1/1/1900#)
  23. '*** If '-f' is not in the options: set the optimizer on
  24. If Not InStr(options, "-f") > 0 Then
  25. Dim msg As String
  26. If old_sources_date > #1/1/1900# Then
  27. msg = msg_list_to_export()
  28. If Not Len(msg) > 0 Then
  29. msg = "** VCS 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 = "** VCS 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. End If
  49. ' new sources date, before export so that date will be exported with tbl_vsc
  50. step = "Updates sources date"
  51. Debug.Print step
  52. Call update_sources_date
  53. Debug.Print "> done"
  54. ' zip the app file
  55. step = "Zip the app file"
  56. Debug.Print step
  57. Call zip_app_file
  58. Debug.Print "> done"
  59. ' run the export
  60. step = "Run VCS Export"
  61. Debug.Print step
  62. Call ExportAllSource
  63. Debug.Print "> done"
  64. make_sources = opCompleted
  65. Exit Function
  66. err:
  67. Call update_vcs_param("sources_date", CStr(old_sources_date))
  68. MsgBox "make_sources - Unknown error at: " & step & vbNewLine & err.Description, vbCritical, "Error"
  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. step = "Creates a backup of the app file"
  80. Debug.Print step
  81. backup = make_backup()
  82. If backup Then
  83. Debug.Print "> done"
  84. Else
  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. Debug.Print 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. Debug.Print step
  102. Call ImportAllSource
  103. Debug.Print "> done"
  104. ' new sources date to keep the optimizer working
  105. step = "Updates sources date"
  106. Debug.Print step
  107. Call update_sources_date
  108. Debug.Print "> done"
  109. update_from_sources = opCompleted
  110. Exit Function
  111. err:
  112. MsgBox "update_from_sources - Unknown error at: " & vbNewLine & step & vbNewLine & err.Description, vbCritical, "Error"
  113. Exit Function
  114. cancelOp:
  115. update_from_sources = opCancelled
  116. Exit Function
  117. End Function
  118. Public Function config_git_repo()
  119. 'configure the application GIT repository for VCS use
  120. 'verify that it is a git repository
  121. If Not is_git_repo() Then
  122. MsgBox "Not a git repository, please use 'git init on this directory first"
  123. Exit Function
  124. End If
  125. ' complete the gitignore file
  126. Call complete_gitignore
  127. End Function
  128. Public Function sync()
  129. '[experimental] complete command to synchronize this app with the distant master branch
  130. 'verify that it is a git repository
  131. If Not is_git_repo() Then
  132. MsgBox "Not a git repository, please use 'git init on this directory first"
  133. Exit Function
  134. End If
  135. 'Call make_sources
  136. Call cmd("echo --ADD FILES-- & git add *" & "& timeout 2", vbNormalFocus)
  137. Dim msg As String
  138. msg = InputBox("Commit message:", "VCS")
  139. If Not Len(msg) > 0 Then GoTo err_msg
  140. Call cmd("echo --COMMIT-- & git commit -a -m " & Chr(34) & msg & Chr(34) & "& timeout 2", vbNormalFocus)
  141. Call cmd("echo --PULL-- & git pull origin master & pause", vbNormalFocus)
  142. Call update_from_sources
  143. Call cmd("echo --PUSH-- & git push origin master" & "& timeout 2", vbNormalFocus)
  144. Exit Function
  145. err_msg:
  146. MsgBox "Invalid value", vbExclamation
  147. End Function
  148. Public Function get_include_tables()
  149. get_include_tables = vcs_param("include_tables")
  150. End Function
  151. Public Function vcs_param(ByVal key As String, Optional ByVal default_value As String = "") As String
  152. vcs_param = default_value
  153. On Error GoTo err_vcs_table
  154. vcs_param = DFirst("val", "ztbl_vcs", "[key]='" & key & "'")
  155. err_vcs_table:
  156. End Function
  157. Public Function zip_app_file() As Boolean
  158. On Error GoTo err
  159. Dim command, shortname As String
  160. zip_app_file = False
  161. shortname = Split(CurrentProject.name, ".")(0)
  162. 'run the shell comand
  163. Call cmd("cd " & CurrentProject.path & " & " & _
  164. "zip tmp_" & shortname & ".zip " & CurrentProject.name & _
  165. " & exit")
  166. 'remove the old zip file
  167. If dir(CurrentProject.path & "\" & shortname & ".zip") <> "" Then
  168. Kill CurrentProject.path & "\" & shortname & ".zip"
  169. End If
  170. 'rename the temporary zip
  171. Call cmd("cd " & CurrentProject.path & " & " & _
  172. "ren tmp_" & shortname & ".zip" & " " & shortname & ".zip" & _
  173. " & exit")
  174. zip_app_file = True
  175. fin:
  176. Exit Function
  177. UnknownErr:
  178. MsgBox "Unknown error: unable to ZIP the app file, do it manually"
  179. GoTo fin
  180. err:
  181. MsgBox "Error while zipping file app: " & err.Description
  182. GoTo fin
  183. End Function
  184. Public Function make_backup() As Boolean
  185. On Error GoTo err
  186. make_backup = False
  187. If dir(CurrentProject.path & "\" & CurrentProject.name & ".old") <> "" Then
  188. Kill CurrentProject.path & "\" & CurrentProject.name & ".old"
  189. End If
  190. Call cmd("copy " & Chr(34) & CurrentProject.path & "\" & CurrentProject.name & Chr(34) & _
  191. " " & Chr(34) & CurrentProject.path & "\" & CurrentProject.name & ".old" & Chr(34))
  192. make_backup = True
  193. Exit Function
  194. err:
  195. MsgBox "Error during backup: " & err.Description
  196. End Function