Eclipse


Eclipse : extraire une feature et ses dépendances

Lors de la création d'applications RCP, il peut être utile de récupérer quelques features indépendantes afin des les intégrer à la distribution (pour ajouter des nouveaux plugins). Quand ces features ne sont pas livrées de manière autonome, il devient vite assez fastidieux de suivre à la main toutes les dépendances pour récupérer les plugins utiles.

Pour faciliter ce travail à tous ceux qui en auront besoin, voici une petite moulinette prête à l'emploi pour copier toutes les dépendances d'une feature :

public class EclipseFeatureExtractor {
 
 private static Set<String> requiredFeatures = new HashSet<String>();
 private static Set<String> requiredPlugins = new HashSet<String>();
 private static DocumentBuilder builder;
 private static XPath xpath;
 private static File featuresFolder;
 private static File pluginsFolder;

 /**
  * @param args
  * @throws Exception 
  */
 public static void main(String[] args) throws Exception {
  if(args.length != 2 || (args.length>=1 && args[0].equals("-help"))) {
   System.out.println("*********** USAGE ***********");
   System.out.println("-help affiche aide.\n");
   System.out.println("Paramètres nécessaires :");
   System.out.println("1- Dossier de la feature à examiner");
   System.out.println("2- Dossier de destination pour la copie des features et plugins");
   return;
  }
  
  String mainFeature = args[0];
  String destination = args[1];
  
  File destFolder = new File(destination);
  File mainFeatureFolder = new File(mainFeature);
  featuresFolder = mainFeatureFolder.getParentFile();
  pluginsFolder = new File(featuresFolder.getParentFile(), "plugins");
  
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  factory.setNamespaceAware(true);
  builder = factory.newDocumentBuilder();
  
  XPathFactory xpathfactory = XPathFactory.newInstance();
  xpath = xpathfactory.newXPath();
  
  parseFeature(mainFeatureFolder);
  
  File destFeaturesFolder = new File(destFolder,"features");
  if(!destFeaturesFolder.exists()) {
   destFeaturesFolder.mkdirs();
  }
  copyFeatures(destFeaturesFolder);
  
  File destPluginsFolder = new File(destFolder,"plugins");
  if(!destPluginsFolder.exists()) {
   destPluginsFolder.mkdirs();
  }
  copyPlugins(destPluginsFolder);
 }

 private static void copyPlugins(File destPluginsFolder) throws Exception {
  for(String required : requiredPlugins) {
   for(File other : pluginsFolder.listFiles()) {
    if(other.getName().startsWith(required)) {
     System.out.println("Copying plugin : "+other.getName());
     if(other.isDirectory()) {
      File specificDest = new File(destPluginsFolder, other.getName());
      specificDest.mkdirs();
      FileUtils.copyDirectory(other, specificDest);
     } else {
      File copiedPlugin = new File(destPluginsFolder, other.getName());
      FileUtils.copyFile(other, copiedPlugin);
     }
    }
   }
  }
 }

 private static void copyFeatures(File destFeaturesFolder) throws Exception {
  for(String required : requiredFeatures) {
   for(File other : featuresFolder.listFiles()) {
    if(other.isDirectory() && other.getName().startsWith(required)) {
     File specificDest = new File(destFeaturesFolder, other.getName());
     specificDest.mkdirs();
     System.out.println("Copying feature : "+other.getName());
     FileUtils.copyDirectory(other, specificDest);
    }
   }
  }
 }

 public static void parseFeature(File featureFolder)
   throws SAXException, IOException, XPathExpressionException {
  File featureDescriptor = new File(featureFolder, "feature.xml");
  System.out.println("Parsing : " + featureDescriptor.getAbsolutePath());
  Document doc = builder.parse(featureDescriptor);
  // Required features
  XPathExpression xRequiredFeatures = xpath.compile("//requires/import/@feature|//includes/@id");
  NodeList nodes = (NodeList)xRequiredFeatures.evaluate(doc, XPathConstants.NODESET);
  String feature = "";
  for (int i = 0; i < nodes.getLength(); i++) {
   feature = nodes.item(i).getNodeValue()+"_";
   requiredFeatures.add(feature);
   for(File other : featuresFolder.listFiles()) {
    if(other.isDirectory() && other.getName().startsWith(feature)) {
     parseFeature(other);
    }
   }
  }
  // Required plugins
  XPathExpression xRequiredPlugins = xpath.compile("//plugin/@id");
  nodes = (NodeList)xRequiredPlugins.evaluate(doc, XPathConstants.NODESET);
  String plugin = "";
  for (int i = 0; i < nodes.getLength(); i++) {
   plugin = nodes.item(i).getNodeValue()+"_";
   requiredPlugins.add(plugin);
  }
 }

}

Ce code peut être amélioré pour gérer de manière plus fine les versions des features/plugins, ici ignorées.


Fichier(s) joint(s) :

0 commentaires: