#lang scheme
(require scheme/system
planet/util
"common.ss"
"defs-parser.ss"
)
(provide package-username
major-version
minor-version
package-name
set-planet-env
parse-module
planet-create
planet-build
planet-inject
planet-remove-package
planet-hard-link
planet-remove-hard-link
delete-bak-files
parse-module
write-doc
write-docs
write-info
write-main-doc
write-main-src
)
(define package-name (make-parameter
(let-values ([(p name r) (split-path (current-directory))])
(path->string name))) )
(define major-version (make-parameter 1) )
(define minor-version (make-parameter 0) )
(define package-username (make-parameter (getenv "username")) )
(define (set-planet-env username major minor [name (package-name)]) [package-username username]
[major-version major]
[minor-version minor]
[package-name name])
(define (package-dir)
(let([d (path->string (current-directory))])
(substring d 0 (- (string-length d) 1))))
(define exec-dir (find-system-path 'orig-dir))
(define planet-exe
(build-path exec-dir "planet"))
(define (system-debug str)
(printf "System: ~a~n" str)
(system str))
(define (planet-create) (let ([package-dir (package-dir)])
(parameterize ([current-directory (build-path (current-directory) 'up)])
(system-debug
(string-append (path->quote-string planet-exe)
" create "
(path->quote-string package-dir)
)))))
(define (planet-inject) (let ([package-dir (package-dir)])
(parameterize ([current-directory (build-path (current-directory) 'up)])
(system-debug
(string-append (path->quote-string planet-exe)
" fileinject "
(package-username) " "
(path->quote-string package-dir) ".plt "
(number->string (major-version)) " "
(number->string (minor-version))
)))))
(define (planet-remove-package) (remove-pkg (package-username)
(string-append (package-name) ".plt")
(major-version) (minor-version)))
(define (this-package-in-string [file #f]) (let*-values ([(user pack-file maj min) (apply values (this-package-version))]
[(pack ext) (file->name-ext pack-file)])
(string-append user "/" pack ":" (number->string maj)
":" (number->string min)
(if file
(string-append "/" file)
""))))
(define ns (make-base-namespace))
(eval '(require (planet cce/scheme:6:0/require-provide)) ns)
(define (parse-module package-name filename [extension "ss"]) (let* ( [file (string-append filename "." extension)]
[req-file (path->string (build-path (current-directory) file))]
[prov (eval `(quote-require (file ,req-file)) ns)]
)
(parameterize ([conventions (conventions)])
(string-append
"#lang scribble/manual
@(require (planet cce/scheme:6:0/scribble)
scribble/eval
(for-label scheme
(this-package-in " filename ")))
@(define make-my-eval (make-eval-factory '(scheme
(planet " (this-package-in-string filename) "))))
"
(scrbl-parse-file filename extension prov)))))
(define (write-doc file [dir 'same] #:exists [exists 'error]) (let-values ([(filename ext) (file->name-ext file)])
(with-output-to-file (build-path dir (string-append filename ".scrbl"))
(λ()(display (parse-module (package-name) filename ext)))
#:exists exists
)))
(define (write-main-src main files #:exists [exists 'replace]) (with-output-to-file main
(λ()(display
"#lang scheme/base
(require (planet cce/scheme:6:0/require-provide)
scheme/require-syntax)
(require/provide
")
(for-each (λ(f)(printf " \"~a\"~n" f))
files)
(display " )\n")
)
#:exists exists))
(define (exn-warning-exists f)
(λ(e)(printf "*** WARNING: ~a already exists. You need to remove it yourself if you want it to be rewritten.~n" f)))
(define (write-main-doc main dir files #:exists [exists 'error]) (with-handlers ([exn:fail:filesystem? (exn-warning-exists main)])
(with-output-to-file (build-path dir (string-append main ".scrbl"))
(λ()(display
(string-append
"#lang scribble/manual
@(require (planet cce/scheme:6:0/scribble)
(for-label scheme))
@title{Package " (package-name) "}
@(defmodule/this-package)
@author{" (package-username) "}
@table-of-contents[]
"))
(for-each (λ(f)(printf "@include-section[\"~a\"]~n"
(regexp-replace "\\.ss$" f ".scrbl")))
files))
#:exists exists)))
(define (write-info dir main-src main-doc #:exists [exists 'error]) (with-handlers ([exn:fail:filesystem? (exn-warning-exists "infos.ss")])
(with-output-to-file "info.ss"
(λ()(display
(string-append
"#lang setup/infotab
(define name \"" (package-name) "\")
(define blurb '(\"This is package " (package-name) "\"))
(define release-notes '(\"Initial release\"))
(define primary-file \"" main-src "\")
(define categories '(misc))
(define scribblings '((\"" dir "/" main-doc ".scrbl\" (multi-page))))
(define repositories '(\"4.x\"))")))
#:exists exists)))
(define (write-docs #:dir [dir "reference"] #:main-src [main-src "main.ss"] #:main-doc [main-doc "manual"] #:info? [info? #t] #:except [except '()] ) (let ([files
(remove* (append (list main-src "info.ss") except)
(map to-string (filter-file-list "(?:\\.ss|\\.scm)$")))])
(when main-src
(printf "Writing main source file: ~a...~n" main-src)
(write-main-src main-src files))
(when info?
(printf "Writing info.ss...~n")
(write-info dir main-src main-doc))
(unless (directory-exists? dir)
(printf "Creating documentation directory: ~a...~n" dir)
(make-directory dir))
(when main-doc
(printf "Writing main documentation file: ~a.scrbl...~n" main-doc)
(write-main-doc main-doc dir files))
(for-each
(λ(f)
(printf "Writing documentation file: ~a...~n" f)
(write-doc f dir #:exists 'replace))
files)
))
(define (delete-bak-files) (for-each delete-file
(filter-file-list "\\.bak$" (directory-list-rec))))
(define (planet-hard-link) (add-hard-link (package-username)
(string-append (package-name) ".plt")
(major-version) (minor-version) (current-directory)))
(define (planet-remove-hard-link) (remove-hard-link (package-username)
(string-append (package-name) ".plt")
(major-version) (minor-version)))
(define (planet-build [username (package-username)] [major (major-version)] [minor (minor-version)] #:dir [dir "reference"] #:main-src [main-src "main.ss"] #:main-doc [main-doc "manual"] #:info? [info? #t] #:except [except '()] ) (parameterize ([package-username username]
[major-version major]
[minor-version minor])
(planet-hard-link)
(write-docs #:dir dir #:main-src main-src #:main-doc main-doc
#:info? info? #:except except)
(delete-bak-files)
(planet-create)
))