ejes consulting

Techincal Consulting Design and Automation

r.vbs

Simplistic source revision control system in visual basic scripting edition:

'
' Copyright (c) 2009 Evan Stawnyczy <ejes@torfree.net>
'
'Constants
Const ForRead=1
Const ForOverWrite=2
Const ForAppend=8
'
Set objFileSys=CreateObject("Scripting.FileSystemObject")
Set objWShell=CreateObject("WScript.Shell")
Set objEnv=objWShell.Environment("Process")
Repository=objEnv("REPOSITORY")
Repository="H:\Development\Repository"
DatabaseName="versions.txt"
Tempdir=objEnv("TEMP")

'''''''''
CScript=True
If Right(WScript.FullName,11) <> "cscript.exe" Then
 ' WScript running
 CScript=False
End If
Source=WScript.Arguments.Item(0)
' Parse Arguments
If Not objFileSys.FileExists(Source) Then
 ' File not found
 WScript.Echo "File " & Source & " not found!"
End If
BaseFile=Replace(objFileSys.GetBaseName(Source)," ","_")
With objFileSys
 RepositoryDatabase=.BuildPath(Repository,DatabaseName)
 If Not .FolderExists(Repository) Then
  .CreateFolder(Repository)
 End If
 If Not .FileExists(RepositoryDatabase) Then
  Set fp=.OpenTextFile(RepositoryDatabase,ForOverWrite,True)
  fp.Close
 End If
End With
Version=0
' Look file up in repository list
Set fp=objFileSys.OpenTextFile(RepositoryDatabase,ForRead)
Do Until fp.AtEndOfStream
 LineIn=fp.ReadLine
 Record=Split(LineIn,"=")
 For i=0 To UBound(Record)
  Record(i)=Trim(Record(i))
 Next'i
 If Record(0)=BaseFile Then
'  Version=CSng(Record(1))
  Version=CInt(Record(1))
 End If
Loop
fp.Close
Version=Version+1
If CScript Then
 WScript.Echo "Adding file " & BaseFile & " Version " & Version
End If
Archive=BaseFile+"-"+CStr(Version)+".cab"
cmd="%WINDIR%\System32\MAKECAB.EXE "+Chr(34)+Source+Chr(34)+" "+Archive
objWShell.Run cmd,0,True
objFileSys.MoveFile Archive,objFileSys.BuildPath(Repository,Archive)
TempFile=objFileSys.BuildPath(TempDir,DatabaseName)
If objFileSys.FileExists(TempFile) Then
 objFileSys.DeleteFile(TempFile)
End If
objFileSys.MoveFile RepositoryDatabase,TempFile
Set fpi=objFileSys.OpenTextFile(TempFile,ForRead,True)
Set fpo=objFileSys.OpenTextFile(RepositoryDatabase,ForOverWrite,True)
Do Until fpi.AtEndOfStream
 LineIn=fpi.ReadLine
 Record=Split(LineIn,"=")
 For i=0 To UBound(Record)
  Record(i)=Trim(Record(i))
 Next'i
 If Record(0)=BaseFile Then
  Record(1)=CStr(Version)
  Written=True
 End If
 fpo.WriteLine Record(0) & "=" & Record(1)
Loop
If Not Written Then
 fpo.WriteLine BaseFile & "=" & Version
End If
fpi.Close
fpo.Close
WScript.Echo BaseFile & " Version: " & Version & " Commited to repository"
Advertisements

Written by ejes

June 15, 2009 at 11:30 am

%d bloggers like this: