55Attribute VB_Name = "ACLibGitHubImporter"
66Attribute VB_GlobalNameSpace = False
77Attribute VB_Creatable = False
8- Attribute VB_PredeclaredId = False
8+ Attribute VB_PredeclaredId = True
99Attribute VB_Exposed = False
1010'---------------------------------------------------------------------------------------
1111' Class: _codelib.addins.shared.ACLibGitHubImporter
@@ -28,11 +28,15 @@ Attribute VB_Exposed = False
2828Option Compare Database
2929Option Explicit
3030
31- Private Const GitHubContentBaseUrl As String = "https://raw.githubusercontent.com/AccessCodeLib/AccessCodeLib /{branch}/{path}"
32- Private Const GitHubApiBaseUrl As String = "https://api.github.com/repos/AccessCodeLib/AccessCodeLib /"
31+ Private Const GitHubContentBaseUrl As String = "https://raw.githubusercontent.com/{owner}/{repo} /{branch}/{path}"
32+ Private Const GitHubApiBaseUrl As String = "https://api.github.com/repos/{owner}/{repo} /"
3333
34+ Private m_GitHubApiAuthorizationToken As String
3435Private m_LastCommit As Date
35- Private m_UseDraftBranch As Boolean
36+
37+ Private m_RepositoryOwner As String
38+ Private m_RepositoryName As String
39+ Private m_BranchName As String
3640
3741#If VBA7 Then
3842Private Declare PtrSafe Function DeleteUrlCacheEntry Lib "wininet .dll " Alias "DeleteUrlCacheEntryA " (ByVal lpszUrlName As String ) As Long
@@ -43,23 +47,68 @@ Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFile
4347#End If
4448
4549'---------------------------------------------------------------------------------------
46- ' Property: UseDraftBranch
50+ ' Property: GitHubAuthorizationubAuthToken
51+ '---------------------------------------------------------------------------------------
52+ Public Property Get GitHubApiAuthorizationToken() As String
53+ GitHubApiAuthorizationToken = m_GitHubApiAuthorizationToken
54+ End Property
55+
56+ Public Property Let GitHubApiAuthorizationToken(ByVal NewValue As String )
57+ m_GitHubApiAuthorizationToken = NewValue
58+ End Property
59+
60+ '---------------------------------------------------------------------------------------
61+ ' Property: RepositoryOwner
4762'---------------------------------------------------------------------------------------
48- Public Property Get UseDraftBranch() As Boolean
49- UseDraftBranch = m_UseDraftBranch
63+ Public Property Get RepositoryOwner() As String
64+ If Len(m_RepositoryOwner) > 0 Then
65+ RepositoryOwner = m_RepositoryOwner
66+ Else ' Default: AccessCodeLib
67+ RepositoryOwner = "AccessCodeLib"
68+ End If
5069End Property
5170
52- Public Property Let UseDraftBranch(ByVal NewValue As Boolean )
53- m_UseDraftBranch = NewValue
71+ Public Property Let RepositoryOwner(ByVal NewValue As String )
72+ m_RepositoryOwner = NewValue
73+ End Property
74+
75+ '---------------------------------------------------------------------------------------
76+ ' Property: RepositoryName
77+ '---------------------------------------------------------------------------------------
78+ Public Property Get RepositoryName() As String
79+ If Len(m_RepositoryName) > 0 Then
80+ RepositoryName = m_RepositoryName
81+ Else ' Default: AccessCodeLib
82+ RepositoryName = "AccessCodeLib"
83+ End If
84+ End Property
85+
86+ Public Property Let RepositoryName(ByVal NewValue As String )
87+ m_RepositoryName = NewValue
88+ End Property
89+
90+ '---------------------------------------------------------------------------------------
91+ ' Property: BranchName
92+ '---------------------------------------------------------------------------------------
93+ Public Property Get BranchName() As String
94+ If Len(m_BranchName) > 0 Then
95+ BranchName = m_BranchName
96+ Else ' Default: master
97+ BranchName = "master"
98+ End If
99+ End Property
100+
101+ Public Property Let BranchName(ByVal NewValue As String )
102+ m_BranchName = NewValue
54103End Property
55104
56105'---------------------------------------------------------------------------------------
57106' Property: RevisionString
58107'---------------------------------------------------------------------------------------
59108Public Property Get RevisionString(Optional ByVal Requery As Boolean = False ) As String
60109 RevisionString = Format(LastCommit, "yyyymmddhhnnss" )
61- If UseDraftBranch Then
62- RevisionString = RevisionString & "-draft"
110+ If BranchName <> "master" Then
111+ RevisionString = RevisionString & "-" & BranchName
63112 End If
64113End Property
65114
@@ -104,25 +153,39 @@ End Sub
104153Private Sub UpdateCodeModuleInTable (ByVal ModuleName As String , ByVal ACLibPath As String , Optional ByVal Requery As Boolean = False )
105154
106155 Dim TempFile As String
107- Dim DownLoadUrl As String
108- Dim BranchName As String
109156
110- TempFile = FileTools.TempPath & ModuleName & FileTools.GetFileExtension(ACLibPath, True )
111157
112- If UseDraftBranch Then
113- BranchName = "draft"
114- Else
115- BranchName = "master"
116- End If
117- DownLoadUrl = Replace(GitHubContentBaseUrl, "{branch}" , BranchName)
118- DownLoadUrl = Replace(DownLoadUrl, "{path}" , ACLibPath)
158+ TempFile = FileTools.TempPath & ModuleName & FileTools.GetFileExtension(ACLibPath, True )
159+ DownloadACLibFileFromWeb ACLibPath, TempFile
119160
120- DownloadFileFromWeb DownLoadUrl, TempFile
121161 CurrentApplication.SaveAppFile ModuleName, TempFile, False , "SccRev" , Me.RevisionString(Requery)
122162 Kill TempFile
123163
124164End Sub
125165
166+ Friend Sub DownloadACLibFileFromWeb (ByVal ACLibPath As String , ByVal TargetFilePath As String )
167+
168+ Dim DownLoadUrl As String
169+
170+ DownLoadUrl = FillRepositoryData(GitHubContentBaseUrl)
171+ DownLoadUrl = Replace(DownLoadUrl, "{path}" , ACLibPath)
172+
173+ DownloadFileFromWeb DownLoadUrl, TargetFilePath
174+
175+ End Sub
176+
177+ Private Function FillRepositoryData (ByVal StringWithPlaceHolder As String ) As String
178+
179+ Dim TempValue As String
180+
181+ TempValue = Replace(StringWithPlaceHolder, "{owner}" , RepositoryOwner)
182+ TempValue = Replace(TempValue, "{repo}" , RepositoryName)
183+ TempValue = Replace(TempValue, "{branch}" , BranchName)
184+
185+ FillRepositoryData = TempValue
186+
187+ End Function
188+
126189Private Function GetLastCommitFromWeb () As Date
127190
128191'alternative: git rev-list HEAD --count
@@ -131,14 +194,9 @@ Private Function GetLastCommitFromWeb() As Date
131194
132195 Dim CommitUrl As String
133196 Dim LastCommitInfo As String
134- CommitUrl = GitHubApiBaseUrl & "commits/"
135-
136- If UseDraftBranch Then
137- CommitUrl = CommitUrl & "draft"
138- Else
139- CommitUrl = CommitUrl & "master"
140- End If
141197
198+ CommitUrl = FillRepositoryData(GitHubApiBaseUrl) & "commits/" & BranchName
199+
142200 Const RevisionTag As String = "Revision "
143201
144202 Dim JsonString As String
@@ -154,23 +212,31 @@ Private Function GetLastCommitFromWeb() As Date
154212
155213End Function
156214
157- Private Function GetJsonString (ByVal ApiUrl As String ) As String
158-
159- Dim ApiResponse As String
160- Dim json As Object
161-
162- Dim xml As Object ' MSXML2.XMLHTTP60
163- Set xml = CreateObject("MSXML2.XMLHTTP.6.0" )
164-
165- xml.Open "GET" , ApiUrl, False
166- xml.setRequestHeader "Content-type" , "application/json"
167- xml.send
168- While xml.ReadyState <> 4
169- DoEvents
170- Wend
171- ApiResponse = xml.responseText
172-
173- GetJsonString = ApiResponse
215+ Friend Function GetJsonString (ByVal ApiUrl As String ) As String
216+
217+ Dim ApiResponse As String
218+ Dim ApiAuthToken As String
219+ Dim json As Object
220+ Dim xml As Object 'MSXML2.XMLHTTP6
221+
222+ ApiUrl = FillRepositoryData(ApiUrl)
223+
224+ ApiAuthToken = GitHubApiAuthorizationToken
225+
226+ Set xml = CreateObject("MSXML2.XMLHTTP.6.0" )
227+
228+ xml.Open "GET" , ApiUrl, False
229+ If Len(ApiAuthToken) > 0 Then
230+ xml.setRequestHeader "Authorization" , ApiAuthToken
231+ End If
232+ xml.setRequestHeader "Content-type" , "application/json"
233+ xml.send
234+ While xml.ReadyState <> 4
235+ DoEvents
236+ Wend
237+ ApiResponse = xml.responseText
238+
239+ GetJsonString = ApiResponse
174240
175241End Function
176242
0 commit comments