1. tutorial// Ruby to Euphoria -- Example on Wiki

My tutorial is too long for a Forum post.

In three parts starting at

http://openeuphoria.org/wiki/view/Ruby%20to%20Euphoria.wc

is a version of Tux (originally by (c) silvernode written in Ruby) as a first draft of a Euphoria version

_tom

new topic     » topic index » view message » categorize

2. Re: tutorial// Ruby to Euphoria -- Example on Wiki

_tom said...

My tutorial

Some quick comments and ideas, several of which may be nonsense:

I took exception to the use of "database". I would replace that word with "definitions" throughout.
Replace file_type()=1 with file_type()=FILETYPE_FILE
When I saw managers[i][1], my immediate thought was to replace the 1 with a more descriptive constant/enum declared in with the Package Manager Definitions...
Looking at options and the definitions, there is an anonymous and invisible correspondence which rang a few alarm bells.
It is a bit like a police raid on a seedy motel: look round and half the shirt buttons are done up all wrong.
First off, I'd move options in with the definitions.
Then I'd seriously consider (it might not work, I haven't thought it through) making it a "help" entry in the main table[1] (skipped in the locate_file scan).
Then I'd add an enum or seven and break all the declarations up into discrete assignments based on those symbolic constants, eg

managers[HELP][DESC] = "help" 
managers[KELP][COMMANDS][REINSTALL] = {"ri", "ri", "reinstall"} 
managers[DNF][DESC] = "dnf" 
managers[DNF][COMMANDS][REINSTALL] = "dnf reinstall" 

Of course much more typing, but hopefully a less brittle design, with far less of that misaligned shirt buttons thing.
Also, I'd move which() into the definitions file as well, and I'd ask whether all the data in PkgMgrs.e should be private, we only need an index outside it?
I also spotted options[i][2..$] used a couple of times, and decided that should really be a [named] subsequence.

I think one of the big differences here between Ruby and Euphoria is that given a "def dnf()" then (I assume) send("dnf") [yes, a text string] invokes that routine. Obviously that is not really any different to routine_id().

HTH, Pete

new topic     » goto parent     » topic index » view message » categorize

3. Re: tutorial// Ruby to Euphoria -- Example on Wiki

petelomax said...

Some quick comments and ideas, ...

HTH, Pete

Thanks Pete, these are all good ideas that I should be using.


Version 2 of the Tux Installer

  • starting to apply some of Pete's ideas

(1) The options sequence starts out as a LibreOffice spreadsheet and is saved as a .csv file.

"Help","h, --help","fedoraBased","rehelBased","debianBased","archLinux","voidLinux","sabayon" 
"Show Current Version","-v, --version","dnf","yum","apt-get","pacman","xbps-install","equo" 
"Install","i, -i, add, get, -S, install","dnf install","yum install","apt install","packman -S","xbps-install","equo i" 
"Reinstall","ri, -ri, reinstall","dnf reinstall","yum reinstall","apt install –reinstall","packman -S --force","xbps-install -f","equo i" 
"Search","s, -s, se, -Ss, search, find","dnf search","yum search","apt search","packman -Ss","xbps-query -Rs","equo s" 
"Update","u, up, -u, -Su, upgrade","dnf upgrade","yum update","apt upgrade","packman -Su","xbps-install -u","equo u" 
"Sync","sy, -Sy, sync, refresh ","dnf --refresh check-update","yum check-update","apt update","packman -Sy","xbps-install -S","equo up" 
"Sync and Update","su, -Syu, sup, syncup","dnf --refresh upgrade","yum check-update; yum update","apt update; sudo apt upgrade","packman -Syu","xbps-install -Su ","equo up && sudo equo u" 
"Remove","r, -R, rm, remove, delete","dnf install","yum remove","apt remove","packman -R","xbps-remove","equo rm" 
"Recursive Remove","-rf. -Rdd, force-remove","dnf install","yum remove","apt remove –purge","packman -Rdd","xbps-remove -f","equo rm –norecursive" 
"Check Updates","su, -Syu, sup, syncup","dnf check-update","yum check-update"," ","packman checkupdates"," "," " 
"Clean","c, -c, clean","dnf autoremove","yum autoremove","apt autoremove","packman -Rsn $(packman -Qdtq)","xbps-remove -O","equo cleanup" 

Edit this file to add more file managers and adjust the options arguments.

Putting everything into one file should solve the brittle aspect of version one.

(2)

Launch this utility to convert the .csv to a Euphoria include file:

include std/io.e 
 
sequence raw = read_lines( "options.csv" ) 
 
for line=1 to length(raw) do 
    raw[line] = "{ " & raw[line] & "}," 
end for 
 
raw = prepend(raw, "export sequence options = {" ) 
raw = append(raw, "$}" ) 
 
write_lines( "options.e", raw ) 
 
printf(1, "done" ) 

(3)

Launch main.ex to test the program:

include std/filesys.e 
include options.e 
clear_screen() 
 
procedure version() 
    if equal(mgr_index, 0) then 
        printf(1, "Can not identify your package manager.\n" ) 
    else 
        printf(1, "\nYou have the %s package manager installed.\n", {mgr_name} ) 
    end if 
    printf(1, "This is euTux 1.0\nplease see orignal Tux (c) by silverstone" ) 
end procedure 
     
procedure usage() 
    integer HELP_MSG = 1, HELP_LIST = 2 
    printf(1, "Usage : tux [options] [arguments]\n\n" ) 
    for arg=1 to length(options) do 
        printf(1, "%30s : %s\n", { options[arg][HELP_MSG], options[arg][HELP_LIST] } ) 
    end for 
    version() 
end procedure 
 
-- identify installed package manager 
    sequence mgr_list = options[2]  -- managers are listed in line two 
    sequence mgr_name = "" 
    integer mgr_index = 0 
     
    for mgr = 3  to length(mgr_list) do 
        sequence pkmgr = locate_file( mgr_list[mgr] ) 
        if file_type( pkmgr ) = FILETYPE_FILE then 
            mgr_index = mgr 
            mgr_name = options[2][mgr_index] 
            exit 
        end if 
    end for 
 
sequence cmd = command_line() 
integer cmd_count = length(cmd) 
if cmd_count < 3 then 
    usage() 
elsif cmd_count = 3 then 
    if find( cmd[3], { "h", "--help" } ) then 
        usage() 
    end if 
    if find( cmd[3], { "v", "--version" } ) then 
        version() 
    end if 
elsif cmd_count = 4 then 
    for i = 1 to length(options) do 
        if match( cmd[3], options[i][2] ) then 
            sequence action = options[i][mgr_index] & " " & cmd[4] 
            system( action ) 
            exit 
        end if 
    end for 
end if 
 
printf(1, "\n\ndone" ) 

(4) Compile with euc.


I tend to develop programs by removing code until the program works.

_tom

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu