gexp: Add 'scheme-file'.

* guix/gexp.scm (<scheme-file>): New record type.
  (scheme-file, scheme-file-compiler): New procedures.
* tests/gexp.scm ("scheme-file"): New test.
* doc/guix.texi (G-Expressions): Document 'scheme-file'.
This commit is contained in:
Ludovic Courtès 2015-09-16 15:03:52 +02:00
parent b893f1aec3
commit e1c153e0ab
3 changed files with 48 additions and 4 deletions

View file

@ -3345,10 +3345,10 @@ The other arguments are as for @code{derivation} (@pxref{Derivations}).
@end deffn @end deffn
@cindex file-like objects @cindex file-like objects
The @code{local-file}, @code{plain-file}, @code{computed-file}, and The @code{local-file}, @code{plain-file}, @code{computed-file},
@code{program-file} procedures below return @dfn{file-like objects}. @code{program-file}, and @code{scheme-file} procedures below return
That is, when unquoted in a G-expression, these objects lead to a file @dfn{file-like objects}. That is, when unquoted in a G-expression,
in the store. Consider this G-expression: these objects lead to a file in the store. Consider this G-expression:
@example @example
#~(system* (string-append #$glibc "/sbin/nscd") "-f" #~(system* (string-append #$glibc "/sbin/nscd") "-f"
@ -3437,6 +3437,13 @@ The resulting file holds references to all the dependencies of @var{exp}
or a subset thereof. or a subset thereof.
@end deffn @end deffn
@deffn {Scheme Procedure} scheme-file @var{name} @var{exp}
Return an object representing the Scheme file @var{name} that contains
@var{exp}.
This is the declarative counterpart of @code{gexp->file}.
@end deffn
@deffn {Monadic Procedure} text-file* @var{name} @var{text} @dots{} @deffn {Monadic Procedure} text-file* @var{name} @var{text} @dots{}
Return as a monadic value a derivation that builds a text file Return as a monadic value a derivation that builds a text file
containing all of @var{text}. @var{text} may list, in addition to containing all of @var{text}. @var{text} may list, in addition to

View file

@ -57,6 +57,11 @@ (define-module (guix gexp)
program-file-modules program-file-modules
program-file-guile program-file-guile
scheme-file
scheme-file?
scheme-file-name
scheme-file-gexp
gexp->derivation gexp->derivation
gexp->file gexp->file
gexp->script gexp->script
@ -281,6 +286,25 @@ (define-gexp-compiler (program-file-compiler (file program-file?)
#:modules modules #:modules modules
#:guile (or guile (default-guile)))))) #:guile (or guile (default-guile))))))
(define-record-type <scheme-file>
(%scheme-file name gexp)
scheme-file?
(name scheme-file-name) ;string
(gexp scheme-file-gexp)) ;gexp
(define* (scheme-file name gexp)
"Return an object representing the Scheme file NAME that contains GEXP.
This is the declarative counterpart of 'gexp->file'."
(%scheme-file name gexp))
(define-gexp-compiler (scheme-file-compiler (file scheme-file?)
system target)
;; Compile FILE by returning a derivation that builds the file.
(match file
(($ <scheme-file> name gexp)
(gexp->file name gexp))))
;;; ;;;
;;; Inputs & outputs. ;;; Inputs & outputs.

View file

@ -636,6 +636,19 @@ (define shebang
(return (and (zero? (close-pipe pipe)) (return (and (zero? (close-pipe pipe))
(= n (string->number str))))))))) (= n (string->number str)))))))))
(test-assertm "scheme-file"
(let* ((text (plain-file "foo" "Hello, world!"))
(scheme (scheme-file "bar" #~(list "foo" #$text))))
(mlet* %store-monad ((drv (lower-object scheme))
(text (lower-object text))
(out -> (derivation->output-path drv)))
(mbegin %store-monad
(built-derivations (list drv))
(mlet %store-monad ((refs ((store-lift references) out)))
(return (and (equal? refs (list text))
(equal? `(list "foo" ,text)
(call-with-input-file out read)))))))))
(test-assert "text-file*" (test-assert "text-file*"
(let ((references (store-lift references))) (let ((references (store-lift references)))
(run-with-store %store (run-with-store %store