├── .classpath
├── .gitignore
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
├── META-INF
└── MANIFEST.MF
├── README.md
├── build.properties
├── feature
├── .project
├── build.properties
└── feature.xml
├── icons
└── sample.gif
├── plugin.xml
└── src
└── org
└── scalastuff
├── esbt
├── BuildSbtFile.java
├── Console.java
├── CopyJars.java
├── CreateSbtPlugin.java
├── Dependency.java
├── DotClassPathFile.java
├── DotProjectFile.java
├── Esbt.java
├── EsbtPropertyTester.java
├── ExecuteSbtCommand.java
├── FileContent.java
├── Initializer.java
├── InvokeSbt.java
├── ManifestFile.java
├── Processor.java
├── ProjectInfo.java
├── ResourceChangeListener.java
├── SbtEclipsePlugin.scala.source
├── UpdateProjectConfigurationCommand.java
├── Utils.java
├── WorkspaceInfo.java
└── sbt-launch.jar
└── osgitools
├── OsgiManifest.java
├── Osgiify.java
├── OsgiifyIvy.java
└── util
└── StringTokenizer.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /project
2 | /target
3 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | scalastuff.esbt
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.pde.ManifestBuilder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.pde.PluginNature
21 | org.eclipse.jdt.core.javanature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | #Sat Jun 11 08:22:36 CEST 2011
2 | eclipse.preferences.version=1
3 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5 | org.eclipse.jdt.core.compiler.compliance=1.6
6 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
7 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
8 | org.eclipse.jdt.core.compiler.source=1.6
9 |
--------------------------------------------------------------------------------
/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Bundle-ManifestVersion: 2
3 | Bundle-Name: Eclipse Sbt Plugin
4 | Bundle-SymbolicName: scalastuff.esbt;singleton:=true
5 | Bundle-Version: 0.11.3
6 | Bundle-Vendor: ScalaStuff
7 | Require-Bundle: org.eclipse.ui,
8 | org.eclipse.core.runtime,
9 | org.eclipse.ui.console;bundle-version="[3.5.0,3.8.0)",
10 | org.eclipse.core.resources;bundle-version="[3.6.0,3.8.0)",
11 | org.eclipse.core.expressions;bundle-version="[3.4.0,3.8.0)"
12 | Bundle-RequiredExecutionEnvironment: JavaSE-1.6
13 | Bundle-ActivationPolicy: lazy
14 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Eclipse SBT Plugin
2 | ==================
3 |
4 | # Features:
5 |
6 | - Based on SBT 0.11.1
7 | - automatically downloads dependencies using SBT
8 | - automatically updates eclipse project configuration
9 | - attaches sources of dependencies that have withSources()
10 | - resolves dependencies between workspace projects
11 | - plays nice with scala IDE (e.g. excludes scala library)
12 | - non-intrusive: respects existing content in .classpath
13 | - graceful: no menu entries are shown when project doesn't contain SBT files
14 | - smart copying of jars to allow updating locked jars
15 |
16 | # Installation
17 |
18 | Update site:
19 |
20 | https://raw.github.com/scalastuff/updatesite/master
21 |
22 | # Usage
23 |
24 | Create an SBT configuration for your project (build.sbt or project/X.scala).
25 | Any change to SBT files will trigger esbt to update dependencies and reconfigure the eclipse project.
26 | One can manually trigger the update by choosing "SBT Update Project Configuration" from the project context menu.
27 |
28 | One can issue an SBT command by choosing "SBT Command..." from the project context menu.
29 |
30 | # Release Notes
31 |
32 | ## 0.11.3
33 |
34 | - upgraded to sbt 0.11.1
35 |
36 | ## 0.11.2
37 |
38 | - Recognize ivy packages with bundles instead of jars
39 |
40 | ## 0.11.1
41 |
42 | - Removed project rename
43 |
44 | ## 0.11.0
45 |
46 | - Based on SBT 0.11
47 | - Included fix from Pablo Lalloni
48 |
49 | ## 0.10.9
50 |
51 | - Based on SBT 0.10.1
52 | - build.sbt is no longer required nor parsed
53 | - Project settings (name, version , source directories) are now correctly obtained through SBT
54 | - Project is renamed automatically to organization.name. Useful for fresh 'trunk' checkouts
55 | - Automatically adds scala nature, builder and classpath entries
56 |
57 | ## 0.10.7
58 |
59 | - Fixed bug with incomplete projects being copied to tmp dir
60 | - Only copy project dir when needed (when project dependencies are detected)
61 | - Robust against jar files locked by eclipse
62 | - Provisional OSGi support (create META-INF/MANIFEST.MF with line Allow-ESBT: true)
63 |
64 | ## 0.10.3
65 |
66 | - Added support for library sources
67 | - Dependencies are now ordered by name
68 |
69 |
--------------------------------------------------------------------------------
/build.properties:
--------------------------------------------------------------------------------
1 | source.. = src/
2 | output.. = bin/
3 | bin.includes = plugin.xml,\
4 | META-INF/,\
5 | .,\
6 | icons/
7 |
--------------------------------------------------------------------------------
/feature/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | scalastuff.esbt.feature
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.pde.FeatureBuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.pde.FeatureNature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/feature/build.properties:
--------------------------------------------------------------------------------
1 | bin.includes = feature.xml
2 |
--------------------------------------------------------------------------------
/feature/feature.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 | Eclipse SBT Plugin
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/icons/sample.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/scalastuff/esbt/052e3ad1ee864aa63fbee6f778e6c3e802ee014b/icons/sample.gif
--------------------------------------------------------------------------------
/plugin.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
7 |
10 |
11 |
16 |
17 |
22 |
23 |
24 |
26 |
28 |
44 |
49 |
50 |
52 |
53 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
65 |
67 |
68 |
69 |
70 |
71 |
77 |
78 |
79 |
80 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/BuildSbtFile.java:
--------------------------------------------------------------------------------
1 | package org.scalastuff.esbt;
2 |
3 | import java.util.ArrayList;
4 | import java.util.HashSet;
5 | import java.util.List;
6 | import java.util.Set;
7 |
8 | public class BuildSbtFile extends FileContent {
9 |
10 | protected BuildSbtFile(ProjectInfo project) {
11 | super(project.getProject().getFile("build.sbt"));
12 | }
13 |
14 | public String getOrganization() {
15 | return getSbtPropertyValue("organization");
16 | }
17 |
18 | public String getName() {
19 | return getSbtPropertyValue("name");
20 | }
21 |
22 | public String getVersion() {
23 | return getSbtPropertyValue("version");
24 | }
25 |
26 |
27 | public Set getProjectDependencies() {
28 | Set result = new HashSet();
29 | for (Dependency dep : getLibraryDependencies()) {
30 | ProjectInfo depPrj = WorkspaceInfo.findProject(dep.organization, dep.name, dep.version);
31 | if (depPrj != null) result.add(depPrj);
32 | }
33 | return result;
34 | }
35 |
36 | public List getLibraryDependencies() {
37 | List result = new ArrayList();
38 | for (int i = 0; i < getContent().size(); i++) {
39 | String line = getContent().get(i);
40 | if (line.trim().startsWith("libraryDependencies")) {
41 | for (int j = 0; countBraces(line) > 0 && i < getContent().size(); i++, j++) {
42 | if (j > 0) {
43 | line = line + getContent().get(i);
44 | }
45 | }
46 |
47 | String[] depStrings = line.split(",");
48 | if (depStrings.length > 0) {
49 | for (int count = countBraces(depStrings[0]); count > 0; count--) {
50 | depStrings[depStrings.length - 1] = removeLast(depStrings[depStrings.length - 1], ')');
51 | }
52 | for (String depString : depStrings) {
53 | result.add(getLibraryDependency(depString));
54 | }
55 | }
56 | }
57 | }
58 | return result;
59 | }
60 |
61 | public boolean hasProjectDependencies() {
62 | for (Dependency dep : getLibraryDependencies()) {
63 | if (WorkspaceInfo.findProject(dep.organization, dep.name, dep.version) != null) {
64 | return true;
65 | }
66 | }
67 | return false;
68 | }
69 |
70 | public static Dependency getLibraryDependency(String depString) {
71 | List values = readStrings(depString);
72 | Dependency dependency = new Dependency();
73 | dependency.crossCompiled = depString.contains("%%");
74 | for (int i = 0; i < values.size(); i++) {
75 | switch (i) {
76 | case 0: dependency.organization = values.get(i).trim(); break;
77 | case 1: dependency.name = values.get(i).trim(); break;
78 | case 2: dependency.version = values.get(i).trim(); break;
79 | case 3: dependency.qualifier = values.get(i).trim(); break;
80 | }
81 | }
82 | dependency.rest = afterLast(depString, '"').trim();
83 | return dependency;
84 | }
85 |
86 | public List getContentWithoutProjectDependencies() {
87 | List result = new ArrayList();
88 | for (int i = 0; i < getContent().size(); i++) {
89 | String line = getContent().get(i);
90 | if (line.trim().startsWith("libraryDependencies")) {
91 | for (int j = 0; countBraces(line) > 0 && i < getContent().size(); i++, j++) {
92 | if (j > 0) {
93 | line = line + getContent().get(i);
94 | }
95 | }
96 | } else {
97 | result.add(line);
98 | }
99 | }
100 | for (Dependency dep : getLibraryDependencies()) {
101 | if (WorkspaceInfo.findProject(dep.organization, dep.name, dep.version) == null) {
102 | result.add("");
103 | result.add("libraryDependencies += \"" + dep.organization + "\" " + (dep.crossCompiled ? "%%" : "%") + " \"" + dep.name + "\" % \"" + dep.version + (!dep.qualifier.equals("") ? "\" % \"" + dep.qualifier + "\"" : "\"") + " " + dep.rest);
104 | }
105 | }
106 | return result;
107 | }
108 |
109 | private String getSbtPropertyValue(String property) {
110 | for (String line : getContent()) {
111 | line = getSbtPropertyLine(line, property);
112 | if (line.startsWith("\"") && line.endsWith("\"")) {
113 | return line.substring(1, line.length() - 1);
114 | }
115 | }
116 | return "";
117 | }
118 |
119 | private static String getSbtPropertyLine(String line, String property) {
120 | line = line.trim();
121 | if (line.startsWith(property)) {
122 | line = line.substring(property.length()).trim();
123 | if (line.startsWith(":=") || line.startsWith("+=")) {
124 | return line.substring(2).trim();
125 | }
126 | }
127 | return "";
128 | }
129 |
130 |
131 | private static List readStrings(String line) {
132 | List result = new ArrayList();
133 | boolean insideString = false;
134 | StringBuilder out = new StringBuilder();
135 | for (int pos = 0; pos < line.length(); pos++) {
136 | char c = line.charAt(pos);
137 | if (c == '"') {
138 | if (insideString) {
139 | result.add(out.toString());
140 | out.setLength(0);
141 | }
142 | insideString = !insideString;
143 | } else {
144 | if (insideString) {
145 | out.append(c);
146 | }
147 | }
148 | }
149 | return result;
150 | }
151 |
152 | private static int countBraces(String line) {
153 | int count = 0;
154 | for (int i = 0; i < line.length(); i++) {
155 | if (line.charAt(i) == '(') count++;
156 | else if (line.charAt(i) == ')') count--;
157 | }
158 | return count;
159 | }
160 |
161 | private static String afterLast(String s, char c) {
162 | int index = s.lastIndexOf(c);
163 | if (index < 0) return "";
164 | else return s.substring(index + 1);
165 | }
166 |
167 | private static String removeLast(String s, char c) {
168 | int index = s.lastIndexOf(c);
169 | if (index < 0) return s;
170 | else return s.substring(0, index) + s.substring(index + 1);
171 | }
172 | }
173 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/Console.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 |
18 | import java.io.IOException;
19 |
20 | import org.eclipse.ui.IWorkbenchWindow;
21 | import org.eclipse.ui.PartInitException;
22 | import org.eclipse.ui.console.ConsolePlugin;
23 | import org.eclipse.ui.console.IConsole;
24 | import org.eclipse.ui.console.IConsoleConstants;
25 | import org.eclipse.ui.console.IConsoleManager;
26 | import org.eclipse.ui.console.IConsoleView;
27 | import org.eclipse.ui.console.IOConsoleInputStream;
28 | import org.eclipse.ui.console.MessageConsole;
29 | import org.eclipse.ui.console.MessageConsoleStream;
30 |
31 | public class Console {
32 |
33 | private MessageConsole console = findConsole("SBT Console");
34 | private MessageConsoleStream stream;
35 |
36 | public void clear() {
37 | console.clearConsole();
38 | console.activate();
39 | }
40 |
41 | public IOConsoleInputStream getInputStream() {
42 | return console.getInputStream();
43 | }
44 |
45 | public void println(String message) {
46 | if (stream == null) {
47 | stream = console.newMessageStream();
48 | }
49 | stream.println(message);
50 | }
51 |
52 | private static MessageConsole findConsole(String name) {
53 | ConsolePlugin plugin = ConsolePlugin.getDefault();
54 | IConsoleManager conMan = plugin.getConsoleManager();
55 | IConsole[] existing = conMan.getConsoles();
56 | for (int i = 0; i < existing.length; i++)
57 | if (name.equals(existing[i].getName()))
58 | return (MessageConsole) existing[i];
59 | //no console found, so create a new one
60 | MessageConsole myConsole = new MessageConsole(name, null);
61 | conMan.addConsoles(new IConsole[]{myConsole});
62 | return myConsole;
63 | }
64 |
65 | public void reveal(IWorkbenchWindow window) throws PartInitException {
66 | String id = IConsoleConstants.ID_CONSOLE_VIEW;
67 | IConsoleView view = (IConsoleView) window.getActivePage().showView(id);
68 | view.display(console);
69 | }
70 |
71 | public void close() throws IOException {
72 | if (stream != null) {
73 | stream.close();
74 | }
75 | stream = null;
76 | }
77 |
78 | public void activate() {
79 | console.activate();
80 |
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/CopyJars.java:
--------------------------------------------------------------------------------
1 | package org.scalastuff.esbt;
2 |
3 | import java.io.File;
4 | import java.io.FileInputStream;
5 | import java.io.FileNotFoundException;
6 | import java.io.FileOutputStream;
7 | import java.io.IOException;
8 |
9 | public class CopyJars {
10 |
11 | public static final File JARS_DIR = new File(Esbt.ESBT_HOME, "jars");
12 |
13 | public static String copyFile(Dependency dep, File baseDir, String file) throws FileNotFoundException, IOException {
14 | if (!file.trim().isEmpty()) {
15 | if (!new File(file).isAbsolute()) {
16 | file = new File(baseDir, file).toString();
17 | }
18 | return copyFile(new File(file), new File(JARS_DIR, dep.organization + "-" + new File(file).getName())).getCanonicalPath();
19 | }
20 | return file;
21 | }
22 |
23 | private static File copyFile(File source, File dest) throws FileNotFoundException, IOException {
24 | int attempt = 0;
25 | String destPrefix = dest.getPath();
26 | String destPostfix = "";
27 | int i = destPrefix.lastIndexOf('.');
28 | if (i >= 0) {
29 | destPostfix = destPrefix.substring(i);
30 | destPrefix = destPrefix.substring(0, i);
31 | }
32 | while (!dest.exists()
33 | ||source.lastModified() != dest.lastModified()
34 | || source.length() != dest.lastModified()) {
35 | try {
36 | dest.getParentFile().mkdirs();
37 | Utils.copy(new FileInputStream(source), new FileOutputStream(dest));
38 | dest.setLastModified(source.lastModified());
39 | break;
40 | } catch (IOException e) {
41 | if (attempt++ > 50) {
42 | throw new IOException("Couldn't copy file " + source + " to " + dest + ": " + e.getMessage(), e);
43 | }
44 | dest = new File(destPrefix + "-" + attempt + destPostfix);
45 | }
46 | }
47 | return dest;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/CreateSbtPlugin.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 |
18 | import static org.scalastuff.esbt.Utils.indexOfLineContaining;
19 | import static org.scalastuff.esbt.Utils.read;
20 | import static org.scalastuff.esbt.Utils.write;
21 |
22 | import java.io.File;
23 | import java.io.FileOutputStream;
24 | import java.io.IOException;
25 | import java.io.InputStream;
26 | import java.util.List;
27 |
28 | public class CreateSbtPlugin {
29 |
30 | public static final File PLUGIN_HOME = Esbt.USER_HOME;
31 |
32 | public static void createSbtPlugin() throws IOException {
33 | File pluginDir = new File(PLUGIN_HOME, ".sbt/plugins");
34 | pluginDir.mkdirs();
35 | createBuildSbt(pluginDir);
36 | createSbtEclipsePluginFile(pluginDir);
37 | }
38 |
39 | private static void createBuildSbt(File pluginDir) throws IOException {
40 | File file = new File(pluginDir, "build.sbt");
41 | if (!file.exists()) {
42 | file.createNewFile();
43 | }
44 | List lines = read(file);
45 | if (indexOfLineContaining(lines, "sbtPlugin := true") < 0) {
46 | lines.add("");
47 | lines.add("sbtPlugin := true");
48 | write(new FileOutputStream(file), lines);
49 | }
50 | }
51 |
52 | private static void createSbtEclipsePluginFile(File pluginDir) throws IOException {
53 | InputStream is = CreateSbtPlugin.class.getResourceAsStream("SbtEclipsePlugin.scala.source");
54 | if (is == null) {
55 | throw new IOException("Coulnd't find SbtEclipsePlugin.scala.source");
56 | }
57 | List content = read(is);
58 | File destFile = new File(pluginDir, "SbtEclipsePlugin.scala");
59 | List existingContent = read(destFile);
60 | if (!content.equals(existingContent)) {
61 | write(new FileOutputStream(destFile), content);
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/Dependency.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 |
18 | public class Dependency {
19 | public boolean crossCompiled = false;
20 | public String organization = "";
21 | public String name = "";
22 | public String version = "";
23 | public String qualifier = "";
24 | public String jar = "";
25 | public String srcJar = "";
26 | public String rest = "";
27 | public String osgiSymbolicName;
28 | public String osgiBundleVersion;
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/DotClassPathFile.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 |
18 | import static org.scalastuff.esbt.Utils.indexOfLineContaining;
19 |
20 | import java.util.ArrayList;
21 | import java.util.List;
22 | import java.util.Set;
23 | import java.util.TreeMap;
24 |
25 | import org.eclipse.core.runtime.CoreException;
26 |
27 | public class DotClassPathFile extends FileContent {
28 |
29 | private final ProjectInfo project;
30 |
31 | public static boolean isUnderSbtControl(List lines) {
32 | return true;//lines.isEmpty() || lines.contains(PLUGIN_INDICATION);
33 | }
34 |
35 | public DotClassPathFile(ProjectInfo project) {
36 | super(project.getProject().getFile(".classpath"));
37 | this.project = project;
38 | }
39 |
40 | public void write(Set projectDeps, List deps) throws CoreException {
41 | super.doWrite(update(project, refresh(), projectDeps, deps));
42 | }
43 |
44 | private static List update(ProjectInfo project, List lines, Set projectDeps, List deps) {
45 | lines = new ArrayList(lines);
46 | if (lines.isEmpty()) {
47 | lines.add("");
48 | lines.add("");
49 | lines.add("");
50 | }
51 | for (Dependency dep : deps) {
52 | if (dep.jar.trim().equals("")) continue;
53 | int line = indexOfLineContaining(lines, dep.jar);
54 | if (line != -1) {
55 | lines.remove(line);
56 | }
57 | }
58 | for (int i = lines.size() - 1; i >= 0; i--) {
59 | if (lines.get(i).contains("from=\"sbt\"")) {
60 | lines.remove(i);
61 | }
62 | }
63 |
64 | int line = indexOfLineContaining(lines, "");
65 | if (line < 0) {
66 | line = lines.size();
67 | }
68 | boolean scalaContainer = indexOfLineContaining(lines, "SCALA_CONTAINER") != -1;
69 | scalaContainer = true; // always skip scala library
70 | TreeMap sortedDeps = new TreeMap();
71 | for (Dependency dep : deps) {
72 | sortedDeps.put(dep.name, dep);
73 | }
74 | for (Dependency dep : sortedDeps.values()) {
75 | if (scalaContainer && dep.name.equals("scala-library")) continue;
76 | lines.add(line++, "\t");
77 | }
78 | for (ProjectInfo dep : projectDeps) {
79 | lines.add(line++, "\t");
80 | }
81 |
82 | if (indexOfLineContaining(lines, "kind=\"con\"") < 0) {
83 | // lines.add(line++, "\t");
84 | }
85 | if (indexOfLineContaining(lines, "kind=\"output\"") < 0) {
86 | lines.add(line++, "\t");
87 | }
88 | @SuppressWarnings("unchecked")
89 | List[] sourcePaths = new List[] { project.getSourceDirectories(), project.getResourceDirectories(), project.getTestSourceDirectories(), project.getTestResourceDirectories() };
90 | for (List paths : sourcePaths) {
91 | for (String path : paths) {
92 | if (project.getProject().getFolder(path).exists()) {
93 | if (indexOfLineContaining(lines, path) < 0) {
94 | lines.add(line++, "\t");
95 | }
96 | }
97 | }
98 | }
99 |
100 | return lines;
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/DotProjectFile.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 |
18 | import static org.scalastuff.esbt.Utils.indexOfLineContaining;
19 |
20 | import java.util.ArrayList;
21 | import java.util.List;
22 | import java.util.Set;
23 | import java.util.TreeMap;
24 |
25 | import org.eclipse.core.resources.IProjectDescription;
26 | import org.eclipse.core.runtime.CoreException;
27 |
28 | public class DotProjectFile extends FileContent {
29 |
30 | private final ProjectInfo project;
31 |
32 | public static boolean isUnderSbtControl(List lines) {
33 | return true;//lines.isEmpty() || lines.contains(PLUGIN_INDICATION);
34 | }
35 |
36 | public DotProjectFile(ProjectInfo project) {
37 | super(project.getProject().getFile(".project"));
38 | this.project = project;
39 | }
40 |
41 | public void write(Set projectDeps, List deps) throws CoreException {
42 | super.doWrite(update(project, refresh(), projectDeps, deps));
43 | }
44 |
45 | private static List update(ProjectInfo project, List lines, Set projectDeps, List deps) throws CoreException {
46 | lines = new ArrayList(lines);
47 | if (lines.isEmpty()) {
48 | lines.add("");
49 | lines.add("");
50 | lines.add("");
51 | }
52 | for (Dependency dep : deps) {
53 | if (dep.jar.trim().equals("")) continue;
54 | int line = indexOfLineContaining(lines, dep.jar);
55 | if (line != -1) {
56 | lines.remove(line);
57 | }
58 | }
59 | IProjectDescription desc = project.getProject().getDescription();
60 | if (!project.getName().equals("")) {
61 | if (!project.getOrganization().equals("")) {
62 | desc.setName(project.getOrganization() + "." + project.getName());
63 | } else {
64 | desc.setName(project.getName());
65 | }
66 | }
67 |
68 | for (int i = lines.size() - 1; i >= 0; i--) {
69 | if (lines.get(i).contains("from=\"sbt\"") && lines.get(i).contains("")) {
70 | // lines.remove(i);
71 | while (i < lines.size() && lines.get(i).contains(""));
72 | }
73 | }
74 |
75 | int line = indexOfLineContaining(lines, "");
76 | if (line < 0) {
77 | line = lines.size();
78 | }
79 | boolean scalaContainer = indexOfLineContaining(lines, "SCALA_CONTAINER") != -1;
80 | TreeMap sortedDeps = new TreeMap();
81 | for (Dependency dep : deps) {
82 | sortedDeps.put(dep.name, dep);
83 | }
84 | for (Dependency dep : sortedDeps.values()) {
85 | if (scalaContainer && dep.name.equals("scala-library")) continue;
86 | lines.add(line++, "\t");
87 | }
88 | for (ProjectInfo dep : projectDeps) {
89 | lines.add(line++, "\t");
90 | }
91 |
92 | if (indexOfLineContaining(lines, "kind=\"con\"") < 0) {
93 | lines.add(line++, "\t");
94 | }
95 | if (indexOfLineContaining(lines, "kind=\"output\"") < 0) {
96 | lines.add(line++, "\t");
97 | }
98 | String[] sourcePaths = new String[] {"src/main/scala", "src/main/java", "src/test/scala", "src/main/java"};
99 | for (String path : sourcePaths) {
100 | if (project.getProject().getFolder(path).exists()) {
101 | if (indexOfLineContaining(lines, path) < 0) {
102 | lines.add(line++, "\t");
103 | }
104 | }
105 | }
106 |
107 | return lines;
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/Esbt.java:
--------------------------------------------------------------------------------
1 | package org.scalastuff.esbt;
2 |
3 | import java.io.File;
4 |
5 | public class Esbt {
6 |
7 | public static File USER_HOME = new File(System.getProperty("user.home"));
8 | public static File ESBT_HOME = new File(USER_HOME, ".esbt");
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/EsbtPropertyTester.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 | import org.eclipse.core.expressions.PropertyTester;
18 |
19 |
20 | public class EsbtPropertyTester extends PropertyTester {
21 |
22 | @Override
23 | public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
24 | ProjectInfo project = WorkspaceInfo.adaptToProject(receiver);
25 | return project != null && project.isSbtProject();
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/ExecuteSbtCommand.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 |
18 | import static org.scalastuff.esbt.Utils.read;
19 |
20 | import java.io.File;
21 | import java.io.FileOutputStream;
22 | import java.util.Arrays;
23 | import java.util.List;
24 |
25 | import org.eclipse.core.commands.AbstractHandler;
26 | import org.eclipse.core.commands.ExecutionEvent;
27 | import org.eclipse.core.commands.ExecutionException;
28 | import org.eclipse.jface.dialogs.InputDialog;
29 | import org.eclipse.swt.widgets.Shell;
30 | import org.eclipse.ui.handlers.HandlerUtil;
31 |
32 | public class ExecuteSbtCommand extends AbstractHandler {
33 | @Override
34 | public Object execute(ExecutionEvent event) throws ExecutionException {
35 | try {
36 | File file = new File(WorkspaceInfo.getMetaDataDir(), "last-command");
37 | List lastCommand = read(file);
38 | String value = lastCommand.isEmpty() ? "" : lastCommand.get(0);
39 |
40 | Shell shell = HandlerUtil.getActiveWorkbenchWindow(event).getShell();
41 | InputDialog dialog = new InputDialog(shell, "Execute SBT command", "SBT command: ", value, null);
42 |
43 | if (dialog.open() == InputDialog.OK) {
44 | value = dialog.getValue();
45 | Utils.write(new FileOutputStream(file), Arrays.asList(value));
46 |
47 | Processor processor = new Processor();
48 | processor.setProjects(WorkspaceInfo.getSelectedProjects(event));
49 | processor.setCommand(value);
50 | processor.schedule();
51 | }
52 | } catch (Exception e) {
53 | // TODO Auto-generated catch block
54 | e.printStackTrace();
55 | }
56 | return null;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/FileContent.java:
--------------------------------------------------------------------------------
1 | package org.scalastuff.esbt;
2 |
3 | import java.util.Collections;
4 | import java.util.List;
5 |
6 | import org.eclipse.core.resources.IFile;
7 | import org.eclipse.core.runtime.CoreException;
8 |
9 | public class FileContent {
10 |
11 | private final IFile file;
12 | private List lines = Collections.emptyList();
13 |
14 | protected FileContent(IFile file) {
15 | this(file, true);
16 | }
17 |
18 | protected FileContent(IFile file, boolean refresh) {
19 | this.file = file;
20 | if (refresh) {
21 | refresh();
22 | }
23 | }
24 |
25 | public IFile getFile() {
26 | return file;
27 | }
28 |
29 | public boolean exists() {
30 | return file.exists();
31 | }
32 |
33 | public synchronized boolean isUpToDate() {
34 | try {
35 | file.refreshLocal(IFile.DEPTH_INFINITE, null);
36 | } catch (CoreException e) {
37 | }
38 | return Utils.read(file).equals(lines);
39 | }
40 |
41 | public synchronized List refresh() {
42 | try {
43 | file.refreshLocal(IFile.DEPTH_INFINITE, null);
44 | setLines(Utils.read(file));
45 | } catch (CoreException e) {
46 | setLines(Collections.emptyList());
47 | }
48 | return lines;
49 | }
50 |
51 | public synchronized List getContent() {
52 | return lines;
53 | }
54 |
55 | protected void setLines(List lines) {
56 | this.lines = lines;
57 | }
58 |
59 | protected final synchronized void doWrite(List lines) throws CoreException {
60 | refresh();
61 | if (!this.lines.equals(lines)) {
62 | if (file.exists()) {
63 | file.setContents(Utils.linesInputStream(lines), 0, null);
64 | } else {
65 | file.create(Utils.linesInputStream(lines), 0, null);
66 | }
67 | setLines(lines);
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/Initializer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 | import org.eclipse.core.resources.IWorkspace;
18 | import org.eclipse.core.resources.ResourcesPlugin;
19 | import org.eclipse.ui.IStartup;
20 |
21 |
22 | public class Initializer implements IStartup {
23 |
24 | @Override
25 | public void earlyStartup() {
26 | IWorkspace workspace = ResourcesPlugin.getWorkspace();
27 | workspace.addResourceChangeListener(new ResourceChangeListener());
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/InvokeSbt.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 |
18 | import static org.scalastuff.esbt.Utils.copy;
19 |
20 | import java.io.BufferedReader;
21 | import java.io.File;
22 | import java.io.FileNotFoundException;
23 | import java.io.FileOutputStream;
24 | import java.io.IOException;
25 | import java.io.InputStream;
26 | import java.io.InputStreamReader;
27 | import java.io.OutputStream;
28 | import java.util.ArrayList;
29 | import java.util.List;
30 |
31 | public class InvokeSbt {
32 |
33 | private static File launchJar;
34 | private final Console console;
35 | private final List result = new ArrayList();
36 | private boolean error;
37 | private String command = "update-eclipse";
38 | private File projectDir;
39 |
40 | public InvokeSbt(ProjectInfo project, String command, Console console) {
41 | this.projectDir = project.getProjectDir();
42 | this.command = command;
43 | this.console = console;
44 | }
45 |
46 | public String getCommand() {
47 | return command;
48 | }
49 |
50 | public File getProjectDir() {
51 | return projectDir;
52 | }
53 |
54 | public void setProjectDir(File projectDir) {
55 | this.projectDir = projectDir;
56 | }
57 |
58 | public void invokeSbt() throws IOException {
59 | Process process = createProcess(projectDir);
60 | readProcessOutput(process);
61 | }
62 |
63 | public boolean hasError() {
64 | return error;
65 | }
66 |
67 | public List getResult() {
68 | return result;
69 | }
70 |
71 | private Process createProcess(File projectDir) throws IOException {
72 |
73 | // find java executable
74 | File javaHome = new File(System.getProperty("java.home"));
75 | File javaExec = new File(javaHome, "bin/java");
76 | if (!javaExec.exists()) {
77 | javaExec = new File(javaHome, "bin/javaw.exe");
78 | }
79 | ArrayList args = new ArrayList();
80 | args.add(javaExec.toString());
81 | args.add("-Dsbt.log.noformat=true");
82 | args.add("-Duser.home=" + CreateSbtPlugin.PLUGIN_HOME);
83 | args.add("-jar");
84 | args.add(getLaunchJar().toString());
85 | args.addAll(parseCommand(command));
86 | final ProcessBuilder pb = new ProcessBuilder(args);
87 | pb.directory(projectDir);
88 | pb.redirectErrorStream(true);
89 | return pb.start();
90 | }
91 |
92 | private List parseCommand(String command) {
93 | List result = new ArrayList();
94 | boolean insideString = false;
95 | StringBuilder out = new StringBuilder();
96 | for (int pos = 0; pos < command.length(); pos++) {
97 | char c = command.charAt(pos);
98 | if (c == '"') {
99 | if (insideString) {
100 | result.add(out.toString());
101 | } else {
102 | result.add(out.toString().trim());
103 | }
104 | out.setLength(0);
105 | insideString = !insideString;
106 | } else {
107 | if (c == ' ') {
108 | result.add(out.toString().trim());
109 | out.setLength(0);
110 | } else {
111 | out.append(c);
112 | }
113 | }
114 | }
115 | result.add(out.toString().trim());
116 | return result;
117 | }
118 |
119 | private void readProcessOutput(final Process process) throws IOException {
120 | BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
121 | String line;
122 |
123 | result.clear();
124 |
125 | // make sure process does not wait for input
126 | process.getOutputStream().close();
127 | while ((line = br.readLine()) != null) {
128 |
129 | if (line.startsWith("Project loading failed:")) {
130 | }
131 |
132 | // detect result
133 | else if (line.startsWith("[result]")) {
134 | result.add(line.substring("[result]".length()).trim());
135 | } else {
136 | if (line.startsWith("[error]")) {
137 | error = true;
138 | }
139 | console.println(line);
140 | }
141 | }
142 | }
143 |
144 | private File getLaunchJar() throws FileNotFoundException, IOException {
145 | if (launchJar == null) {
146 | File launchJar = File.createTempFile("sbt-launch", ".jar");
147 | InputStream is = InvokeSbt.class.getResourceAsStream("sbt-launch.jar");
148 | if (is == null) {
149 | console.println("Coulnd't find sbt-launch.jar");
150 | }
151 | OutputStream os = new FileOutputStream(launchJar);
152 | copy(is, os);
153 | InvokeSbt.launchJar = launchJar;
154 | }
155 | return launchJar;
156 | }
157 | }
158 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/ManifestFile.java:
--------------------------------------------------------------------------------
1 | package org.scalastuff.esbt;
2 |
3 | import static org.scalastuff.esbt.Utils.copy;
4 |
5 | import java.io.File;
6 | import java.io.IOException;
7 | import java.net.URI;
8 | import java.util.ArrayList;
9 | import java.util.List;
10 | import java.util.Set;
11 |
12 | import org.eclipse.core.runtime.CoreException;
13 | import org.scalastuff.osgitools.OsgiManifest;
14 | import org.scalastuff.osgitools.OsgiManifest.Attribute;
15 | import org.scalastuff.osgitools.OsgiManifest.Value;
16 | import org.scalastuff.osgitools.OsgiifyIvy;
17 |
18 | public class ManifestFile extends FileContent {
19 |
20 | private final ProjectInfo project;
21 | private OsgiManifest manifest;
22 |
23 | protected ManifestFile(ProjectInfo project) {
24 | super(project.getProject().getFile("META-INF/MANIFEST.MF"));
25 | this.project = project;
26 | }
27 |
28 | @Override
29 | protected void setLines(List lines) {
30 | super.setLines(lines);
31 | manifest = OsgiManifest.read(lines);
32 | }
33 |
34 | public List write(Set projectDeps, List deps) throws CoreException, IOException {
35 |
36 | List lines = new ArrayList(getContent());
37 |
38 | if (!isTrue(lines, "Allow-ESBT")) return deps;
39 |
40 | for (int i = lines.size() - 1; i >= 0; i--) {
41 | String line = lines.get(i).trim();
42 | if (line.isEmpty()) {
43 | lines.remove(i);
44 | }
45 | }
46 |
47 | // boolean embedDependencies = isTrue(lines, "Embed-Dependencies");
48 |
49 | manifest.getAttribute("Allow-ESBT").setValue("true");
50 | manifest.getAttribute("Bundle-ManifestVersion").setValue("2", false);
51 | manifest.getAttribute("Bundle-Name").setValue(project.getName());
52 | manifest.getAttribute("Bundle-SymbolicName").setValue(getSymbolicName());
53 | manifest.getAttribute("Bundle-Version").setValue(getVersion());
54 |
55 |
56 | // add direct dependencies
57 | List depLines = new ArrayList();
58 | Attribute requireBundles = manifest.getAttribute("Require-Bundle");
59 | for (Dependency dep : project.getDependencies()) {
60 | String version = dep.version.replace('-', '.');
61 | String symbolicName = dep.organization + "." + dep.name;
62 | Value value = requireBundles.addUnique(symbolicName);
63 | value.setAnnotation("bundle-version", version);
64 | // depLines.add(symbolicName + ";bundle-version=\"" + version + "\""+(embedDependencies ? ";visibility:=reexport" : ""));
65 | }
66 |
67 | // remove org.osgi.core
68 | requireBundles.removeValue("org.osgi.core");
69 | Attribute importPackage = manifest.getAttribute("Import-Package");
70 | importPackage.addUnique("org.osgi.framework");
71 |
72 |
73 | // copy dep extent into osgi dir
74 | for (Dependency dep : deps) {
75 | OsgiifyIvy.osgiify(new File(dep.jar), dep.organization + "." + dep.name, dep.version, false);
76 | }
77 | // copy eclipse.osgi bundles
78 | File pluginsDir = new File(new File(URI.create(System.getProperty("eclipse.home.location"))), "plugins");
79 | if (pluginsDir.isDirectory()) {
80 | for (File jar : pluginsDir.listFiles()) {
81 | if (jar.getName().startsWith("org.eclipse.osgi.") && jar.getName().endsWith(".jar")) {
82 | copy(jar, new File(OsgiifyIvy.targetDir, jar.getName()), true);
83 | }
84 | }
85 | }
86 |
87 | set(lines, "Require-Bundle", depLines, true);
88 |
89 | // add libs
90 | // List cp = new ArrayList();
91 | // cp.add(".");
92 | // libDir.refreshLocal(1, null);
93 | // if (libDir.exists()) {
94 | // for (IResource resource : libDir.members()) {
95 | // if (resource.getName().endsWith(".jar")) {
96 | // cp.add("lib/" + resource.getName());
97 | // }
98 | // }
99 | // }
100 | // set(lines, "Bundle-ClassPath", cp, true);
101 | // super.doWrite(asList(manifest.toString(",\n ", new StringBuilder()).toString().split("\n")));
102 | super.doWrite(manifest.write());
103 | // super.doWrite(lines);
104 | return deps;
105 | }
106 |
107 | private String getVersion() {
108 | return project.getVersion().replace('-', '.');
109 | }
110 |
111 | private String getSymbolicName() {
112 | return project.getOrganization() + "." + project.getName();
113 | }
114 |
115 | private static boolean isTrue(List lines, String property) {
116 | List list = get(lines, property);
117 | return (!list.isEmpty() && list.get(0).toLowerCase().equals("true"));
118 | }
119 |
120 | private static List get(List lines, String property) {
121 | List result = new ArrayList();
122 | for (int index = 0; index < lines.size(); index++) {
123 | String line = lines.get(index);
124 | if (line.startsWith(property)) {
125 | line = line.substring(property.length()).trim();
126 | if (line.startsWith(":")) {
127 | result.add(line.substring(1).trim());
128 | }
129 | }
130 | }
131 | return result;
132 | }
133 |
134 | private static List set(List lines, String property, List values, boolean replaceExisting) {
135 | List result = new ArrayList();
136 | int index = 0;
137 | for (; index < lines.size(); index++) {
138 | String line = lines.get(index);
139 | if (line.startsWith(property)) {
140 | line = line.substring(property.length()).trim();
141 | if (line.startsWith(":")) {
142 | if (!replaceExisting) {
143 | return result;
144 | }
145 | String stringResult = "";
146 | line = " " + line.substring(1);
147 | while (line.isEmpty() || Character.isWhitespace(line.charAt(0))) {
148 | stringResult += line;
149 | lines.remove(index);
150 | if (index < lines.size()) {
151 | line = lines.get(index);
152 | } else {
153 | line = "QQ";
154 | }
155 | }
156 | for (String resultValue : stringResult.split(",")) {
157 | resultValue = resultValue.trim();
158 | if (!resultValue.isEmpty()) {
159 | result.add(resultValue);
160 | }
161 | }
162 | break;
163 | }
164 | }
165 | }
166 | if (values.size() == 1) {
167 | lines.add(index, property + ": " + values.get(0));
168 | } else if (values.size() > 1) {
169 | lines.add(index++, property + ": ");
170 | for (int i = 0; i < values.size(); i++) {
171 | lines.add(index++, " " + values.get(i)+ (i < values.size() - 1 ? "," : ""));
172 | }
173 | }
174 | return result;
175 | }
176 | }
177 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/Processor.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 |
18 | import java.io.IOException;
19 | import java.util.ArrayList;
20 | import java.util.Collection;
21 | import java.util.List;
22 |
23 | import org.eclipse.core.runtime.CoreException;
24 | import org.eclipse.core.runtime.IProgressMonitor;
25 | import org.eclipse.core.runtime.IStatus;
26 | import org.eclipse.core.runtime.Status;
27 | import org.eclipse.core.runtime.jobs.ISchedulingRule;
28 | import org.eclipse.core.runtime.jobs.Job;
29 |
30 | public class Processor extends Job {
31 |
32 | // The plug-in ID
33 | public static final String PLUGIN_ID = "scalastuff.eclipse.sbt"; //$NON-NLS-1$
34 |
35 | private Console console;
36 | private List projects;
37 | private String command;
38 |
39 | public Processor() throws CoreException, IOException {
40 | super("SBT Project Update");
41 | setPriority(Job.LONG);
42 | List rules = new ArrayList();
43 | for (ProjectInfo prg : WorkspaceInfo.getAllProjects()) {
44 | if (prg.isSbtProject()) {
45 | for (FileContent file : prg.getSbtFiles()) {
46 | rules.add(file.getFile());
47 | }
48 | }
49 | if (prg.getClassPathFile().exists()) {
50 | rules.add(prg.getClassPathFile().getFile());
51 | }
52 | }
53 | // setRule(new MultiRule(rules.toArray(new ISchedulingRule[0])));
54 | }
55 |
56 | public void setProjects(List projects) {
57 | this.projects = projects;
58 | }
59 |
60 | public void setCommand(String command) {
61 | this.command = command;
62 | }
63 |
64 | @Override
65 | protected IStatus run(IProgressMonitor monitor) {
66 | synchronized (Processor.class) {
67 | try {
68 | try {
69 | boolean force = false;
70 | Collection projects = this.projects;
71 | if (projects == null) {
72 | projects = new ArrayList(WorkspaceInfo.getAllProjects());
73 | } else {
74 | force = true;
75 | projects = new ArrayList(projects);
76 | }
77 | CreateSbtPlugin.createSbtPlugin();
78 | console = new Console();
79 | for (ProjectInfo project : projects) {
80 | process(project, force);
81 | }
82 | if (command == null) {
83 | for (ProjectInfo project : WorkspaceInfo.getAllProjects()) {
84 | if (project.isSbtProject()) {
85 | project.checkProjectDependencies();
86 | }
87 | }
88 | }
89 | return Status.OK_STATUS;
90 | } catch (Throwable t) {
91 | return new Status(Status.ERROR, PLUGIN_ID, t.getMessage(), t);
92 | }
93 | } catch (Throwable t) {
94 | return new Status(Status.ERROR, PLUGIN_ID, t.getMessage(), t);
95 | } finally {
96 | try {
97 | if (console != null) {
98 | console.close();
99 | }
100 | } catch (IOException e) {
101 | e.printStackTrace();
102 | }
103 | }
104 | }
105 | }
106 |
107 | private void process(ProjectInfo project, boolean force) throws IOException, CoreException {
108 | if (project.isSbtProject()) {
109 | long start = System.currentTimeMillis();
110 | if (command != null) {
111 | InvokeSbt sbt = new InvokeSbt(project, command, console);
112 | console.activate();
113 | console.println("");
114 | console.println("------ Processing project: " + project.getProject().getName() + " ------");
115 | sbt.setProjectDir(project.getProjectDir());
116 | sbt.invokeSbt();
117 | console.println("----- Done (" + (System.currentTimeMillis() - start) + " ms)------");
118 | } else {
119 | project.updateProject(force, console);
120 | }
121 | }
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/ProjectInfo.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 |
18 |
19 | import java.io.File;
20 | import java.io.FileNotFoundException;
21 | import java.io.FileOutputStream;
22 | import java.io.IOException;
23 | import java.util.ArrayList;
24 | import java.util.Arrays;
25 | import java.util.Collections;
26 | import java.util.LinkedHashSet;
27 | import java.util.List;
28 | import java.util.Set;
29 |
30 | import org.eclipse.core.resources.IFile;
31 | import org.eclipse.core.resources.IFolder;
32 | import org.eclipse.core.resources.IProject;
33 | import org.eclipse.core.resources.IProjectDescription;
34 | import org.eclipse.core.resources.IResource;
35 | import org.eclipse.core.resources.IWorkspaceRoot;
36 | import org.eclipse.core.resources.ResourcesPlugin;
37 | import org.eclipse.core.runtime.CoreException;
38 |
39 | public class ProjectInfo {
40 |
41 | private File infoFile;
42 | private IProject project;
43 | private DotClassPathFile classpathFile;
44 | private ManifestFile manifestFile;
45 | private List sbtFiles;
46 |
47 | // info from parse
48 | private String organization = "";
49 | private String name = "";
50 | private String version = "";
51 | private String scalaVersion = "";
52 | private final List sourceDirectories = new ArrayList();
53 | private final List testSourceDirectories = new ArrayList();
54 | private final List resourceDirectories = new ArrayList();
55 | private final List testResourceDirectories = new ArrayList();
56 | private final List classDirectories = new ArrayList();
57 | private final List testClassDirectories = new ArrayList();
58 | private final List dependencies = new ArrayList();
59 | private Set projectDependencies = Collections.emptySet();
60 |
61 | public ProjectInfo(IProject project) {
62 | initialize(project);
63 | }
64 |
65 | private void initialize(IProject project) {
66 | this.project = project;
67 | this.infoFile = new File(WorkspaceInfo.getMetaDataDir(), project.getName() + "-lastresult.txt");
68 | this.classpathFile = new DotClassPathFile(this);
69 | this.manifestFile = new ManifestFile(this);
70 | this.sbtFiles = getFileContent(scanSbtFiles());
71 | if (isSbtProject()) {
72 | parse(Utils.read(infoFile));
73 | }
74 | }
75 |
76 | public IProject getProject() {
77 | return project;
78 | }
79 |
80 | public File getProjectDir() {
81 | return new File(project.getLocationURI());
82 | }
83 |
84 | public DotClassPathFile getClassPathFile() {
85 | return classpathFile;
86 | }
87 |
88 | public ManifestFile getManifestFile() {
89 | return manifestFile;
90 | }
91 |
92 | public boolean isSbtProject() {
93 | return project.exists() && !scanSbtFiles().isEmpty();
94 | }
95 |
96 | public List getSbtFiles() {
97 | return sbtFiles;
98 | }
99 |
100 | public String getOrganization() {
101 | return organization;
102 | }
103 |
104 | public String getName() {
105 | return name;
106 | }
107 |
108 | public String getVersion() {
109 | return version;
110 | }
111 |
112 | public String getScalaVersion() {
113 | return scalaVersion;
114 | }
115 |
116 | public List getSourceDirectories() {
117 | return sourceDirectories;
118 | }
119 |
120 | public List getResourceDirectories() {
121 | return resourceDirectories;
122 | }
123 |
124 | public List getClassDirectories() {
125 | return classDirectories;
126 | }
127 |
128 | public List getTestSourceDirectories() {
129 | return testSourceDirectories;
130 | }
131 |
132 | public List getTestResourceDirectories() {
133 | return testResourceDirectories;
134 | }
135 |
136 | public List getTestClassDirectories() {
137 | return testClassDirectories;
138 | }
139 |
140 | public List getDependencies() {
141 | return dependencies;
142 | }
143 |
144 | public void updateProject(boolean force, Console console) throws IOException, CoreException {
145 | long sbtFilesLastModified = getSbtFilesLastModified();
146 | if (force || infoFile.lastModified() < sbtFilesLastModified) {
147 | try {
148 | initialize(project);
149 | console.activate();
150 | console.println("");
151 | console.println("------ Update project: " + project.getProject().getName() + " ------");
152 | long start = System.currentTimeMillis();
153 | InvokeSbt invokeSbt = new InvokeSbt(this, "update-eclipse", console);
154 | invokeSbt.setProjectDir(getProjectDir());
155 | invokeSbt.invokeSbt();
156 | Utils.write(new FileOutputStream(infoFile), invokeSbt.getResult());
157 | parse(invokeSbt.getResult());
158 | writeProjectFiles();
159 | console.println("----- Done (" + (System.currentTimeMillis() - start) + " ms)------");
160 | } finally {
161 | infoFile.createNewFile();
162 | infoFile.setLastModified(sbtFilesLastModified);
163 | project.refreshLocal(IResource.DEPTH_INFINITE, null);
164 | }
165 | }
166 | }
167 |
168 | public void checkProjectDependencies() throws FileNotFoundException, IOException, CoreException {
169 | if (!this.projectDependencies.equals(findProjectDependencies())) {
170 | writeProjectFiles();
171 | }
172 | }
173 |
174 | private long getSbtFilesLastModified() {
175 | long sbtFileLastModified = 0;
176 | List sbtFiles = scanSbtFiles();
177 | for (IFile file : sbtFiles) {
178 | if (sbtFileLastModified < file.getLocalTimeStamp()) {
179 | sbtFileLastModified = file.getLocalTimeStamp();
180 | }
181 | }
182 | return sbtFileLastModified;
183 | }
184 |
185 | private void writeProjectFiles() throws FileNotFoundException, IOException, CoreException {
186 | this.projectDependencies = findProjectDependencies();
187 |
188 | // combine sbt project deps with sbt project deps
189 | List deps = new ArrayList();
190 | for (Dependency dep : getDependencies()) {
191 | if (findProjectDependency(dep) == null) {
192 | dep.jar = CopyJars.copyFile(dep, getProjectDir(), dep.jar);
193 | dep.srcJar = CopyJars.copyFile(dep, getProjectDir(), dep.srcJar);
194 | deps.add(dep);
195 | }
196 | }
197 |
198 | classpathFile.write(projectDependencies, deps);
199 | manifestFile.write(projectDependencies, deps);
200 |
201 | // update eclipse project
202 | IProjectDescription desc = project.getProject().getDescription();
203 |
204 | // add scala nature
205 | String javaNatureId = "org.eclipse.jdt.core.javanature";
206 | String scalaNatureId = "org.scala-ide.sdt.core.scalanature";
207 | boolean javaNatureFound = false;
208 | boolean scalaNatureFound = false;
209 | for (String nature : desc.getNatureIds()) {
210 | if (nature.equals(scalaNatureId)) {
211 | scalaNatureFound = true;
212 | }
213 | if (nature.equals(javaNatureId)) {
214 | javaNatureFound = true;
215 | }
216 | }
217 | String[] natureIds = desc.getNatureIds();
218 | if (!javaNatureFound) {
219 | natureIds = (String[]) Arrays.copyOf(natureIds, natureIds.length + 1);
220 | natureIds[natureIds.length - 1] = javaNatureId;
221 | }
222 | if (!scalaNatureFound) {
223 | natureIds = (String[]) Arrays.copyOf(natureIds, natureIds.length + 1);
224 | natureIds[natureIds.length - 1] = scalaNatureId;
225 | }
226 | desc.setNatureIds(natureIds);
227 | project.setDescription(desc, null);
228 |
229 | // rename project?
230 | // renameProject();
231 |
232 | }
233 |
234 | private void renameProject() throws CoreException, IOException {
235 | String name = getProjectName();
236 | if (!name.isEmpty() && !getOrganization().equals("default") && !getName().equals("default")) {
237 | IProjectDescription desc = project.getProject().getDescription();
238 | if (!desc.getName().equals(name)) {
239 |
240 | // // backup existing target folder
241 | File newProjectDir = new File(getProjectDir().getParentFile(), name);
242 | // for (int i = 0; i < 100 && newProjectDir.exists(); i++) {
243 | // newProjectDir.renameTo(new File(newProjectDir.getPath() + ".old" + i));
244 | // }
245 | //
246 | // rename folder on disk
247 | if (getProjectDir().renameTo(newProjectDir)) {
248 |
249 | // remove existing .project file
250 | File projectFile = new File(newProjectDir, ".project");
251 | projectFile.delete();
252 |
253 | // create a new eclipse project
254 | IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
255 | IProject newProject = root.getProject(name);
256 | newProject.create(null);
257 | newProject.open(null);
258 |
259 | // write .project file
260 | desc.setName(name);
261 | newProject.setDescription(desc, null);
262 |
263 | // remove current eclipse project
264 | this.project.delete(true, null);
265 |
266 | // move info file
267 | File oldInfoFile = this.infoFile;
268 |
269 | // re-initialize needed
270 | initialize(newProject);
271 |
272 | // copy old info file
273 | Utils.copy(oldInfoFile, infoFile, true);
274 | }
275 | }
276 | }
277 | }
278 |
279 | private String getProjectName() {
280 | if (getName().equals("") || getOrganization().equals("")) {
281 | return getName();
282 | } else {
283 | return getOrganization() + "." + getName();
284 | }
285 | }
286 |
287 | private Set findProjectDependencies() {
288 | Set projectDeps = new LinkedHashSet();
289 | for (Dependency dep : getDependencies()) {
290 | ProjectInfo depPrj = findProjectDependency(dep);
291 | if (depPrj != null) projectDeps.add(depPrj);
292 | }
293 | return projectDeps;
294 | }
295 |
296 | private ProjectInfo findProjectDependency(Dependency dep) {
297 | String projectName = dep.name;
298 | String suffix = "_" + scalaVersion;
299 | if (projectName.endsWith(suffix)) {
300 | projectName = projectName.substring(0, projectName.length() - suffix.length());
301 | }
302 | return WorkspaceInfo.findProject(dep.organization, projectName, dep.version);
303 | }
304 |
305 | public void parse(List lines) {
306 | organization = "";
307 | name = "";
308 | version = "";
309 | scalaVersion = "";
310 | sourceDirectories.clear();
311 | testSourceDirectories.clear();
312 | resourceDirectories.clear();
313 | testResourceDirectories.clear();
314 | classDirectories.clear();
315 | testClassDirectories.clear();
316 | dependencies.clear();
317 | projectDependencies.clear();
318 | for (String line : lines) {
319 | String[] fields = line.split("::");
320 | if (fields[0].trim().equals("dependency")) {
321 | Dependency dependency = new Dependency();
322 | for (int i = 1; i < fields.length; i++) {
323 | switch (i) {
324 | case 1: dependency.organization = fields[i].trim(); break;
325 | case 2: dependency.name = fields[i].trim(); break;
326 | case 3: dependency.version = fields[i].trim(); break;
327 | case 4: dependency.jar = fields[i].trim(); break;
328 | case 5: dependency.srcJar = fields[i].trim(); break;
329 | }
330 | }
331 | dependencies.add(dependency);
332 | } else if (fields[0].trim().equals("organization")) {
333 | organization = fields[1].trim();
334 | } else if (fields[0].trim().equals("name")) {
335 | name = fields[1].trim();
336 | } else if (fields[0].trim().equals("version")) {
337 | version = fields[1].trim();
338 | } else if (fields[0].trim().equals("scalaVersion")) {
339 | scalaVersion = fields[1].trim();
340 | } else if (fields[0].trim().equals("sourceDirectory")) {
341 | sourceDirectories.add(fields[1].trim());
342 | } else if (fields[0].trim().equals("testSourceDirectory")) {
343 | testSourceDirectories.add(fields[1].trim());
344 | } else if (fields[0].trim().equals("resourceDirectory")) {
345 | resourceDirectories.add(fields[1].trim());
346 | } else if (fields[0].trim().equals("testResourceDirectory")) {
347 | testResourceDirectories.add(fields[1].trim());
348 | } else if (fields[0].trim().equals("classDirectory")) {
349 | classDirectories.add(fields[1].trim());
350 | } else if (fields[0].trim().equals("testClassDirectory")) {
351 | testClassDirectories.add(fields[1].trim());
352 | }
353 | }
354 | }
355 |
356 | private List getFileContent(List files) {
357 | ArrayList list = new ArrayList(files.size());
358 | for (IFile file : files) {
359 | list.add(new FileContent(file));
360 | }
361 | return list;
362 | }
363 |
364 | private List scanSbtFiles() {
365 | List list = new ArrayList();
366 | IFile buildSbt = project.getProject().getFile("build.sbt");
367 | if (buildSbt.exists()) {
368 | list.add(buildSbt);
369 | }
370 | IFolder projectDir = project.getProject().getFolder("project");
371 | try {
372 | for (IResource scalaFile : projectDir.members()) {
373 | if (scalaFile instanceof IFile && scalaFile.getFileExtension().equals("scala")) {
374 | list.add((IFile) scalaFile);
375 | }
376 | }
377 | } catch (CoreException e) {
378 | }
379 | return list;
380 | }
381 | }
382 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/ResourceChangeListener.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 |
18 | import java.util.ArrayList;
19 | import java.util.HashSet;
20 | import java.util.Set;
21 |
22 | import org.eclipse.core.resources.IProject;
23 | import org.eclipse.core.resources.IResource;
24 | import org.eclipse.core.resources.IResourceChangeEvent;
25 | import org.eclipse.core.resources.IResourceChangeListener;
26 | import org.eclipse.core.resources.IResourceDelta;
27 | import org.eclipse.core.resources.IResourceDeltaVisitor;
28 | import org.eclipse.core.runtime.CoreException;
29 |
30 | public class ResourceChangeListener implements IResourceChangeListener {
31 |
32 | @Override
33 | public void resourceChanged(IResourceChangeEvent event) {
34 | //we are only interested in POST_CHANGE events
35 | if (event.getType() != IResourceChangeEvent.POST_CHANGE)
36 | return;
37 | IResourceDelta rootDelta = event.getDelta();
38 | //get the delta, if any, for the documentation directory
39 | final ArrayList changed = new ArrayList();
40 | IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() {
41 | public boolean visit(IResourceDelta delta) {
42 | //only interested in changed resources (not added or removed)
43 | if (delta.getKind() != IResourceDelta.CHANGED)
44 | return true;
45 | //only interested in content changes
46 | if ((delta.getFlags() & IResourceDelta.CONTENT) == 0)
47 | return true;
48 | IResource resource = delta.getResource();
49 | //only interested in files with the "txt" extension
50 | if (resource.getType() == IResource.FILE &&
51 | "build.sbt".equals(resource.getName())
52 | || (resource.getName().endsWith("scala") && resource.getParent().getName().equals("project"))) {
53 | changed.add(resource);
54 | }
55 | return true;
56 | }
57 | };
58 | try {
59 | rootDelta.accept(visitor);
60 | } catch (CoreException e) {
61 | // TODO Auto-generated catch block
62 | e.printStackTrace();
63 | }
64 | Set projects = new HashSet();
65 | for (IResource resource : changed) {
66 | projects.add(resource.getProject());
67 | }
68 | try {
69 | new Processor().schedule();
70 | }
71 | catch (Exception e) {
72 | e.printStackTrace();
73 | }
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/SbtEclipsePlugin.scala.source:
--------------------------------------------------------------------------------
1 |
2 | import sbt._
3 | import Keys._
4 | object SbtEclipsePlugin extends Plugin
5 | {
6 | private def generalInfoTask = {
7 | (organization, name, version, scalaVersion) map {
8 | (organization, name, version, scalaVersion) => Seq(
9 | "organization :: " + organization,
10 | "name :: " + name,
11 | "version :: " + version,
12 | "scalaVersion :: " + scalaVersion
13 | )
14 | }
15 | }
16 |
17 | private def relativize(base : File, file : File) = {
18 | val rel = base.toURI().relativize(file.toURI())
19 | if (rel.isAbsolute) new File(rel).toString
20 | else rel.toString
21 | }
22 |
23 | private def dependencyInfoTask = {
24 | (baseDirectory, update) map {
25 | (base, report) =>
26 | val tuples = for (configuration <- report.configurations; module <- configuration.modules) yield {
27 | module.artifacts.find(a => a._1.`type` == "jar" || a._1.`type` == "bundle") match {
28 | case Some(art) =>
29 | val source = module.artifacts.find(_._1.`type` == "src") match {
30 | case Some(art) => " :: " + relativize(base, art._2)
31 | case None => ""
32 | }
33 | (module.module.organization, module.module.name, module.module.revision, relativize(base, art._2), source)
34 | case None =>
35 | }
36 | }
37 | for ((organization, name, version, jar, src) <- tuples.toSet.toSeq)
38 | yield "dependency :: " + organization + " :: " + name + " :: " + version + " :: " + jar + src
39 | }
40 | }
41 |
42 | private def sourceDirsInfoTask = {
43 | (baseDirectory, unmanagedSourceDirectories in Compile, unmanagedSourceDirectories in Test, unmanagedResourceDirectories in Compile, unmanagedResourceDirectories in Test, classDirectory in Compile, classDirectory in Test) map {
44 | (base, src, testsrc, res, testres, classDir, testClassDir) =>
45 | val s1 = for (dir <- src) yield "sourceDirectory :: " + relativize(base, dir)
46 | val s2 = for (dir <- testsrc) yield "testSourceDirectory :: " + relativize(base, dir)
47 | val s3 = for (dir <- res) yield "resourceDirectory :: " + relativize(base, dir)
48 | val s4 = for (dir <- testres) yield "testResourceDirectory :: " + relativize(base, dir)
49 | s1 ++ s2 ++ s3 ++ s4 ++ Seq(
50 | "classDirectory :: " + relativize(base, classDir),
51 | "testClassDirectory :: " + relativize(base, testClassDir))
52 |
53 | }
54 | }
55 |
56 | val generalInfo = TaskKey[Seq[String]]("generalInfo")
57 | val dependencyInfo = TaskKey[Seq[String]]("dependencyInfo")
58 | val sourceDirsInfo = TaskKey[Seq[String]]("sourceDirsInfo")
59 |
60 | override lazy val settings = Defaults.defaultConfigs ++ Seq(
61 | generalInfo <<= generalInfoTask,
62 | dependencyInfo <<= dependencyInfoTask,
63 | sourceDirsInfo <<= sourceDirsInfoTask,
64 | commands += myCommand
65 | )
66 |
67 | private def print(result : Option[Result[Seq[String]]]) {
68 | result match {
69 | case Some(Value(ss)) =>
70 | for (s <- ss) {
71 | println("[result] " + s)
72 | }
73 | case _ =>
74 | }
75 | }
76 |
77 | lazy val myCommand =
78 | Command.command("update-eclipse") { (state: State) =>
79 | print(Project.evaluateTask(generalInfo, state))
80 | print(Project.evaluateTask(dependencyInfo, state))
81 | print(Project.evaluateTask(sourceDirsInfo, state))
82 | state
83 | }
84 | }
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/UpdateProjectConfigurationCommand.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 |
18 | import java.util.List;
19 |
20 | import org.eclipse.core.commands.AbstractHandler;
21 | import org.eclipse.core.commands.ExecutionEvent;
22 | import org.eclipse.core.commands.ExecutionException;
23 |
24 | public class UpdateProjectConfigurationCommand extends AbstractHandler {
25 | @Override
26 | public Object execute(ExecutionEvent event) throws ExecutionException {
27 | try {
28 | List selectedProjects = WorkspaceInfo.getSelectedProjects(event);
29 | Processor processor = new Processor();
30 | processor.setProjects(selectedProjects);
31 | processor.schedule();
32 | } catch (Exception e) {
33 | }
34 | return null;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/Utils.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 |
18 | import java.io.BufferedReader;
19 | import java.io.ByteArrayInputStream;
20 | import java.io.File;
21 | import java.io.FileInputStream;
22 | import java.io.FileNotFoundException;
23 | import java.io.FileOutputStream;
24 | import java.io.IOException;
25 | import java.io.InputStream;
26 | import java.io.InputStreamReader;
27 | import java.io.OutputStream;
28 | import java.util.ArrayList;
29 | import java.util.Collections;
30 | import java.util.List;
31 |
32 | import org.eclipse.core.resources.IFile;
33 | import org.eclipse.core.runtime.CoreException;
34 |
35 | public class Utils {
36 |
37 | public static List read(IFile file) {
38 | try {
39 | return read(file.getContents());
40 | } catch (CoreException e) {
41 | }
42 | return Collections.emptyList();
43 | }
44 |
45 | public static List read(File file) {
46 | try {
47 | return read(new FileInputStream(file));
48 | } catch (FileNotFoundException e) {
49 | }
50 | return Collections.emptyList();
51 | }
52 |
53 | public static List read(InputStream is) {
54 | List lines = new ArrayList();
55 | final BufferedReader br = new BufferedReader(new InputStreamReader(is));
56 | String line;
57 | try {
58 | while ((line = br.readLine()) != null) {
59 | lines.add(line);
60 | }
61 | } catch (IOException e) {
62 | } finally {
63 | try {
64 | is.close();
65 | } catch (IOException e) {
66 | }
67 | }
68 | return lines;
69 | }
70 |
71 | public static InputStream linesInputStream(List lines) {
72 | StringBuilder builder = new StringBuilder();
73 | for (String line : lines) {
74 | builder.append(line).append("\n");
75 | }
76 | return new ByteArrayInputStream(builder.toString().getBytes());
77 | }
78 |
79 | public static void write(OutputStream os, List lines) throws IOException {
80 | try {
81 | for (String line : lines) {
82 | os.write((line + "\n").getBytes());
83 | }
84 | } finally {
85 | os.close();
86 | }
87 | }
88 |
89 | public static void copy(File in, File out, boolean preserveTimestamp) throws IOException {
90 | if (out.exists() && in.length() == out.length() && in.lastModified() == out.lastModified()) {
91 | return;
92 | }
93 | copy(new FileInputStream(in), new FileOutputStream(out));
94 | if (preserveTimestamp) {
95 | out.setLastModified(in.lastModified());
96 | }
97 | }
98 |
99 | public static void copy(InputStream is, OutputStream os) throws IOException {
100 | try {
101 | byte[] b = new byte[2000];
102 | int read;
103 | while ((read = is.read(b)) != -1) {
104 | os.write(b, 0, read);
105 | }
106 | } finally {
107 | is.close();
108 | os.close();
109 | }
110 | }
111 |
112 | public static int indexOfLineContaining(List lines, String s) {
113 | s = stripSpaces(s);
114 | for (int i = 0; i < lines.size(); i++) {
115 | if (stripSpaces(lines.get(i)).contains(s)) {
116 | return i;
117 | }
118 | }
119 | return -1;
120 | }
121 |
122 | public static String stripSpaces(String s) {
123 | StringBuilder builder = new StringBuilder();
124 | for (int i = 0; i < s.length(); i++) {
125 | if (!Character.isWhitespace(s.charAt(i))) {
126 | builder.append(s.charAt(i));
127 | }
128 | }
129 | return builder.toString();
130 | }
131 |
132 | public static String qname(String organization, String name, String version, String qualifier) {
133 | String common = "";
134 | for (int i = organization.length() - 1; i >= -1; i--) {
135 | if (i < 0 || organization.charAt(i) == '.') {
136 | common = organization.substring(i + 1);
137 | break;
138 | }
139 | }
140 | if (name.equals(common) || name.startsWith(common + ".")) {
141 | name = name.substring(common.length());
142 | }
143 | if (organization.endsWith("." + common)) {
144 | organization = organization.substring(0, organization.length() - common.length() - 1);
145 | }
146 | if (name.startsWith("-")) {
147 | name = name.substring(1);
148 | }
149 | if (!name.equals("") && !name.startsWith(".")) {
150 | name = "." + name;
151 | }
152 | if (!version.equals("") && !version.startsWith("-")) {
153 | version = "-" + version;
154 | }
155 | if (!qualifier.equals("") && !qualifier.startsWith("-")) {
156 | qualifier = "-" + qualifier;
157 | }
158 | return organization + name + version + qualifier;
159 | }
160 |
161 | public static String orElse(String s, String alt) {
162 | if (s != null) return s;
163 | else return alt;
164 | }
165 | }
166 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/WorkspaceInfo.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2011 ScalaStuff.org (joint venture of Alexander Dvorkovyy and Ruud Diterwich)
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.scalastuff.esbt;
17 |
18 | import java.io.File;
19 | import java.io.IOException;
20 | import java.util.ArrayList;
21 | import java.util.Collection;
22 | import java.util.HashMap;
23 | import java.util.HashSet;
24 | import java.util.Iterator;
25 | import java.util.List;
26 | import java.util.Map;
27 | import java.util.Set;
28 |
29 | import org.eclipse.core.commands.ExecutionEvent;
30 | import org.eclipse.core.resources.IProject;
31 | import org.eclipse.core.resources.ResourcesPlugin;
32 | import org.eclipse.core.runtime.CoreException;
33 | import org.eclipse.core.runtime.IAdaptable;
34 | import org.eclipse.jface.viewers.ISelection;
35 | import org.eclipse.jface.viewers.IStructuredSelection;
36 | import org.eclipse.ui.handlers.HandlerUtil;
37 |
38 | public class WorkspaceInfo {
39 |
40 | private static final Map projects = new HashMap();
41 |
42 | public static File getMetaDataDir() {
43 | File dir = new File(new File(ResourcesPlugin.getWorkspace().getRoot().getLocationURI()), ".metadata/sbt-plugin");
44 | dir.mkdirs();
45 | return dir;
46 | }
47 |
48 | public static Collection getAllProjects() throws CoreException, IOException {
49 | updateProjects();
50 | return projects.values();
51 | }
52 |
53 | public static ProjectInfo getProject(IProject project) throws CoreException, IOException {
54 | updateProjects();
55 | return projects.get(project);
56 | }
57 |
58 | public static Set getProjects(List projects) throws CoreException, IOException {
59 | updateProjects();
60 | Set result = new HashSet();
61 | for (IProject project : projects) {
62 | ProjectInfo projectInfo = WorkspaceInfo.projects.get(project);
63 | if (projectInfo != null) {
64 | result.add(projectInfo);
65 | }
66 | }
67 | return result;
68 | }
69 |
70 | public static ProjectInfo findProject(String organization, String name, String version) {
71 | for (ProjectInfo project : projects.values()) {
72 | if (project.getOrganization().equals(organization)
73 | && project.getName().equals(name)
74 | && project.getVersion().equals(version)) {
75 | return project;
76 | }
77 | }
78 | return null;
79 | }
80 |
81 | public static ProjectInfo adaptToProject(Object obj) {
82 | if (obj instanceof IAdaptable) {
83 | IProject project = (IProject) ((IAdaptable) obj).getAdapter(IProject.class);
84 | if (project != null) {
85 | try {
86 | return getProject(project);
87 | } catch (Exception e) {
88 | }
89 | }
90 | }
91 | return null;
92 | }
93 |
94 | public static List getSelectedProjects(ExecutionEvent event) {
95 | List projects = new ArrayList();
96 | ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
97 | if (selection != null & selection instanceof IStructuredSelection) {
98 | IStructuredSelection strucSelection = (IStructuredSelection) selection;
99 | for (Iterator> iterator = strucSelection.iterator(); iterator.hasNext();) {
100 | ProjectInfo project = WorkspaceInfo.adaptToProject(iterator.next());
101 | if (project != null && project.isSbtProject()) {
102 | projects.add(project);
103 | }
104 | }
105 | }
106 | return projects;
107 |
108 | }
109 |
110 | private static void updateProjects() {
111 | for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
112 | ProjectInfo projectInfo = projects.get(project);
113 | if (projectInfo == null) {
114 | projectInfo = new ProjectInfo(project);
115 | projects.put(project, projectInfo);
116 | }
117 | }
118 | }
119 | }
120 |
--------------------------------------------------------------------------------
/src/org/scalastuff/esbt/sbt-launch.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/scalastuff/esbt/052e3ad1ee864aa63fbee6f778e6c3e802ee014b/src/org/scalastuff/esbt/sbt-launch.jar
--------------------------------------------------------------------------------
/src/org/scalastuff/osgitools/OsgiManifest.java:
--------------------------------------------------------------------------------
1 | package org.scalastuff.osgitools;
2 |
3 | import static org.scalastuff.osgitools.util.StringTokenizer.tokenize;
4 |
5 | import java.io.BufferedReader;
6 | import java.io.IOException;
7 | import java.util.ArrayList;
8 | import java.util.Arrays;
9 | import java.util.LinkedHashMap;
10 | import java.util.List;
11 | import java.util.Map;
12 | import java.util.Map.Entry;
13 | import java.util.Set;
14 |
15 | public class OsgiManifest {
16 |
17 | private final Map attributes = new LinkedHashMap();
18 |
19 | public Set getAttributeNames() {
20 | return attributes.keySet();
21 | }
22 |
23 | public Attribute getAttribute(String name) {
24 | Attribute values = attributes.get(name);
25 | if (values == null) {
26 | values = new Attribute();
27 | attributes.put(name, values);
28 | }
29 | return values;
30 | }
31 |
32 | public String toString() {
33 | return toString(",", new StringBuilder()).toString();
34 | }
35 |
36 | public StringBuilder toString(String sep, StringBuilder out) {
37 | for (Entry attr : attributes.entrySet()) {
38 | out.append(attr.getKey()).append(": ");
39 | attr.getValue().toString(sep, out);
40 | out.append("\n");
41 | }
42 | return out;
43 | }
44 |
45 | public static class Attribute extends ArrayList {
46 |
47 | private static final long serialVersionUID = 1L;
48 |
49 | public boolean isSet() {
50 | return !super.isEmpty();
51 | }
52 |
53 | public void addValues(Value... values) {
54 | super.addAll(Arrays.asList(values));
55 | }
56 |
57 | public Value findValue(String stringValue) {
58 | for (Value value : this) {
59 | if (value.getValue().equals(stringValue)) {
60 | return value;
61 | }
62 | }
63 | return null;
64 | }
65 |
66 | public void removeValue(String string) {
67 | remove(findValue(string));
68 | }
69 |
70 | public Value addUnique(String stringValue) {
71 | for (Value value : this) {
72 | if (value.getValue().equals(stringValue)) {
73 | return value;
74 | }
75 | }
76 | Value value = new Value();
77 | value.setValue(stringValue);
78 | add(value);
79 | return value;
80 | }
81 |
82 | public Value setValue(String stringValue, boolean overwriteExisting) {
83 | if (overwriteExisting || isEmpty()) {
84 | clear();
85 | return setValue(stringValue);
86 | }
87 | return get(0);
88 | }
89 |
90 | public Value setValue(String stringValue) {
91 | if (isEmpty()) {
92 | add(new Value());
93 | } else {
94 | super.removeRange(1, size());
95 | }
96 | get(0).setValue(stringValue);
97 | return get(0);
98 | }
99 |
100 | @Override
101 | public String toString() {
102 | return toString(",", new StringBuilder()).toString();
103 | }
104 |
105 | public StringBuilder toString(String sep, StringBuilder out) {
106 | for (int i = 0; i < size(); i++) {
107 | if (i > 0) out.append(sep);
108 | out.append(get(i));
109 | }
110 | return out;
111 | }
112 | }
113 |
114 | public static class Value {
115 | private String value;
116 | private Map annotations = new LinkedHashMap();
117 |
118 | public String getValue() {
119 | return value;
120 | }
121 |
122 | public Value setValue(String value) {
123 | this.value = value;
124 | return this;
125 | }
126 |
127 | public Set getAnnotationNames() {
128 | return annotations.keySet();
129 | }
130 |
131 | public String getAnnotation(String annotationName) {
132 | String annotation = annotations.get(annotationName);
133 | if (annotation == null) {
134 | annotation = "";
135 | }
136 | return annotation;
137 | }
138 |
139 | public void setAnnotation(String annotationName, String annotation) {
140 | if (annotation.equals("")) {
141 | annotations.remove(annotationName);
142 | } else {
143 | annotations.put(annotationName, annotation);
144 | }
145 | }
146 |
147 | @Override
148 | public String toString() {
149 | return toString(new StringBuilder()).toString();
150 | }
151 |
152 | public StringBuilder toString(StringBuilder out) {
153 | out.append(value);
154 | for (Entry annotation : annotations.entrySet()) {
155 | out.append(';').append(annotation.getKey()).append("=").append(annotation.getValue());
156 | }
157 | return out;
158 | }
159 | }
160 |
161 | public static OsgiManifest read(BufferedReader reader) throws IOException {
162 | try {
163 | List lines = new ArrayList();
164 | for (String line = ""; line != null; line = reader.readLine()) {
165 | lines.add(line);
166 | }
167 | return read(lines);
168 | } finally {
169 | reader.close();
170 | }
171 | }
172 |
173 | public static OsgiManifest read(List lines) {
174 |
175 | // parse lines
176 | OsgiManifest manifest = new OsgiManifest();
177 | for (int i = 0; i < lines.size(); i++) {
178 | String line = lines.get(i);
179 | for (int j = i + 1; j < lines.size() && lines.get(j).startsWith(" "); j++, i++) {
180 | line = line + lines.get(j);
181 | }
182 | int index = line.indexOf(':');
183 | if (index > 0) {
184 | String attributeName = line.substring(0, index).trim();
185 | List valueTokens = tokenize(line.substring(index + 1), ',');
186 | Attribute values = manifest.getAttribute(attributeName);
187 | for (String valueToken : valueTokens) {
188 | if (valueToken.trim().isEmpty()) continue;
189 | Value value = new Value();
190 | values.add(value);
191 | List annotationTokens = tokenize(valueToken, ';');
192 | value.setValue(annotationTokens.get(0));
193 | for (int k = 1; k < annotationTokens.size(); k++) {
194 | String annotationToken = annotationTokens.get(k);
195 | index = annotationToken.indexOf('=');
196 | if (index > 0) {
197 | value.setAnnotation(annotationToken.substring(0, index), annotationToken.substring(index + 1));
198 | }
199 | }
200 | }
201 | }
202 | }
203 | return manifest;
204 | }
205 |
206 | public List write() {
207 | List lines = new ArrayList();
208 | StringBuilder out = new StringBuilder();
209 | for (Entry attribute : attributes.entrySet()) {
210 | out.append(attribute.getKey()).append(": ");
211 | Attribute attributeValue = attribute.getValue();
212 | if (attributeValue.size() > 1) {
213 | lines.add(out.toString());
214 | out.setLength(0);
215 | for (int i = 0; i < attributeValue.size(); i++) {
216 | Value value = attributeValue.get(i);
217 | out.append(" ");
218 | value.toString(out);
219 | if (i + 1 < attributeValue.size()) {
220 | out.append(",");
221 | }
222 | lines.add(out.toString());
223 | out.setLength(0);
224 | }
225 | } else {
226 | attributeValue.toString("", out);
227 | lines.add(out.toString());
228 | out.setLength(0);
229 | }
230 | }
231 | return lines;
232 | }
233 | }
234 |
--------------------------------------------------------------------------------
/src/org/scalastuff/osgitools/Osgiify.java:
--------------------------------------------------------------------------------
1 | package org.scalastuff.osgitools;
2 |
3 | import java.io.File;
4 | import java.io.FileInputStream;
5 | import java.io.FileOutputStream;
6 | import java.io.IOException;
7 | import java.io.InputStream;
8 | import java.util.Enumeration;
9 | import java.util.TreeSet;
10 | import java.util.jar.JarEntry;
11 | import java.util.jar.JarFile;
12 | import java.util.jar.JarOutputStream;
13 | import java.util.jar.Manifest;
14 |
15 | import org.scalastuff.esbt.Utils;
16 |
17 | public class Osgiify {
18 |
19 | public static File osgiify(File sourceFile, String symbolicName, String version, File targetDir, boolean release) throws IOException {
20 |
21 | if (!targetDir.exists()) {
22 | targetDir.mkdirs();
23 | }
24 |
25 | // version should contain dots only
26 | version = version.replace('-', '.');
27 |
28 | // construct targetFile
29 | File targetFile = new File(targetDir, symbolicName + "-" + version + ".jar");
30 |
31 | // if target file exists and is up to date, we're done
32 | if (targetFile.exists() && sourceFile.lastModified() == targetFile.lastModified()) {
33 | return targetFile;
34 | }
35 |
36 | JarFile sourceJarFile = new JarFile(sourceFile);
37 | try {
38 | Manifest manifest = sourceJarFile.getManifest();
39 | if (manifest != null) {
40 |
41 | // prevent overwrite released bundles
42 | if (targetFile.exists() && release && !version.endsWith("SNAPSHOT")) {
43 | return targetFile;
44 | }
45 | } else {
46 | manifest = new Manifest();
47 | }
48 |
49 | if (symbolicName.equals(getValue(manifest, "Bundle-SymbolicName"))
50 | && version.equals(getValue(manifest, "Bundle-Version"))) {
51 | Utils.copy(new FileInputStream(sourceFile), new FileOutputStream(targetFile));
52 | return targetFile;
53 | } else {
54 | manifest.getMainAttributes().putValue("Bundle-SymbolicName", symbolicName);
55 | manifest.getMainAttributes().putValue("Bundle-Version", version);
56 | if (manifest.getMainAttributes().getValue("Bundle-ManifestVersion") == null) {
57 | manifest.getMainAttributes().putValue("Bundle-ManifestVersion", "2");
58 | }
59 | if (getValue(manifest, "Export-Package") == null) {
60 | StringBuilder out = new StringBuilder();
61 | for (String pkg : findPackages(sourceFile)) {
62 | out.append(out.length() == 0 ? "" : ",");
63 | out.append(pkg).append(";version=\"" + version + "\"");
64 | }
65 | manifest.getMainAttributes().putValue("Export-Package", out.toString());
66 | }
67 | copyJar(sourceFile, targetFile, manifest);
68 | return targetFile;
69 | }
70 | } finally {
71 | sourceJarFile.close();
72 | }
73 | }
74 |
75 | private static TreeSet findPackages(File file) throws IOException {
76 | TreeSet result = new TreeSet();
77 | JarFile jarFile = new JarFile(file);
78 | try {
79 | for (Enumeration e = jarFile.entries(); e.hasMoreElements(); ) {
80 | JarEntry entry = e.nextElement();
81 | if (entry.getName().endsWith(".class")) {
82 | int index = entry.getName().lastIndexOf('/');
83 | if (index > 0) {
84 | String packageName = entry.getName().substring(0, index).replace('/', '.');
85 | result.add(packageName);
86 | }
87 | }
88 | }
89 | } finally {
90 | jarFile.close();
91 | }
92 | return result;
93 | }
94 |
95 | private static void copyJar(File sourceFile, File targetFile, Manifest manifest) throws IOException {
96 | JarFile sourceJarFile = new JarFile(sourceFile);
97 | try {
98 | System.out.println("Creating jar: " + targetFile);
99 | byte buffer[] = new byte[10240];
100 | FileOutputStream os = new FileOutputStream(targetFile);
101 | JarOutputStream out = new JarOutputStream(os, manifest);
102 | try {
103 | for (Enumeration e = sourceJarFile.entries(); e.hasMoreElements(); ) {
104 | JarEntry entry = e.nextElement();
105 |
106 | // skip dirs
107 | if (entry.isDirectory()) continue;
108 |
109 | // skip manifest
110 | if (entry.getName().equals("META-INF/MANIFEST.MF")) continue;
111 |
112 | // Add archive entry
113 | JarEntry jarAdd = new JarEntry(entry.getName());
114 | jarAdd.setTime(entry.getTime());
115 | jarAdd.setComment(entry.getComment());
116 | jarAdd.setCompressedSize(entry.getCompressedSize());
117 | jarAdd.setCrc(entry.getCrc());
118 | jarAdd.setSize(entry.getSize());
119 | jarAdd.setExtra(entry.getExtra());
120 | out.putNextEntry(jarAdd);
121 |
122 | // Write file to archive
123 | InputStream in = sourceJarFile.getInputStream(entry);
124 | try {
125 | while (true) {
126 | int nRead = in.read(buffer, 0, buffer.length);
127 | if (nRead <= 0)
128 | break;
129 | out.write(buffer, 0, nRead);
130 | }
131 | } finally {
132 | in.close();
133 | }
134 | }
135 | } finally {
136 | out.close();
137 | os.close();
138 | targetFile.setLastModified(sourceFile.lastModified());
139 | }
140 | } finally {
141 | sourceJarFile.close();
142 | }
143 | }
144 |
145 | public static String getValue(Manifest manifest, String name) {
146 | String value = manifest.getMainAttributes().getValue(name);
147 | if (value != null) {
148 | int index = value.indexOf(';');
149 | if (index >= 0) {
150 | value = value.substring(0, index);
151 | }
152 | }
153 | return value;
154 | }
155 |
156 | }
157 |
--------------------------------------------------------------------------------
/src/org/scalastuff/osgitools/OsgiifyIvy.java:
--------------------------------------------------------------------------------
1 | package org.scalastuff.osgitools;
2 |
3 | import java.io.File;
4 | import java.io.IOException;
5 |
6 | public class OsgiifyIvy {
7 |
8 | private static final File userHome = new File(System.getProperty("user.home"));
9 | private static final File ivyHome = new File(userHome, ".ivy2");
10 | public static final File targetDir = new File(ivyHome, "osgi");
11 |
12 | public static void osgiifyIvy(boolean release) throws IOException {
13 | osgiifyIvy(new File(ivyHome, "cache"), targetDir, release);
14 | }
15 |
16 | public static File osgiify(File sourceFile, String symbolicName, String version, boolean release) throws IOException {
17 | return Osgiify.osgiify(sourceFile, symbolicName, version, targetDir, release);
18 | }
19 |
20 | public static void osgiifyIvy(File ivyDir, File targetDir, boolean release) throws IOException {
21 | for (File orgDir : ivyDir.listFiles()) {
22 | if (orgDir.isDirectory()) {
23 | for (File nameDir : orgDir.listFiles()) {
24 | if (nameDir.isDirectory()) {
25 | String symbolicName = orgDir.getName() + "." + nameDir.getName();
26 | File jarsDir = new File(nameDir, "jars");
27 | if (jarsDir.isDirectory()) {
28 | for (File jar : jarsDir.listFiles()) {
29 | if (jar.getName().startsWith(nameDir.getName() + "-") && jar.getName().endsWith(".jar")) {
30 | String version = jar.getName().substring(nameDir.getName().length() + 1, jar.getName().length() - ".jar".length());
31 | Osgiify.osgiify(jar, symbolicName, version, targetDir, release);
32 | }
33 | }
34 | }
35 | }
36 | }
37 | }
38 | }
39 | }
40 |
41 | public static void main(String[] args) throws IOException {
42 | osgiifyIvy(true);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/org/scalastuff/osgitools/util/StringTokenizer.java:
--------------------------------------------------------------------------------
1 | package org.scalastuff.osgitools.util;
2 |
3 | import java.util.ArrayList;
4 | import java.util.EnumSet;
5 | import java.util.List;
6 |
7 | public class StringTokenizer {
8 |
9 | public enum TokenizeOptions { DETECT_DBLQUOTES, DETECT_QUOTES, DETECT_BRACES, TRIM_TOKENS, DETECT_BRACKETS }
10 |
11 | private final String s;
12 | private final char sep;
13 | private final EnumSet options;
14 | private final StringBuilder out = new StringBuilder();
15 | private int i = 0;
16 |
17 | private StringTokenizer(String s, char sep, EnumSet options) {
18 | this.s = s;
19 | this.sep = sep;
20 | this.options = options;
21 | }
22 |
23 | public static List tokenize(String s, char sep) {
24 | return tokenize(s, sep, EnumSet.allOf(TokenizeOptions.class));
25 | }
26 |
27 | public static List tokenize(String s, char sep, EnumSet options) {
28 | return new StringTokenizer(s, sep, options).tokenize();
29 | }
30 |
31 | private List tokenize() {
32 | List tokens = new ArrayList();
33 | for (i = 0; i <= s.length(); i++) {
34 | if (i == s.length() || s.charAt(i) == sep) {
35 | if (options.contains(TokenizeOptions.TRIM_TOKENS)) {
36 | tokens.add(out.toString().trim());
37 | } else {
38 | tokens.add(out.toString());
39 | }
40 | out.setLength(0);
41 | } else {
42 | read();
43 | }
44 | }
45 | return tokens;
46 | }
47 |
48 | private void read() {
49 | char c = s.charAt(i);
50 | if (c == '\"' && options.contains(TokenizeOptions.DETECT_DBLQUOTES)) skipQuotes('\"');
51 | else if (c == '\'' && options.contains(TokenizeOptions.DETECT_QUOTES)) skipQuotes('\"');
52 | else if (c == '(' && options.contains(TokenizeOptions.DETECT_BRACES)) skipBraces('(', ')');
53 | else if (c == '{' && options.contains(TokenizeOptions.DETECT_BRACKETS)) skipBraces('{', '}');
54 | else out.append(c);
55 | }
56 |
57 | private void skipQuotes(char delim) {
58 | out.append(delim);
59 | for (i++; i < s.length(); i++) {
60 | char c = s.charAt(i);
61 | out.append(c);
62 | if (c == delim) break;
63 | }
64 | }
65 |
66 | private void skipBraces(char left, char right) {
67 | out.append(left);
68 | for (i++; i < s.length(); i++) {
69 | char c = s.charAt(i);
70 | out.append(c);
71 | if (c == right) {
72 | out.append(c);
73 | return;
74 | }
75 | read();
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------