zilch/lang/go/src/package.sld
Puck Meerburg fc8aea8fb4 (zilch lang go): introduce proper vfs record
This should hopefully make the code a bit clearer, and is preparation
for Rust support.
2024-11-01 00:12:04 +00:00

33 lines
1.4 KiB
Scheme

(define-library (zilch lang go package)
(import
(scheme base)
(zilch file) (zilch magic)
(zilch nixpkgs) (zilch zexpr)
json
(chicken foreign)
(zilch lang go core) (zilch lang go) (zilch lang go vfs) (zilch lang go stdlib))
(export find-packages-inside-vfs)
(begin
(foreign-declare "#include \"parser_source.h\"")
(define go-import-parser
(go-package-link
(go-package-compile "main"
(map go-stdlib-ref '("encoding/json" "fmt" "go/build" "io" "io/fs" "os" "path" "path/filepath" "sort" "strings" "time"))
(list (cons "main.go" (zfile (foreign-value "parser_source" nonnull-c-string)))))))
;; Uses IFD to find each Go package defined inside this virtual filesystem,
;; returning a vector containing pairs, mapping each directory to the
;; package defined within.
(define (find-packages-inside-vfs vfs)
(define input
#~,(call-with-port
(open-output-bytevector)
(lambda (bv)
(json-write (vector (cons "GOARCH" (%goarch)) (cons "GOOS" "linux") (cons "files" #$(vfs-to-json (vfs-filter-for-go-package vfs)))) bv)
(get-output-bytevector bv))))
(define input-file (zfile input))
(define store-path (cdar (store-path-for-ca-drv* "find-packages" "x86_64-linux" #~(#$go-import-parser #$input-file) '() '("out"))))
(call-with-port (store-path-open store-path)
(lambda (p) (json-read p))))))