vcs.bas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. Option Compare Database
  2. Dim private_optimizer As Boolean
  3. '****
  4. '*
  5. '* Main methods for VCS add-in
  6. '*
  7. '****
  8. Public Function vcsprompt()
  9. DoCmd.OpenForm "frm_vcs"
  10. End Function
  11. Public Function make_sources(Optional ByVal options As String = "")
  12. 'exports the source-code of the app
  13. On Error GoTo err
  14. Dim step As String
  15. step = "Initialization"
  16. If Not InStr(options, "-f") > 0 Then
  17. Dim msg As String
  18. msg = msg_list_modified()
  19. If Not Len(msg) > 0 Then
  20. msg = "Nothing new to export" & vbNewLine & _
  21. "Only the following will be exported:" & vbNewLine & _
  22. " - included table data" & vbNewLine & _
  23. " - relations" & vbNewLine & vbNewLine & _
  24. "TIP: use 'makesources -f' to force a complete export (could be long)."
  25. If MsgBox(msg, vbOKCancel + vbExclamation, "Export") = vbCancel Then Exit Function
  26. Else
  27. msg = "** VCS OPTIMIZER **" & vbNewLine & "Seuls les objets suivant seront exportés:" & vbNewLine & msg
  28. If MsgBox(msg, vbOKCancel) = vbCancel Then Exit Function
  29. End If
  30. Call activate_optimizer
  31. End If
  32. step = "Updates sources date"
  33. Debug.Print step
  34. Call update_sources_date
  35. Debug.Print "> done"
  36. step = "Zip the app file"
  37. Debug.Print step
  38. Call zip_app_file
  39. Debug.Print "> done"
  40. step = "Run VCS Export"
  41. Debug.Print step
  42. Call ExportAllSource
  43. Debug.Print "> done"
  44. Exit Function
  45. err:
  46. MsgBox "makesources - Unknown error at: " & step & vbNewLine & err.Description
  47. End Function
  48. Public Function update_from_sources(Optional ByVal options As String = "")
  49. 'updates the application from the sources
  50. Dim backup As Boolean
  51. Debug.Print "Creates a backup of the app file"
  52. backup = make_backup()
  53. If backup Then
  54. Debug.Print "> done"
  55. Else
  56. MsgBox "Error: unable to backup the app file, do it manually, then click OK"
  57. End If
  58. If MsgBox("WARNING: the current application is going to be updated " & _
  59. "with the source files. " & _
  60. "Any non committed work would be lost, " & _
  61. "are you sure you want to continue?" & _
  62. "", vbOKCancel) = vbCancel Then Exit Function
  63. Debug.Print "Run VCS Import"
  64. Call ImportAllSource
  65. Debug.Print "> done"
  66. End Function
  67. Public Function config_git_repo()
  68. 'configure the application GIT repository for VCS use
  69. 'verify that it is a git repository
  70. If Not is_git_repo() Then
  71. MsgBox "Not a git repository, please use 'git init on this directory first"
  72. Exit Function
  73. End If
  74. ' complete the gitignore file
  75. Call complete_gitignore
  76. End Function
  77. Public Function sync()
  78. 'complete command to synchronize this app with the distant master branch
  79. 'verify that it is a git repository
  80. If Not is_git_repo() Then
  81. MsgBox "Not a git repository, please use 'git init on this directory first"
  82. Exit Function
  83. End If
  84. 'Call make_sources
  85. Call cmd("echo --ADD FILES-- & git add *" & "& timeout 2", vbNormalFocus)
  86. Dim msg As String
  87. msg = InputBox("Commit message:", "VCS")
  88. If Not Len(msg) > 0 Then GoTo err_msg
  89. Call cmd("echo --COMMIT-- & git commit -a -m " & Chr(34) & msg & Chr(34) & "& timeout 2", vbNormalFocus)
  90. Call cmd("echo --PULL-- & git pull origin master & pause", vbNormalFocus)
  91. Call update_from_sources
  92. Call cmd("echo --PUSH-- & git push origin master" & "& timeout 2", vbNormalFocus)
  93. Exit Function
  94. err_msg:
  95. MsgBox "Invalid value", vbExclamation
  96. End Function
  97. Public Function is_git_repo() As Boolean
  98. is_git_repo = (dir(CurrentProject.Path & "\.git\", vbDirectory) <> "")
  99. End Function
  100. Public Function get_include_tables()
  101. If CurrentProject.name = "VCS.accda" Then
  102. get_include_tables = "tbl_commands,modele_ztbl_vcs"
  103. Else
  104. get_include_tables = vcs_param("include_tables")
  105. End If
  106. End Function
  107. Public Function vcs_param(ByVal key As String, Optional ByVal default_value As String = "") As String
  108. vcs_param = default_value
  109. On Error GoTo err_vcs_table
  110. vcs_param = DFirst("val", "ztbl_vcs", "[key]='" & key & "'")
  111. err_vcs_table:
  112. End Function
  113. Public Function gitcmd(args)
  114. Call cmd("echo -- " & args & " -- & git " & args & "& pause", vbNormalFocus)
  115. End Function
  116. Public Function zip_app_file() As Boolean
  117. On Error GoTo err
  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. zip_app_file = True
  134. fin:
  135. Exit Function
  136. UnknownErr:
  137. MsgBox "Unknown error: unable to ZIP the app file, do it manually"
  138. GoTo fin
  139. err:
  140. MsgBox "Error while zipping file app: " & err.Description
  141. GoTo fin
  142. End Function
  143. Public Function make_backup() As Boolean
  144. On Error GoTo err
  145. make_backup = False
  146. If dir(CurrentProject.Path & "\" & CurrentProject.name & ".old") <> "" Then
  147. Kill CurrentProject.Path & "\" & CurrentProject.name & ".old"
  148. End If
  149. 'FileCopy CurrentProject.Path & "\" & CurrentProject.name, CurrentProject.Path & "\" & CurrentProject.name & ".old"
  150. Call cmd("copy " & Chr(34) & CurrentProject.Path & "\" & CurrentProject.name & Chr(34) & _
  151. " " & Chr(34) & CurrentProject.Path & "\" & CurrentProject.name & ".old" & Chr(34))
  152. make_backup = True
  153. Exit Function
  154. err:
  155. MsgBox "Error during backup: " & err.Description
  156. End Function
  157. Public Function complete_gitignore()
  158. ' creates or complete the .gitignore file of the repo
  159. Dim gitignore_path, str_existing_keys, str As String
  160. Dim keys() As String
  161. keys = Split("*.accdb;*.laccdb;*.mdb;*.ldb;*.accde;*.mde;*.accda", ";")
  162. gitignore_path = CurrentProject.Path & "\.gitignore"
  163. Dim fso As Object
  164. Set fso = CreateObject("Scripting.FileSystemObject")
  165. Dim oFile As Object
  166. If Not fso.FileExists(gitignore_path) Then
  167. Set oFile = fso.CreateTextFile(gitignore_path)
  168. Else
  169. Set oFile = fso.OpenTextFile(gitignore_path, ForReading)
  170. str_existing_keys = ""
  171. While Not oFile.AtEndOfStream
  172. str = oFile.readline
  173. If Len(str_existing_keys) = 0 Then
  174. str_existing_keys = str
  175. Else
  176. str_existing_keys = str_existing_keys & ";" & str
  177. End If
  178. Wend
  179. oFile.Close
  180. Dim existing_keys() As String
  181. existing_keys = Split(str_existing_keys, ";")
  182. Set oFile = fso.OpenTextFile(gitignore_path, ForAppending)
  183. End If
  184. oFile.WriteBlankLines (2)
  185. oFile.WriteLine ("#[ automatically added by VCS")
  186. For Each key In keys
  187. If Not IsInArray(key, existing_keys) Then
  188. oFile.WriteLine key
  189. End If
  190. Next key
  191. oFile.WriteLine "#]"
  192. oFile.WriteBlankLines (2)
  193. oFile.Close
  194. Set fso = Nothing
  195. Set oFile = Nothing
  196. End Function
  197. Private Function IsInArray(ByVal stringToBeFound As String, ByRef arr As Variant) As Boolean
  198. IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
  199. End Function
  200. Function vcs_tbl_exists()
  201. On Error GoTo err
  202. vcs_tbl_exists = (CurrentDb.TableDefs("ztbl_vcs").name = "ztbl_vcs")
  203. Exit Function
  204. err:
  205. If err.number = 3265 Then
  206. vcs_tbl_exists = False
  207. Else
  208. MsgBox "Error: " & err.Description, vbCritical
  209. End If
  210. End Function
  211. Public Function create_vcs_tbl()
  212. CurrentDb.execute "SELECT 'include_tables' as key, '' as val INTO ztbl_vcs " & _
  213. "FROM modele_ztbl_vcs;"
  214. End Function
  215. Public Function update_vcs_param(ByVal key As String, ByVal val As String)
  216. If DCount("key", "ztbl_vcs", "[key]='" & key & "'") = 1 Then
  217. CurrentDb.execute "UPDATE ztbl_vcs SET ztbl_vcs.val = '" & val & "' " & _
  218. "WHERE (((ztbl_vcs.key)='" & key & "'));"
  219. Else
  220. CurrentDb.execute "INSERT INTO ztbl_vcs ( val, [key] ) " & _
  221. "SELECT '" & val & "' AS Expr1, '" & key & "' AS Expr2;"
  222. End If
  223. End Function
  224. Public Sub activate_optimizer()
  225. private_optimizer = True
  226. End Sub
  227. Public Function optimizer_activated()
  228. optimizer_activated = private_optimizer
  229. End Function