Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenrik Rentz-Reichert2011-10-01 06:07:30 +0000
committerHenrik Rentz-Reichert2011-10-01 06:07:30 +0000
commit77597cd1f117794512309c1199fd8c13062d01c5 (patch)
tree1c159538a09d07d36793574cdb9f4ee9470667df
parentde3ffd23c0f6f9f20e79431841cae4407700c15e (diff)
downloadorg.eclipse.etrice-77597cd1f117794512309c1199fd8c13062d01c5.tar.gz
org.eclipse.etrice-77597cd1f117794512309c1199fd8c13062d01c5.tar.xz
org.eclipse.etrice-77597cd1f117794512309c1199fd8c13062d01c5.zip
[generator] added helpers to track generated files
-rw-r--r--plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenDir.java47
-rw-r--r--plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenFile.java42
-rw-r--r--plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenFileTreeBuilder.java90
-rw-r--r--plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenItem.java43
-rw-r--r--plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/RecordingFileSystemAccess.java42
5 files changed, 264 insertions, 0 deletions
diff --git a/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenDir.java b/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenDir.java
new file mode 100644
index 000000000..aeb466939
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenDir.java
@@ -0,0 +1,47 @@
+package org.eclipse.etrice.generator.base;
+
+import java.util.ArrayList;
+
+public class GenDir extends GenItem {
+ private ArrayList<GenItem> contents = new ArrayList<GenItem>();
+
+ public GenDir(GenDir parent, String name) {
+ super(parent, name);
+ }
+
+ public ArrayList<GenItem> getContents() {
+ return contents;
+ }
+
+ public ArrayList<GenFile> getSources() {
+ ArrayList<GenFile> sources = new ArrayList<GenFile>();
+ for (GenItem item : contents) {
+ if (item instanceof GenFile)
+ sources.add((GenFile) item);
+ }
+ return sources;
+ }
+
+ public ArrayList<GenDir> getDirs() {
+ ArrayList<GenDir> dirs = new ArrayList<GenDir>();
+ for (GenItem item : contents) {
+ if (item instanceof GenDir)
+ dirs.add((GenDir) item);
+ }
+ return dirs;
+ }
+
+ public boolean hasFilesWithExtension(String ext) {
+ ArrayList<GenFile> sources = getSources();
+ for (GenFile source : sources) {
+ if (source.getExtension().equals(ext))
+ return true;
+ }
+ ArrayList<GenDir> dirs = getDirs();
+ for (GenDir dir : dirs) {
+ if (dir.hasFilesWithExtension(ext))
+ return true;
+ }
+ return false;
+ }
+} \ No newline at end of file
diff --git a/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenFile.java b/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenFile.java
new file mode 100644
index 000000000..bc6d8e96a
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenFile.java
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Henrik Rentz-Reichert (initial contribution)
+ *
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base;
+
+/**
+ * this object represents an (generated) file in a file system
+ *
+ * @author Henrik Rentz-Reichert
+ *
+ */
+public class GenFile extends GenItem {
+
+ public GenFile(GenDir parent, String name) {
+ super(parent, name);
+ }
+
+ public String getBaseName() {
+ int pos = getName().lastIndexOf('.');
+ if (pos>=0)
+ return getName().substring(0, pos);
+ else
+ return getName();
+ }
+
+ public String getExtension() {
+ int pos = getName().lastIndexOf('.');
+ if (pos>=0)
+ return getName().substring(pos+1);
+ else
+ return "";
+ }
+} \ No newline at end of file
diff --git a/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenFileTreeBuilder.java b/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenFileTreeBuilder.java
new file mode 100644
index 000000000..935a43701
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenFileTreeBuilder.java
@@ -0,0 +1,90 @@
+/*******************************************************************************
+ * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Thomas Schuetz and Henrik Rentz-Reichert (initial contribution)
+ *
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base;
+
+import java.util.ArrayList;
+import java.util.Collections;
+
+import org.eclipse.emf.common.util.URI;
+
+/**
+ * @author Henrik Rentz-Reichert
+ *
+ */
+public class GenFileTreeBuilder {
+
+ GenDir genFileTree;
+
+ public GenFileTreeBuilder(String uri, ArrayList<String> files) {
+ this(getURI(uri), files);
+ }
+
+ public GenFileTreeBuilder(URI base, ArrayList<String> files) {
+ ArrayList<String> relPaths = computeFilesAsRelativePaths(base, files);
+ genFileTree = computeGenTree(relPaths);
+ }
+
+ /**
+ * @return the genTree
+ */
+ public GenDir getGenFileTree() {
+ return genFileTree;
+ }
+
+ private ArrayList<String> computeFilesAsRelativePaths(URI base, ArrayList<String> files) {
+ ArrayList<String> relFiles = new ArrayList<String>(files.size());
+ for (String file : files) {
+ String relPath = FileSystemHelpers.getRelativePath(base, URI.createFileURI(file.replace("\\\\", "\\")));
+ if (relPath!=null)
+ relFiles.add(relPath);
+ }
+ Collections.sort(relFiles);
+ return relFiles;
+ }
+
+ private GenDir computeGenTree(ArrayList<String> relPaths) {
+ GenDir root = new GenDir(null, "root");
+ ArrayList<String> paths = relPaths;
+ for (String path : paths) {
+ String[] segments = path.split("/");
+ GenDir dir = makeDir(root, segments);
+ new GenFile(dir, segments[segments.length-1]);
+ }
+ return root;
+ }
+
+ private GenDir makeDir(GenDir current, String[] segments) {
+ for (int i = 0; i < segments.length-1; i++) {
+ GenDir next = null;
+ for (GenItem item : current.getContents()) {
+ if (item instanceof GenDir && item.getName().equals(segments[i])) {
+ next = (GenDir) item;
+ break;
+ }
+ }
+ if (next==null) {
+ next = new GenDir(current, segments[i]);
+ }
+ current = next;
+ }
+ return current;
+ }
+
+ private static URI getURI(String uri) {
+ URI base = URI.createFileURI(uri);
+ if (base.hasTrailingPathSeparator())
+ base = base.trimSegments(1);
+ return base;
+ }
+
+}
diff --git a/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenItem.java b/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenItem.java
new file mode 100644
index 000000000..15bb52ad7
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/GenItem.java
@@ -0,0 +1,43 @@
+/*******************************************************************************
+ * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Henrik Rentz-Reichert (initial contribution)
+ *
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base;
+
+
+public class GenItem {
+ private GenDir parent;
+ private String name;
+
+ public GenItem(GenDir parent, String name) {
+ super();
+ this.parent = parent;
+ this.name = name;
+
+ if (parent!=null)
+ parent.getContents().add(this);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public GenDir getParent() {
+ return parent;
+ }
+
+ public String getPath() {
+ if (parent==null)
+ return "";
+
+ return parent.getPath()+name+"/";
+ }
+} \ No newline at end of file
diff --git a/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/RecordingFileSystemAccess.java b/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/RecordingFileSystemAccess.java
new file mode 100644
index 000000000..103948bcf
--- /dev/null
+++ b/plugins/org.eclipse.etrice.generator/src/org/eclipse/etrice/generator/base/RecordingFileSystemAccess.java
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * CONTRIBUTORS:
+ * Henrik Rentz-Reichert (initial contribution)
+ *
+ *******************************************************************************/
+
+package org.eclipse.etrice.generator.base;
+
+import java.util.ArrayList;
+
+import org.eclipse.xtext.generator.JavaIoFileSystemAccess;
+
+/**
+ * a {@link JavaIoFileSystemAccess} which is recording the generated files
+ *
+ * @author Henrik Rentz-Reichert
+ *
+ */
+public class RecordingFileSystemAccess extends JavaIoFileSystemAccess {
+
+ private ArrayList<String> files = new ArrayList<String>();
+
+ @Override
+ public void generateFile(String fileName, String slot, CharSequence contents) {
+ String outlet = getPathes().get(slot);
+ if (outlet!=null) {
+ String pathName = toSystemFileName(outlet + "/" + fileName);
+ files.add(pathName);
+ }
+ super.generateFile(fileName, slot, contents);
+ }
+
+ public ArrayList<String> getFiles() {
+ return files;
+ }
+}

Back to the top