src/main/java/de/unixwork/uwproj/Main.java

changeset 39
3ca85da78515
parent 7
686991ae6e2f
child 42
becfbf7af928
equal deleted inserted replaced
38:02b000e40e5e 39:3ca85da78515
1 /*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package de.unixwork.uwproj; 1 package de.unixwork.uwproj;
7 2
8 import java.io.BufferedWriter;
9 import java.io.File;
10 import java.io.FileNotFoundException;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13 import java.io.OutputStreamWriter;
14 import java.io.StringWriter;
15 import java.util.logging.Level;
16 import java.util.logging.Logger;
17 import org.apache.velocity.Template;
18 import org.apache.velocity.VelocityContext; 3 import org.apache.velocity.VelocityContext;
19 import org.apache.velocity.app.VelocityEngine; 4 import org.apache.velocity.app.VelocityEngine;
20 import org.xml.sax.SAXException;
21 5
22 /** 6 import java.io.*;
23 * 7
24 * @author olaf
25 */
26 public class Main { 8 public class Main {
9
10 static Project loadProjectFile(String fileName) {
11 try {
12 return new Project(new File(fileName));
13 } catch (Exception e) {
14 throw new RuntimeException(e);
15 }
16 }
17
18 static void writeConfigureScript(Writer out, String tplFileName, Project project) {
19 var context = new VelocityContext();
20 context.put("targets", project.getTargets());
21 context.put("namedDependencies", project.getNamedDependencies());
22 context.put("dependencies", project.getDependencies());
23 context.put("options", project.getOptions());
24 context.put("features", project.getFeatures());
25 context.put("project", project);
26 context.put("vars", project.getVars());
27 new VelocityEngine().getTemplate(tplFileName).merge(context, out);
28 }
29
27 public static void main(String[] args){ 30 public static void main(String[] args){
28 File f = new File("make/project.xml"); 31 final var inFileName = "make/project.xml";
29 Project p = null; 32 final var tplFileName = "make/configure.vm";
30 try { 33 final var outFileName = "configure";
31 p = new Project(f); 34
32 } catch (SAXException ex) { 35 try (var out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFileName)))) {
33 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 36 writeConfigureScript(out, tplFileName, loadProjectFile(inFileName));
34 } catch (IOException ex) {
35 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
36 } catch (Exception ex) { 37 } catch (Exception ex) {
37 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 38 ex.printStackTrace();
38 }
39 if(p == null) {
40 System.exit(1); 39 System.exit(1);
41 } 40 }
42 41
43 VelocityEngine ve = new VelocityEngine(); 42 System.out.println("Out: "+outFileName);
44 Template t = ve.getTemplate("make/configure.vm");
45 VelocityContext context = new VelocityContext();
46 context.put("targets", p.getTargets());
47 context.put("namedDependencies", p.getNamedDependencies());
48 context.put("dependencies", p.getDependencies());
49 context.put("options", p.getOptions());
50 context.put("features", p.getFeatures());
51 context.put("project", p);
52 context.put("vars", p.getVars());
53
54 int ret = 1;
55 try {
56 FileOutputStream fout = new FileOutputStream(new File("configure"));
57 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fout));
58 t.merge(context, out);
59 out.close();
60 ret = 0;
61 System.out.println("Out: configure");
62 } catch (FileNotFoundException ex) {
63 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
64 } catch (IOException ex) {
65 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
66 }
67 System.exit(ret);
68 } 43 }
69 } 44 }

mercurial