| « An Overview Of Package Management | Factor Package Manager » |
Factor Package Manager Preview
Work has been keeping me quite busy lately, but I use my spare tie to write a package manager for Factor. This post provides an extremely early preview of the code so far. The following code is just for the
pkg-install command. Currently implemented is a really bare-bones pkg-install command that will be used to install a package. Any comments / suggestions are welcome
(as always).
USING: kernel http.client system sequences splitting io io.files io.launcher
crypto.md5 ;
IN: packages
<PRIVATE
: factor-base-dir ( -- path )
vm "/" last-split1 drop ;
: base-dir ( -- path )
! push the full path of the package/ dir onto the stack
factor-base-dir "/extra/packages" append ;
: pkg-download ( url -- ? )
! download a package from a remote repo (using the specified URL) to the
! local package cache
dup download-name base-dir "/data/pkg-cache/" rot 3append
download-to
t ;
: pkg-in-cache? ( name -- ? )
! check if a package with the specified name exists in the local package
! cache
base-dir "/data/pkg-cache" append directory
[ first over ".fpkg" append = ] contains? nip ;
: registry-to-lines ( -- seq )
! converts the pkg-registry file to a sequence of lines so it can be
! read / parsed
base-dir "/data/pkg-registry" append <file-reader> lines ;
: pkg-installed? ( name -- ? )
! check if a package is installed by reading the pkg-registry file
registry-to-lines [ over = ] contains? nip ;
: pkg-extract ( name -- ? )
! extracts the files from the package tarball into the data/tmp/ dir
cwd >r
"tar -xjf " base-dir "/data/pkg-cache/" 3append swap ".fpkg" 3append
base-dir "/data/tmp" append cd
run-process drop r> cd
t ;
: (pkg-valid?) ( path -- ? )
cd cwd directory
[
dup second
[ first (pkg-valid?) ".." cd ]
[ first cwd "/" rot 3append dup file>md5str " :: " rot 3append print ] if
] each ;
: pkg-valid? ( name -- ? )
! verify the integrity of the package and return the result
cwd swap base-dir "/data/tmp/" rot 3append (pkg-valid?) cd t ;
: (pkg-install) ( name -- )
! moves the extracted package files to the extra/ dir in the local Factor
! installation
dup base-dir "/data/tmp/" rot 3append dup
rot factor-base-dir "/extra/" rot 3append
copy-tree delete-tree ;
PRIVATE>
: pkg-install ( name -- )
dup pkg-installed? [
">> The package is already installed.\n" write
] [
! sync the local & remote index for the repo
! check the cached repo index for the latest version of the package
! check if that version is cached in the local package cache
! check dependencies of the package to install
! Prompt user if he would like to continue installation
"Would you like to continue the install? [y/n]: " write readln
"n" = [
">> Nothing was installed" print
] [
! download the required packages from the remote repo
">> downloading required packages" print
dup "http://zenhackers.net/files/" swap append ".fpkg" append pkg-download
drop ! drop result of pkg-download word, we should use this result to test for errors
! extract files from packages into their separate directories
">> extracting packages to temporary directory" print
dup pkg-extract drop
! verify integrity of downloaded packages using checksums
">> verifying integrity of packages" print
dup pkg-valid? [
! install the package in the extra/ directory
">> installing packages" print
(pkg-install)
">> install completed" print
] [
">> The package integrity check FAILED !!!" print
] if
] if
] if ;