-
Notifications
You must be signed in to change notification settings - Fork 11
/
NS2JSApplicationPackaging.ns
157 lines (141 loc) · 6.03 KB
/
NS2JSApplicationPackaging.ns
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
Newspeak3
'NS2JS'
class NS2JSApplicationPackaging packageUsing: ns = (
(* An app for the Newspeak-to-JavaScript compiler.
Derived from Newspeak2DartApplicationPackaging.
Copyright 2012 SAP AG.
Copyright 2012 Google Inc.
Copyright 2013 Ryan Macnak
Licensed under the Apache License, Version 2.0 (the ''License''); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 *)|
CombinatorialParsing = ns CombinatorialParsing.
Grammar = ns NewspeakGrammar.
ASTs = ns NewspeakASTs.
Parsing = ns NewspeakParsing.
Generation = ns JavascriptGeneration.
Compilation = ns Newspeak2JSCompilation mixinApply: ns NewspeakCompilation.
public Runtime = ns RuntimeForJS.
public RuntimeWithMirrorBuilders = ns RuntimeForJSWithMirrorBuilders.
|) (
public class WebPagePackager usingPlatform: p = (|
private parserLib = CombinatorialParsing usingPlatform: p.
private grammar = Grammar usingPlatform: p parsers: parserLib.
private asts = ASTs usingPlatform: p.
private parsing = Parsing usingPlatform: p grammar: grammar asts: asts.
private generation = Generation usingPlatform: p.
private compilation = Compilation
usingPlatform: p
asts: asts
parsing: parsing
generation: generation.
private List = p collections List.
private ClassMirror = p mirrors ClassMirror.
private PNGReadWriter = p squeak PNGReadWriter.
private FileStream = p squeak FileStream.
private FileDirectory = p squeak FileDirectory.
private CrLfFileStream = p squeak CrLfFileStream.
public outDirectory
|) (
class ManifestForJS usingNamespace: ns = (|
namespace = ns.
public (* bogus *) imports = List new.
|) (
protected doesNotUnderstand: message = (
imports add: message selector.
^namespace
at: message selector
ifAbsent: [super doesNotUnderstand: message]
)
) : (
)
public compileAppDefn: appDefn runtimeDefn: rtDefn usingNamespace: ns to: output to: sourcesOutput = (
|
resourcePaths = List new.
builder = compilation ProgramBuilder new.
|
builder runtimeSources: (extractSourcesFromConfiguration: rtDefn usingNamespace: ns).
builder applicationSources: (extractSourcesFromConfiguration: appDefn usingNamespace: ns).
resourcePaths addAll: (extractResourcesFromConfiguration: rtDefn usingNamespace: ns into: builder).
resourcePaths addAll: (extractResourcesFromConfiguration: appDefn usingNamespace: ns into: builder).
output nextPutAll: builder outputProgram.
sourcesOutput nextPutAll: builder outputSources.
^resourcePaths
)
public compileSource: appDefnSource usingNamespace: ns to: output = (
| builder = compilation ProgramBuilder new. |
builder runtimeSources: (extractSourcesFromConfiguration: Runtime usingNamespace: ns).
builder applicationSources: {appDefnSource}.
extractResourcesFromConfiguration: Runtime usingNamespace: ns into: builder.
output nextPutAll: builder outputProgram.
)
private extractResourcesFromConfiguration: config usingNamespace: ns into: builder = (
|
resourcePaths = List new.
importsRecorder = ManifestForJS usingNamespace: ns.
|
config packageUsing: importsRecorder.
importsRecorder imports do: [:import <Symbol> |
| resource |
resource:: ns at: import.
resource isKindOfString ifTrue:
[builder addStringResource: resource under: import].
resource isForm ifTrue:
[ | stream |
[stream:: outDirectory forceNewFileNamed: import, '.png'.
PNGReadWriter putForm: resource onStream: stream] ensure: [stream close].
builder addImageResource: import, '.png' under: import.
resourcePaths add: import, '.png']].
^resourcePaths
)
private extractSourcesFromConfiguration: appDefn usingNamespace: ns = (
(* Ordered is important: the app definition must come first *)
|
modules = List new.
importsRecorder = ManifestForJS usingNamespace: ns.
|
modules add: (sourceOf: appDefn).
appDefn packageUsing: importsRecorder.
importsRecorder imports do: [:import <Symbol> |
| resource |
resource:: ns at: import.
resource isBehavior ifTrue: [modules include: (sourceOf: (resource))]].
^modules asArray
)
public packageApplicationConfiguration: config <Class> withRuntimeConfiguration: rtConfig usingNamespace: ns = (
| name resourcePaths scriptStream sourcesStream pageStream |
name:: config name.
outDirectory:: FileDirectory default / 'out' / name.
outDirectory assureExistence.
scriptStream:: outDirectory forceNewFileNamed: name, '.js'.
sourcesStream:: outDirectory forceNewFileNamed: name, '.sources.js'.
[scriptStream lineEndConvention: #lf.
sourcesStream lineEndConvention: #lf.
scriptStream nextPutAll: (String streamContents: [:bufferStream |
resourcePaths:: compileAppDefn: config runtimeDefn: rtConfig usingNamespace: ns to: bufferStream to: sourcesStream])]
ensure: [scriptStream close. sourcesStream close].
pageStream:: outDirectory forceNewFileNamed: name, '.html'.
[pageStream nextPutAll: '<!DOCTYPE html><html><head>'; lf.
pageStream nextPutAll: '<meta charset="utf-8"/>'; lf.
pageStream nextPutAll: '<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">'; lf.
pageStream nextPutAll: '<title>'; nextPutAll: name; nextPutAll: '</title>'; lf.
pageStream nextPutAll: '</head><body>'; lf.
pageStream nextPutAll: '<script type="text/javascript" src="CodeMirror/lib/codemirror.js"></script>'.
pageStream nextPutAll: '<link rel="stylesheet" href="CodeMirror/lib/codemirror.css"></link>'.
pageStream nextPutAll: '<script type="text/javascript" src="CodeMirror/addon/display/autorefresh.js"></script>'.
pageStream nextPutAll: '<script type="text/javascript" src="'.
pageStream nextPutAll: name.
pageStream nextPutAll: '.sources.js"></script><script type="text/javascript" src="'.
pageStream nextPutAll: name.
pageStream nextPutAll: '.js"></script></body></html>']
ensure: [pageStream close].
resourcePaths add: name, '.html'.
resourcePaths addFirst: name, '.js'. (* Maybe the browser will fetch this first? *)
resourcePaths addFirst: name, '.sources.js'.
^(outDirectory fileNamed: (name, '.html')) fullName
)
private sourceOf: klass = (
^(ClassMirror reflecting: klass) mixin declaration compilationUnitSource
)
) : (
)
) : (
)