vcs.bas 7.6 KB

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