Paste: platform sections in action

Author: erg
Mode: factor
Date: Sat, 22 Jan 2022 00:03:32
Plain Text |
! Copyright (C) 2011 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: combinators io.pathnames kernel sequences system
vocabs vocabs.platforms ;
IN: io.standard-paths

HOOK: find-native-bundle os ( string -- path )

HOOK: find-in-path* os ( string -- path/f )

HOOK: find-in-applications os ( directories filename -- path )

HOOK: find-in-standard-login-path* os ( string -- path/f )

M: object find-in-standard-login-path*
    find-in-path* ;

: find-in-path ( string -- path/f )
    [ f ] [
        [ find-in-path* ] keep over
        [ append-path ] [ 2drop f ] if
    ] if-empty ;

: ?find-in-path ( string -- path/string )
    [ find-in-path ] [ or ] bi ;

: find-in-standard-login-path ( string -- path/f )
    [ f ] [
        [ find-in-standard-login-path* ] keep over
        [ append-path ] [ 2drop f ] if
    ] if-empty ;

<UNIX

USING: accessors ascii environment io io.encodings.binary
io.encodings.string io.encodings.utf8 io.files io.launcher
io.pathnames io.standard-paths kernel math sequences splitting
system unix.users ;
IN: io.standard-paths.unix

M: unix find-in-path*
    [ "PATH" os-env ":" split ] dip
    '[ _ append-path file-exists? ] find nip ;

! iterm2 spews some terminal info on every bash command.
: parse-login-paths ( seq -- strings )
    dup [ 7 = ] find-last drop [ 1 + tail-slice ] when*
    utf8 decode [ blank? ] trim ":" split ;

: standard-login-paths ( -- strings )
    { "-l" "-c" "echo \"$PATH\"" }
    effective-user-id user-passwd shell>> prefix
    binary <process-reader> stream-contents parse-login-paths ;

M: unix find-in-standard-login-path*
    [ standard-login-paths ] dip '[ _ append-path file-exists? ] find nip ;
UNIX>

<MACOSX

USING: core-foundation.launch-services io.standard-paths
system ;
IN: io.standard-paths.macosx

M: macosx find-native-bundle launch-services-path ;

MACOSX>

<WINDOWS

USING: arrays combinators.smart environment fry
io.directories io.files io.pathnames io.standard-paths
kernel sequences sets splitting system unicode windows.shell32 ;
IN: io.standard-paths.windows

: program-files-directories ( -- array )
    [
        program-files
        program-files-x86
        "ProgramW6432" os-env
        "LOCALAPPDATA" os-env "Programs" append-path
    ] output>array harvest members ; inline

: find-in-program-files ( base-directory quot -- path )
    [ program-files-directories ]
    [ '[ _ append-path ] map ]
    [ find-file-in-directories ] tri* ; inline

M: windows find-in-applications
    >lower
    '[ [ >lower _ tail? ] find-in-program-files ] map-find drop ;

M: windows find-in-path*
    [ "PATH" os-env ";" split ] dip
    '[ _ append-path file-exists? ] find nip ;

WINDOWS>

New Annotation

Summary:
Author:
Mode:
Body: