Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 71b712b7ca9d468324cffb866050873377c4ff50 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*******************************************************************************
 * Copyright (c) 2009 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat Incorporated - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.rpmstubby;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.linuxtools.internal.rpmstubby.model.PomModel;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 * Generator for RPM specfile from maven pom.xml.
 *
 */
public class StubbyPomGenerator {

	private PomModel model;

	/**
	 * Creates the generator by parsing the pom.xml file.
	 * @param pomFile The pom.xml file to generate specfile for.
	 */
	public StubbyPomGenerator(IFile pomFile) {
		parse(pomFile);
	}

	private void parse(IFile pomFile) {
		DocumentBuilderFactory docfactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder docbuilder;
		try {
			docbuilder = docfactory.newDocumentBuilder();
			Document docroot = docbuilder.parse(pomFile.getContents());
			model = new PomModel(docroot);

		} catch (ParserConfigurationException e) {
			StubbyLog.logError(e);
		} catch (SAXException e) {
			StubbyLog.logError(e);
		} catch (IOException e) {
			StubbyLog.logError(e);
		} catch (CoreException e) {
			StubbyLog.logError(e);
		}
	}

	/**
	 * Generates a RPM specfile based on the parsed data from the pom file.
	 *
	 * @return The generated specfile.
	 */
	public String generateSpecfile() {
		StringBuilder buffer = new StringBuilder();
		String packageName = model.getPackageName();
		buffer.append("Name:           " + packageName.toLowerCase() + "\n");
		buffer.append("Version:        " + model.getVersion() + "\n");
		buffer.append("Release:        1%{?dist}" + "\n");
		buffer.append("Summary:        " + model.getSummary() + "\n\n");
		buffer.append("Group:          Development/Libraries\n");
		buffer.append("License:        " + model.getLicense() + "\n");
		buffer.append("URL:            " + model.getURL() + "\n");
		buffer.append("Source0:        #FIXME\n");
		buffer.append("BuildArch: noarch\n\n");
		generateRequires(buffer);
		buffer.append("\n%description\n" + model.getDescription() + "\n\n");
		generateJavadocSubpackage(buffer);
		generatePrepSection(buffer);
		generateBuildSection(buffer);
		generateInstallSection(buffer);
		generateFilesSections(buffer);
		generateChangelog(buffer);

		return buffer.toString();
	}

	private void generateRequires(StringBuilder buffer) {
		for (Map.Entry<String,String> entry : model.getDependencies().entrySet()) {
			buffer.append("BuildRequires: mvn("+entry.getKey()+":"+entry.getValue()+")\n");
		}
		for (Map.Entry<String,String> entry : model.getDependencies().entrySet()) {
			buffer.append("Requires: mvn("+entry.getKey()+":"+entry.getValue()+")\n");
		}
	}

	private void generateJavadocSubpackage(StringBuilder buffer) {
		buffer.append("%package javadoc\n");
		buffer.append("Group:          Documentation\n");
		buffer.append("Summary:        Javadoc for %{name}\n");
		buffer.append("Requires:       jpackage-utils\n\n");

		buffer.append("%description javadoc\n");
		buffer.append("API documentation for %{name}.\n\n");

	}

	private void generateChangelog(StringBuilder buffer) {
		buffer.append("%changelog\n\n");
		buffer.append("#FIXME\n");
	}

	private void generateInstallSection(StringBuilder buffer) {
		buffer.append("%install\n");
		buffer.append("# jars\n");
		buffer.append("install -d -m 0755 %{buildroot}%{_javadir}\n");
		buffer
				.append("install -m 644 target/%{name}-%{version}.jar   %{buildroot}%{_javadir}/%{name}.jar\n\n");


		buffer.append("# poms\n");
		buffer
				.append("install -d -m 755 %{buildroot}%{_mavenpomdir}\n");
		buffer.append("install -pm 644 pom.xml \\\n");
		buffer
				.append("    %{buildroot}%{_mavenpomdir}/JPP.%{name}.pom\n\n");

		buffer.append("%add_maven_depmap JPP.%{name}.pom %{name}.jar\n\n");
		
		buffer.append("# javadoc\n");
		buffer
				.append("install -d -m 0755 %{buildroot}%{_javadocdir}/%{name}\n");
		buffer
				.append("cp -pr target/site/api*/* %{buildroot}%{_javadocdir}/%{name}/\n");
		buffer.append("rm -rf target/site/api*\n\n");
	}

	private void generateFilesSections(StringBuilder buffer) {
		buffer.append("%files\n");
		buffer.append("%{_javadir}/*\n");
		buffer.append("%{_mavenpomdir}/*\n");
		buffer.append("%{_mavendepmapfragdir}/*\n\n");

		buffer.append("%files javadoc\n");
		buffer.append("%{_javadocdir}/%{name}\n\n");
	}

	private void generatePrepSection(StringBuilder buffer) {
		buffer.append("\n%prep\n");
		buffer.append("%setup -q #You may need to update this according to your Source0\n\n");
	}

	private void generateBuildSection(StringBuilder buffer) {
		buffer.append("%build\n");
		buffer.append("mvn-rpmbuild \\\n");
		buffer.append("        -e \\\n");
		buffer.append("        install javadoc:javadoc\n\n");
	}

	/**
	 * Writes the given contents to a file with the given fileName in the
	 * specified project.
	 *
	 * @param projectName
	 *            The name of the project to put the file into.
	 * @throws CoreException
	 *             Thrown when the project doesn't exist.
	 */
	public void writeContent(String projectName) throws CoreException {
		String fileName = model.getPackageName().toLowerCase() + ".spec";
		String contents = generateSpecfile();
		InputStream contentInputStream = new ByteArrayInputStream(contents
				.getBytes());
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IResource resource = root.findMember(new Path(projectName));
		if (!resource.exists() || !(resource instanceof IContainer)) {
			throwCoreException("Project \"" + projectName
					+ "\" does not exist.");
		}
		IContainer container = (IContainer) resource;
		final IFile file = container.getFile(new Path(fileName));
		try {
			InputStream stream = contentInputStream;
			if (file.exists()) {
				file.setContents(stream, true, true, null);
			} else {
				file.create(stream, true, null);
			}
			stream.close();
		} catch (IOException e) {
			StubbyLog.logError(e);
		}
		StubbyPlugin.getActiveWorkbenchShell().getDisplay().asyncExec(
				new Runnable() {
					public void run() {
						IWorkbenchPage page = PlatformUI.getWorkbench()
								.getActiveWorkbenchWindow().getActivePage();
						try {
							IDE.openEditor(page, file, true);
						} catch (PartInitException e) {
							StubbyLog.logError(e);
						}
					}
				});
	}

	private void throwCoreException(String message) throws CoreException {
		IStatus status = new Status(IStatus.ERROR, StubbyPlugin.PLUGIN_ID,
				IStatus.OK, message, null);
		throw new CoreException(status);
	}
}

Back to the top