zilch/lang/go/src/package.sld

36 lines
1.6 KiB
Text
Raw Normal View History

;; Routines to locate Go packages in a `<vfs>`.
2024-10-03 23:57:22 +00:00
(define-library (zilch lang go package)
(import
(scheme base)
(zilch file) (zilch magic)
(zilch nixpkgs) (zilch zexpr)
(zilch vfs)
2024-10-03 23:57:22 +00:00
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)))))))
;; Finds each Go package defined inside this virtual filesystem,
;; and returns a vector containing pairs, mapping the name of each directory to the
;; a vector-based structure describing the package defined in said directory.
2024-10-03 23:57:22 +00:00
(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)
2024-10-03 23:57:22 +00:00
(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"))))
2024-10-03 23:57:22 +00:00
(call-with-port (store-path-open store-path)
(lambda (p) (json-read p))))))