示例#1
0
  /**
   * Convert path from sources module descriptor for using on distribution /classes && /classes_gen
   * converts to bundle home path
   *
   * @param originalPath Original path from sources module descriptor
   * @return Converted path, null if path meaningless on packaged module
   */
  @Nullable
  private String convertPath(
      String originalPath,
      IFile bundleHome,
      IFile sourcesDescriptorFile,
      ModuleDescriptor descriptor) {
    MacroHelper macroHelper = MacrosFactory.forModuleFile(sourcesDescriptorFile);

    String canonicalPath = FileUtil.getCanonicalPath(originalPath).toLowerCase();

    // /classes && /classes_gen hack
    String suffix = descriptor.getCompileInMPS() ? CLASSES_GEN : CLASSES;
    if (canonicalPath.endsWith(suffix)) {
      // MacrosFactory based on original descriptor file because we use original descriptor file for
      // ModuleDescriptor reading, so all paths expanded to original descriptor file
      String classes = macroHelper.expandPath("${module}/" + suffix);
      if (FileUtil.getCanonicalPath(classes).equalsIgnoreCase(canonicalPath)) {
        return bundleHome.getPath();
      }
    } else if (FileUtil.getCanonicalPath(bundleHome.getPath()).equalsIgnoreCase(canonicalPath)) {
      return bundleHome.getPath();
    }

    // ${mps_home}/lib
    String mpsHomeLibPath =
        FileUtil.getCanonicalPath(PathManager.getHomePath() + File.separator + "lib").toLowerCase();
    if (canonicalPath.startsWith(mpsHomeLibPath)) {
      return canonicalPath;
    }

    // we used to keep originalPath if it has a macro not known to MPS here.
    // However, the check has been deprecated in 2012 and thus removed. I'm not 100% sure what
    // 'meaningless' in the contract of the method means. Of course, unknown macros make no sense
    // for us
    // and thus null is legitimate answer, OTOH, custom macros might have a lot of meaning to
    // someone else.
    //
    // ignore paths starts from ${module}/${project} etc
    return null;
  }
  public static void saveLanguageDescriptor(
      IFile file, LanguageDescriptor descriptor, MacroHelper macroHelper) {
    if (file.isReadOnly()) {
      if (LOG.isEnabledFor(Priority.ERROR)) {
        LOG.error("Cant't save " + file.getPath());
      }
      return;
    }

    Element languageElement = new Element("language");
    languageElement.setAttribute("namespace", descriptor.getNamespace());
    String uuid = descriptor.getUUID();
    if (uuid != null) {
      languageElement.setAttribute("uuid", uuid);
    }
    if (descriptor.getGenPath() != null) {
      languageElement.setAttribute(
          "generatorOutputPath", macroHelper.shrinkPath(descriptor.getGenPath()));
    }

    Element models = new Element("models");
    ModuleDescriptorPersistence.saveModelRoots(
        models, descriptor.getModelRootDescriptors(), macroHelper);
    languageElement.addContent(models);

    if (!(descriptor.getModuleFacetDescriptors().isEmpty())) {
      Element facets = new Element("facets");
      ModuleDescriptorPersistence.saveFacets(
          facets, descriptor.getModuleFacetDescriptors(), macroHelper);
      languageElement.addContent(facets);
    }

    Element accessoryModels = new Element("accessoryModels");
    for (SModelReference model : SetSequence.fromSet(descriptor.getAccessoryModels())) {
      XmlUtil.tagWithAttribute(accessoryModels, "model", "modelUID", model.toString());
    }
    languageElement.addContent(accessoryModels);

    Element generators = new Element("generators");
    for (GeneratorDescriptor generatorDescriptor :
        ListSequence.fromList(descriptor.getGenerators())) {
      GeneratorDescriptorPersistence.saveGeneratorDescriptor(
          generators, generatorDescriptor, macroHelper);
    }
    languageElement.addContent(generators);

    if (!(descriptor.getAdditionalJavaStubPaths().isEmpty())) {
      Element stubModelEntries = new Element("stubModelEntries");
      ModuleDescriptorPersistence.saveStubModelEntries(
          stubModelEntries, descriptor.getAdditionalJavaStubPaths(), macroHelper);
      languageElement.addContent(stubModelEntries);
    }

    Element sourcePath = new Element("sourcePath");
    for (String p : descriptor.getSourcePaths()) {
      XmlUtil.tagWithAttribute(sourcePath, "source", "path", macroHelper.shrinkPath(p));
    }
    languageElement.addContent(sourcePath);

    ModuleDescriptorPersistence.saveDependencies(languageElement, descriptor);

    Element extendedLanguages = new Element("extendedLanguages");
    for (SModuleReference ref : SetSequence.fromSet(descriptor.getExtendedLanguages())) {
      XmlUtil.tagWithText(extendedLanguages, "extendedLanguage", ref.toString());
    }
    languageElement.addContent(extendedLanguages);

    try {
      OutputStream os = file.openOutputStream();
      JDOMUtil.writeDocument(new Document(languageElement), os);
    } catch (Exception e) {
      if (LOG.isEnabledFor(Priority.ERROR)) {
        LOG.error("", e);
      }
    }
    ModuleDescriptorPersistence.setTimestamp(descriptor, file);
  }