Skip to content

Commit

Permalink
Verify header when importing module/class
Browse files Browse the repository at this point in the history
This allows us to natively import legacy SaveAsText modules and classes, as well as VBE modules and classes. The VBE header is dynamically added if needed. Closes #230
  • Loading branch information
joyfullservice committed Jun 5, 2021
1 parent c359026 commit 4829748
Showing 1 changed file with 65 additions and 3 deletions.
68 changes: 65 additions & 3 deletions Version Control.accda.src/modules/clsDbModule.cls
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,74 @@ Private Sub IDbComponent_Import(strFile As String)

Dim proj As VBProject
Dim strTempFile As String
Dim strLines() As String
Dim lngLine As Long
Dim cData As clsConcat
Dim blnIsClass As Boolean
Dim blnHasHeader As Boolean

' Only import files with the correct extension.
If Not strFile Like "*.bas" Then Exit Sub
If Not (strFile Like "*.bas" Or strFile Like "*.cls") Then Exit Sub

' Convert back to ANSI from UTF-8
' Log performance of file conversion
Perf.OperationStart "Enc. Module as " & GetSystemEncoding

' Read file contents into array of lines
strLines = Split(ReadFile(strFile), vbCrLf)

' Loop through first 10 lines to determine type and header
For lngLine = 0 To 9
If strLines(lngLine) = "VERSION 1.0 CLASS" Then
' Class with VBE header
blnIsClass = True
blnHasHeader = True
Exit For
ElseIf StartsWith(strLines(lngLine), "Attribute VB_Name = """) Then
' Module with VBE header
blnHasHeader = True
Exit For
ElseIf StartsWith(strLines(lngLine), "Attribute VB_GlobalNameSpace = ") Then
' Class with no header
blnIsClass = True
Exit For
End If
Next lngLine

' Use concatenation class to build file contents
Set cData = New clsConcat
With cData
.AppendOnAdd = vbCrLf

' Build header, if needed
If Not blnHasHeader Then
If blnIsClass Then
.Add "VERSION 1.0 CLASS"
.Add "BEGIN"
.Add " MultiUse = -1 'True"
.Add "END"
End If
.Add "Attribute VB_Name = """, GetObjectNameFromFileName(strFile), """"
End If

' Add in file contents
For lngLine = 0 To UBound(strLines)
.Add strLines(lngLine)
Next lngLine
End With

' Write to a new file using system encoding (converting from UTF-8)
strTempFile = GetTempFile
ConvertUtf8Ansi strFile, strTempFile, False
With New ADODB.Stream
.Type = adTypeText
.Open
.Charset = GetSystemEncoding
.WriteText cData.GetStr
' Write to disk
.SaveToFile strTempFile, adSaveCreateOverWrite
.Close
End With

Perf.OperationEnd

' Import the source file
Set proj = GetVBProjectForCurrentDB
Expand Down Expand Up @@ -369,3 +430,4 @@ End Property
Public Property Get Parent() As IDbComponent
Set Parent = Me
End Property

0 comments on commit 4829748

Please sign in to comment.