public
Description: make build.xml less, well, xml
Homepage:
Clone URL: git://github.com/apk/anteater.git
anteater / src / Gen.java
100644 321 lines (299 sloc) 11.924 kb
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// -*- Mode: Java; c-basic-offset: 4; tab-width: 8; indent-tabs-mode: nil; -*-
// Copyright (C) 2008 Andreas Krey, Ulm, Germany <a.krey@gmx.de>
 
package org.uberluser.ant;
 
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
 
import java.io.*;
import java.util.Vector;
import java.util.HashMap;
import java.io.PrintStream;
 
public class Gen extends Task {
    private String say, txt;
    private File dst = new File ("anteater.xml"), src;
 
    public void setSay (String say) { this.say = say; }
    public void setDest (File dst) { this.dst = dst; }
    public void setSrc (File src) { this.src = src; }
 
    public void addText (String txt) { this.txt = txt;
        System.out.println ("Setting txt: <<" + txt + ">>");
    }
 
    private HashMap<String, Target> targets;
    private Vector<Node> toplevel = new Vector<Node> ();
 
    private Vector<String> readFile (String src) throws IOException {
        return readFile (new File (src));
    }
 
    /** Read file as string vector.
* Reads file, converts tabs to spaces (eight of them), and splits
* lines according to newlines. Returns a Vector<String> with
* the lines.
*/
    private Vector<String> readFile (File src) throws IOException {
        Vector<String> res = new Vector<String> ();
        StringBuffer sb = new StringBuffer ();
        Reader f = new FileReader (src);
        int n = 0;
        while (true) {
            int c = f.read ();
            if (c == -1) break;
            if (c == '\n') {
                String r = sb.toString ();
                res.addElement (r);
                sb = new StringBuffer ();
                n = 0;
            } else {
                if (c == '\t') {
                    do {
                        sb.append (' ');
                        n ++;
                    } while ((n % 8) != 0);
                } else {
                    sb.append ((char)c);
                    n ++;
                }
            }
        }
        String r = sb.toString ();
        if (r.length () > 0) {
            res.addElement (r);
        }
        return res;
    }
 
    public void execute () throws BuildException {
        if (say != null) {
            System.out.println (say);
        }
        System.out.println (" -> " + dst);
        Node ls = new Node ("*top*", "", 1, null);
        try {
            if (src != null) {
                System.out.println (" <- " + src);
                ls.parseBody (readFile (src));
            }
            if (txt != null) {
                Vector<String> res = new Vector<String> ();
                for (String a: txt.split ("\n")) {
                    res.addElement (a);
                }
                ls.parseBody (res);
            }
            targets = new HashMap<String, Target> ();
            for (Node l: ls.getNodes ()) {
                interp (l);
            }
            PrintStream ps = new PrintStream (new FileOutputStream (dst));
            ps.println ("<!-- generated - do not edit - use " + src + " -->");
            ps.println ("<project xmlns:ivy=\"antlib:org.apache.ivy.ant\">");
            for (Node n: toplevel) {
                n.dumpXML (ps, " ");
            }
            for (Target t: targets.values ()) {
                t.dump (ps, " ");
            }
            ps.println ("</project>");
            ps.close ();
        } catch (IOException e) {
            throw new BuildException (e);
        } catch (IllegalArgumentException e) {
            throw new BuildException (e);
        }
    }
 
    private Target getTarget (String n) {
        Target t = targets.get (n);
        if (t == null) {
            t = new Target (n);
            targets.put (n, t);
        }
        return t;
    }
 
    private Target defTarget (String n) {
        Target t = getTarget (n);
        t.setDefined ();
        return t;
    }
 
    private static int cnt = 0;
 
    private void interp (Node l) {
        if (l.tag == "read") {
            AttrList al = new AttrList (l.getParam ());
            String base = al.pull ("file");
            if (base == null || !al.empty ()) {
                throw new IllegalArgumentException ("extra args in read");
            }
            try {
                Node ls = new Node ("*file*", base, 1, readFile (base));
                for (Node x: ls.getNodes ()) {
                    interp (x);
                }
            } catch (IOException e) {
                throw new BuildException (e);
            }
            return;
        }
        if (l.tag == "write") {
            AttrList al = new AttrList (l.getParam ());
            String base = al.pull ("file");
            if (base == null || !al.empty ()) {
                throw new IllegalArgumentException ("extra args in write");
            }
            Vector<Node> xs = l.getNodes ();
            if (xs.size () != 1) {
                throw new IllegalArgumentException ("not one node in write");
            }
 
            try {
                PrintStream ps = new PrintStream (new FileOutputStream (base));
                ps.println ("<!-- generated - do not edit - use " + src + " -->");
                Node n = xs.elementAt (0);
                n.dumpXML (ps, "");
                ps.close ();
            } catch (IOException e) {
                throw new BuildException (e);
            } catch (IllegalArgumentException e) {
                throw new BuildException (e);
            }
            return;
        }
        if (l.tag == "javac") {
            AttrList al = new AttrList (l.getParam ());
            Basifier base = new Basifier (al.pull ("dir"));
 
            final String srcdir = base.basify (al.pull ("src"), "src");
            final String clsdir = base.basify (al.pull ("classes"), "classes");
            final String jardir = base.basify (al.pull ("jardir"), "jar");
            final String jarname = base.basify (al.pull ("jar"), null);
            final String [] cps = al.pullAll ("cp");
            final String [] dcps = al.pullAll ("dep-cp");
            final String [] asrc = al.pullAll ("extra-src");
            if (!al.empty ()) {
                throw new IllegalArgumentException ("extra args in javac");
            }
            final String tag = clsdir.replace ('/', '-');
            final Target lcomp = defTarget ("compile-" + tag);
            final Target lclean = defTarget ("clean-" + tag);
            defTarget ("compile").addDep (lcomp);
            lcomp.addDep (defTarget ("pre-compile"));
            defTarget ("clean").addDep (lclean);
 
            final Target ljar =
                jarname == null ? null : defTarget ("jar-" + tag);
            if (ljar != null) {
                defTarget ("jar").addDep (ljar);
                ljar.addDep (lcomp);
            }
 
            lclean.addNode (new Node ("delete") {{
                addParam ("dir", clsdir);
            }});
            lcomp.addNode (new Node ("delete") {{
                addParam ("dir", clsdir);
            }});
            lcomp.addNode (new Node ("mkdir") {{
                addParam ("dir", clsdir);
            }});
            lcomp.addNode (new Node ("javac") {{
                addParam ("srcdir", srcdir);
                if (asrc != null) {
                    for (final String as: asrc) {
                        addNode (new Node ("src") {{
                            addParam ("path", as);
                        }});
                    }
                }
                addParam ("destdir", clsdir);
                addNode (new Node ("classpath") {{
                    if (cps != null) {
                        for (final String cp: cps) {
                            addNode (new Node ("pathelement") {{
                                addParam ("location", cp);
                            }});
                        }
                    }
                    if (dcps != null) {
                        for (final String cp: dcps) {
                            addNode (new Node ("pathelement") {{
                                addParam ("location", cp);
                            }});
                            lcomp.addDep (getTarget ("compile-" +
                                                     cp.replace ('/',
                                                                 '-')));
                        }
                    }
                    addNode (new Node ("fileset") {{
                        addParam ("dir", "${basedir}");
                        addNode (new Node ("include") {{
                            addParam ("name", "lib/*.jar");
                        }});
                    }});
                }});
            }});
 
            if (ljar != null) {
                ljar.addNode (new Node ("mkdir") {{
                    addParam ("dir", jardir);
                }});
                ljar.addNode (new Node ("jar") {{
                    addParam ("destfile", jardir + "/" + jarname + ".jar");
                    addNode (new Node ("fileset") {{
                        addParam ("dir", clsdir);
                    }});
                }});
                ljar.addNode (new Node ("jar") {{
                    addParam ("destfile", jardir + "/" + jarname + "-sources.jar");
                    addNode (new Node ("fileset") {{
                        addParam ("dir", srcdir);
                    }});
                }});
                ljar.addNode (new Node ("jar") {{
                    addParam ("destfile", jardir + "/" + jarname + "-git.jar");
                    addNode (new Node ("fileset") {{
                        addParam ("dir", ".");
                        addParam ("excludes", "jar/**, classes/**, lib/**, tmp/**");
                    }});
                }});
                lclean.addNode (new Node ("delete") {{
                    addParam ("dir", jardir);
                }});
            }
            // <target name="compile" depends="resolve" description="--> compile the project">
            // <mkdir dir="${classes.dir}" />
            // <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true" source="1.6" target="1.6">
            // <classpath>
            // <fileset dir="${basedir}">
            // <include name="${lib.dir}/*.jar"/>
            // <include name="${libspecial.dir}/*.jar" />
            // </fileset>
            // </classpath>
            // </javac>
            // </target>
 
            return;
        }
        if (l.tag == "toplevel") {
            AttrList al = new AttrList (l.getParam ());
            if (!al.empty ()) {
                throw new IllegalArgumentException ("toplevel has no attrs");
            }
            for (Node x: l.getNodes ()) {
                toplevel.addElement (x);
            }
            return;
        }
            
        if (l.tag == "target") {
            AttrList al = new AttrList (l.getParam ());
            String n = al.pull ("name");
            if (n == null) throw new IllegalArgumentException ("no name in target");
            Target t = defTarget (n);
            while ((n = al.pull ("dep")) != null) {
                t.addDep (getTarget (n));
            }
            while ((n = al.pull ("from")) != null) {
                getTarget (n).addDep (t);
            }
            if (al.isPresent ("depends")) {
                throw new IllegalArgumentException ("sorry, no dependencies in target allowed. Use multiple deps");
            }
            // positive name check? Can leave that to ant...
            while ((n = al.firstName ()) != null) {
                String v = al.pull (n);
                t.addAttr (n, v);
            }
            for (Node x: l.getNodes ()) {
                t.addNode (x);
            }
            return;
        }
        throw new IllegalArgumentException ("Bad tag " + l.tag);
    }
}