private TargetPlatform handleDependencies()

in flex/src/com/intellij/javascript/flex/maven/Flexmojos3Configurator.java [229:346]


  private TargetPlatform handleDependencies(final ModifiableFlexBuildConfiguration bc) {
    bc.getDependencies().getModifiableEntries().clear();

    boolean playerglobal = false;
    boolean airglobal = false;
    boolean mobilecomponents = false;

    final ModifiableRootModel rootModel = myModelsProvider.getModifiableRootModel(myModule);
    for (OrderEntry entry : rootModel.getOrderEntries()) {
      final DependencyScope scope = entry instanceof ExportableOrderEntry ? ((ExportableOrderEntry)entry).getScope()
                                                                          : DependencyScope.COMPILE;
      final boolean isExported = entry instanceof ExportableOrderEntry && ((ExportableOrderEntry)entry).isExported();

      if (entry instanceof ModuleOrderEntry) {
        rootModel.removeOrderEntry(entry);

        final String dependencyModuleName = ((ModuleOrderEntry)entry).getModuleName();

        final MavenProject dependencyMavenProject = findMavenProjectByModuleName(dependencyModuleName);

        if (dependencyMavenProject == null) {
          MavenLog.LOG.warn("Maven project not found, module dependency skipped: " + myModule.getName() + " on " + dependencyModuleName);
          continue;
        }
        if (!ArrayUtil.contains(dependencyMavenProject.getPackaging(), FlexmojosImporter.SUPPORTED_PACKAGINGS)) {
          MavenLog.LOG.info("Unexpected packaging (" + dependencyMavenProject.getPackaging() + "), module dependency skipped: " +
                            myModule.getName() + " on " + dependencyModuleName);
          continue;
        }

        final ModifiableDependencyEntry existingEntry = ContainerUtil
          .find(bc.getDependencies().getModifiableEntries(),
                entry1 -> (entry1 instanceof BuildConfigurationEntry) &&
                          ((BuildConfigurationEntry)entry1).getModuleName().equals(dependencyModuleName) &&
                          ((BuildConfigurationEntry)entry1).getBcName().equals(dependencyModuleName));

        final LinkageType linkageType = "swc".equals(dependencyMavenProject.getPackaging())
                                        ? FlexUtils.convertLinkageType(scope, isExported)
                                        : LinkageType.LoadInRuntime;

        if (existingEntry != null) {
          if (existingEntry.getDependencyType().getLinkageType() == LinkageType.Test) {
            existingEntry.getDependencyType().setLinkageType(linkageType);
          }
          continue;
        }

        final ModifiableBuildConfigurationEntry bcEntry =
          myFlexEditor.createBcEntry(bc.getDependencies(), dependencyModuleName, dependencyModuleName);
        bcEntry.getDependencyType().setLinkageType(linkageType);
        bc.getDependencies().getModifiableEntries().add(0, bcEntry);

        continue;
      }

      if (entry instanceof JdkOrderEntry) {
        rootModel.removeOrderEntry(entry);
      }

      if (!(entry instanceof LibraryOrderEntry)) continue;
      rootModel.removeOrderEntry(entry);

      if (!LibraryTablesRegistrar.PROJECT_LEVEL.equals(((LibraryOrderEntry)entry).getLibraryLevel())) continue;
      final Library library = ((LibraryOrderEntry)entry).getLibrary();
      if (library == null || !MavenRootModelAdapter.isMavenLibrary(library)) continue;

      final String libraryName = library.getName();
      if (libraryName.contains(":rb.swc:") || libraryName.contains(":resource-bundle:")) {
        // fix rb.swc placeholders to real SWCs according to used locales
        final Library.ModifiableModel libraryModifiableModel = myModelsProvider.getModifiableLibraryModel(library);
        for (final String rbSwcPlaceholdersUrl : findRbSwcPlaceholderUrls(libraryModifiableModel)) {
          final Collection<String> rootsToAdd = getRbSwcUrlsForCompiledLocales(rbSwcPlaceholdersUrl);
          libraryModifiableModel.removeRoot(rbSwcPlaceholdersUrl, OrderRootType.CLASSES);
          for (final String rootToAdd : rootsToAdd) {
            if (!ArrayUtil.contains(rootToAdd, libraryModifiableModel.getUrls(OrderRootType.CLASSES))) {
              libraryModifiableModel.addRoot(rootToAdd, OrderRootType.CLASSES);
            }
          }
          // sources and docs could be updated as well, but currently they are always senseless, because they do not exist
        }
      }

      if (libraryName.contains(":swc:") ||
          libraryName.contains(":rb.swc:") ||
          libraryName.contains(":resource-bundle:") ||
          libraryName.contains(":ane:")) {
        playerglobal |= libraryName.contains("playerglobal");
        airglobal |= libraryName.contains("airglobal");
        mobilecomponents |= libraryName.contains("mobilecomponents");
        final boolean ane = libraryName.contains(":ane:") && !libraryName.contains(":swc:");

        final LibraryKind kind = ((LibraryEx)library).getKind();

        if (kind != FlexLibraryType.FLEX_LIBRARY) {
          if (kind == null) {
            final LibraryEx.ModifiableModelEx libraryModel = (LibraryEx.ModifiableModelEx)myModelsProvider.getModifiableLibraryModel(library);
            libraryModel.setKind(FlexLibraryType.FLEX_LIBRARY);
          }
        }

        final ModifiableDependencyEntry sharedLibraryEntry =
          myFlexEditor.createSharedLibraryEntry(bc.getDependencies(), ((LibraryOrderEntry)entry).getLibraryName(),
                                                ((LibraryOrderEntry)entry).getLibraryLevel());
        final LinkageType linkageType = ane ? DependencyType.DEFAULT_LINKAGE
                                            : FlexUtils.convertLinkageType(scope, isExported);
        sharedLibraryEntry.getDependencyType().setLinkageType(linkageType);
        bc.getDependencies().getModifiableEntries().add(sharedLibraryEntry);
      }
      else {
        MavenLog.LOG.info("Non-swc dependency for flexmojos project '" + myModule.getName() + "': " + libraryName);
      }
    }

    // todo better target platform detection if both airglobal and playerglobal present?
    return mobilecomponents && airglobal ? TargetPlatform.Mobile
                                         : airglobal && !playerglobal ? TargetPlatform.Desktop
                                                                      : TargetPlatform.Web;
  }