vcs.bas 7.7 KB

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