| 1 | /* |
| 2 | * $Id: CvsVersionProperties.java,v 1.8 2006/02/15 08:31:37 hastings Exp $ |
| 3 | * |
| 4 | * Copyright (c) 2004, Moebius Solutions, Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * |
| 11 | * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * |
| 14 | * Redistributions in binary form must reproduce the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer in the documentation and/or other materials provided |
| 17 | * with the distribution. |
| 18 | * |
| 19 | * Neither the name of Moebius Solutions, Inc. nor the names of |
| 20 | * its contributors may be used to endorse or promote products |
| 21 | * derived from this software without specific prior written |
| 22 | * permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 27 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 28 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 30 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 33 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 35 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | */ |
| 37 | |
| 38 | package com.moesol.util; |
| 39 | |
| 40 | import java.io.BufferedReader; |
| 41 | import java.io.File; |
| 42 | import java.io.FileReader; |
| 43 | import java.io.IOException; |
| 44 | import java.io.Reader; |
| 45 | import java.text.MessageFormat; |
| 46 | import java.text.SimpleDateFormat; |
| 47 | import java.util.Date; |
| 48 | |
| 49 | /** |
| 50 | * @author Robert Hastings |
| 51 | * |
| 52 | * Extract the cvs tag from the CVS/Entries file if one exists and then |
| 53 | * write out a properties file that represents the extracted version |
| 54 | * information. The tag is expected to be formated {tag_prefix}_{a}_{b}_{c}_{d}. |
| 55 | * The generated version number will then be a.b.c.d. If no tag exists the |
| 56 | * generated version number is 0.{build_year}.{build_date}.{build_time}. |
| 57 | */ |
| 58 | public class CvsVersionProperties extends BaseVersionProperties { |
| 59 | public static void main(String[] args) { |
| 60 | if (args.length != 3) { |
| 61 | System.err.println("usage: " + CvsVersionProperties.class.getName() |
| 62 | + " <tag_prefix> <filename> <outputname>"); |
| 63 | System.exit(2); |
| 64 | } |
| 65 | CvsVersionProperties vp = new CvsVersionProperties(); |
| 66 | try { |
| 67 | vp.m_tag_prefix = args[0]; |
| 68 | vp.m_filename = args[1]; |
| 69 | vp.readVersionProperties(); |
| 70 | vp.writeVersionProperties(args[2]); |
| 71 | } catch (IOException e) { |
| 72 | System.err.println("Failed to setup properties:"); |
| 73 | e.printStackTrace(System.err); |
| 74 | System.exit(1); |
| 75 | } |
| 76 | System.exit(0); |
| 77 | } |
| 78 | private void readVersionProperties() throws IOException { |
| 79 | String entries_file = "CVS" + File.separator + "Entries"; |
| 80 | FileReader fr = new FileReader(entries_file); |
| 81 | try { |
| 82 | readVersionProperties(fr); |
| 83 | } finally { |
| 84 | fr.close(); |
| 85 | } |
| 86 | } |
| 87 | void readVersionProperties(Reader fr) throws IOException { |
| 88 | if (m_filename == null) { |
| 89 | throw new IllegalStateException("Filename was never set"); |
| 90 | } |
| 91 | |
| 92 | initVersionDefaults(new Date()); |
| 93 | |
| 94 | BufferedReader br = new BufferedReader(fr); |
| 95 | String line; |
| 96 | while ((line = br.readLine()) != null) { |
| 97 | if (line.startsWith("/"+ m_filename +"/")) { |
| 98 | parseLine(line); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void initVersionDefaults(Date today) { |
| 104 | m_properties.setProperty("buildVersion", new SimpleDateFormat( |
| 105 | "0.yyyy.MMdd.HHmm").format(today)); |
| 106 | m_properties.setProperty("buildVersionCommas", new SimpleDateFormat( |
| 107 | "0,yyyy,MMdd,HHmm").format(today)); |
| 108 | m_properties.setProperty("buildDate", |
| 109 | new SimpleDateFormat("MM/dd/yyyy").format(today)); |
| 110 | } |
| 111 | |
| 112 | void parseLine(String line) { |
| 113 | String[] parts = line.split("/"); |
| 114 | String tag = parts.length == 6 ? parts[5] : ""; |
| 115 | parts = tag.split("_"); |
| 116 | oldTagStyle(parts); |
| 117 | newTagStyle(parts); |
| 118 | } |
| 119 | /** |
| 120 | * The old tag style is |
| 121 | * {prefix}_a_b_c_d. |
| 122 | * |
| 123 | * @param parts |
| 124 | */ |
| 125 | private void oldTagStyle(String[] parts) { |
| 126 | String prefix = parts[0]; |
| 127 | |
| 128 | if (("T" + m_tag_prefix).equals(prefix)) { |
| 129 | m_properties.setProperty("buildVersion", |
| 130 | new MessageFormat("{1}.{2}.{3}.{4}").format(parts)); |
| 131 | m_properties.setProperty("buildVersionCommas", |
| 132 | new MessageFormat("{1},{2},{3},{4}").format(parts)); |
| 133 | } |
| 134 | } |
| 135 | /** |
| 136 | * The new tag style is |
| 137 | * va_b_c_d, for example v1_0_0_273 |
| 138 | * @param parts |
| 139 | */ |
| 140 | private void newTagStyle(String[] parts) { |
| 141 | if (isNewStylePrefix(parts)) { |
| 142 | parts[0] = parts[0].substring(2); |
| 143 | m_properties.setProperty("buildVersion", |
| 144 | new MessageFormat("{0}.{1}.{2}.{3}").format(parts)); |
| 145 | m_properties.setProperty("buildVersionCommas", |
| 146 | new MessageFormat("{0},{1},{2},{3}").format(parts)); |
| 147 | } |
| 148 | } |
| 149 | /** |
| 150 | * @param parts |
| 151 | * @return true if this looks like a new style tag |
| 152 | */ |
| 153 | private boolean isNewStylePrefix(String[] parts) { |
| 154 | if (!parts[0].startsWith("Tv")) { |
| 155 | return false; |
| 156 | } |
| 157 | return Character.isDigit(parts[0].charAt(2)); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Set what cvs tag prefix we look for. |
| 162 | * |
| 163 | * @param v |
| 164 | */ |
| 165 | public void setTagPrefix(String v) { |
| 166 | m_tag_prefix = v; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @param string |
| 171 | */ |
| 172 | public void setFilename(String string) { |
| 173 | m_filename = string; |
| 174 | } |
| 175 | |
| 176 | private String m_tag_prefix = "NONE"; |
| 177 | private String m_filename = null; |
| 178 | } |