~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to rpmstubby/org.eclipse.linuxtools.rpmstubby/src/org/eclipse/linuxtools/internal/rpmstubby/StubbyGenerator.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
9
 *    Alphonse Van Assche - initial API and implementation
 
10
 *******************************************************************************/
 
11
package org.eclipse.linuxtools.internal.rpmstubby;
 
12
 
 
13
import java.io.FileInputStream;
 
14
import java.io.FileNotFoundException;
 
15
import java.io.IOException;
 
16
import java.util.Properties;
 
17
 
 
18
import javax.xml.parsers.DocumentBuilder;
 
19
import javax.xml.parsers.DocumentBuilderFactory;
 
20
import javax.xml.parsers.ParserConfigurationException;
 
21
 
 
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;
 
28
 
 
29
/**
 
30
 * Generates the RPM specfile and the fetch script based on the feature and user
 
31
 * preferences.
 
32
 * 
 
33
 */
 
34
public class StubbyGenerator extends AbstractGenerator {
 
35
 
 
36
        private FeatureModel model;
 
37
        private IFile featureFile;
 
38
 
 
39
        /**
 
40
         * Creates the specfile and fetch script generator for the given packages.
 
41
         * 
 
42
         * @param featureFile
 
43
         *            The feature.xml file to generate from.
 
44
         */
 
45
        public StubbyGenerator(IFile featureFile) {
 
46
                this.featureFile = featureFile;
 
47
                parse(featureFile);
 
48
                specfileName = model.getPackageName().toLowerCase() + ".spec";
 
49
                projectName = featureFile.getProject().getName();
 
50
        }
 
51
 
 
52
        private void parse(IFile featureFile) {
 
53
                DocumentBuilderFactory docfactory = DocumentBuilderFactory
 
54
                                .newInstance();
 
55
                IPath featureDir = featureFile.getLocation().removeLastSegments(1);
 
56
                String featurePropertiesFile = featureDir.toOSString()
 
57
                                + "/feature.properties";
 
58
                Properties featureProperties = new Properties();
 
59
                try {
 
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);
 
65
                }
 
66
                DocumentBuilder docbuilder;
 
67
                try {
 
68
                        docbuilder = docfactory.newDocumentBuilder();
 
69
                        Document docroot = docbuilder.parse(featureFile.getContents());
 
70
                        model = new FeatureModel(docroot, featureProperties);
 
71
 
 
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);
 
80
                }
 
81
        }
 
82
 
 
83
        /**
 
84
         * Generates a RPM specfile based on the parsed data from the pom file.
 
85
         * 
 
86
         * @return The generated specfile.
 
87
         */
 
88
        @Override
 
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()
 
95
                                + "\n");
 
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);
 
111
 
 
112
                return buffer.toString();
 
113
        }
 
114
 
 
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");
 
118
 
 
119
        }
 
120
 
 
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()
 
126
                                + ".zip \n\n");
 
127
 
 
128
        }
 
129
 
 
130
        private void generateFilesSections(StringBuilder buffer) {
 
131
                buffer.append("%files\n");
 
132
                buffer.append("%{install_loc}\n");
 
133
                String docsRoot = featureFile.getLocation().removeLastSegments(1)
 
134
                                .lastSegment();
 
135
                String[] files = featureFile.getLocation().removeLastSegments(1)
 
136
                                .toFile().list();
 
137
                for (String file : files) {
 
138
                        if (file.matches("(epl-.*|license)\\.html")) {
 
139
                                buffer.append("%doc " + docsRoot + "/" + file + "\n");
 
140
                        }
 
141
                }
 
142
                buffer.append("\n");
 
143
        }
 
144
 
 
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");
 
149
        }
 
150
 
 
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");
 
156
        }
 
157
 
 
158
        /**
 
159
         * Returns the last meaningful part of the feature id before the feature
 
160
         * substring.
 
161
         * 
 
162
         * @param packageName
 
163
         *            The feature id from which to extract the name.
 
164
         * @return The part of the feature id to be used for package name.
 
165
         */
 
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];
 
171
                }
 
172
                return "eclipse-" + name;
 
173
        }
 
174
 
 
175
        private void generateChangelog(StringBuilder buffer) {
 
176
                buffer.append("%changelog\n\n");
 
177
                buffer.append("#FIXME\n");
 
178
        }
 
179
 
 
180
}