vcs.bas 7.7 KB

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