forked from dyoo/whalesong
/
whalesong-cmd.rkt
174 lines (152 loc) · 6.38 KB
/
whalesong-cmd.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#lang racket/base
(require "private/command.rkt"
"parameters.rkt"
"whalesong-helpers.rkt"
profile profile/render-text
racket/path
(for-syntax racket/base))
;; Command line for running Whalesong.
;; TODO: we may want to adopt this as a raco command, as described in:
;;
;; http://docs.racket-lang.org/raco/command.html
;; Usage:
;;
;; * Build standalone .xhtml application.
;;
;; $ whalesong build main-module-name.rkt
;;
;;
;; * Print out the runtime library to standard output.
;;
;; $ whalesong get-runtime
;;
;;
;; * Print out the JavaScript for the program.
;;
;; $ whalesong get-javascript main-module-name.rkt
(define as-standalone-html? (make-parameter #f))
(define with-profiling? (make-parameter #f))
(define-syntax (maybe-with-profiling stx)
(syntax-case stx ()
[(_ expr)
(syntax/loc stx
(if (with-profiling?)
(profile expr
#:threads #t
#:delay 0.0001
#:render (lambda (profile)
(render profile
#:truncate-source 500)))
expr))]))
(define (set-root-path! root-path)
(unless (directory-exists? root-path)
(printf "ERROR: root path ~a does not appear to exist.\n" root-path)
(printf "Aborting compilation.\n")
(exit))
(current-root-path (normalize-path root-path)))
(define (at-toplevel)
(svn-style-command-line
#:program "whalesong"
#:argv (current-command-line-arguments)
"The Whalesong command-line tool for compiling Racket to JavaScript"
["version" "Print the current version"
"Print the current version"
#:args ()
(print-version)]
["build" "build a standalone html and javascript package"
"Builds a Racket program and its required dependencies into a .html and .js file."
#:once-each
[("-v" "--verbose")
("Display verbose messages.")
(current-verbose? #t)]
[("--debug-show-timings")
("Display debug messages about compilation time.")
(current-timing-port (current-output-port))]
[("--enable-profiling")
("Enable profiling to standard output")
(with-profiling? #t)]
[("--without-ie")
("Disable IE legacy support")
(current-with-legacy-ie-support? #f)]
[("--without-cache")
("Turn off the internal compilation cache")
(current-with-cache? #f)]
[("--compress-javascript")
("Compress JavaScript with Google Closure (requires Java)")
(current-compress-javascript? #t)]
[("--split-modules")
("Write one file per module")
(current-one-module-per-file? #t)]
[("--root-dir")
root-path
("Set the root package path (default: current-directory)")
(set-root-path! root-path)]
[("--dest-dir")
dest-dir
("Set destination directory (default: current-directory)")
(current-output-dir dest-dir)]
[("--as-standalone-xhtml")
("Write single standalone xhtml file")
(as-standalone-html? #t)]
#:args (path)
(maybe-with-profiling
(if (as-standalone-html?)
(build-standalone-xhtml path)
(build-html-and-javascript path)))]
["get-runtime" "print the runtime library to standard output"
"Prints the runtime JavaScript library that's used by Whalesong programs."
#:once-each
[("-v" "--verbose")
("Display verbose messages.")
(current-verbose? #t)]
[("--debug-show-timings")
("Display debug messages about compilation time.")
(current-timing-port (current-output-port))]
[("--enable-profiling")
("Enable profiling to standard output")
(with-profiling? #t)]
[("--without-ie")
("Disable IE legacy support")
(current-with-legacy-ie-support? #f)]
[("--without-cache")
("Turn off the internal compilation cache")
(current-with-cache? #f)]
[("--root-dir")
root-path
("Set the root package path (default: current-directory)")
(set-root-path! root-path)]
[("--compress-javascript")
("Compress JavaScript with Google Closure (requires Java)")
(current-compress-javascript? #t)]
#:args ()
(maybe-with-profiling
(print-the-runtime))]
["get-javascript" "Gets just the JavaScript code and prints it to standard output"
"Builds a racket program into JavaScript. The outputted file depends on the runtime."
#:once-each
[("-v" "--verbose")
("Display verbose messages.")
(current-verbose? #t)]
[("--debug-show-timings")
("Display debug messages about compilation time.")
(current-timing-port (current-output-port))]
[("--enable-profiling")
("Enable profiling to standard output")
(with-profiling? #t)]
[("--without-ie")
("Disable IE legacy support")
(current-with-legacy-ie-support? #f)]
[("--without-cache")
("Turn off the internal compilation cache")
(current-with-cache? #f)]
[("--root-dir")
root-path
("Set the root package path (default: current-directory)")
(set-root-path! root-path)]
[("--compress-javascript")
("Compress JavaScript with Google Closure (requires Java)")
(current-compress-javascript? #t)]
#:args (file)
(maybe-with-profiling
(get-javascript-code file))]))
(at-toplevel)