vcs.bas 7.6 KB

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