[Gambas-user] gbs (gambas script) hack :)

Rob sourceforge-raindog2 at ...94...
Mon Nov 15 06:04:35 CET 2004


While away this weekend I was trying to get to sleep late Friday 
night and the laptop was nearby, so I did this :)

Usage: gbs -a /path/to/project >project.gbs
         - make gambas project into a gambas script
       gbs project.gbs
         - execute a gambas script

gbs lets you distribute programs as a single, editable text file 
with run-time compilation invisible to the end user.  It is very 
much a hack (or a proof of concept, if anything ever comes of 
it) but so far it seems to work.  The only limitation I'm aware 
of presently is that it only includes .project, .class, .form 
and .module files, so (as with most script languages) you need 
to refer to image files and the like externally.

A "real" script engine would not do what this thing does, namely, 
burst the script into the various source files of a project (in 
a temp$() folder), compile it ("just in time" :) ) and execute 
it, but I don't think I could have written a "real" script 
engine in Gambas in a couple hours.  I do hope to add some 
features to it like being able to include base64 encoded images 
(for icons) and being able to just dash off a one-module script 
without the filename headers within the script.

Attached also is the notepad example from the gambas IDE 
converted to a gambas script.  Amusingly enough, the .gbs 
version is smaller than the executable created by the gambas 
archiver.

Rob

-------------- next part --------------
A non-text attachment was scrubbed...
Name: gbs-0.0.15.tar.gz
Type: application/x-tgz
Size: 6118 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20041115/7ac195f1/attachment.bin>
-------------- next part --------------
#!/usr/bin/gbx /usr/bin/gbs --
GBSfile FAbout.class
' Gambas class file


STATIC PUBLIC SUB Run()
  
  DIM hForm AS Form
  
  hForm = NEW FAbout
  hForm.ShowModal
  
END


PUBLIC SUB _new()
  
  ME.Center
  
END


PUBLIC SUB btnOK_Click()
  
  ME.Close
  
END

GBSfile .project
# Gambas Project File 1.0
Project=Notepad
Icon=notepad.png
Startup=FNotepad
TabSize=2
Version=0.0.1
Library=gb.qt
SnapToGrid=1
ShowGrid=1
Snap=8
Localize=0
KeepDebugInfo=0
ControlPublic=0
ExecPath=/home/benoit/gambas/gambas.link/share/gambas/examples/Miscellaneous/Notepad/Notepad
Prefix=0

GBSfile FNotepad.class
' Gambas class file

PRIVATE $sPath AS String
PRIVATE $bModify AS Boolean

STATIC PUBLIC SUB Main()
  
  DIM hForm AS Form
  
  hForm = NEW FNotepad
  hForm.Show
  
END


PUBLIC SUB _new()
  
  txtNotepad.Text = ""
  $bModify = FALSE
  RefreshTitle
  txtNotePad.SetFocus
  
END


PRIVATE FUNCTION GetName() AS String
  
  IF $sPath THEN RETURN $sPath
  
  RETURN "(New document)"
  
END


PRIVATE SUB RefreshTitle()
  
  DIM sTitle AS String
  
  IF $bModify THEN sTitle = "*"
  sTitle = sTitle & GetName()
  
  ME.Title = sTitle
  
END


PRIVATE SUB SetPath(sPath AS String)
  
  $sPath = sPath
  RefreshTitle
  
END

PRIVATE SUB SetModify(bModify AS Boolean)
  
  IF bModify = bModify THEN RETURN
  bModify = bModify
  RefreshTitle
  
END


PRIVATE FUNCTION CloseDoc() AS Boolean
  
  IF $bModify THEN
    SELECT CASE Message.Question(GetName() & "\n\nFile has been modified. Do you want to save it ?", "Yes", "No", "Cancel")
      CASE 1
        Save
      CASE 3
        RETURN TRUE
    END SELECT
  ENDIF
  
  $sPath = ""
  txtNotepad.Text = ""
  $bModify = FALSE
  RefreshTitle
  
END



PUBLIC SUB Load(sPath AS String)
  
  IF CloseDoc() THEN RETURN
  
  txtNotepad.Text = File.Load(sPath)
  $bModify = FALSE
  SetPath(sPath)

CATCH

  Message.Error(sPath & "\nUnable to load file.\n" & Error.Text)
  
END


PUBLIC SUB Save(OPTIONAL bSaveAs AS Boolean)
  
  IF bSaveAs OR NOT $sPath THEN
    IF Dialog.SaveFile() THEN RETURN
    SetPath(Dialog.Path)
  ENDIF
  
  File.Save($sPath, txtNotepad.Text)
  
END



PUBLIC SUB Form_Resize()
  
  txtNotepad.Move(0, 0, ME.ClientWidth, ME.ClientHeight)
  
END


PUBLIC SUB mnuOpen_Click()
  
  DIM sPath AS String

  Dialog.Filter = [ "Text files (*.txt)", "Desktop files (*.desktop)" ]

  IF Dialog.OpenFile() THEN RETURN
  Load(Dialog.Path)
  
END


PUBLIC SUB mnuSave_Click()
  
  Save
  
END


PUBLIC SUB mnuSaveAs_Click()
  
  Save(TRUE)
  
END


PUBLIC SUB mnuQuit_Click()
  
  ME.Close
  
END


PUBLIC SUB txtNotepad_Change()
  
  SetModify(TRUE)
  
END


PUBLIC SUB mnuClose_Click()
  
  CloseDoc
  
END


PUBLIC FUNCTION Form_Close() AS Boolean
  
  RETURN CloseDoc()
  
END


PUBLIC SUB mnuAbout_Click()
  
  FAbout.Run
  
END


PUBLIC SUB mnuCopy_Click()
  
  txtNotepad.Copy
  
END


PUBLIC SUB mnuPaste_Click()
  
  txtNotepad.Paste
  
END


PUBLIC SUB mnuCut_Click()
  
  txtNotepad.Cut
  
END


PUBLIC SUB mnuUndo_Click()
  
  txtNotepad.Undo
  
END


PUBLIC SUB mnuRedo_Click()
  
  txtNotepad.Redo
  
END


PUBLIC SUB mnuFont_Click()
  
  IF Dialog.SelectFont() THEN RETURN
  txtNotepad.Font = Dialog.Font
  
END

GBSfile FAbout.form
# Gambas Form File 1.0

{ FAbout Form
  Move(377,387,228,122)
  Text = ("About...")
  Border = Window.Fixed
  { Image1 PictureBox
    Move(16,16,32,32)
    Picture = Picture["notepad.png"]
    Stretch = True
  }
  { btnOK Button
    Move(64,88,96,24)
    Text = ("OK")
    Default = True
    Cancel = True
  }
  { TextLabel1 TextLabel
    Move(64,16,160,56)
    Text = ("This is a little notepad sample program.")
  }
}

GBSfile FNotepad.form
# Gambas Form File 1.0

{ FNotepad Form
  Move(327,225,400,400)
  Text = ("Little notepad")
  Icon = Picture["notepad.png"]
  { mnuFile Menu
    Text = ("&File")
    { mnuOpen Menu
      Text = ("&Open...")
      Shortcut = "Ctrl+O"
    }
    { mnuClose Menu
      Text = ("Close")
      Shortcut = "Ctrl+W"
    }
    { Menu1 Menu
      Text = ("")
    }
    { mnuSave Menu
      Text = ("&Save")
      Shortcut = "Ctrl+S"
    }
    { mnuSaveAs Menu
      Text = ("S&ave As...")
      Shortcut = "Ctrl+Shift+S"
    }
    { Menu2 Menu
      Text = ("")
    }
    { mnuQuit Menu
      Text = ("&Quit")
      Shortcut = "Ctrl+Q"
    }
  }
  { mnuEdit Menu
    Text = ("&Edit")
    { mnuCopy Menu
      Text = ("&Copy")
      Shortcut = "Ctrl+C"
    }
    { mnuCut Menu
      Text = ("C&ut")
      Shortcut = "Ctrl+X"
    }
    { mnuPaste Menu
      Text = ("&Paste")
      Shortcut = "Ctrl+V"
    }
    { Menu3 Menu
      Text = ("")
    }
    { mnuUndo Menu
      Text = ("&Undo")
      Shortcut = "Ctrl+Z"
    }
    { mnuRedo Menu
      Text = ("&Redo")
      Shortcut = "Ctrl+Y"
    }
    { Menu4 Menu
      Text = ("")
    }
    { mnuFont Menu
      Text = ("Choose &Font...")
    }
  }
  { mnuHelp Menu
    Text = ("&?")
    { mnuAbout Menu
      Text = ("&About...")
    }
  }
  { txtNotepad TextArea
    Move(8,8,280,208)
    Font = Font["Monospace,10"]
    Text = ("txtNotepad")
  }
}



More information about the User mailing list