-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcelGit.bas
154 lines (127 loc) · 5.32 KB
/
ExcelGit.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
Attribute VB_Name = "ExcelGit"
Option Explicit
Option Compare Text
Option Base 0
'============================================================================================================================
'
'
' Author : John Greenan
' Email :
' Company : Alignment Systems Limited
' Date : 28th March 2014
'
' Purpose : Matching Engine in Excel VBA for Alignment Systems Limited
'
' References : See VB Module FL for list extracted from VBE
' References :
'============================================================================================================================
Dim mWsh As IWshRuntimeLibrary.WshNetwork
Dim mWshell As IWshRuntimeLibrary.WshShell
Private Function StatusToString(Received As IWshRuntimeLibrary.WshExecStatus) As String
'============================================================================================================================
'
'
' Author : John Greenan
' Email :
' Company : Alignment Systems Limited
' Date : 28th March 2014
'
' Purpose : Matching Engine in Excel VBA for Alignment Systems Limited
'
' References : See VB Module FL for list extracted from VBE
' References :
'============================================================================================================================
Const strWshRunning = "Running"
Const strWshFinished = "Finished"
Const strWshFailed = "Failed"
Select Case Received
Case IWshRuntimeLibrary.WshRunning
StatusToString = strWshRunning
Case IWshRuntimeLibrary.WshFinished
StatusToString = strWshFinished
Case IWshRuntimeLibrary.WshFailed
StatusToString = strWshFailed
End Select
End Function
Public Function WriteToGit()
'============================================================================================================================
'
'
' Author : John Greenan
' Email :
' Company : Alignment Systems Limited
' Date : 28th March 2014
'
' Purpose : Matching Engine in Excel VBA for Alignment Systems Limited
'
' References : See VB Module FL for list extracted from VBE
' References :
'============================================================================================================================
'Const
Const strSourceDirectory As String = "C:\path\to\your\code"
Const strCMD As String = "cmd /K"
Const strChangeDirectoryTo As String = "cd"
Const strGitCommit As String = "git commit -am"
Const strGitStatus As String = "git status"
Const strProcessID As String = "PID="
Const strTitle As String = "title Alignment-Systems.com Git Integration"
'Variables
Dim strExecStatus As String
Dim dtNow As Date
Dim strTextFromStdStream As String
Dim strBuiltCommand As String
Dim strUserName As String
Set mWshell = New IWshRuntimeLibrary.WshShell
Set mWsh = New IWshRuntimeLibrary.WshNetwork
strUserName = mWsh.UserName
dtNow = Now()
' Change to the correct folder with cmd:>cd folder
strBuiltCommand = strCMD
With mWshell.Exec(strBuiltCommand)
'----------------------------------
strBuiltCommand = strChangeDirectoryTo
Debug.Print "[" & strProcessID & .ProcessID & "]>" & strBuiltCommand
.StdIn.WriteLine strBuiltCommand
strExecStatus = StatusToString(.Status)
Debug.Print "[" & strProcessID & .ProcessID & "]>" & strBuiltCommand & "=" & strExecStatus
'----------------------------------
strBuiltCommand = strTitle
Debug.Print "[" & strProcessID & .ProcessID & "]>" & strBuiltCommand
.StdIn.WriteLine strBuiltCommand
strExecStatus = StatusToString(.Status)
Debug.Print "[" & strProcessID & .ProcessID & "]>" & strBuiltCommand & "=" & strExecStatus
'----------------------------------
strBuiltCommand = strChangeDirectoryTo & Chr(VBA.KeyCodeConstants.vbKeySpace) & strSourceDirectory
Debug.Print "[" & strProcessID & .ProcessID & "]>" & strBuiltCommand
.StdIn.WriteLine strBuiltCommand
strExecStatus = StatusToString(.Status)
Debug.Print "[" & strProcessID & .ProcessID & "]>" & strBuiltCommand & "=" & strExecStatus
'----------------------------------
strBuiltCommand = strGitCommit & Chr(VBA.KeyCodeConstants.vbKeySpace) & """" & strUserName & Chr(VBA.KeyCodeConstants.vbKeySpace) & dtNow & """"
Debug.Print "[" & strProcessID & .ProcessID & "]>" & strBuiltCommand
.StdIn.WriteLine strBuiltCommand
strExecStatus = StatusToString(.Status)
Debug.Print "[" & strProcessID & .ProcessID & "]>" & strBuiltCommand & "=" & strExecStatus
'----------------------------------
'Cleanup
'----------------------------------
.StdIn.Close
If Not .StdOut.AtEndOfStream Then
Debug.Print "Dumping out Process StdOut"
End If
Do While Not .StdOut.AtEndOfStream
strTextFromStdStream = "[" & strProcessID & .ProcessID & "]" & .StdOut.ReadLine()
Debug.Print strTextFromStdStream
Loop
If Not .StdErr.AtEndOfStream Then
Debug.Print "Dumping out Process StdErr"
End If
Do While Not .StdErr.AtEndOfStream
strTextFromStdStream = "[" & strProcessID & .ProcessID & "]" & .StdErr.ReadLine()
Debug.Print strTextFromStdStream
Loop
.StdErr.Close
.StdOut.Close
.Terminate
End With
End Function