Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

VersionUptodate.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.internal.tools.ant.taskdefs;
  13. import java.io.BufferedReader;
  14. import java.io.File;
  15. import java.io.FileReader;
  16. import java.io.FileWriter;
  17. import java.io.IOException;
  18. import org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.Task;
  20. /**
  21. * Check if version source file has the specified build version,
  22. * and ensure a tag file reflects whether it does or not.
  23. */
  24. public class VersionUptodate extends Task {
  25. public VersionUptodate() {}
  26. private String buildVersion;
  27. private File versionSource;
  28. private File versionTagFile;
  29. /**
  30. * @param buildVersion String expected as Version.text - required
  31. */
  32. public void setVersion(String buildVersion) {
  33. this.buildVersion = buildVersion;
  34. }
  35. /**
  36. * @param versionSource the File Version.java containing text constant
  37. * - required
  38. */
  39. public void setVersionSourceFile(File versionSource) {
  40. this.versionSource = versionSource;
  41. }
  42. /**
  43. * @param versionTagFile the File whose existence signals that the version
  44. * is uptodate after this task executes - required.
  45. */
  46. public void setVersionTagFile(File versionTagFile) {
  47. this.versionTagFile = versionTagFile;
  48. }
  49. /**
  50. * If the Version.java source file contains the correct
  51. * build version, then create the output tag file,
  52. * else delete it if it exists.
  53. * @throws BuildException if tagFile not creatable and version is incorrect
  54. * or if version is correct and tagFile cannot be deleted.
  55. */
  56. public void execute() throws BuildException {
  57. if (null == buildVersion) {
  58. throw new BuildException("require buildVersion");
  59. }
  60. if ((null == versionSource) || !versionSource.canRead()){
  61. throw new BuildException("require versionSource");
  62. }
  63. if (null == versionTagFile){
  64. throw new BuildException("require versionTagFile");
  65. }
  66. if (sameVersion(versionSource, buildVersion)) {
  67. if (!versionTagFile.exists()) {
  68. createFile(versionTagFile, buildVersion);
  69. }
  70. } else if (null == versionTagFile) {
  71. throw new BuildException("no tag file, and version out of date");
  72. } else if (versionTagFile.exists()) {
  73. if (!versionTagFile.delete()) {
  74. throw new BuildException("version out of date, but cannot delete " + versionTagFile);
  75. }
  76. }
  77. }
  78. /**
  79. * Detect whether version is correct in Java sources.
  80. * @param versionSource
  81. * @param buildVersion
  82. * @return boolean
  83. */
  84. private boolean sameVersion(File versionSource, String buildVersion) {
  85. // XXX build and load instead of scanning?
  86. FileReader fileReader = null;
  87. try {
  88. fileReader = new FileReader(versionSource);
  89. BufferedReader reader = new BufferedReader(fileReader);
  90. String line;
  91. while (null != (line = reader.readLine())) {
  92. int loc = line.indexOf("static final String text = ");
  93. if (-1 != loc) {
  94. return (-1 != line.indexOf(buildVersion , loc));
  95. }
  96. }
  97. return false;
  98. } catch (IOException e) {
  99. return false;
  100. } finally {
  101. if (null != fileReader) {
  102. try {
  103. fileReader.close();
  104. } catch (IOException e) {
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * Create file with contents
  111. */
  112. private void createFile(File versionTagFile, String contents) {
  113. FileWriter writer = null;
  114. try {
  115. writer = new FileWriter(versionTagFile);
  116. char[] buf = new char[contents.length()];
  117. contents.getChars(0, buf.length, buf, 0);
  118. writer.write(contents);
  119. } catch (IOException e) {
  120. throw new BuildException("writing " + versionTagFile, e);
  121. } finally {
  122. if (null != writer) {
  123. try {
  124. writer.close();
  125. } catch (IOException e){
  126. // ignore
  127. }
  128. }
  129. }
  130. }
  131. }