001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.build;
028:
029: import java.io.BufferedReader;
030: import java.io.File;
031: import java.io.FileInputStream;
032: import java.io.FileOutputStream;
033: import java.io.InputStream;
034: import java.io.InputStreamReader;
035: import java.io.OutputStreamWriter;
036: import java.io.PrintWriter;
037: import java.text.SimpleDateFormat;
038: import java.util.Date;
039:
040: /**
041: * Write a class Version.java using <code>version.def</code> to get the Version number,
042: * and system properties <code>repository.tag</code>, <code>repository.time</code>,
043: * and <code>repository.modified</code> to get Repository information.
044: **/
045: public class VersionWriter extends WriterBase {
046: private String[] args;
047: boolean isVerbose = false;
048: boolean didIt = false;
049:
050: String version = null;
051: String deffilename = null;
052:
053: private void readFile(String filename) {
054: InputStream stream = null;
055: try {
056: setDirectories(filename);
057: if (filename.equals("-")) {
058: debug("Reading from standard input.");
059: stream = new java.io.DataInputStream(System.in);
060: } else {
061: deffilename = filename;
062: debug("Reading \"" + filename + "\".");
063: stream = new FileInputStream(filename);
064: }
065:
066: InputStreamReader isr = new InputStreamReader(stream);
067: BufferedReader br = new BufferedReader(isr);
068:
069: for (String line = br.readLine(); line != null; line = br
070: .readLine()) {
071: if (!(line.startsWith(";") || line.startsWith("#"))) {
072: line = line.trim();
073: // not a comment
074: if (line.length() > 0) {
075: if (version != null) {
076: version = version + "/" + line;
077: } else {
078: version = line;
079: }
080: }
081: }
082: }
083:
084: br.close();
085: didIt = true;
086: } catch (Exception e) {
087: e.printStackTrace();
088: }
089: }
090:
091: public void writeFile() {
092: try {
093: PrintWriter filelist = new PrintWriter(
094: new OutputStreamWriter(new FileOutputStream(
095: new File(getTargetDir(), "version.gen"))));
096: println(filelist, "Version.java");
097: filelist.close();
098:
099: PrintWriter out = new PrintWriter(new OutputStreamWriter(
100: new FileOutputStream(new File(getTargetDir(),
101: "Version.java"))));
102: writeCR(out, deffilename);
103: println(out);
104:
105: // Make package match getTargetDir()
106: println(out, "package " + getPackageFromDir(getSourceDir())
107: + ";");
108: if (version == null)
109: version = "unknown";
110: String rtag = System.getProperty("repository.tag");
111: String rdate = System.getProperty("repository.time");
112: boolean rmod = "true".equals(System
113: .getProperty("repository.modified"));
114: long rtime = -1;
115: if (rdate != null) {
116: try {
117: // example rdate: "4/5/2002 16:00:07" (GMT?)
118: SimpleDateFormat df = new SimpleDateFormat(
119: "M/d/yyyy H:mm:ss");
120: Date d = df.parse(rdate);
121: rtime = d.getTime();
122: } catch (Exception eDateFormat) {
123: }
124: }
125: println(
126: out,
127: "public final class Version {\n"
128: + " public final static String version = \""
129: + version
130: + "\";\n"
131: + " public final static long buildTime = "
132: + System.currentTimeMillis()
133: + "L;\n"
134: + " public final static String repositoryTag = "
135: + ((rtag != null) ? ("\"" + rtag + "\"")
136: : "null")
137: + ";\n"
138: + " public final static boolean repositoryModified = "
139: + ((rtag != null) ? rmod : false)
140: + ";\n"
141: + " public final static long repositoryTime = "
142: + rtime
143: + "L;\n"
144: + " public final static void main(String args[]) {\n"
145: + " System.out.println(\"version=\"+version);\n"
146: + " System.out.println(\"build time=\"+new java.util.Date(buildTime));");
147: if (rtag != null) {
148: println(out, " System.out.println(\"repository tag="
149: + rtag + (rmod ? " (modified)" : "") + "\");");
150: }
151: if (rdate != null) {
152: println(
153: out,
154: " System.out.println(\"repository time="
155: + ((rtime > 0) ? ("\"+new java.util.Date(repositoryTime));")
156: : ("\\\"" + rdate + "\\\"\");")));
157: }
158: println(out, " }\n" + "}");
159: out.close();
160: } catch (Exception e) {
161: e.printStackTrace();
162: }
163: }
164:
165: public void debug(String s) {
166: if (isVerbose)
167: System.err.println(s);
168: }
169:
170: void usage(String s) {
171: System.err.println(s);
172: System.err.println("Usage: VersionWriter [-v] version.def");
173: System.exit(1);
174: }
175:
176: public void start() {
177: for (int i = 0; i < args.length; i++) {
178: String arg = args[i];
179: if (arg.startsWith("-")) { // parse flags
180: if (arg.equals("-")) {
181: readFile("-");
182: } else if (arg.equals("-v")) {
183: isVerbose = (!isVerbose);
184: } else if (arg.equals("-d")) {
185: targetDirName = args[++i];
186: } else {
187: usage("Unknown option \"" + arg + "\"");
188: }
189: } else { // deal with files
190: readFile(arg);
191: }
192: }
193: if (!didIt) {
194: readFile("version.def");
195: }
196:
197: writeFile();
198: }
199:
200: public VersionWriter(String args[]) {
201: this .args = args;
202: }
203:
204: public static void main(String args[]) {
205: VersionWriter vw = new VersionWriter(args);
206: vw.start();
207: }
208: }
|