Re: New and having problems
- Posted by _tom (admin) May 24, 2016
- 1629 views
- I tried to run you Ruby code, but with no luck.
- I compiled Ruby
$ ruby -v ruby 2.3.1p112 (2016-04-26 revision 54768) [i686-linux]
- result of running tux.rb
$ ./tux.rb ./tux.rb:59:in `<main>': undefined method `[]=' for nil:NilClass (NoMethodError)
Looking at your code, and trying some Euphoria code.
- file PklgMgrs.rb
This looks like database. The identifiers with a prefixed $ are global in scope.
The def is declaring a routine (procedure in this example).
I often nest stuff like this into one sequence.
#!/usr/bin/env ruby #Add your package managers here $fedoraBased="dnf" $rhelBased="yum" $debianBased="apt-get" $archLinux="pacman" $voidLinux="xbps-install" $sabayon="equo" $opensuse="zypper" #command binding methods for different package managers def dnf() $installCmd = "dnf install" $reinstallCmd = "dnf reinstall" $searchCmd = "dnf search" $updateCmd = "dnf upgrade" $syncCmd = "dnf --refresh check-update" $syncANDupdateCmd = "dnf --refresh upgrade" $refreshSyncCmd = "dnf --refresh check-update" $removeCmd = "dnf remove" $recursiveRemoveCmd = "dnf remove" $checkUpdatesCmd = "dnf check-update" $cleanCmd = "dnf autoremove" end def yum() $installCmd = "yum install" $reinstallCmd = "yum reinstall" $searchCmd = "yum search" $updateCmd = "yum update" $syncCmd = "yum check-update" $syncANDupdateCmd = "yum check-update; yum update" $refreshSyncCmd = "yum check-update" $removeCmd = "yum remove" $recursiveRemoveCmd = "yum remove" $checkUpdatesCmd = "yum check-update" $cleanCmd = "yum autoremove" end def pacman() $installCmd = "pacman -S" $reinstallCmd = "pacman -S --force" $searchCmd = "pacman -Ss" $updateCmd = "pacman -Su" $syncCmd = "pacman -Sy" $syncANDupdateCmd = "pacman -Syu" $refreshSyncCmd = "pacman -Syy" $removeCmd = "pacman -R" $recursiveRemoveCmd = "pacman -Rdd" $checkUpdatesCmd = "checkupdates" $cleanCmd = "pacman -Rsn $(pacman -Qdtq)" end def apt_get() $installCmd = "apt install" $reinstallCmd = "apt install --reinstall" $searchCmd = "apt search" $updateCmd = "apt upgrade" $syncCmd = "apt update" $syncANDupdateCmd = "apt update; sudo apt upgrade" $refreshSyncCmd = "apt update" $removeCmd = "apt remove" $recursiveRemoveCmd = "apt remove --purge" $cleanCmd = "apt autoremove" end def xbps_install() $installCmd = "xbps-install" $reinstallCmd = "xbps-install -f" $searchCmd = "xbps-query -Rs" $updateCmd = "xbps-install -u" $syncCmd = "xbps-install -S" $syncANDupdateCmd = "xbps-install -Su" $refreshSyncCmd = "xbps-install -f -S" $removeCmd = "xbps-remove" $recursiveRemoveCmd = "xbps-remove -f" $cleanCmd = "xbps-remove -O" end def equo()
In Euphoria sequences can be used in a variety of ways:
export enum installCmd, reinstallCmd, searchCmd, updateCmd, syncCmd, syncANDupdateCmd, refreshSyncCmd, removeCmd, recursiveRemoveCmd, cleanCmd -- I assume the same commands in the same order for all package managers -- you get a list of constants numbered starting with value one -- ( no need to capitalize a constant like in Ruby ) -- ( no need to use all caps as is the convention for some languages.) export sequence managers = { { "dnf", -- fedoraBased {"dnf install", "dnf reinstall", "dnf search", "dnf upgrade", "dnf --refresh check-update", "dnf --refresh upgrade", "dnf --refresh check-update", "dnf remove", "dnf remove", "dnf check-update", "dnf autoremove", } }, -- add more as needed { "apt_get", { "apt install" "apt install --reinstall" "apt search" "apt upgrade" "apt update" "apt update; sudo apt upgrade" "apt update" "apt remove" "apt remove --purge" "apt autoremove" } }, { "$sabayon="equo", -- saybayon { "equo i" "equo i" "equo s" "equo u" "equo up" "equo up && sudo equo u" "equo update --force" "equo rm" "equo rm --norecursive" "equo cleanup" } }, -- all items followed by , if you use $ end marker $ } -- the $ end marker lets you update a list easily -- the comment line double dash can go anywhere to the right of code -- now the database is in an isolated scope -- rather than make variables global, you "export" them to just the file -- that includes this database (result is in less name pollution.) -- you can name this file PkgMgrs.e -- the .e extension suggests that it is to be used as an include file -- (but, the extension is optional, or could be anything you choose) -- you execute "$ eui PkmMgrs.e" to test for typos -- again, the .e extension is not important -- every file that wants access to this database will include this file -- instead of making everything global
- which.rb
#!/usr/bin/env ruby # Cross-platform way of finding an executable in the $PATH. # # which('ruby') #=> /usr/bin/ruby def which(cmd) exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| exts.each { |ext| exe = File.join(path, "#{cmd}#{ext}") return exe if File.executable?(exe) && !File.directory?(exe) } end return nil end
Not going to try an figure out this Ruby code; instead just copied your code snippet.
using your first example
include std/filesys.e include std/io.e include std/console.e include PkgMgrs.ex export function PkIterate() object packmgr = 0 for i=1 to length(managers) do packmgr = locate_file( managers[i][1] ) if file_exists(packmgr) then return packmgr end if end for return 0 end function -- test this function --------------------------------------------------------------------- -- this file is intended to be an include file so it has no executable code -- however, I often tack on a few test lines to evaluate the routines -- then comment out when debugging is finished -- to test this code -- remove the /* */ multiline comments -- $ eui which.e object pkmgr = PkIterate() if atom( pkmgr ) then display( "\n no manager found" ) else display( "\nfound " & PkIterate() ) end if -- ok, this found apt-get on my Mint Linux
Now re-write this to use the suggestion by Irv Mullins:
-- use Irv Mullin's idea -- use file_type to evaluate your return value include std/filesys.e include std/io.e include std/console.e include PkgMgrs.ex export function PkIterate() for i=1 to length( managers ) do sequence pkmgr = locate_file( managers[i][1] ) if file_type( pkmgr ) = 1 then return i end if end for return 0 end function -- test this function --------------------------------------------------------------------- -- this file is intended to be an include file, so it has no executable code -- however, I often tack on a few test lines to evaluate the routines -- then comment out when debugging is finished object test = PkIterate() display( test ) display( managers[test][1] ) -- ok, apt-get is on my Mint Linux
Often we just remember that zero means false and not zero means true. Code is easier to understand if you define
constant FALSE = 0 constant TRUE = 1
(Usually too lazy to do this...)
This leaves you main file to decode. No time for this now.
The database file may require a re-write after I understand your main file. But, that is part of program development.
_tom