Search in sources :

Example 71 with HashMap

use of com.intellij.util.containers.HashMap in project android by JetBrains.

the class NonGradleApkProvider method getApks.

@Override
@NotNull
public Collection<ApkInfo> getApks(@NotNull IDevice device) throws ApkProvisionException {
    String packageName = myApplicationIdProvider.getPackageName();
    // Gather up all the dependency APKs to install, and check that none conflict.
    HashMap<AndroidFacet, String> depFacet2PackageName = new HashMap<AndroidFacet, String>();
    fillRuntimeAndTestDependencies(myFacet.getModule(), depFacet2PackageName);
    checkPackageNames(depFacet2PackageName, myFacet, packageName);
    List<ApkInfo> apkList = new ArrayList<ApkInfo>();
    addApk(apkList, packageName, myFacet);
    for (AndroidFacet depFacet : depFacet2PackageName.keySet()) {
        addApk(apkList, depFacet2PackageName.get(depFacet), depFacet);
    }
    return apkList;
}
Also used : HashMap(com.intellij.util.containers.HashMap) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Example 72 with HashMap

use of com.intellij.util.containers.HashMap in project android by JetBrains.

the class AndroidApkBuilder method finalPackage.

private static Map<AndroidCompilerMessageKind, List<String>> finalPackage(@NotNull String dexPath, @NotNull String[] javaResourceRoots, @NotNull String[] externalJars, @NotNull String[] nativeLibsFolders, @NotNull String outputApk, @NotNull String apkPath, @Nullable String customKeystorePath, boolean signed, @NotNull Condition<File> resourceFilter) {
    final Map<AndroidCompilerMessageKind, List<String>> result = new HashMap<AndroidCompilerMessageKind, List<String>>();
    result.put(ERROR, new ArrayList<String>());
    result.put(INFORMATION, new ArrayList<String>());
    result.put(WARNING, new ArrayList<String>());
    FileOutputStream fos = null;
    SignedJarBuilder builder = null;
    try {
        String keyStoreOsPath = customKeystorePath != null && customKeystorePath.length() > 0 ? customKeystorePath : DebugKeyProvider.getDefaultKeyStoreOsPath();
        DebugKeyProvider provider = createDebugKeyProvider(result, keyStoreOsPath);
        X509Certificate certificate = signed ? (X509Certificate) provider.getCertificate() : null;
        if (certificate != null && certificate.getNotAfter().compareTo(new Date()) < 0) {
            // generate a new one
            File keyStoreFile = new File(keyStoreOsPath);
            if (keyStoreFile.exists()) {
                keyStoreFile.delete();
            }
            provider = createDebugKeyProvider(result, keyStoreOsPath);
            certificate = (X509Certificate) provider.getCertificate();
        }
        if (certificate != null && certificate.getNotAfter().compareTo(new Date()) < 0) {
            String date = DateFormatUtil.formatPrettyDateTime(certificate.getNotAfter());
            result.get(ERROR).add(("Debug certificate expired on " + date + ". Cannot regenerate it, please delete file \"" + keyStoreOsPath + "\" manually."));
            return result;
        }
        PrivateKey key = provider.getDebugKey();
        if (key == null) {
            result.get(ERROR).add("Cannot create new key or keystore");
            return result;
        }
        if (!new File(apkPath).exists()) {
            result.get(ERROR).add("File " + apkPath + " not found. Try to rebuild project");
            return result;
        }
        File dexEntryFile = new File(dexPath);
        if (!dexEntryFile.exists()) {
            result.get(ERROR).add("File " + dexEntryFile.getPath() + " not found. Try to rebuild project");
            return result;
        }
        for (String externalJar : externalJars) {
            if (new File(externalJar).isDirectory()) {
                result.get(ERROR).add(externalJar + " is directory. Directory libraries are not supported");
            }
        }
        if (result.get(ERROR).size() > 0) {
            return result;
        }
        fos = new FileOutputStream(outputApk);
        builder = new SafeSignedJarBuilder(fos, key, certificate, outputApk);
        FileInputStream fis = new FileInputStream(apkPath);
        try {
            builder.writeZip(fis, null);
        } finally {
            fis.close();
        }
        builder.writeFile(dexEntryFile, AndroidCommonUtils.CLASSES_FILE_NAME);
        final HashSet<String> added = new HashSet<String>();
        for (String resourceRootPath : javaResourceRoots) {
            final HashSet<File> javaResources = new HashSet<File>();
            final File resourceRoot = new File(resourceRootPath);
            collectStandardJavaResources(resourceRoot, javaResources, resourceFilter);
            writeStandardJavaResources(javaResources, resourceRoot, builder, added);
        }
        Set<String> duplicates = new HashSet<String>();
        Set<String> entries = new HashSet<String>();
        for (String externalJar : externalJars) {
            collectDuplicateEntries(externalJar, entries, duplicates);
        }
        for (String duplicate : duplicates) {
            result.get(WARNING).add("Duplicate entry " + duplicate + ". The file won't be added");
        }
        MyResourceFilter filter = new MyResourceFilter(duplicates);
        for (String externalJar : externalJars) {
            fis = new FileInputStream(externalJar);
            try {
                builder.writeZip(fis, filter);
            } finally {
                fis.close();
            }
        }
        final HashSet<String> nativeLibs = new HashSet<String>();
        for (String nativeLibsFolderPath : nativeLibsFolders) {
            final File nativeLibsFolder = new File(nativeLibsFolderPath);
            final File[] children = nativeLibsFolder.listFiles();
            if (children != null) {
                for (File child : children) {
                    writeNativeLibraries(builder, nativeLibsFolder, child, signed, nativeLibs);
                }
            }
        }
    } catch (IOException e) {
        return addExceptionMessage(e, result);
    } catch (CertificateException e) {
        return addExceptionMessage(e, result);
    } catch (DebugKeyProvider.KeytoolException e) {
        return addExceptionMessage(e, result);
    } catch (AndroidLocation.AndroidLocationException e) {
        return addExceptionMessage(e, result);
    } catch (NoSuchAlgorithmException e) {
        return addExceptionMessage(e, result);
    } catch (UnrecoverableEntryException e) {
        return addExceptionMessage(e, result);
    } catch (KeyStoreException e) {
        return addExceptionMessage(e, result);
    } catch (GeneralSecurityException e) {
        return addExceptionMessage(e, result);
    } finally {
        if (builder != null) {
            try {
                builder.close();
            } catch (IOException e) {
                addExceptionMessage(e, result);
            } catch (GeneralSecurityException e) {
                addExceptionMessage(e, result);
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ignored) {
            }
        }
    }
    return result;
}
Also used : HashMap(com.intellij.util.containers.HashMap) CertificateException(java.security.cert.CertificateException) AndroidCompilerMessageKind(org.jetbrains.android.util.AndroidCompilerMessageKind) DebugKeyProvider(com.android.jarutils.DebugKeyProvider) HashSet(com.intellij.util.containers.HashSet) AndroidLocation(com.android.prefs.AndroidLocation) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) FileInputStream(java.io.FileInputStream) SignedJarBuilder(com.android.jarutils.SignedJarBuilder) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 73 with HashMap

use of com.intellij.util.containers.HashMap in project intellij-plugins by JetBrains.

the class ChooseBuildConfigurationDialog method createForApplicableBCs.

/**
   * @param project
   * @param filter
   * @return <code>null</code> if there's no applicable BC configurables according to the filter provided
   */
@Nullable
public static ChooseBuildConfigurationDialog createForApplicableBCs(String title, @Nullable String labelText, Project project, boolean allowEmptySelection, Condition<FlexBCConfigurable> filter) {
    Map<Module, List<FlexBCConfigurable>> treeItems = new HashMap<>();
    FlexBCConfigurator configurator = FlexBuildConfigurationsExtension.getInstance().getConfigurator();
    for (Module module : ModuleStructureConfigurable.getInstance(project).getModules()) {
        if (ModuleType.get(module) != FlexModuleType.getInstance()) {
            continue;
        }
        for (CompositeConfigurable configurable : configurator.getBCConfigurables(module)) {
            FlexBCConfigurable flexBCConfigurable = FlexBCConfigurable.unwrap(configurable);
            if (!filter.value(flexBCConfigurable)) {
                continue;
            }
            List<FlexBCConfigurable> list = treeItems.get(module);
            if (list == null) {
                list = new ArrayList<>();
                treeItems.put(module, list);
            }
            list.add(flexBCConfigurable);
        }
    }
    if (treeItems.isEmpty()) {
        return null;
    }
    return new ChooseBuildConfigurationDialog(title, labelText, project, allowEmptySelection, treeItems);
}
Also used : FlexBCConfigurator(com.intellij.lang.javascript.flex.projectStructure.FlexBCConfigurator) HashMap(com.intellij.util.containers.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 74 with HashMap

use of com.intellij.util.containers.HashMap in project intellij-plugins by JetBrains.

the class FlexProjectConfigurationEditor method commit.

public void commit() throws ConfigurationException {
    final Map<Pair<String, String>, String> renamedConfigs = new HashMap<>();
    for (Module module : myModule2Editors.keySet()) {
        ModifiableRootModel modifiableModel = myProvider.getModuleModifiableModel(module);
        Collection<String> usedModulesLibrariesIds = new ArrayList<>();
        // ---------------- SDK and shared libraries entries ----------------------
        // Library -> add_library_entry_flag
        Map<Library, Boolean> librariesToAdd = new LinkedHashMap<>();
        final Collection<String> sdkNames = new HashSet<>();
        for (Editor editor : myModule2Editors.get(module)) {
            final SdkEntry sdkEntry = editor.getDependencies().getSdkEntry();
            if (sdkEntry != null) {
                sdkNames.add(sdkEntry.getName());
            }
            for (DependencyEntry dependencyEntry : editor.getDependencies().getEntries()) {
                if (dependencyEntry instanceof ModuleLibraryEntry) {
                    ModuleLibraryEntry moduleLibraryEntry = (ModuleLibraryEntry) dependencyEntry;
                    usedModulesLibrariesIds.add(moduleLibraryEntry.getLibraryId());
                }
                if (dependencyEntry instanceof SharedLibraryEntry) {
                    SharedLibraryEntry sharedLibraryEntry = (SharedLibraryEntry) dependencyEntry;
                    Library library = myProvider.findSourceLibraryForLiveName(sharedLibraryEntry.getLibraryName(), sharedLibraryEntry.getLibraryLevel());
                    if (library != null) {
                        librariesToAdd.put(library, true);
                    }
                }
            }
            String originalName = editor.getOriginalName();
            if (originalName != null && !originalName.equals(editor.getName())) {
                renamedConfigs.put(Pair.create(module.getName(), originalName), editor.getName());
            }
        }
        final Sdk sdk;
        if (sdkNames.isEmpty()) {
            sdk = null;
        } else if (sdkNames.size() == 1) {
            sdk = FlexSdkUtils.findFlexOrFlexmojosSdk(sdkNames.iterator().next());
        } else {
            sdk = new FlexCompositeSdk(ArrayUtil.toStringArray(sdkNames));
        }
        modifiableModel.setSdk(sdk);
        Collection<OrderEntry> entriesToRemove = new ArrayList<>();
        for (OrderEntry orderEntry : modifiableModel.getOrderEntries()) {
            if (orderEntry instanceof LibraryOrderEntry) {
                if (((LibraryOrderEntry) orderEntry).isModuleLevel()) {
                    LibraryEx library = (LibraryEx) ((LibraryOrderEntry) orderEntry).getLibrary();
                    if (FlexProjectRootsUtil.isFlexLibrary(library) && !usedModulesLibrariesIds.contains(FlexProjectRootsUtil.getLibraryId(library))) {
                        entriesToRemove.add(orderEntry);
                    }
                } else {
                    LibraryEx library = (LibraryEx) ((LibraryOrderEntry) orderEntry).getLibrary();
                    if (librariesToAdd.containsKey(library)) {
                        // entry already exists for this library
                        librariesToAdd.put(library, false);
                    } else if (library != null && FlexProjectRootsUtil.isFlexLibrary(library)) {
                        entriesToRemove.add(orderEntry);
                    }
                }
            }
        }
        for (OrderEntry e : entriesToRemove) {
            modifiableModel.removeOrderEntry(e);
        }
        for (Library library : librariesToAdd.keySet()) {
            if (!((LibraryEx) library).isDisposed() && librariesToAdd.get(library) && myProvider.findSourceLibrary(library.getName(), library.getTable().getTableLevel()) != null) {
                modifiableModel.addLibraryEntry(library);
            }
        }
        // ---------------- modules entries ----------------------
        final Set<Module> modulesToAdd = new THashSet<>();
        for (Editor editor : myModule2Editors.get(module)) {
            for (DependencyEntry dependencyEntry : editor.getDependencies().getEntries()) {
                if (dependencyEntry instanceof BuildConfigurationEntry) {
                    final Module dependencyModule = findModuleWithBC((BuildConfigurationEntry) dependencyEntry);
                    if (dependencyModule != null && dependencyModule != module) {
                        modulesToAdd.add(dependencyModule);
                    }
                }
            }
        }
        List<OrderEntry> moduleOrderEntriesToRemove = ContainerUtil.filter(modifiableModel.getOrderEntries(), orderEntry -> orderEntry instanceof ModuleOrderEntry && !modulesToAdd.remove(((ModuleOrderEntry) orderEntry).getModule()));
        for (OrderEntry orderEntry : moduleOrderEntriesToRemove) {
            modifiableModel.removeOrderEntry(orderEntry);
        }
        for (Module moduleToAdd : modulesToAdd) {
            modifiableModel.addModuleOrderEntry(moduleToAdd);
        }
        for (OrderEntry entry : modifiableModel.getOrderEntries()) {
            if (entry instanceof ExportableOrderEntry) {
                // transitiveness will be filtered out in FlexOrderEnumeratorHandler if needed
                ((ExportableOrderEntry) entry).setExported(true);
            }
        }
    }
    // ---------------- do commit ----------------------
    Collection<Module> modulesWithChangedModifiableModel = ContainerUtil.findAll(myModule2Editors.keySet(), module -> myProvider.getModuleModifiableModel(module).isChanged());
    if (!modulesWithChangedModifiableModel.isEmpty()) {
        myProvider.commitModifiableModels();
        myModulesModelChangeEventDispatcher.getMulticaster().modulesModelsChanged(modulesWithChangedModifiableModel);
    }
    ApplicationManager.getApplication().runWriteAction(() -> {
        for (Module module : myModule2Editors.keySet()) {
            Function<Editor, FlexBuildConfigurationImpl> f = editor -> editor.commit();
            FlexBuildConfigurationImpl[] current = ContainerUtil.map2Array(myModule2Editors.get(module), FlexBuildConfigurationImpl.class, f);
            ((FlexBuildConfigurationManagerImpl) FlexBuildConfigurationManager.getInstance(module)).setBuildConfigurations(current);
        }
        if (myProject != null) {
            FlexBuildConfigurationManagerImpl.resetHighlighting(myProject);
            if (!renamedConfigs.isEmpty()) {
                myProject.getMessageBus().syncPublisher(FlexBuildConfigurationChangeListener.TOPIC).buildConfigurationsRenamed(renamedConfigs);
            }
        }
    });
}
Also used : FlexCompositeSdk(com.intellij.lang.javascript.flex.projectStructure.FlexCompositeSdk) java.util(java.util) OutputType(com.intellij.flex.model.bc.OutputType) ArrayUtil(com.intellij.util.ArrayUtil) EventDispatcher(com.intellij.util.EventDispatcher) FlexModuleType(com.intellij.lang.javascript.flex.FlexModuleType) HashMap(com.intellij.util.containers.HashMap) THashSet(gnu.trove.THashSet) ContainerUtil(com.intellij.util.containers.ContainerUtil) TargetPlatform(com.intellij.flex.model.bc.TargetPlatform) FlexCommonUtils(com.intellij.flex.FlexCommonUtils) com.intellij.openapi.roots(com.intellij.openapi.roots) Library(com.intellij.openapi.roots.libraries.Library) BCUtils(com.intellij.lang.javascript.flex.projectStructure.options.BCUtils) FlexBCConfigurable(com.intellij.lang.javascript.flex.projectStructure.ui.FlexBCConfigurable) Project(com.intellij.openapi.project.Project) ModuleType(com.intellij.openapi.module.ModuleType) ChangeListener(javax.swing.event.ChangeListener) FlexSdkUtils(com.intellij.lang.javascript.flex.sdk.FlexSdkUtils) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) FlexLibraryType(com.intellij.lang.javascript.flex.library.FlexLibraryType) FlexBuildConfigurationsExtension(com.intellij.lang.javascript.flex.projectStructure.FlexBuildConfigurationsExtension) LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx) FlexBCConfigurator(com.intellij.lang.javascript.flex.projectStructure.FlexBCConfigurator) LibraryTablesRegistrar(com.intellij.openapi.roots.libraries.LibraryTablesRegistrar) com.intellij.lang.javascript.flex.projectStructure.model(com.intellij.lang.javascript.flex.projectStructure.model) Disposable(com.intellij.openapi.Disposable) Sdk(com.intellij.openapi.projectRoots.Sdk) BuildConfigurationNature(com.intellij.flex.model.bc.BuildConfigurationNature) FlexProjectRootsUtil(com.intellij.lang.javascript.flex.projectStructure.options.FlexProjectRootsUtil) Nullable(org.jetbrains.annotations.Nullable) LibraryEditingUtil(com.intellij.openapi.roots.ui.configuration.libraries.LibraryEditingUtil) Function(com.intellij.util.Function) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) Pair(com.intellij.openapi.util.Pair) ApplicationManager(com.intellij.openapi.application.ApplicationManager) ConfigurationException(com.intellij.openapi.options.ConfigurationException) NotNull(org.jetbrains.annotations.NotNull) FlexLibraryProperties(com.intellij.lang.javascript.flex.library.FlexLibraryProperties) Consumer(com.intellij.util.Consumer) HashMap(com.intellij.util.containers.HashMap) LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx) FlexCompositeSdk(com.intellij.lang.javascript.flex.projectStructure.FlexCompositeSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) Pair(com.intellij.openapi.util.Pair) THashSet(gnu.trove.THashSet) FlexCompositeSdk(com.intellij.lang.javascript.flex.projectStructure.FlexCompositeSdk) THashSet(gnu.trove.THashSet) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module)

Example 75 with HashMap

use of com.intellij.util.containers.HashMap in project android by JetBrains.

the class AndroidResourceCachingBuilder method runPngCaching.

private static boolean runPngCaching(AndroidResourceCachingBuildTarget target, CompileContext context, BuildOutputConsumer outputConsumer) throws IOException {
    final JpsModule module = target.getModule();
    final JpsAndroidModuleExtension extension = AndroidJpsUtil.getExtension(module);
    assert extension != null;
    context.processMessage(new CompilerMessage(BUILDER_NAME, BuildMessage.Kind.INFO, AndroidJpsBundle.message("android.jps.progress.res.caching", module.getName())));
    final AndroidPlatform platform = AndroidJpsUtil.getAndroidPlatform(module, context, BUILDER_NAME);
    if (platform == null) {
        return false;
    }
    final File resCacheDir = target.getOutputDir(context);
    // todo: probably it may be done automatically
    if (context.getScope().isBuildForced(target) && resCacheDir.exists()) {
        if (!FileUtil.delete(resCacheDir)) {
            context.processMessage(new CompilerMessage(BUILDER_NAME, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.cannot.create.directory", resCacheDir.getPath())));
            return false;
        }
    }
    if (!resCacheDir.exists()) {
        if (!resCacheDir.mkdirs()) {
            context.processMessage(new CompilerMessage(BUILDER_NAME, BuildMessage.Kind.ERROR, AndroidJpsBundle.message("android.jps.cannot.create.directory", resCacheDir.getPath())));
            return false;
        }
    }
    final IAndroidTarget androidTarget = platform.getTarget();
    final List<BuildRootDescriptor> roots = context.getProjectDescriptor().getBuildRootIndex().getTargetRoots(target, context);
    if (roots.size() == 0) {
        return true;
    }
    final List<String> inputDirs = new ArrayList<String>();
    for (BuildRootDescriptor root : roots) {
        final File f = root.getRootFile();
        if (f.exists()) {
            inputDirs.add(f.getPath());
        }
    }
    final Map<AndroidCompilerMessageKind, List<String>> messages = AndroidApt.crunch(androidTarget, inputDirs, resCacheDir.getPath());
    AndroidJpsUtil.addMessages(context, messages, BUILDER_NAME, module.getName());
    final boolean success = messages.get(AndroidCompilerMessageKind.ERROR).isEmpty();
    if (success) {
        final Map<String, File> outputFiles = new HashMap<String, File>();
        FileUtil.processFilesRecursively(resCacheDir, new Processor<File>() {

            @Override
            public boolean process(File file) {
                if (file.isFile()) {
                    final String relativePath = FileUtil.getRelativePath(resCacheDir, file);
                    if (relativePath != null) {
                        outputFiles.put(relativePath, file);
                    }
                }
                return true;
            }
        });
        for (Map.Entry<String, File> entry : outputFiles.entrySet()) {
            final String relativePath = entry.getKey();
            final File outputFile = entry.getValue();
            for (String inputDir : inputDirs) {
                final File srcFile = new File(inputDir, relativePath);
                outputConsumer.registerOutputFile(outputFile, Collections.singletonList(srcFile.getPath()));
            }
        }
    }
    return success;
}
Also used : BuildRootDescriptor(org.jetbrains.jps.builders.BuildRootDescriptor) CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) HashMap(com.intellij.util.containers.HashMap) ArrayList(java.util.ArrayList) IAndroidTarget(com.android.sdklib.IAndroidTarget) AndroidCompilerMessageKind(org.jetbrains.android.util.AndroidCompilerMessageKind) JpsModule(org.jetbrains.jps.model.module.JpsModule) JpsAndroidModuleExtension(org.jetbrains.jps.android.model.JpsAndroidModuleExtension) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) HashMap(com.intellij.util.containers.HashMap) Map(java.util.Map)

Aggregations

HashMap (com.intellij.util.containers.HashMap)118 NotNull (org.jetbrains.annotations.NotNull)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 File (java.io.File)22 Nullable (org.jetbrains.annotations.Nullable)18 Map (java.util.Map)17 Module (com.intellij.openapi.module.Module)15 Project (com.intellij.openapi.project.Project)15 ArrayList (java.util.ArrayList)14 PsiElement (com.intellij.psi.PsiElement)11 HashSet (com.intellij.util.containers.HashSet)10 List (java.util.List)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 IOException (java.io.IOException)8 Pair (com.intellij.openapi.util.Pair)6 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)6 SearchScope (com.intellij.psi.search.SearchScope)6 Logger (com.intellij.openapi.diagnostic.Logger)5 PsiFile (com.intellij.psi.PsiFile)5 UsageInfo (com.intellij.usageView.UsageInfo)5