Page 1 of 1

Small backup tool for PBIde

Posted: Sun Aug 19, 2007 8:02 pm
by technicorn
I always missed the feature to have the IDE make backups of your source on save,
so I wrote this small tool.

It handles sources with our without the *.cfg file.

Before compiling, you have to set #PathSlash to the right type of slash for your system,
or just edit the prog and put the right slash in.

To use it, you compile it to an executable of any name, set type to "Console",
I use MakeBackup.exe,
and put it for example in your PB directory under IDE_Plugins.

Than you have to set the triggers for it to work in the tools menu:

Common settings:
Set "Commandline" to point to the backup program you created
Leave the "Working Directory" empty
Leave "Wait until tool quits" unchecked
Set "Run Hidden" to checked
Set "Hide tool from main menu" to checked

Individual settings:

1. Trigger for loading sources
Arguments: Src_loaded "%PATH" "%FILE"
Name: SrcLoaded
Event: Sourcecode loaded

2. Trigger for saving sources
Arguments: Src_saved "%PATH" "%FILE"
Name: SrcSaved
Event: Sourcecode saved

3. Trigger for closing sources
Arguments: Src_closed "%PATH" "%FILE"
Name: SrcClosed
Event: Sourcecode closed

If you create a "bak" directory in your source directory, than the tool
will save backups there, instead of crowding your source directory.

You can set #MaxBackup on compiletime to the number of backup level you want, or to 0 for just the simple *.bak file,
all older backups will be numbered *.bkX, where X is the level of backup,
the higher the number the older the backup.

Because the IDE triggers the "Sourcode saved" after it saved the source,
it would be useless to make a backup of that, as your unedited file is already overwriten,
so the program makes a copy of all sources with a .tmp extension on loading,
if your computer or just the IDE crashes, these files will stay in your source or bak directory,
but will be overwritten/erased on next normal closing of the source.

Code: Select all

EnableExplicit

DebugLevel 10

#ProgramName = "MakeBackup"

#MaxBackup = 9

Global CmdLineIdx.l
Global SrcDPNE.s, SrcDP.s, BakDP.s, BakDPNE.s, BackupCnt.l
Global ErrStr.s

; MessageRequester(#ProgramName, ProgramParameter(0))

; If CountProgramParameters() <> 1 And CountProgramParameters() <> 3
;   ErrStr = "Invalide number of parameters: " + Str(CountProgramParameters())
;   For CmdLineIdx = 0 To CountProgramParameters() - 1
;     ErrStr + Chr(10) + ProgramParameter(CmdLineIdx)
;   Next CmdLineIdx
;   MessageRequester(#ProgramName, ErrStr)
;   End 1
; EndIf

If ProgramParameter(1) = "" Or ProgramParameter(2) = ""
  End 1
EndIf

;MessageRequester(#ProgramName, ProgramParameter(0)+#LF$+ProgramParameter(1)+#LF$+ProgramParameter(2))

SrcDP = ProgramParameter(1)
SrcDPNE = ProgramParameter(2)

If FileSize(SrcDP + "bak") = -2
  BakDP = SrcDP + "bak" + #PathSlash
  BakDPNE = BakDP + GetFilePart(SrcDPNE)
Else
  BakDP = SrcDP
  BakDPNE = SrcDPNE
EndIf

Select UCase(ProgramParameter(0))
  Case "SRC_LOADED"
    If FileSize(SrcDPNE) >= 0
      CopyFile(SrcDPNE, BakDPNE + ".tmp")
    EndIf
    If FileSize(SrcDPNE + ".cfg") >= 0
      CopyFile(SrcDPNE + ".cfg", BakDPNE + ".cfg.tmp")
    EndIf
  Case "SRC_SAVED"
    If FileSize(BakDPNE + ".bk" + Str(#MaxBackup)) >= 0
      DeleteFile(BakDPNE + ".bk" + Str(#MaxBackup))
    EndIf
    If FileSize(BakDPNE + ".cfg.bk" + Str(#MaxBackup)) >= 0
      DeleteFile(BakDPNE + ".cfg.bk" + Str(#MaxBackup))
    EndIf
    For BackupCnt = #MaxBackup To 2 Step -1
      If FileSize(BakDPNE + ".bk" + Str(BackupCnt - 1)) >= 0
        RenameFile(BakDPNE + ".bk" + Str(BackupCnt - 1), BakDPNE + ".bk" + Str(BackupCnt))
      EndIf
      If FileSize(BakDPNE + ".cfg.bk" + Str(BackupCnt - 1)) >= 0
        RenameFile(BakDPNE + ".cfg.bk" + Str(BackupCnt - 1), BakDPNE + ".cfg.bk" + Str(BackupCnt))
      EndIf
    Next BackupCnt
    If #MaxBackup
      If FileSize(BakDPNE + ".bak") >= 0
        CopyFile(BakDPNE + ".bak", BakDPNE + ".bk1")
      EndIf
      If FileSize(BakDPNE + ".cfg.bak") >= 0
        CopyFile(BakDPNE + ".cfg.bak", BakDPNE + ".cfg.bk1")
      EndIf
    EndIf
    If FileSize(BakDPNE + ".tmp") >= 0
      CopyFile(BakDPNE + ".tmp", BakDPNE + ".bak")
    EndIf
    If FileSize(BakDPNE + ".cfg.tmp") >= 0
      CopyFile(BakDPNE + ".cfg.tmp", BakDPNE + ".cfg.bak")
    EndIf
    If FileSize(SrcDPNE) >= 0
      CopyFile(SrcDPNE, BakDPNE + ".tmp")
    EndIf
    If FileSize(SrcDPNE + ".cfg") >= 0
      CopyFile(SrcDPNE + ".cfg", BakDPNE + ".cfg.tmp")
    EndIf
  Case "SRC_CLOSED"
    If FileSize(BakDPNE + ".tmp") >= 0
      DeleteFile(BakDPNE + ".tmp")
    EndIf
    If FileSize(BakDPNE + ".cfg.tmp") >= 0
      DeleteFile(BakDPNE + ".cfg.tmp")
    EndIf
  Default
    ;  MessageRequester(#ProgramName, "Unknown command: '" + ProgramParameter(0) + "'")
EndSelect

End 0
There are some commented lines, you can uncomment them for testing,
but I would recommend to do testing with only one source loaded,
or you'll get popup messages for every loaded source.

Posted: Sun Aug 19, 2007 10:29 pm
by blueznl
Ah, does the 'save' trigger now work properly in the IDE? Time to modify CodeCaddy then...

Posted: Sun Aug 19, 2007 10:42 pm
by technicorn
Yes, the triggers are working correct now,
I'm using PB4.10b3, but with the IDE from b2, but only because of the autocompletion bug at the moment.

Posted: Tue Aug 21, 2007 12:35 pm
by blueznl
The b2 editor caused too many hickups to work comfortably, at least for me and for long codes :?

Anyway, good to know it works now. Thx.