Skip to content

Commit

Permalink
Allow duplicate import/includes
Browse files Browse the repository at this point in the history
  • Loading branch information
Praveen Nandagiri committed Jun 16, 2021
1 parent eb4a55c commit 8147fa8
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 47 deletions.
1 change: 1 addition & 0 deletions bin/build.wsf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</runtime>

<script language="VBScript" src="lib/core/init.vbs" />
<script language="VBScript" src="lib/core/ArrayUtil/ArrayUtil.vbs" />
<script language="VBScript" src="lib/core/FSO/FSO.vbs" />
<script language="VBScript" src="lib/core/Wshell/Wshell.vbs" />
<script language="VBScript" src="lib/core/VbsJson/VbsJson.vbs" />
Expand Down
38 changes: 38 additions & 0 deletions lib/core/ArrayUtil/ArrayUtil.vbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Class ArrayUtil

Public Function toString(arr)
If Not isArray(arr) Then
toString = "Supplied parameter is not an array."
Exit Function
End If

Dim s, i
s = "Array{" & UBound(arr) & "} [" & vbCrLf
For i = 0 to UBound(arr)
s = s & vbTab & "[" & i & "] => [" & arr(i) & "]"
if i < UBound(arr) Then s = s & ", "
s = s & vbCrLf
Next
s = s & "]"
toString = s

End Function

Public Function contains(arr, s)
If Not isArray(arr) Then
toString = "Supplied parameter is not an array."
Exit Function
End If

Dim i, bFlag
bFlag = false
For i = 0 to UBound(arr)
If arr(i) = s Then
bFlag = true
Exit For
End If
Next
contains = bFlag
End Function

End Class
1 change: 1 addition & 0 deletions lib/core/ArrayUtil/test.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cscript //nologo .\test.wsf //job:Test1
24 changes: 24 additions & 0 deletions lib/core/ArrayUtil/test.wsf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<package>
<job id="Test0">
<script language="VBscript" src="ArrayUtil.vbs" />
<script language="VBScript">
arr = Array("item1", "2", "I'm 3")
set au = new ArrayUtil
Wscript.Echo au.toString(arr)
</script>
</job>
<job id="Test1">
<script language="VBscript" src="ArrayUtil.vbs" />
<script language="VBScript">
arr = Array("item1", "item2", "item3")
set au = new ArrayUtil
Wscript.Echo "item1 contains: " & au.contains(arr, "item1")
Wscript.Echo "[ item1] contains: " & au.contains(arr, " item1")
Wscript.Echo "[item1 ] contains: " & au.contains(arr, "item1 ")
Wscript.Echo "Item1 contains: " & au.contains(arr, "Item1")
Wscript.Echo "item2 contains: " & au.contains(arr, "item2")
Wscript.Echo "item3 contains: " & au.contains(arr, "item3")
Wscript.Echo "item4 contains: " & au.contains(arr, "item4")
</script>
</job>
</package>
4 changes: 4 additions & 0 deletions lib/core/FSO/FSO.vbs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Class FSO
GetFileDir = objFSO.GetParentFolderName(objFile)
End Function

Public Function GetFilePath(ByVal file)
GetFilePath = objFSO.GetFile(file).Path
End Function

Public Function ReadFile(file)
If Not FileExists(file) Then
Wscript.Echo "File " & file & " does not exists."
Expand Down
33 changes: 26 additions & 7 deletions lib/core/globals.vbs
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
Dim baseDir
Dim cFS
Redim IncludedScripts(-1)
Dim arrUtil
set cFS = new FSO
set arrUtil = new ArrayUtil


public Sub Echo(msg)
Wscript.Echo msg
End Sub

Function log(msg)
cFS.WriteFile "build.log", msg, false
End Function

log "================================= Call ================================="

Sub Include(file)
log "Include(" + file + ")"
if Lcase(cFS.GetExtn(file)) = "" Then
log "File extension missing. Adding .vbs"
file = file + ".vbs"
end if
Dim path: path = cFS.GetFilePath(file)
log "File full path: " & path

Dim content: content = cFS.ReadFile(file)
if content <> "" Then
' Dim pkg
' pkg = Replace(file, "\node_modules\", "")
' pkg = Replace(pkg, "\index.vbs", "")
' cFS.WriteFile "build\imported\" & pkg & ".vbs", content, true
ExecuteGlobal content
' Dim pkg
' pkg = Replace(file, "\node_modules\", "")
' pkg = Replace(pkg, "\index.vbs", "")
' cFS.WriteFile "build\imported\" & pkg & ".vbs", content, true
If Not arrUtil.contains(IncludedScripts, path) Then
Redim Preserve IncludedScripts(UBound(IncludedScripts)+1)
IncludedScripts(UBound(IncludedScripts)) = path
Dim content: content = cFS.ReadFile(file)
if content <> "" Then
ExecuteGlobal content
Else
log "File content is empty. Not loaded."
End If
Else
log "File: " & path & " already loaded."
End If
End Sub

Expand Down
22 changes: 6 additions & 16 deletions lib/core/params.vbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,15 @@ End If
' TODO: Assess all possible combinations a user can send in command line
file = baseDir & "\" & file

if Lcase(cFS.GetExtn(file)) = "vbs" Then
log "File extension is: .vbs"
Else
if Lcase(cFS.GetExtn(file)) = "" Then
log "File extension missing. Adding .vbs"
file = file + ".vbs"
end if

log "File: " & file


Dim script
script = cFS.ReadFile(file)
if script = "" Then
log "No file supplied or is empty."
Wscript.Quit
End if


log "Main Script: " & file

'===========================
Execute script
'===========================
Include file
'===========================

' Wscript.Echo arrUtil.toString(IncludedScripts)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vbspm",
"version": "1.1.3",
"version": "1.1.4",
"description": "Vbs Package Manager",
"main": "vbspm.vbs",
"bin": "vbspm.cmd",
Expand Down
98 changes: 75 additions & 23 deletions vbspm.vbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,45 @@

' ================= src : lib/core/init.vbs =================
Option Explicit
' ================= src : lib/core/ArrayUtil/ArrayUtil.vbs =================
Class ArrayUtil

Public Function toString(arr)
If Not isArray(arr) Then
toString = "Supplied parameter is not an array."
Exit Function
End If

Dim s, i
s = "Array{" & UBound(arr) & "} [" & vbCrLf
For i = 0 to UBound(arr)
s = s & vbTab & "[" & i & "] => [" & arr(i) & "]"
if i < UBound(arr) Then s = s & ", "
s = s & vbCrLf
Next
s = s & "]"
toString = s

End Function

Public Function contains(arr, s)
If Not isArray(arr) Then
toString = "Supplied parameter is not an array."
Exit Function
End If

Dim i, bFlag
bFlag = false
For i = 0 to UBound(arr)
If arr(i) = s Then
bFlag = true
Exit For
End If
Next
contains = bFlag
End Function

End Class
' ================= src : lib/core/FSO/FSO.vbs =================
' ==============================================================================================
' Implementation of several use cases of FileSystemObject into this class
Expand Down Expand Up @@ -68,6 +107,10 @@ Class FSO
GetFileDir = objFSO.GetParentFolderName(objFile)
End Function

Public Function GetFilePath(ByVal file)
GetFilePath = objFSO.GetFile(file).Path
End Function

Public Function ReadFile(file)
If Not FileExists(file) Then
Wscript.Echo "File " & file & " does not exists."
Expand Down Expand Up @@ -836,27 +879,46 @@ end function
' ================= src : lib/core/globals.vbs =================
Dim baseDir
Dim cFS
Redim IncludedScripts(-1)
Dim arrUtil
set cFS = new FSO
set arrUtil = new ArrayUtil


public Sub Echo(msg)
Wscript.Echo msg
End Sub

Function log(msg)
cFS.WriteFile "build.log", msg, false
End Function

log "================================= Call ================================="

Sub Include(file)
log "Include(" + file + ")"
if Lcase(cFS.GetExtn(file)) = "" Then
log "File extension missing. Adding .vbs"
file = file + ".vbs"
end if
Dim path: path = cFS.GetFilePath(file)
log "File full path: " & path

Dim content: content = cFS.ReadFile(file)
if content <> "" Then
' Dim pkg
' pkg = Replace(file, "\node_modules\", "")
' pkg = Replace(pkg, "\index.vbs", "")
' cFS.WriteFile "build\imported\" & pkg & ".vbs", content, true
ExecuteGlobal content
' Dim pkg
' pkg = Replace(file, "\node_modules\", "")
' pkg = Replace(pkg, "\index.vbs", "")
' cFS.WriteFile "build\imported\" & pkg & ".vbs", content, true
If Not arrUtil.contains(IncludedScripts, path) Then
Redim Preserve IncludedScripts(UBound(IncludedScripts)+1)
IncludedScripts(UBound(IncludedScripts)) = path
Dim content: content = cFS.ReadFile(file)
if content <> "" Then
ExecuteGlobal content
Else
log "File content is empty. Not loaded."
End If
Else
log "File: " & path & " already loaded."
End If
End Sub

Expand Down Expand Up @@ -892,25 +954,15 @@ End If
' TODO: Assess all possible combinations a user can send in command line
file = baseDir & "\" & file

if Lcase(cFS.GetExtn(file)) = "vbs" Then
log "File extension is: .vbs"
Else
if Lcase(cFS.GetExtn(file)) = "" Then
log "File extension missing. Adding .vbs"
file = file + ".vbs"
end if

log "File: " & file


Dim script
script = cFS.ReadFile(file)
if script = "" Then
log "No file supplied or is empty."
Wscript.Quit
End if


log "Main Script: " & file

'===========================
Execute script
'===========================
Include file
'===========================

' Wscript.Echo arrUtil.toString(IncludedScripts)

0 comments on commit 8147fa8

Please sign in to comment.