vcs.bas 6.7 KB

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