PreviousUpNext

15.4.820  src/lib/make-library-glue/opt-junk.pkg

## opt-junk.pkg
#
# Random utility code for use by (e.g.):
#
#     sh/opt
#
# This junk doesn't really belong in standard.lib,
# but that's the path of least resistance at the moment.

# Compiled by:
#     src/lib/std/standard.lib

stipulate
    package sm  =  string_map;                                          # string_map            is from   src/lib/src/string-map.pkg
herein
    api  Opt_Junk
    {
        print_strings:  List(String) -> Void;                           # Just what you think.

        find_available_opt_modules:  Void -> sm::Map(String);           
            #
            # Result maps
            #
            #     "gtk"    -> "src/opt/gtk"
            #     "opengl" -> "src/opt/opengl"
            #
            # and so on for the subdirectories of src/opt.

        validate_mythryl_directory: Void -> Void;
            #
            # Create   $HOME/.mythryl/   if it doesn't exist -- or die trying.  


        validate__selected_opt_modules__file: Void -> String;
            #
            # Create   $HOME/.mythryl/opt-selections   if it doesn't exist -- or die trying.    

        validate_opt_selections:  List(String) -> sm::Map(String)      -> (Void -> Void) -> List(String);
            #                      ============    ===============         ==============    =========================
            #                      glue selections available_glue_modules  usage             sorted, uniq'd selections.
            #
            # Given list of glue module selections and available_glue_modules map,
            # verify that all name available modules and return sorted duplicate-free
            # list of glue modules.  Treat ["all"] as being equivalent to list of all
            # available glue modules.
    };
end;

stipulate
    package lms =  list_mergesort;                              # list_mergesort        is from   src/lib/src/list-mergesort.pkg
    package psx =  posixlib;                                    # posixlib              is from   src/lib/std/src/psx/posixlib.pkg
    package sm  =  string_map;                                  # string_map            is from   src/lib/src/string-map.pkg
    package wnx =  winix;                                       # winix                 is from   src/lib/std/winix.pkg
    package paf =  patchfile;                                   # patchfile             is from   src/lib/make-library-glue/patchfile.pkg
    #
    getenv      =  wnx::process::get_env;
    mkdir       =  (\\ path = psx::mkdir (path, psx::s::flags [ psx::s::irwxu, psx::s::irgrp, psx::s::iwgrp, psx::s::iroth, psx::s::iwoth ]));          # XXX BUGGO FIXME somehow this is producing 744 instead of 755.
    #
#    fun die   message =        {   print message;      wnx::process::exit 1;   };
herein

    # This package is invoked in:
    #
    #     sh/opt
    #
    package  opt_junk
    :        Opt_Junk
    {

        fun print_strings  strings
            =
            {   print "[";
                print_strings' strings;
                print " ]";
            }
            where
                fun print_strings' []               =>  {   ();                                         };
                    print_strings' [ string ]       =>  {   printf " %s"  string;                               };
                    print_strings' ( string ! rest) =>  {   printf " %s," string;  print_strings' rest; };
                end;
            end;

        fun find_available_opt_modules ()
            =
            {   if (not (dir::is_directory "src"))       raise exception DIE "I see no src/ dir here -- sh/opt must be run from root install directory.";   fi;
                if (not (dir::is_directory "src/opt"))   raise exception DIE "I see no src/opt/ dir here -- sh/opt must be run from root install directory.";   fi;
                #

                # This will construct and return a map from keys
                # like "opengl" to paths like "src/opt/opengl":
                #
                list::fold_backward
                    (\\ (glue, tree) = sm::set (tree, glue, "src/opt" + "/" + glue))
                    sm::empty
                    (dir::directory_names "src/opt");
            };

        fun validate_mythryl_directory ()
            =
            case (getenv "HOME")
                #
                NULL    =>  raise exception DIE "Giving up because no HOME dir defined(?!)\n";
                #
                THE homedir =>  {   mythdir = homedir + "/" + ".mythryl";
                                    #
                                    if (not (dir::is_directory  mythdir))
                                        #
                                        mkdir mythdir   except _ = raise exception DIE (sprintf "Unable to create %s\n" mythdir);
                                    fi;
                                };
            esac;

        fun validate__selected_opt_modules__file ()
            =
            {   validate_mythryl_directory ();
                #
                selected_opt_modules__filename = (the (getenv "HOME")) + "/" + ".mythryl" + "/" + "selected-opt-modules";

                if (not (dir::is_file  selected_opt_modules__filename))
                    #
                    printf "Creating empty %s file\n" selected_opt_modules__filename;

                    file::from_lines selected_opt_modules__filename
                      [
                        "# This file contains the set of currently selected Mythryl library glue modules.\n",
                        "# This file is automatically created and maintained by sh/opt;\n",
                        "# manual modification is inadvisable.\n",
                        "#" + (paf::make_patch_beginline { patchname => "modules" }) + "\n",
                        "#" + (paf::make_patch_endline   { patchname => "modules" }) + "\n"
                      ];

                    if (not (dir::is_file selected_opt_modules__filename))
                        raise exception DIE (sprintf "Failed to create empty %s file\n" selected_opt_modules__filename);
                    else
                        printf "Created empty %s file\n"  selected_opt_modules__filename;
                    fi;
                fi;

                selected_opt_modules__filename;
            };



        fun validate_opt_selections  selections  available_opt_modules  usage
            =
            {   selections = (selections == ["all"])  ??  sm::keys_list  available_opt_modules          # Expand ["all"] into list of all available glue modules.
                                                      ::  selections;

                selections =  lms::sort_list_and_drop_duplicates  string::compare  selections;          # Sort and drop duplicates.

                apply  validate_selection  selections;                                                  # Verify that every selection corresponds to an available module.

                selections;                                                                             # Return sorted uniq'd selection list.
            }
            where
                fun validate_selection  selection
                    =
                    if (not (sm::contains_key (available_opt_modules, selection)))
                        printf "Invalid selection: %s is not a subdirectory of src/opt\n" selection;
                        usage();
                    fi;
            end;
    };
end;


## Code by Jeff Prothero: Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext