1
/*******************************************************************************
2
* Copyright (c) 2007 Alphonse Van Assche.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Alphonse Van Assche - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.linuxtools.internal.rpmstubby;
13
import java.io.FileInputStream;
14
import java.io.FileNotFoundException;
15
import java.io.IOException;
16
import java.util.Properties;
18
import javax.xml.parsers.DocumentBuilder;
19
import javax.xml.parsers.DocumentBuilderFactory;
20
import javax.xml.parsers.ParserConfigurationException;
22
import org.eclipse.core.resources.IFile;
23
import org.eclipse.core.runtime.CoreException;
24
import org.eclipse.core.runtime.IPath;
25
import org.eclipse.linuxtools.internal.rpmstubby.model.FeatureModel;
26
import org.w3c.dom.Document;
27
import org.xml.sax.SAXException;
30
* Generates the RPM specfile and the fetch script based on the feature and user
34
public class StubbyGenerator extends AbstractGenerator {
36
private FeatureModel model;
37
private IFile featureFile;
40
* Creates the specfile and fetch script generator for the given packages.
43
* The feature.xml file to generate from.
45
public StubbyGenerator(IFile featureFile) {
46
this.featureFile = featureFile;
48
specfileName = model.getPackageName().toLowerCase() + ".spec";
49
projectName = featureFile.getProject().getName();
52
private void parse(IFile featureFile) {
53
DocumentBuilderFactory docfactory = DocumentBuilderFactory
55
IPath featureDir = featureFile.getLocation().removeLastSegments(1);
56
String featurePropertiesFile = featureDir.toOSString()
57
+ "/feature.properties";
58
Properties featureProperties = new Properties();
60
featureProperties.load(new FileInputStream(featurePropertiesFile));
61
} catch (FileNotFoundException e) {
62
// Do nothing if the feature.properties is not found
63
} catch (IOException e) {
64
StubbyLog.logError(e);
66
DocumentBuilder docbuilder;
68
docbuilder = docfactory.newDocumentBuilder();
69
Document docroot = docbuilder.parse(featureFile.getContents());
70
model = new FeatureModel(docroot, featureProperties);
72
} catch (ParserConfigurationException e) {
73
StubbyLog.logError(e);
74
} catch (SAXException e) {
75
StubbyLog.logError(e);
76
} catch (IOException e) {
77
StubbyLog.logError(e);
78
} catch (CoreException e) {
79
StubbyLog.logError(e);
84
* Generates a RPM specfile based on the parsed data from the pom file.
86
* @return The generated specfile.
89
public String generateSpecfile() {
90
StringBuilder buffer = new StringBuilder();
91
buffer.append("%global eclipse_base %{_libdir}/eclipse\n");
92
buffer.append("%global install_loc %{_datadir}/eclipse/dropins/"
93
+ model.getSimplePackageName() + "\n\n");
94
buffer.append("Name: " + model.getPackageName().toLowerCase()
96
buffer.append("Version: " + model.getVersion() + "\n");
97
buffer.append("Release: 1%{?dist}" + "\n");
98
buffer.append("Summary: " + model.getSummary() + "\n\n");
99
buffer.append("Group: Development/Tools\n");
100
buffer.append("License: " + model.getLicense() + "\n");
101
buffer.append("URL: " + model.getURL() + "\n");
102
buffer.append("Source0: #FIXME\n");
103
buffer.append("BuildArch: noarch\n\n");
104
generateRequires(buffer);
105
buffer.append("\n%description\n" + model.getDescription() + "\n");
106
generatePrepSection(buffer);
107
generateBuildSection(buffer);
108
generateInstallSection(buffer);
109
generateFilesSections(buffer);
110
generateChangelog(buffer);
112
return buffer.toString();
115
private void generateRequires(StringBuilder buffer) {
116
buffer.append("BuildRequires: eclipse-pde >= 1:3.4.0\n");
117
buffer.append("Requires: eclipse-platform >= 3.4.0\n");
121
private void generateInstallSection(StringBuilder buffer) {
122
buffer.append("%install\n");
123
buffer.append("install -d -m 755 %{buildroot}%{install_loc}\n\n");
124
buffer.append("%{__unzip} -q -d %{buildroot}%{install_loc} \\\n");
125
buffer.append(" build/rpmBuild/" + model.getFeatureId()
130
private void generateFilesSections(StringBuilder buffer) {
131
buffer.append("%files\n");
132
buffer.append("%{install_loc}\n");
133
String docsRoot = featureFile.getLocation().removeLastSegments(1)
135
String[] files = featureFile.getLocation().removeLastSegments(1)
137
for (String file : files) {
138
if (file.matches("(epl-.*|license)\\.html")) {
139
buffer.append("%doc " + docsRoot + "/" + file + "\n");
145
private void generatePrepSection(StringBuilder buffer) {
146
buffer.append("\n%prep\n");
147
buffer.append("#FIXME Replace FIXME with the root directory name in Source0\n");
148
buffer.append("%setup -q -n FIXME\n\n");
151
private void generateBuildSection(StringBuilder buffer) {
152
buffer.append("%build\n");
153
buffer.append("%{eclipse_base}/buildscripts/pdebuild -f ").append(
154
model.getFeatureId());
155
buffer.append("\n\n");
159
* Returns the last meaningful part of the feature id before the feature
163
* The feature id from which to extract the name.
164
* @return The part of the feature id to be used for package name.
166
public String getPackageName(String packageName) {
167
String[] packageItems = packageName.split("\\.");
168
String name = packageItems[packageItems.length - 1];
169
if (name.equalsIgnoreCase("feature")) {
170
name = packageItems[packageItems.length - 2];
172
return "eclipse-" + name;
175
private void generateChangelog(StringBuilder buffer) {
176
buffer.append("%changelog\n\n");
177
buffer.append("#FIXME\n");