Procházet zdrojové kódy

issue1726 version increment script (#1730)

* Initial commit of script to increase version qualifiers

not completed yet
only works for feature.xml
Techniques: Xpath, java.nio

* Updated / finished for statecharts

* Refactored UpdateVersion
svenjawendler před 7 roky
rodič
revize
1164a731a5

+ 1 - 1
features/org.yakindu.sct.generator.csharp-feature/feature.xml

@@ -2,7 +2,7 @@
 <feature
       id="org.yakindu.sct.generator.csharp"
       label="Yakindu Statechart Tools (SCT) C# Generator Feature"
-      version="2.5.0.qualifier"
+      version="3.1.0.qualifier"
       provider-name="statecharts.org"
       plugin="org.yakindu.sct.doc.user">
 

+ 8 - 0
releng/org.yakindu.sct.renamescript/.classpath

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-9"/>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="src" path="test"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

+ 17 - 0
releng/org.yakindu.sct.renamescript/.project

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>org.yakindu.sct.updateVersion</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

+ 1 - 0
releng/org.yakindu.sct.renamescript/bin/.gitignore

@@ -0,0 +1 @@
+/org/

+ 81 - 0
releng/org.yakindu.sct.renamescript/src/org/yakindu/sct/renamescript/FeatureXMLGetVersionVisitor.java

@@ -0,0 +1,81 @@
+package org.yakindu.sct.renamescript;
+
+import java.io.IOException;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.FileVisitResult;
+import java.nio.file.FileVisitor;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.PathMatcher;
+import java.nio.file.attribute.BasicFileAttributes;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathFactory;
+
+import org.w3c.dom.Document;
+
+public class FeatureXMLGetVersionVisitor implements FileVisitor<Path> {
+
+	private String version;
+	private String filePattern = "feature.xml";
+
+	public static String getVersion(Path startDir) throws IOException {
+		FeatureXMLGetVersionVisitor fileVisitor = new FeatureXMLGetVersionVisitor();
+		Files.walkFileTree(startDir, fileVisitor);
+
+		return fileVisitor.getVersion();
+	}
+
+	private String getVersion() {
+		return this.version;
+	}
+
+	@Override
+	public FileVisitResult postVisitDirectory(Path path, IOException arg1) throws IOException {
+		return FileVisitResult.CONTINUE;
+	}
+
+	@Override
+	public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes arg1) throws IOException {
+		return FileVisitResult.CONTINUE;
+	}
+
+	@Override
+	public FileVisitResult visitFile(Path path, BasicFileAttributes arg1) throws IOException {
+		FileSystem fileSystem = FileSystems.getDefault();
+		PathMatcher pathMatcher = fileSystem.getPathMatcher("glob:" + filePattern);
+
+		if (pathMatcher.matches(path.getFileName())) {
+			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+			try {
+				DocumentBuilder builder = factory.newDocumentBuilder();
+				Document doc = builder.parse(path.toFile());
+
+				XPathFactory xpathfactory = XPathFactory.newInstance();
+				XPath xpath = xpathfactory.newXPath();
+
+				XPathExpression expr = xpath.compile("string(//feature[1]/@version)");
+				Object result = expr.evaluate(doc, XPathConstants.STRING);
+				if (result instanceof String) {
+					this.version = result.toString();
+				}
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+
+			return FileVisitResult.TERMINATE;
+		}
+		return FileVisitResult.CONTINUE;
+	}
+
+	@Override
+	public FileVisitResult visitFileFailed(Path path, IOException arg1) throws IOException {
+		return FileVisitResult.CONTINUE;
+	}
+
+}

+ 93 - 0
releng/org.yakindu.sct.renamescript/src/org/yakindu/sct/renamescript/PomXMLGetVersionVisitor.java

@@ -0,0 +1,93 @@
+package org.yakindu.sct.renamescript;
+
+import java.io.IOException;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.FileVisitResult;
+import java.nio.file.FileVisitor;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.PathMatcher;
+import java.nio.file.attribute.BasicFileAttributes;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathFactory;
+
+import org.w3c.dom.Document;
+
+public class PomXMLGetVersionVisitor implements FileVisitor<Path> {
+
+	private String version;
+	private String filePattern = "pom.xml";
+
+	public static String getVersion(Path startDir) throws IOException {
+		PomXMLGetVersionVisitor fileVisitor = new PomXMLGetVersionVisitor();
+		Files.walkFileTree(startDir, fileVisitor);
+
+		return fileVisitor.getVersion();
+	}
+
+	private String getVersion() {
+		return this.version;
+	}
+
+	@Override
+	public FileVisitResult postVisitDirectory(Path path, IOException arg1) throws IOException {
+		return FileVisitResult.CONTINUE;
+	}
+
+	@Override
+	public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes arg1) throws IOException {
+		return FileVisitResult.CONTINUE;
+	}
+
+	@Override
+	public FileVisitResult visitFile(Path path, BasicFileAttributes arg1) throws IOException {
+		FileSystem fileSystem = FileSystems.getDefault();
+		PathMatcher pathMatcher = fileSystem.getPathMatcher("glob:" + filePattern);
+
+		if (pathMatcher.matches(path.getFileName())) {
+			System.out.println("File: "+path.toAbsolutePath());
+			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+			try {
+				DocumentBuilder builder = factory.newDocumentBuilder();
+				Document doc = builder.parse(path.toFile());
+
+				XPathFactory xpathfactory = XPathFactory.newInstance();
+				XPath xpath = xpathfactory.newXPath();
+
+				XPathExpression expr = xpath.compile("//project[1]/version/text()");
+				Object result = expr.evaluate(doc, XPathConstants.STRING);
+				if (result instanceof String && !((String)result).isEmpty()) {
+					this.version = result.toString();
+				} else {
+					//try in parent tag
+					expr = xpath.compile("//project[1]/parent/version/text()");
+					result = expr.evaluate(doc, XPathConstants.STRING);
+					if (result instanceof String && !((String)result).isEmpty()) {
+						this.version = result.toString();
+					} else {
+						System.out.println("Error: Unable to find version within pom.xmls.");
+						version = null;
+					}
+					
+				}
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+
+			return FileVisitResult.TERMINATE;
+		}
+		return FileVisitResult.CONTINUE;
+	}
+
+	@Override
+	public FileVisitResult visitFileFailed(Path path, IOException arg1) throws IOException {
+		return FileVisitResult.CONTINUE;
+	}
+
+}

+ 104 - 0
releng/org.yakindu.sct.renamescript/src/org/yakindu/sct/renamescript/UpdateVersion.java

@@ -0,0 +1,104 @@
+package org.yakindu.sct.renamescript;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+
+public class UpdateVersion {
+
+	public static final String FEATURE_XML = "feature.xml";
+	public static final String POM_XML = "pom.xml";
+	public static final String MANIFEST = "MANIFEST.MF";
+	public static final String QUALIFIER = ".qualifier";
+	public static final String SNAPSHOT = "-SNAPSHOT";
+
+	public static void updateFeatureXMLs(Path f, String currentVersion, String newVersion) {
+		try {
+			List<Path> result = VersionUpdateVisitor.searchFor(f, FEATURE_XML, currentVersion, newVersion);
+			for (Path path : result) {
+				System.out.println(path + " size = " + Files.size(path) + " bytes");
+			}
+		} catch (IOException ex) {
+			ex.printStackTrace();
+		}
+	}
+
+	public static void updateManifestXMLs(Path f, String currentVersion, String newVersion) {
+		try {
+			List<Path> result = VersionUpdateVisitor.searchFor(f, MANIFEST, currentVersion, newVersion);
+			for (Path path : result) {
+				System.out.println(path + " size = " + Files.size(path) + " bytes");
+			}
+		} catch (IOException ex) {
+			ex.printStackTrace();
+		}
+	}
+
+	public static void updatePomXMLs(Path f, String currentVersion, String newVersion) {
+		try {
+			currentVersion = PomXMLGetVersionVisitor.getVersion(f);
+		} catch (IOException ex) {
+			ex.printStackTrace();
+		}
+
+		newVersion = newVersion.replace(QUALIFIER, SNAPSHOT);
+		try {
+
+			List<Path> result = VersionUpdateVisitor.searchFor(f, POM_XML, currentVersion, newVersion);
+			for (Path path : result) {
+				System.out.println(path + " size = " + Files.size(path) + " bytes");
+			}
+		} catch (IOException ex) {
+			ex.printStackTrace();
+		}
+	}
+
+	public static String getCurrentVersion() {
+		Path f = Paths.get(System.getProperty("user.dir") + "\\..\\..\\");
+		String currentVersion = "error";
+		try {
+			currentVersion = FeatureXMLGetVersionVisitor.getVersion(f);
+		} catch (IOException e1) {
+			e1.printStackTrace();
+		}
+
+		return currentVersion;
+	}
+
+	public static String ask4NewVersion(String currentVersion) {
+		InputStreamReader isr = new InputStreamReader(System.in);
+		BufferedReader br = new BufferedReader(isr);
+		String newVersion = "";
+		System.out
+				.print("Current Version is " + currentVersion + ". Please type the new version (without .qualifier!!).");
+		try {
+			String input = br.readLine();
+			newVersion = input.trim() + QUALIFIER;
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return newVersion;
+	}
+
+	public static void main(String[] args) {
+
+		Path f = Paths.get(System.getProperty("user.dir") + "\\..\\..\\");
+
+		String currentVersion = getCurrentVersion();
+		String newVersion = ask4NewVersion(currentVersion);
+
+		System.out.println("Will update to version: " + newVersion);
+		System.out.println("Search within folder: " + f + " for " + FEATURE_XML + ".");
+
+		updateFeatureXMLs(f, currentVersion, newVersion);
+		updateManifestXMLs(f, currentVersion, newVersion);
+		updatePomXMLs(f, currentVersion, newVersion);
+
+		System.exit(0);
+	}
+
+}

+ 93 - 0
releng/org.yakindu.sct.renamescript/src/org/yakindu/sct/renamescript/VersionUpdateVisitor.java

@@ -0,0 +1,93 @@
+package org.yakindu.sct.renamescript;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.FileVisitResult;
+import java.nio.file.FileVisitor;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.PathMatcher;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.ArrayList;
+import java.util.List;
+
+public class VersionUpdateVisitor implements FileVisitor<Path> {
+
+	private int fileCount = 0;
+	private int dirCount = 0;
+	private String searchPattern = "";
+	private List<Path> searchList;
+	private String replacePattern = "";
+	private String filePattern = "";
+
+	/**
+	 * Pattern examples: "abc*.*" ;  "abc?fg?.j*"
+	 */
+	public static List<Path> searchFor(Path startDir, String filePattern, String searchPattern, String replacePattern)
+			throws IOException {
+
+		VersionUpdateVisitor fileVisitor = new VersionUpdateVisitor(filePattern, searchPattern, replacePattern);
+		Files.walkFileTree(startDir, fileVisitor);
+
+		return fileVisitor.getResultList();
+	}
+
+	private VersionUpdateVisitor(String filePattern, String searchPattern, String newValue) {
+		this.filePattern = filePattern;
+		this.searchPattern = searchPattern;
+		this.replacePattern = newValue;
+		this.searchList = new ArrayList<>();
+	}
+
+	@Override
+	public FileVisitResult postVisitDirectory(Path path, IOException arg1) throws IOException {
+		// System.out.println("postVisitDirectory: " + path + " Exception = " + arg1);
+		return FileVisitResult.CONTINUE;
+	}
+
+	@Override
+	public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes arg1) throws IOException {
+		// System.out.println("preVisitDirectory: " + path + " size = " + arg1.size() +
+		// " bytes");
+		dirCount++;
+		return FileVisitResult.CONTINUE;
+	}
+
+	@Override
+	public FileVisitResult visitFile(Path path, BasicFileAttributes arg1) throws IOException {
+		FileSystem fileSystem = FileSystems.getDefault();
+		PathMatcher pathMatcher = fileSystem.getPathMatcher("glob:" + filePattern);
+
+		if (pathMatcher.matches(path.getFileName())) {
+			searchList.add(path);
+			String charset = Charset.defaultCharset().name();
+			//System.out.println(charset);
+			String content = new String(Files.readAllBytes(path), charset);
+			content = content.replaceAll(this.searchPattern, this.replacePattern);
+			Files.write(path, content.getBytes(charset));
+			fileCount++;
+		}
+		return FileVisitResult.CONTINUE;
+	}
+
+	@Override
+	public FileVisitResult visitFileFailed(Path path, IOException arg1) throws IOException {
+		// System.out.println("visitFileFailed " + " Exception = " + arg1);
+		return FileVisitResult.CONTINUE;
+	}
+
+	public int getFileCount() {
+		return fileCount;
+	}
+
+	public int getDirCount() {
+		return dirCount;
+	}
+
+	public List<Path> getResultList() {
+		return searchList;
+	}
+
+}

+ 22 - 0
releng/org.yakindu.sct.renamescript/test/org/yakindu/sct/renamescript/FeatureXMLGetVersionVisitorTest.java

@@ -0,0 +1,22 @@
+package org.yakindu.sct.renamescript;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.junit.jupiter.api.Test;
+
+class FeatureXMLGetVersionVisitorTest {
+
+	@Test
+	void test() throws IOException
+	{
+		Path f = Paths.get(System.getProperty("user.dir")+"\\..\\..\\");
+		String currentVersion = "error";
+		currentVersion = FeatureXMLGetVersionVisitor.getVersion(f);
+		assertEquals("3.1.0.qualifier",currentVersion);
+	}
+
+}

+ 22 - 0
releng/org.yakindu.sct.renamescript/test/org/yakindu/sct/renamescript/PomXMLGetVersionVisitorTest.java

@@ -0,0 +1,22 @@
+package org.yakindu.sct.renamescript;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.junit.jupiter.api.Test;
+
+class PomXMLGetVersionVisitorTest {
+
+	@Test
+	void test() throws IOException
+	{
+		Path f = Paths.get(System.getProperty("user.dir")+"\\..\\..\\");
+		String currentVersion = "error";
+		currentVersion = PomXMLGetVersionVisitor.getVersion(f);
+		assertEquals("3.1.0-SNAPSHOT",currentVersion);
+	}
+
+}