Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1f48cab5ebfdee00690837b647c58b4be02aaa5a (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/*
 * 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.version;

import org.eclipse.emf.cdo.releng.version.Release.Element;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.service.resolver.BundleSpecification;
import org.eclipse.osgi.service.resolver.ExportPackageDescription;
import org.eclipse.osgi.service.resolver.ImportPackageSpecification;
import org.eclipse.osgi.service.resolver.VersionRange;
import org.eclipse.pde.core.IModel;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.core.plugin.PluginRegistry;

import org.osgi.framework.Version;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author Eike Stepper
 */
public class VersionBuilder extends IncrementalProjectBuilder
{
  public static final String BUILDER_ID = "org.eclipse.emf.cdo.releng.version.VersionBuilder";

  public static final String RELEASE_PATH_ARGUMENT = "release.path";

  public static final String VALIDATOR_CLASS_ARGUMENT = "validator.class";

  public static final String IGNORE_DEPENDENCY_RANGES_ARGUMENT = "ignore.missing.dependency.ranges";

  public static final String IGNORE_EXPORT_VERSIONS_ARGUMENT = "ignore.missing.export.versions";

  public static final String IGNORE_CONTENT_CHANGES_ARGUMENT = "ignore.feature.content.changes";

  private static final Path MANIFEST_PATH = new Path("META-INF/MANIFEST.MF");

  private static final Path FEATURE_PATH = new Path("feature.xml");

  private static final Version ADDITION = new Version(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);

  private static final Version REMOVAL = new Version(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);

  private static final int NO_CHANGE = 0;

  private static final int MICRO_CHANGE = 1;

  private static final int MINOR_CHANGE = 2;

  private static final int MAJOR_CHANGE = 3;

  private static final boolean DEBUG = false;

  private Release release;

  public VersionBuilder()
  {
  }

  @Override
  protected final IProject[] build(int kind, @SuppressWarnings("rawtypes") Map args, IProgressMonitor monitor)
      throws CoreException
  {
    IProject project = getProject();
    List<IProject> buildDpependencies = new ArrayList<IProject>();

    BuildState buildState = Activator.getBuildState(project);
    boolean fullBuild = buildState.getReleaseTag() == null;
    VersionValidator validator = null;

    monitor.beginTask(null, 1);

    try
    {
      Markers.deleteAllMarkers(project);

      IModel componentModel = getComponentModel(project);
      IFile projectDescription = project.getFile(new Path(".project"));

      /*
       * Determine release data to validate against
       */

      String releasePath = (String)args.get(RELEASE_PATH_ARGUMENT);
      if (releasePath == null)
      {
        String msg = "Build command argument missing: " + RELEASE_PATH_ARGUMENT;
        Markers.addMarker(projectDescription, msg, IMarker.SEVERITY_ERROR, "(" + BUILDER_ID + ")");
        return buildDpependencies.toArray(new IProject[buildDpependencies.size()]);
      }

      try
      {
        IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(releasePath));
        buildDpependencies.add(file.getProject());

        Release release;
        if (!file.exists())
        {
          release = ReleaseManager.INSTANCE.createRelease(file);
        }
        else
        {
          release = ReleaseManager.INSTANCE.getRelease(file);
        }

        boolean releaseHasChanged = !release.getTag().equals(buildState.getReleaseTag());
        if (releaseHasChanged)
        {
          buildState.setReleaseTag(release.getTag());
          fullBuild = true;
        }

        this.release = release;
      }
      catch (CoreException ex)
      {
        ex.printStackTrace();
        String msg = "Problem with release spec: " + releasePath;
        Markers.addMarker(projectDescription, msg, IMarker.SEVERITY_ERROR, "(" + releasePath.replace(".", "\\.") + ")");
        return buildDpependencies.toArray(new IProject[buildDpependencies.size()]);
      }

      /*
       * Determine if a validation is needed or if the version has already been increased properly
       */

      Element element = ReleaseManager.INSTANCE.createElement(componentModel);
      Element releaseElement = release.getElements().get(element);
      if (releaseElement == null)
      {
        trace("Project has not been released: " + project.getName());
        return buildDpependencies.toArray(new IProject[buildDpependencies.size()]);
      }

      Version elementVersion = element.getVersion();
      Version releaseVersion = releaseElement.getVersion();
      Version nextImplVersion = new Version(releaseVersion.getMajor(), releaseVersion.getMinor(),
          releaseVersion.getMicro() + (release.isIntegration() ? 100 : 1));
      int comparison = releaseVersion.compareTo(elementVersion);
      if (comparison < 0)
      {
        if (!nextImplVersion.equals(elementVersion))
        {
          if (elementVersion.getMajor() == nextImplVersion.getMajor()
              && elementVersion.getMinor() == nextImplVersion.getMinor())
          {
            addVersionMarker("Version should be " + nextImplVersion);
          }
        }

        if (componentModel instanceof IPluginModelBase)
        {
          return buildDpependencies.toArray(new IProject[buildDpependencies.size()]);
        }
      }

      if (comparison > 0)
      {
        addVersionMarker("Version has been decreased after release " + releaseVersion);
        return buildDpependencies.toArray(new IProject[buildDpependencies.size()]);
      }

      if (componentModel instanceof IPluginModelBase)
      {
        if (!"true".equals(args.get(IGNORE_DEPENDENCY_RANGES_ARGUMENT)))
        {
          checkDependencyRanges((IPluginModelBase)componentModel);
        }

        if (!"true".equals(args.get(IGNORE_EXPORT_VERSIONS_ARGUMENT)))
        {
          checkPackageExports((IPluginModelBase)componentModel);
        }
      }
      else if (!"true".equals(args.get(IGNORE_CONTENT_CHANGES_ARGUMENT)))
      {
        List<Map.Entry<Element, Version>> warnings = new ArrayList<Entry<Element, Version>>();
        int change = checkFeatureAPI(componentModel, element, releaseElement, buildDpependencies, warnings);
        if (change != NO_CHANGE)
        {
          Version nextFeatureVersion = null;
          if (change == MAJOR_CHANGE)
          {
            nextFeatureVersion = new Version(releaseVersion.getMajor() + 1, 0, 0);
          }
          else if (change == MINOR_CHANGE)
          {
            nextFeatureVersion = new Version(releaseVersion.getMajor(), releaseVersion.getMinor() + 1, 0);
          }
          else if (change == MICRO_CHANGE)
          {
            nextFeatureVersion = new Version(releaseVersion.getMajor(), releaseVersion.getMinor(),
                releaseVersion.getMicro() + 1);
          }

          if (elementVersion.compareTo(nextFeatureVersion) < 0)
          {
            addVersionMarker("Version should be " + nextFeatureVersion);

            for (Entry<Element, Version> entry : warnings)
            {
              addIncludeMarker(entry.getKey(), entry.getValue());
            }
          }

          return buildDpependencies.toArray(new IProject[buildDpependencies.size()]);
        }

        if (!elementVersion.equals(releaseVersion))
        {
          return buildDpependencies.toArray(new IProject[buildDpependencies.size()]);
        }
      }

      /*
       * Determine validator to use
       */

      String validatorClass = (String)args.get(VALIDATOR_CLASS_ARGUMENT);
      if (validatorClass == null)
      {
        validatorClass = "org.eclipse.emf.cdo.releng.version.digest.DigestValidator$BuildModel";
      }

      try
      {
        Class<?> c = Class.forName(validatorClass, true, VersionBuilder.class.getClassLoader());
        validator = (VersionValidator)c.newInstance();
      }
      catch (Exception ex)
      {
        String msg = ex.getLocalizedMessage() + ": " + validatorClass;
        Markers.addMarker(projectDescription, msg, IMarker.SEVERITY_ERROR, ".*(" + validatorClass + ").*");
        return buildDpependencies.toArray(new IProject[buildDpependencies.size()]);
      }

      trace(validator.getClass().getName() + ": " + project.getName());

      /*
       * Do the validation
       */

      IResourceDelta delta = null;
      fullBuild |= kind == FULL_BUILD;
      if (!fullBuild)
      {
        delta = getDelta(project);
      }

      validator.updateBuildState(buildState, releasePath, release, project, delta, componentModel, monitor);

      try
      {
        if (buildState.isChangedSinceRelease())
        {
          addVersionMarker("Version must be increased to " + nextImplVersion);
        }
      }
      catch (Exception ignore)
      {
        Activator.log(ignore);
      }
    }
    catch (Exception ex)
    {
      try
      {
        if (validator != null)
        {
          validator.abort(buildState, project, ex, monitor);
        }

        String msg = Activator.log(ex);
        Markers.addMarker(project, msg);
      }
      catch (Exception ignore)
      {
        Activator.log(ignore);
      }
    }
    finally
    {
      monitor.done();
    }

    return buildDpependencies.toArray(new IProject[buildDpependencies.size()]);
  }

  @SuppressWarnings("restriction")
  private int checkFeatureAPI(IModel componentModel, Element element, Element releasedElement,
      List<IProject> buildDpependencies, List<Entry<Element, Version>> warnings)
  {
    org.eclipse.pde.internal.core.ifeature.IFeatureModel featureModel = (org.eclipse.pde.internal.core.ifeature.IFeatureModel)componentModel;
    org.eclipse.pde.internal.core.ifeature.IFeature feature = featureModel.getFeature();

    for (org.eclipse.pde.internal.core.ifeature.IFeatureChild versionable : feature.getIncludedFeatures())
    {
      Element childElement = new Element(Element.Type.FEATURE, versionable.getId(), versionable.getVersion());
      element.getChildren().add(childElement);
    }

    for (org.eclipse.pde.internal.core.ifeature.IFeaturePlugin versionable : feature.getPlugins())
    {
      Element childElement = new Element(Element.Type.PLUGIN, versionable.getId(), versionable.getVersion());
      element.getChildren().add(childElement);
    }

    int biggestChange = NO_CHANGE;
    List<Element> allChildren = element.getAllChildren();
    for (Element child : allChildren)
    {
      int change = checkFeatureAPI(element, releasedElement, child, warnings);
      biggestChange = Math.max(biggestChange, change);

      IProject project = getProject(child);
      if (project != null)
      {
        buildDpependencies.add(project);
      }
    }

    for (Element releasedElementsChild : releasedElement.getAllChildren())
    {
      if (!allChildren.contains(releasedElementsChild))
      {
        addWarning(releasedElementsChild, REMOVAL, warnings);
        biggestChange = MAJOR_CHANGE; // REMOVAL
      }
    }

    return biggestChange;
  }

  private int checkFeatureAPI(Element element, Element releasedElement, Element childElement,
      List<Entry<Element, Version>> warnings)
  {
    if (childElement.isUnresolved())
    {
      return NO_CHANGE;
    }

    Element releasedElementsChild = releasedElement.getChild(childElement);
    if (releasedElementsChild == null)
    {
      addWarning(childElement, ADDITION, warnings);
      return MINOR_CHANGE; // ADDITION
    }

    Element childsReleasedElement = release.getElements().get(childElement);
    if (childsReleasedElement == null)
    {
      return NO_CHANGE;
    }

    Version releasedVersion = childsReleasedElement.getVersion();
    Version version = childElement.getVersion();
    if (version == null)
    {
      return NO_CHANGE;
    }

    if (version.getMajor() != releasedVersion.getMajor())
    {
      addWarning(childsReleasedElement, version, warnings);
      return MAJOR_CHANGE;
    }

    if (version.getMinor() != releasedVersion.getMinor())
    {
      addWarning(childsReleasedElement, version, warnings);
      return MINOR_CHANGE;
    }

    if (version.getMicro() != releasedVersion.getMicro())
    {
      addWarning(childsReleasedElement, version, warnings);
      return MICRO_CHANGE;
    }

    return NO_CHANGE;
  }

  private void addWarning(final Element releasedElement, final Version version,
      List<Map.Entry<Element, Version>> warnings)
  {
    warnings.add(new Map.Entry<Element, Version>()
    {
      public Element getKey()
      {
        return releasedElement;
      }

      public Version getValue()
      {
        return version;
      }

      public Version setValue(Version value)
      {
        throw new UnsupportedOperationException();
      }
    });
  }

  private IProject getProject(Element element)
  {
    String name = element.getName();
    if (element.getType() == Element.Type.PLUGIN)
    {
      return getPluginProject(name);
    }
  
    return getFeatureProject(name);
  }

  private IProject getPluginProject(String name)
  {
    IPluginModelBase pluginModel = PluginRegistry.findModel(name);
    if (pluginModel != null)
    {
      IResource resource = pluginModel.getUnderlyingResource();
      if (resource != null)
      {
        return resource.getProject();
      }
    }

    return null;
  }

  @SuppressWarnings("restriction")
  private IProject getFeatureProject(String name)
  {
    org.eclipse.pde.internal.core.ifeature.IFeatureModel[] featureModels = org.eclipse.pde.internal.core.PDECore
        .getDefault().getFeatureModelManager().getWorkspaceModels();

    for (org.eclipse.pde.internal.core.ifeature.IFeatureModel featureModel : featureModels)
    {
      IResource resource = featureModel.getUnderlyingResource();
      if (resource != null)
      {
        return resource.getProject();
      }
    }

    return null;
  }

  private void checkDependencyRanges(IPluginModelBase pluginModel) throws CoreException, IOException
  {
    BundleDescription description = pluginModel.getBundleDescription();
    if (description == null)
    {
      return;
    }

    for (BundleSpecification requiredBundle : description.getRequiredBundles())
    {
      VersionRange range = requiredBundle.getVersionRange();
      if (isUnspecified(getMaximum(range)))
      {
        addRequireMarker(requiredBundle.getName(), "dependency must specify a version range");
      }
      else
      {
        if (!range.getIncludeMinimum())
        {
          addRequireMarker(requiredBundle.getName(), "dependency range must include the minimum");
        }

        if (range.getIncludeMaximum())
        {
          addRequireMarker(requiredBundle.getName(), "dependency range must not include the maximum");
        }
      }
    }

    for (ImportPackageSpecification importPackage : description.getImportPackages())
    {
      VersionRange range = importPackage.getVersionRange();
      if (isUnspecified(getMaximum(range)))
      {
        addImportMarker(importPackage.getName(), "dependency must specify a version range");
      }
      else
      {
        if (!range.getIncludeMinimum())
        {
          addImportMarker(importPackage.getName(), "dependency range must include the minimum");
        }

        if (range.getIncludeMaximum())
        {
          addImportMarker(importPackage.getName(), "dependency range must not include the maximum");
        }
      }
    }
  }

  @SuppressWarnings("deprecation")
  private Version getMaximum(VersionRange range)
  {
    VersionUtil.someDeprecatedCode(); // Just make sure that this method refers to some deprecated code
    return range.getMaximum();
  }

  private boolean isUnspecified(Version version)
  {
    if (version.getMajor() != Integer.MAX_VALUE)
    {
      return false;
    }

    if (version.getMinor() != Integer.MAX_VALUE)
    {
      return false;
    }

    if (version.getMicro() != Integer.MAX_VALUE)
    {
      return false;
    }

    return true;
  }

  private void checkPackageExports(IPluginModelBase pluginModel) throws CoreException, IOException
  {
    BundleDescription description = pluginModel.getBundleDescription();
    String bundleName = description.getSymbolicName();
    Version bundleVersion = VersionUtil.normalize(description.getVersion());

    for (ExportPackageDescription packageExport : description.getExportPackages())
    {
      String packageName = packageExport.getName();
      if (isBundlePackage(packageName, bundleName))
      {
        Version packageVersion = packageExport.getVersion();
        if (packageVersion != null && !packageVersion.equals(Version.emptyVersion)
            && !packageVersion.equals(bundleVersion))
        {
          addExportMarker(packageName);
        }
      }
    }
  }

  private boolean isBundlePackage(String packageName, String bundleName)
  {
    if (packageName.startsWith(bundleName))
    {
      return true;
    }

    int lastDot = bundleName.lastIndexOf('.');
    if (lastDot != -1)
    {
      String bundleStart = bundleName.substring(0, lastDot);
      String bundleEnd = bundleName.substring(lastDot + 1);
      if (packageName.startsWith(bundleStart + ".internal." + bundleEnd))
      {
        return true;
      }

      if (packageName.startsWith(bundleStart + ".spi." + bundleEnd))
      {
        return true;
      }
    }

    return false;
  }

  private void addRequireMarker(String name, String message) throws CoreException, IOException
  {
    IFile file = getProject().getFile(MANIFEST_PATH);
    String regex = name.replaceAll("\\.", "\\\\.") + ";bundle-version=\"([^\\\"]*)\"";
    Markers.addMarker(file, "'" + name + "' " + message, IMarker.SEVERITY_ERROR, regex);
  }

  private void addImportMarker(String name, String message) throws CoreException, IOException
  {
    IFile file = getProject().getFile(MANIFEST_PATH);
    String regex = name.replaceAll("\\.", "\\\\.") + ";version=\"([^\\\"]*)\"";
    Markers.addMarker(file, "'" + name + "' " + message, IMarker.SEVERITY_ERROR, regex);
  }

  private void addExportMarker(String name) throws CoreException, IOException
  {
    IFile file = getProject().getFile(MANIFEST_PATH);
    String message = "'" + name + "' export has wrong version information";
    String regex = name.replaceAll("\\.", "\\\\.") + ";version=\"([0123456789\\.]*)\"";
    Markers.addMarker(file, message, IMarker.SEVERITY_ERROR, regex);
  }

  private void addVersionMarker(String message) throws CoreException, IOException
  {
    String regex;
    IFile file = getProject().getFile(FEATURE_PATH);
    if (file.exists())
    {
      regex = "feature.*?version\\s*=\\s*[\"'](\\d+(\\.\\d+(\\.\\d+)?)?)";
    }
    else
    {
      file = getProject().getFile(MANIFEST_PATH);
      regex = "Bundle-Version: *(\\d+(\\.\\d+(\\.\\d+)?)?)";
    }

    Markers.addMarker(file, message, IMarker.SEVERITY_ERROR, regex);
  }

  private void addIncludeMarker(Element releasedElement, Version version)
  {
    IFile file = getProject().getFile(FEATURE_PATH);
    String type;
    String tag;
    if (releasedElement.getType() == Element.Type.PLUGIN)
    {
      type = "Plug-in";
      tag = "plugin";
    }
    else
    {
      type = "Feature";
      tag = "includes";
    }

    try
    {
      String name = releasedElement.getName();

      if (version == REMOVAL)
      {
        String msg = type + " reference '" + name + "' has been removed";
        Markers.addMarker(file, msg, IMarker.SEVERITY_WARNING);
      }
      else
      {
        String regex = "<" + tag + "\\s+.*?id\\s*=\\s*[\"'](" + name.replace(".", "\\.") + ")";
        String msg;

        if (version == ADDITION)
        {
          msg = type + " reference '" + name + "' has been added with " + releasedElement.getVersion();
        }
        else
        {
          msg = type + " reference '" + name + "' has been changed from " + releasedElement.getVersion() + " to "
              + version;
        }

        Markers.addMarker(file, msg, IMarker.SEVERITY_WARNING, regex);
      }
    }
    catch (Exception ex)
    {
      Activator.log(ex);
    }
  }

  public static void trace(String msg)
  {
    if (DEBUG)
    {
      System.out.println(msg);
    }
  }

  public static IModel getComponentModel(IProject project)
  {
    IModel componentModel = PluginRegistry.findModel(project);
    if (componentModel == null)
    {
      componentModel = getFeatureModel(project);
      if (componentModel == null)
      {
        throw new IllegalStateException("The project " + project.getName() + " is neither a plugin nor a feature");
      }
    }

    return componentModel;
  }

  @SuppressWarnings("restriction")
  private static org.eclipse.pde.internal.core.ifeature.IFeatureModel getFeatureModel(IProject project)
  {
    org.eclipse.pde.internal.core.ifeature.IFeatureModel[] featureModels = org.eclipse.pde.internal.core.PDECore
        .getDefault().getFeatureModelManager().getWorkspaceModels();

    for (org.eclipse.pde.internal.core.ifeature.IFeatureModel featureModel : featureModels)
    {
      if (featureModel.getUnderlyingResource().getProject() == project)
      {
        return featureModel;
      }
    }

    return null;
  }

  @SuppressWarnings("restriction")
  public static Version getComponentVersion(IModel componentModel)
  {
    if (componentModel instanceof IPluginModelBase)
    {
      IPluginModelBase pluginModel = (IPluginModelBase)componentModel;
      return VersionUtil.normalize(pluginModel.getBundleDescription().getVersion());
    }

    Version version = new Version(((org.eclipse.pde.internal.core.ifeature.IFeatureModel)componentModel).getFeature()
        .getVersion());
    return VersionUtil.normalize(version);
  }
}

Back to the top