Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 188a1bb89b4aed1287784565135899a1761da9eb (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.releng;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Eike Stepper
 */
public class GenerateExampleBuilders
{
  private static final String TOKEN_EXAMPLE_PROJECT = "#EXAMPLE_PROJECT#";

  private static final String TOKEN_INSTALLER_PROJECT = "#INSTALLER_PROJECT#";

  private static final String TOKEN_RELEVANT_RESOURCES = "#RELEVANT_RESOURCES#";

  private static final String TOKEN_REFRESH_RESOURCES = "#REFRESH_RESOURCES#";

  private static final String NL = System.getProperty("line.separator");

  public static void main(String[] args) throws Exception
  {
    File installerPoject = new File(args[0]).getCanonicalFile();
    File pluginXML = new File(installerPoject, "plugin.xml");
    System.out.println("Analyzing " + pluginXML);

    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    XMLHandler handler = new XMLHandler(installerPoject);
    InputStream in = new FileInputStream(pluginXML);

    try
    {
      parser.parse(in, handler);
    }
    finally
    {
      in.close();
    }
  }

  private static void processProjectDescriptor(File exampleProject, File installerPoject, String targetFolder)
      throws Exception
  {
    String exampleProjectName = exampleProject.getName();
    String targetPath = installerPoject.getName() + "/" + targetFolder;

    StringBuilder relevantResources = new StringBuilder();
    relevantResources.append(getItemPath(targetPath, 2));

    for (File file : exampleProject.listFiles())
    {
      String name = file.getName();
      int type = file.isDirectory() ? 2 : 1;

      if (!"bin".equals(name))
      {
        String path = exampleProjectName + "/" + name;
        relevantResources.append(getItemPath(path, type));
      }
    }

    Map<String, String> substitutes = new HashMap<String, String>();
    addSubtitute(substitutes, TOKEN_EXAMPLE_PROJECT, exampleProject.getName());
    addSubtitute(substitutes, TOKEN_INSTALLER_PROJECT, installerPoject.getName());
    addSubtitute(substitutes, TOKEN_RELEVANT_RESOURCES, relevantResources.toString());
    addSubtitute(substitutes, TOKEN_REFRESH_RESOURCES, getItemPath(new File(targetPath).getParent(), 2));

    copy(exampleProject, substitutes, "copyExample.ant", ".externalToolBuilders/copyExample.ant");
    copy(exampleProject, substitutes, "template.launch", ".externalToolBuilders/" + exampleProjectName + ".launch");

    updateProjectDescription(exampleProject, substitutes);
  }

  private static void copy(File targetProject, Map<String, String> substitutes, String template, String targetPath)
      throws IOException
  {
    File source = getTemplate(targetProject.getParentFile(), template);
    String content = substitute(readFile(source), substitutes);

    File target = new File(targetProject, targetPath);
    System.out.println("      Generating " + target.getCanonicalPath());
    writeFile(target, content);
  }

  private static void updateProjectDescription(File targetProject, Map<String, String> substitutes) throws IOException
  {
    File snippetTemplate = getTemplate(targetProject.getParentFile(), "template.project");
    String snippet = substitute(readFile(snippetTemplate), substitutes);

    File descriptionFile = new File(targetProject, ".project");
    String description = readFile(descriptionFile);
    System.out.println("      Updating " + descriptionFile.getCanonicalPath());

    if (description.indexOf(snippet) != -1)
    {
      // Nothing to update
      return;
    }

    String newDescription = description;

    Pattern pattern = Pattern.compile(
        "(\\s*<buildCommand>.*?</buildCommand>)*(\\s*<buildCommand>.*?<value>&lt;project&gt;/\\.externalToolBuilders/"
            + targetProject.getName().replace(".", "\\.") + "\\.launch</value>.*?</buildCommand>)", Pattern.DOTALL);
    Matcher matcher = pattern.matcher(description);
    if (matcher.find())
    {
      String remove = matcher.group(2);
      newDescription = newDescription.replace(remove, "");
    }

    newDescription = newDescription.replaceFirst("[ \\t]*</buildSpec>", snippet + "\t</buildSpec>");
    if (!newDescription.equals(description))
    {
      writeFile(descriptionFile, newDescription);
    }
  }

  private static String readFile(File file) throws IOException
  {
    StringBuilder result = new StringBuilder();
    FileReader in = null;

    try
    {
      in = new FileReader(file);
      BufferedReader reader = new BufferedReader(in);

      String line;
      while ((line = reader.readLine()) != null)
      {
        result.append(line);
        result.append(NL);
      }

      return result.toString();
    }
    finally
    {
      if (in != null)
      {
        in.close();
      }
    }
  }

  private static void writeFile(File file, String content) throws IOException
  {
    FileWriter out = null;

    try
    {
      out = new FileWriter(file);
      BufferedWriter writer = new BufferedWriter(out);

      writer.write(content);
      writer.flush();
    }
    finally
    {
      if (out != null)
      {
        out.close();
      }
    }
  }

  private static String getItemPath(String resource, int type)
  {
    return "&lt;item path=&quot;/" + resource.replace('\\', '/') + "&quot; type=&quot;" + type
        + "&quot;/&gt;&#13;&#10;";
  }

  private static File getTemplate(File root, String template)
  {
    return new File(root, "org.eclipse.emf.cdo.releng/exampleBuilderTemplates/" + template);
  }

  private static void addSubtitute(Map<String, String> substitutes, String token, String substitute)
  {
    System.out.println("      " + token + " = " + substitute);
    substitutes.put(token, substitute);
  }

  private static String substitute(String content, Map<String, String> substitutes)
  {
    for (Entry<String, String> entry : substitutes.entrySet())
    {
      content = content.replace(entry.getKey(), entry.getValue());
    }

    return content;
  }

  /**
   * @author Eike Stepper
   */
  private static class XMLHandler extends DefaultHandler
  {
    private File installerPoject;

    public XMLHandler(File installerPoject)
    {
      this.installerPoject = installerPoject;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
    {
      if ("projectDescriptor".equalsIgnoreCase(qName))
      {
        try
        {
          String name = attributes.getValue("name");
          String contentURI = attributes.getValue("contentURI");
          File exampleProject = new File(installerPoject, "../" + name).getCanonicalFile();

          System.out.println("   Processing " + name + " --> " + contentURI);
          processProjectDescriptor(exampleProject, installerPoject, contentURI);
        }
        catch (Exception ex)
        {
          ex.printStackTrace();
        }
      }
    }
  }
}

Back to the top