[Gambas-user] Desktop.InstallPackage() ?
Bruce Steers
bsteers4 at gmail.com
Fri Feb 4 18:16:02 CET 2022
I added a function called Install() to a version of gb.desktop i have
imported into an app.
Could be a nice addition to the Desktop.class I thought?
Detects if user has apt-get, dnf, zypper, pacman or apk (could add more)
has a NoConfirm arg to use the -y or --noconfirm args of the installers.
Downside is it uses TerminalView so the user must have
gambas3-gb-form-terminal installed (it is auto-loaded so no need to add
TerminalView to your application)
one or more package names can be given.
uses su -c not sudo
function returns 0 if all okay or an error code if install fails/is
cancelled
Any thoughts from any others on a function like this?
All the best
BruceS
Here's the code, someone may find it useful anyway...
'' Install a package (or packages) using the system installer, Supports
systems with...
'' * apt-get
'' * zypper
'' * pacman
'' * dnf
'' * apk
'' <p>
'' Requires gb.form.terminal is installed but does not need adding to your
application as will self-load.\
'' Opens a TerminalView window and uses "su -c" to run the systems install
command with the supplied ~PackageNames~
''
'' Optional Title can be given for window title.
''
'' Optional **~NoConfirm~** argument will suppress installation
confirmation.
Static Public Sub Install(PackageNames As String, Optional Title As String,
Optional NoConfirm As Boolean) As Integer
Dim iInstaller As Integer
Dim sInstallers As String[] = ["apt-get install", "zypper install",
"pacman -S --needed", "apk add", "dnf install"]
Dim sInstallerQuiet As String[] = ["-y", "-y", "--noconfirm",
"--no-progress -q", "-y"]
For iInstaller = 0 To sInstallers.Max
If System.Exist(Split(sInstallers[iInstaller], " ")[0]) Then Break
Inc iInstaller
Next
If iInstaller > sInstallers.Max Then Error.Raise("Sorry, no supported
installer was found...\nSupports: apt-get, zypper, dnf, pacman, apk")
If NoConfirm Then sInstallers[iInstaller] &= " " &
sInstallerQuiet[iInstaller]
sInstallers[iInstaller] &= " " & PackageNames
Dim aCmd As String[]
aCmd = ["su", "-c", sInstallers[iInstaller]]
Dim fWin As Form
With fWin = New Form As "TViewWin"
.Width = Screen.Width - 100
.TakeFocus = True
.Background = Color.Black
.Foreground = Color.White
.Arrangement = Arrange.Vertical
.Spacing = True
.Margin = True
If Title Then fWin.Title = Title
.Left = (Screen.Width - .Width) / 2
' .Center
End With
Dim hLabel As TextLabel
With hLabel = New TextLabel(fWin)
.AutoResize = True
.Border = Border.Sunken
.Padding = 5
.Wrap = True
.Text = "<i>Installing<font color=green> " & PackageNames &
"</font><br>With command:</i> " & aCmd.Join(" ")
fWin.Height = .Font.RichTextHeight(.Text, .Width) + 200
End With
Try Component.Load("gb.form.terminal")
If Error Then Error.Raise("gambas3-gb-form-termial must be installed for
this installer")
Dim tv As Object = Object.New("TerminalView", [fWin])
With tv
.Expand = True
.Blink = True
.Background = Color.Black
.Foreground = Color.White
End With
hLabel.Font = tv.Font
Object.Attach(tv, Me, "TView")
Object.Attach(fWin, Me, "TViewWin")
Dim pProc As Process
fWin.Show
tv.SetFocus
pProc = tv.Exec(aCmd)
While pProc.State = Process.Running
If Not Object.IsValid(tv) Then
Try pProc.Close
pProc = Null
Return -1
Endif
Wait 0.1
Wend
Try pProc.Close
pProc = Null
Try fWin.Close
Return Process.LastValue
Catch
Message(Error.Text)
Return Error.Code
End
Public Sub TViewWin_Close()
Dim o As Object = Last.Children[1]
Try o.Kill
Try o.Delete
End
Public Sub TView_KeyPress()
If Key.Control And If Key.Code = Key["C"] Then
Try Last.CloseInput
Endif
End
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20220204/9b1aacce/attachment-0001.htm>
More information about the User
mailing list