#!/usr/bin/env guile !# (use-modules (sph) (sph filesystem) (sph process) (sph alist) (sph list) (sph cli) (srfi srfi-1) (sph other) (sph lang sescript)) (define uglifyjs (let (uglifyjs-path (search-env-path-one "uglifyjs")) (and uglifyjs-path (l (action) "symbol:compress/format -> list:process-chain-config" (case action ((compress) (list uglifyjs-path "--compress" "--mangle")) ((format) (list uglifyjs-path "--beautify"))))))) (define (rw-port->port read write port port-2) (let loop ((a (read port))) (if (eof-object? a) a (begin (write a port-2) (loop (read port)))))) (define (call-with-ports paths c) (let* ( (destination? (> (length paths) 1)) (destination (if destination? (open-file (last paths) "w") (current-output-port))) (source (if (null? paths) (list (current-input-port)) (map (l (a) (open-file a "r")) (if destination? (drop-right paths 1) paths))))) (begin-first (c source destination) (if (not (eq? (current-output-port) destination)) (close-port destination)) (if (not (eq? (current-input-port) (first source))) (map close-port source))))) (define (process in out) (rw-port->port read (l (data out) (display (sescript->ecmascript-string data ses-default-load-paths) out) (display ";" out)) in out)) (define (ses post-process-action paths) (call-with-ports paths (l (sources destination) (sescript-use-strict destination) (if (and post-process-action uglifyjs) (apply execute-with-pipe (l (port) (each (l (a) (process a port)) sources)) OPEN_WRITE (uglifyjs post-process-action)) (each (l (a) (process a destination)) sources))))) (let (options ( (cli-create #:description "compile sescript to ecmascript. read from files or standard input, and write to a file or standard output depending on if paths are given as arguments. input/output depends on the number of given paths: none: read from standard input and write to standard output one: read from file and write to standard output two: read from the first file and write to the second more: read from all files except the last one and write to the last one" #:options (qq ( (format #:names #\f #:description "format output with uglifyjs") (compress #:names #\c #:description "compress output with uglifyjs") ((paths ...))))))) (alist-bind options (format compress paths) (ses (if compress (q compress) (if format (q format) #f)) (or paths (list)))))