vcs.bas 7.2 KB

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