Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5dfe4ae80ccd82d79141d4cf2600766b223a8411 (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Copyright (c) 2014 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.internal.setup.targlets;

import org.eclipse.emf.cdo.releng.internal.setup.Activator;
import org.eclipse.emf.cdo.releng.internal.setup.targlets.IUGenerator.FeatureIUGenerator;
import org.eclipse.emf.cdo.releng.internal.setup.ui.ErrorDialog;
import org.eclipse.emf.cdo.releng.predicates.PredicatesFactory;
import org.eclipse.emf.cdo.releng.setup.AutomaticSourceLocator;
import org.eclipse.emf.cdo.releng.setup.InstallableUnit;
import org.eclipse.emf.cdo.releng.setup.P2Repository;
import org.eclipse.emf.cdo.releng.setup.RepositoryList;
import org.eclipse.emf.cdo.releng.setup.SetupFactory;
import org.eclipse.emf.cdo.releng.setup.Targlet;

import org.eclipse.net4j.util.concurrent.ConcurrencyUtil;
import org.eclipse.net4j.util.om.monitor.SubMonitor;

import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.pde.core.target.ITargetDefinition;
import org.eclipse.pde.core.target.ITargetHandle;
import org.eclipse.pde.core.target.ITargetLocation;
import org.eclipse.pde.core.target.ITargetPlatformService;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

/**
 * @author Eike Stepper
 */
@SuppressWarnings("unused")
public class TestAction implements IWorkbenchWindowActionDelegate
{
  private static final String TARGET_NAME = "Modular Target";

  private static final String CONTAINER_ID = "Test";

  public TestAction()
  {
  }

  public void init(IWorkbenchWindow window)
  {
  }

  public void dispose()
  {
  }

  public void selectionChanged(IAction action, ISelection selection)
  {
  }

  public void run(IAction action)
  {
    try
    {
      // testProgress();
      // generateFeatureIU();
      initTargetPlatform();
    }
    catch (Throwable ex)
    {
      Activator.log(ex);
      ErrorDialog.open(ex);
    }
  }

  private static void testProgress() throws Exception
  {
    Shell shell = PlatformUI.getWorkbench().getDisplay().getActiveShell();
    new ProgressMonitorDialog(shell).run(true, true, new IRunnableWithProgress()
    {
      public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
      {
        try
        {
          recurse(ResourcesPlugin.getWorkspace().getRoot(), monitor);
        }
        catch (OperationCanceledException ex)
        {
          //$FALL-THROUGH$
        }
        catch (CoreException ex)
        {
          throw new InvocationTargetException(ex);
        }
      }

      public void recurse(IContainer container, IProgressMonitor monitor) throws CoreException
      {
        IResource[] members = container.members();

        SubMonitor progress = SubMonitor.convert(monitor, members.length).detectCancelation();
        progress.subTask(container.getFullPath().toString());

        for (IResource member : members)
        {
          if (member instanceof IContainer)
          {
            ConcurrencyUtil.sleep(2);
            recurse((IContainer)member, progress.newChild());
          }
          else
          {
            progress.skipped();
          }
        }
      }
    });
  }

  private static void generateFeatureIU() throws Exception
  {
    IUGenerator generator = FeatureIUGenerator.INSTANCE;

    System.out.println(generator.generateIU(
        new File("C:/develop/cdo/master/git/cdo/features/org.eclipse.emf.cdo.site-feature")).getRequirements());

    System.out.println(generator.generateIU(
        new File("C:/develop/cdo/master/git/cdo/features/org.eclipse.emf.cdo.sdk-feature")).getRequirements());

    System.out.println(generator.generateIU(
        new File("C:/develop/cdo/master/git/cdo/features/org.eclipse.emf.cdo-feature")).getRequirements());
  }

  private static void initTargetPlatform() throws Exception
  {
    // Container
    TargletContainer container = getContainer();

    // Name
    String name = "Targlet 1";
    EList<Targlet> targlets = container.getTarglets();
    if (!targlets.isEmpty())
    {
      Targlet targlet = targlets.get(0);
      if (name.equals(targlet.getName()))
      {
        name = "Targlet 2";
      }
    }

    // Targlet
    Targlet targlet = SetupFactory.eINSTANCE.createTarglet();
    targlet.setName(name);
    targlet.setIncludeSources(true);

    // Roots
    EList<InstallableUnit> roots = targlet.getRoots();
    roots.add(component("org.eclipse.emf.cdo.releng.all.feature.group"));
    roots.add(component("org.eclipse.net4j.util.ui.feature.group"));
    roots.add(component("org.eclipse.net4j.db.feature.group"));
    roots.add(component("org.eclipse.sdk.feature.group"));

    // Sources
    AutomaticSourceLocator sourceLocator = sourceLocator("C:/cdo", false);
    sourceLocator.getPredicates().add(
        PredicatesFactory.eINSTANCE.createNotPredicate(PredicatesFactory.eINSTANCE
            .createNamePredicate("org\\.eclipse\\.net4j.*")));
    targlet.getSourceLocators().add(sourceLocator);

    // Repos
    RepositoryList repositoryList = SetupFactory.eINSTANCE.createRepositoryList();
    repositoryList.setName("Milestones");
    EList<P2Repository> repos = repositoryList.getP2Repositories();
    repos.add(repository("http://download.eclipse.org/eclipse/updates/4.4milestones"));
    repos.add(repository("http://download.eclipse.org/tools/orbit/downloads/drops/S20140116105218/repository"));
    repos.add(repository("http://download.eclipse.org/tools/buckminster/updates-4.3"));
    repos.add(repository("http://download.eclipse.org/tools/gef/updates/milestones"));
    repos.add(repository("http://download.eclipse.org/modeling/emf/emf/updates/2.10milestones"));
    repos.add(repository("http://download.eclipse.org/egit/updates-stable-nightly"));
    repos.add(repository("http://download.eclipse.org/mylyn/snapshots/weekly"));
    repos.add(repository("http://download.eclipse.org/technology/nebula/snapshot"));
    repos.add(repository("https://hudson.eclipse.org/hudson/job/emf-cdo-integration/lastSuccessfulBuild/artifact"));
    targlet.getRepositoryLists().add(repositoryList);
    targlet.setActiveRepositoryList(repositoryList.getName());
    container.setTarglets(ECollections.singletonEList(targlet));
  }

  private static ITargetDefinition getTarget() throws CoreException
  {
    @SuppressWarnings("restriction")
    ITargetPlatformService service = (ITargetPlatformService)org.eclipse.pde.internal.core.PDECore.getDefault()
        .acquireService(ITargetPlatformService.class.getName());

    for (ITargetHandle handle : service.getTargets(new NullProgressMonitor()))
    {
      ITargetDefinition target = handle.getTargetDefinition();
      if (TARGET_NAME.equals(target.getName()))
      {
        return target;
      }
    }

    ITargetDefinition target = service.newTarget();
    target.setName(TARGET_NAME);
    return target;
  }

  private static TargletContainer getContainer() throws Exception
  {
    ITargetDefinition target = getTarget();

    ITargetLocation[] locations = target.getTargetLocations();
    if (locations != null)
    {
      for (ITargetLocation location : locations)
      {
        if (location instanceof TargletContainer)
        {
          TargletContainer container = (TargletContainer)location;
          if (CONTAINER_ID.equals(container.getID()))
          {
            return container;
          }
        }
      }
    }

    TargletContainer container = new TargletContainer(CONTAINER_ID);

    ITargetLocation[] newLocations;
    ITargetLocation[] oldLocations = locations;
    if (oldLocations != null && oldLocations.length != 0)
    {
      newLocations = new ITargetLocation[oldLocations.length + 1];
      System.arraycopy(oldLocations, 0, newLocations, 0, oldLocations.length);
      newLocations[oldLocations.length] = container;
    }
    else
    {
      newLocations = new ITargetLocation[] { container };
    }

    target.setTargetLocations(newLocations);
    return container;
  }

  private static InstallableUnit component(String id)
  {
    InstallableUnit installableUnit = SetupFactory.eINSTANCE.createInstallableUnit();
    installableUnit.setID(id);
    return installableUnit;
  }

  private static AutomaticSourceLocator sourceLocator(String rootFolder, boolean locateNestedProjects)
  {
    AutomaticSourceLocator sourceLocator = SetupFactory.eINSTANCE.createAutomaticSourceLocator();
    sourceLocator.setRootFolder(rootFolder);
    sourceLocator.setLocateNestedProjects(locateNestedProjects);
    return sourceLocator;
  }

  private static P2Repository repository(String url)
  {
    P2Repository p2Repository = SetupFactory.eINSTANCE.createP2Repository();
    p2Repository.setURL(url);
    return p2Repository;
  }
}

Back to the top