Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/liii/pi/app/cli/help.scm
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@
"Usage:\n"
" gf pi [options] [--] [@files...] [messages...]\n"
" gf pi auth print-api-key --provider <provider>\n"
" gf pi install [--local|-l] [--force|-f] <file.scm | dir>\n"
" Install extension (single .scm file or package dir with gfcode/<name>/index.scm)\n"
" gf pi install [--local|-l] [--force|-f] <file.scm | dir | url>\n"
" Install extension (.scm file, package dir, or git url)\n"
" gf pi uninstall [--local|-l] <name> Uninstall extension\n"
" gf pi link [--local|-l] <dir> Link local extension dir for development\n"
"\n"
Expand Down
4 changes: 2 additions & 2 deletions app/liii/pi/app/cli/impl.scm
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@

(define (%handle-install-command args)
(%exec-subcmd (%parse-extension-subcmd-args args "install"
"a .scm file or package directory"
"gf pi install [--local|-l] [--force|-f] <file.scm | dir>"
"a .scm file, package directory, or git url"
"gf pi install [--local|-l] [--force|-f] <file.scm | dir | url>"
#t
) ;%parse-extension-subcmd-args
(lambda (p) (pi-install-extension (caddr p) :local? (car p) :force? (cadr p)))
Expand Down
117 changes: 117 additions & 0 deletions app/tests/liii/pi/app/cli/impl/run-pi-test.scm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
(liii path)
(liii os)
(liii shutil)
(liii subprocess)
(liii either)
(liii pi code core failure)
(liii pi app cli impl)
) ;import

Expand Down Expand Up @@ -269,4 +272,118 @@
) ;dynamic-wind
) ;let*

;; 8. install -l 远程仓库完整流程(本地裸仓库模拟远程 URL)
(let* ((temp-dir
(path-join (path-temp-dir)
(string-append "gf-cli-install-remote-" (number->string (getpid)))
) ;path-join
) ;temp-dir
(work-dir (path-join temp-dir "work"))
(work-dir-str (path->string work-dir))
(bare-dir-str (path->string (path-join temp-dir "remote.git")))
(remote-url (string-append "file://" bare-dir-str))
(bad-url (string-append "file://" (path->string (path-join temp-dir "no-such.git")))
) ;bad-url
(pkg-dir (path-join work-dir "gfcode" "simplify"))
(orig-cwd (getcwd))
) ;
(when (path-exists? temp-dir)
(rmtree (path->string temp-dir) #t)
) ;when
(path-mkdir pkg-dir :parents #t :exist-ok #t)
(path-write-text (path-join pkg-dir "index.scm")
"(define-library (gfcode simplify index) (import (scheme base)) (export extension) (begin (define (extension api) 'simplify)))\n"
) ;path-write-text
(define (git-ok? args)
(let ((res (run-either (cons "git" args)
:cwd work-dir-str
:timeout 30
:stdout 'capture
:stderr 'capture
) ;run-either
) ;res
) ;
(if (either-right? res)
#t
(error 'test-setup
(string-append "git setup failed: " (pi-command-failure-detail (to-left res)))
) ;error
) ;if
) ;let
) ;define
(git-ok? '("init"))
(git-ok? '("config" "user.email" "test@gf.local"))
(git-ok? '("config" "user.name" "gf-test"))
(git-ok? '("add" "-A"))
(git-ok? '("commit" "-m" "init"))
(git-ok? (list "clone" "--bare" work-dir-str bare-dir-str))

(chdir work-dir-str)
(dynamic-wind (lambda () #f)
(lambda ()
;; 首次局部远程仓库安装成功 (0)
(let ((status #f))
(let ((out
(with-output-to-string
(lambda () (set! status (run-pi (list "gf" "pi" "install" "-l" remote-url))))
) ;with-output-to-string
) ;out
) ;
(check status => 0)
(check-true (string-contains? out "Installed extension to "))
(check-true (path-exists? (path-join work-dir ".gf" "gfcode" "simplify" "index.scm"))
) ;check-true
) ;let
) ;let

;; 未带 -f 重复安装失败 (1)
(let ((status #f))
(let ((p (open-output-string)))
(let ((orig *stderr*))
(dynamic-wind (lambda () (set! *stderr* p))
(lambda () (set! status (run-pi (list "gf" "pi" "install" "-l" remote-url))))
(lambda () (set! *stderr* orig))
) ;dynamic-wind
) ;let
) ;let
(check status => 1)
) ;let

;; 局部卸载目录成功 (0)
(let ((status #f))
(let ((out
(with-output-to-string
(lambda () (set! status (run-pi '("gf" "pi" "uninstall" "-l"
"simplify"))))
) ;with-output-to-string
) ;out
) ;
(check status => 0)
(check-true (string-contains? out "Uninstalled extension from "))
(check-false (path-exists? (path-join work-dir ".gf" "gfcode" "simplify")))
) ;let
) ;let

;; 克隆失败退出码 1
(let ((status #f))
(let ((p (open-output-string)))
(let ((orig *stderr*))
(dynamic-wind (lambda () (set! *stderr* p))
(lambda () (set! status (run-pi (list "gf" "pi" "install" "-l" bad-url))))
(lambda () (set! *stderr* orig))
) ;dynamic-wind
) ;let
) ;let
(check status => 1)
) ;let
) ;lambda
(lambda ()
(chdir orig-cwd)
(when (path-exists? temp-dir)
(rmtree (path->string temp-dir) #t)
) ;when
) ;lambda
) ;dynamic-wind
) ;let*

(check-report)
221 changes: 142 additions & 79 deletions code/liii/pi/code/core/extensions/install.scm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
(define-library (liii pi code core extensions install)
(import (scheme base)
(scheme char)
(scheme time)
(liii string)
(liii list)
(liii path)
Expand Down Expand Up @@ -210,90 +211,152 @@
) ;for-each
) ;define

;; 远程仓库 URL 判定:http(s)://、git@host:path 或 *.git。
;; 其余一律视为本地路径,避免与本地名为 "foo.git" 的目录冲突。
(define (%git-remote-url? s)
(and (string? s)
(or (string-starts? s "http://")
(string-starts? s "https://")
(string-starts? s "git@")
(string-ends? s ".git")
) ;or
) ;and
) ;define

;; 浅克隆远程仓库到唯一临时目录,返回克隆后目录路径字符串。
;; 失败时抛 install-error(含 git 诊断文本)。
(define (%git-clone url)
(let* ((tag (string-append "gf-clone-"
(number->string (getpid))
"-"
(number->string (current-jiffy))
) ;string-append
) ;tag
(dest (path->string (path-join (path-temp-dir) tag)))
) ;
(when (path-exists? (path dest))
(rmtree dest #t)
) ;when
(path-mkdir (path dest) :parents #t :exist-ok #t)
(let ((res (run-either (list "git" "clone" "--depth" "1" url dest)
:timeout 120
:stdout 'capture
:stderr 'capture
) ;run-either
) ;res
) ;
(unless (either-right? res)
(rmtree dest #t)
(error 'install-error
(string-append "git clone failed: " (pi-command-failure-detail (to-left res)))
) ;error
) ;unless
dest
) ;let
) ;let*
) ;define

;; 安装多文件包扩展:src-path 指向已存在的包目录(含 gfcode/<name>/index.scm)。
;; 成功返回安装目标路径字符串。
(define (%install-package-dir src-path src-p opts)
(let ((resolved (%resolve-package-source src-path)))
(unless resolved
(value-error (string-append "Source package directory has no gfcode/<name>/index.scm: "
src-path
) ;string-append
) ;value-error
) ;unless
(let* ((name (car resolved))
(src-dir (path (cdr resolved)))
(target (%link-target name opts))
(target-p (path target))
(force? (pi-keyword-value :force? opts #f))
) ;
(when (string=? (path->string (path-resolve src-dir))
(path->string (path-resolve target-p))
) ;string=?
(value-error (string-append "Source and target are the same directory: " target)
) ;value-error
) ;when
(when (and (path-exists? target-p) (not force?))
(value-error (string-append "Extension already exists: "
target
" (use --force to overwrite)"
) ;string-append
) ;value-error
) ;when
(when (and force? (path-exists? target-p))
(rmtree (path->string target-p) #t)
) ;when
(unless (path-exists? (path-parent target-p))
(path-mkdir (path-parent target-p) :parents #t :exist-ok #t)
) ;unless
(%copytree-rec src-dir target-p)
target
) ;let*
) ;let
) ;define

(define (pi-install-extension src-path . opts)
(unless (string? src-path)
(type-error "pi-install-extension: path must be a string")
) ;unless
(let ((src-p (path src-path)))
(unless (path-exists? src-p)
(value-error (string-append "Source not found: " src-path))
) ;unless
(cond
;; 单文件扩展:源须为 .scm 文件
((path-file? src-p)
(unless (string-ends? src-path ".scm")
(value-error (string-append "Source extension must be a .scm file: " src-path))
) ;unless
(let* ((name (path-stem src-p))
(target (apply pi-resolve-install-target name opts))
(target-p (path target))
(force? (pi-keyword-value :force? opts #f))
) ;
(when (string=? (path->string (path-resolve src-p))
(path->string (path-resolve target-p))
) ;string=?
(value-error (string-append "Source and target are the same file: " target))
) ;when
(when (and (path-exists? target-p) (not force?))
(value-error (string-append "Extension already exists: "
target
" (use --force to overwrite)"
) ;string-append
) ;value-error
) ;when
(unless (path-exists? (path-parent target-p))
(path-mkdir (path-parent target-p) :parents #t :exist-ok #t)
) ;unless
(unless (path-copy src-p target-p)
(error 'install-error (string-append "Failed to copy extension to " target))
) ;unless
target
) ;let*
) ;
;; 多文件包扩展:源目录须含 gfcode/<name>/index.scm
((path-dir? src-p)
(let ((resolved (%resolve-package-source src-path)))
(unless resolved
(value-error (string-append "Source package directory has no gfcode/<name>/index.scm: "
src-path
) ;string-append
) ;value-error
) ;unless
(let* ((name (car resolved))
(src-dir (path (cdr resolved)))
(target (%link-target name opts))
(target-p (path target))
(force? (pi-keyword-value :force? opts #f))
) ;
(when (string=? (path->string (path-resolve src-dir))
(path->string (path-resolve target-p))
) ;string=?
(value-error (string-append "Source and target are the same directory: " target)
) ;value-error
) ;when
(when (and (path-exists? target-p) (not force?))
(value-error (string-append "Extension already exists: "
target
" (use --force to overwrite)"
) ;string-append
) ;value-error
) ;when
(when (and force? (path-exists? target-p))
(rmtree (path->string target-p) #t)
) ;when
(unless (path-exists? (path-parent target-p))
(path-mkdir (path-parent target-p) :parents #t :exist-ok #t)
(cond
;; 远程仓库:浅克隆到临时目录后走包目录安装
((%git-remote-url? src-path)
(let ((clone-dir (%git-clone src-path)))
(dynamic-wind (lambda () #f)
(lambda () (%install-package-dir clone-dir (path clone-dir) opts))
(lambda () (when (path-exists? (path clone-dir)) (rmtree clone-dir #t)))
) ;dynamic-wind
) ;let
) ;
(else
(let ((src-p (path src-path)))
(unless (path-exists? src-p)
(value-error (string-append "Source not found: " src-path))
) ;unless
(cond
;; 单文件扩展:源须为 .scm 文件
((path-file? src-p)
(unless (string-ends? src-path ".scm")
(value-error (string-append "Source extension must be a .scm file: " src-path))
) ;unless
(%copytree-rec src-dir target-p)
target
) ;let*
) ;let
) ;
(else (value-error (string-append "Source is neither a file nor a directory: " src-path)
) ;value-error
) ;else
) ;cond
) ;let
(let* ((name (path-stem src-p))
(target (apply pi-resolve-install-target name opts))
(target-p (path target))
(force? (pi-keyword-value :force? opts #f))
) ;
(when (string=? (path->string (path-resolve src-p))
(path->string (path-resolve target-p))
) ;string=?
(value-error (string-append "Source and target are the same file: " target))
) ;when
(when (and (path-exists? target-p) (not force?))
(value-error (string-append "Extension already exists: "
target
" (use --force to overwrite)"
) ;string-append
) ;value-error
) ;when
(unless (path-exists? (path-parent target-p))
(path-mkdir (path-parent target-p) :parents #t :exist-ok #t)
) ;unless
(unless (path-copy src-p target-p)
(error 'install-error (string-append "Failed to copy extension to " target))
) ;unless
target
) ;let*
) ;
;; 多文件包扩展:源目录须含 gfcode/<name>/index.scm
((path-dir? src-p) (%install-package-dir src-path src-p opts))
(else (value-error (string-append "Source is neither a file nor a directory: " src-path)
) ;value-error
) ;else
) ;cond
) ;let
) ;else
) ;cond
) ;define

(define (pi-uninstall-extension name-or-path . opts)
Expand Down
Loading
Loading