private static String getArtifactResolverJar(boolean isMaven3) throws IOException {
    Class marker =
        isMaven3 ? MavenArtifactResolvedM3RtMarker.class : MavenArtifactResolvedM2RtMarker.class;

    File classDirOrJar = new File(PathUtil.getJarPathForClass(marker));

    if (!classDirOrJar.isDirectory()) {
      return classDirOrJar.getAbsolutePath(); // it's a jar in IDEA installation.
    }

    // it's a classes directory, we are in development mode.
    File tempFile = FileUtil.createTempFile("idea-", "-artifactResolver.jar");
    tempFile.deleteOnExit();

    ZipOutputStream zipOutput = new ZipOutputStream(new FileOutputStream(tempFile));
    try {
      ZipUtil.addDirToZipRecursively(zipOutput, null, classDirOrJar, "", null, null);

      if (isMaven3) {
        File m2Module = new File(PathUtil.getJarPathForClass(MavenModuleMap.class));

        String commonClassesPath = MavenModuleMap.class.getPackage().getName().replace('.', '/');
        ZipUtil.addDirToZipRecursively(
            zipOutput, null, new File(m2Module, commonClassesPath), commonClassesPath, null, null);
      }
    } finally {
      zipOutput.close();
    }

    return tempFile.getAbsolutePath();
  }
 private static void processLibrariesAndJpsPlugins(
     final File jarFile,
     final File zipFile,
     final String pluginName,
     final Set<Library> libs,
     Map<Module, String> jpsModules,
     final ProgressIndicator progressIndicator)
     throws IOException {
   if (FileUtil.ensureCanCreateFile(zipFile)) {
     ZipOutputStream zos = null;
     try {
       zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
       addStructure(pluginName, zos);
       addStructure(pluginName + "/" + MIDDLE_LIB_DIR, zos);
       final String entryName = pluginName + JAR_EXTENSION;
       ZipUtil.addFileToZip(
           zos,
           jarFile,
           getZipPath(pluginName, entryName),
           new HashSet<String>(),
           createFilter(progressIndicator, FileTypeManager.getInstance()));
       for (Map.Entry<Module, String> entry : jpsModules.entrySet()) {
         File jpsPluginJar = jarModulesOutput(Collections.singleton(entry.getKey()), null, null);
         ZipUtil.addFileToZip(
             zos, jpsPluginJar, getZipPath(pluginName, entry.getValue()), null, null);
       }
       Set<String> usedJarNames = new HashSet<String>();
       usedJarNames.add(entryName);
       Set<VirtualFile> jarredVirtualFiles = new HashSet<VirtualFile>();
       for (Library library : libs) {
         final VirtualFile[] files = library.getFiles(OrderRootType.CLASSES);
         for (VirtualFile virtualFile : files) {
           if (jarredVirtualFiles.add(virtualFile)) {
             if (virtualFile.getFileSystem() instanceof JarFileSystem) {
               addLibraryJar(
                   virtualFile, zipFile, pluginName, zos, usedJarNames, progressIndicator);
             } else {
               makeAndAddLibraryJar(
                   virtualFile,
                   zipFile,
                   pluginName,
                   zos,
                   usedJarNames,
                   progressIndicator,
                   library.getName());
             }
           }
         }
       }
     } finally {
       if (zos != null) zos.close();
     }
   }
 }
 private static File jarModulesOutput(
     @NotNull Set<Module> modules,
     @Nullable Manifest manifest,
     final @Nullable String pluginXmlPath)
     throws IOException {
   File jarFile = FileUtil.createTempFile(TEMP_PREFIX, JAR_EXTENSION);
   jarFile.deleteOnExit();
   ZipOutputStream jarPlugin = null;
   try {
     BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(jarFile));
     jarPlugin = manifest != null ? new JarOutputStream(out, manifest) : new JarOutputStream(out);
     final ProgressIndicator progressIndicator =
         ProgressManager.getInstance().getProgressIndicator();
     final Set<String> writtenItemRelativePaths = new HashSet<String>();
     for (Module module : modules) {
       final VirtualFile compilerOutputPath =
           CompilerModuleExtension.getInstance(module).getCompilerOutputPath();
       if (compilerOutputPath == null)
         continue; // pre-condition: output dirs for all modules are up-to-date
       ZipUtil.addDirToZipRecursively(
           jarPlugin,
           jarFile,
           new File(compilerOutputPath.getPath()),
           "",
           createFilter(progressIndicator, FileTypeManager.getInstance()),
           writtenItemRelativePaths);
     }
     if (pluginXmlPath != null) {
       ZipUtil.addFileToZip(
           jarPlugin,
           new File(pluginXmlPath),
           "/META-INF/plugin.xml",
           writtenItemRelativePaths,
           createFilter(progressIndicator, null));
     }
   } finally {
     if (jarPlugin != null) jarPlugin.close();
   }
   return jarFile;
 }
 private void createKotlinJavaScriptLibraryArchive() {
   File jarFile = new File(workDir, KOTLIN_JS_LIBRARY_JAR);
   try {
     ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(jarFile));
     ZipUtil.addDirToZipRecursively(
         zip, jarFile, new File(PATH_TO_KOTLIN_JS_LIBRARY), "", null, null);
     zip.close();
   } catch (FileNotFoundException ex) {
     throw new IllegalStateException(ex.getMessage());
   } catch (IOException ex) {
     throw new IllegalStateException(ex.getMessage());
   }
 }
 private static void makeAndAddLibraryJar(
     final VirtualFile virtualFile,
     final File zipFile,
     final String pluginName,
     final ZipOutputStream zos,
     final Set<String> usedJarNames,
     final ProgressIndicator progressIndicator,
     final String preferredName)
     throws IOException {
   File libraryJar = FileUtil.createTempFile(TEMP_PREFIX, JAR_EXTENSION);
   libraryJar.deleteOnExit();
   ZipOutputStream jar = null;
   try {
     jar = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(libraryJar)));
     ZipUtil.addFileOrDirRecursively(
         jar,
         libraryJar,
         VfsUtilCore.virtualToIoFile(virtualFile),
         "",
         createFilter(progressIndicator, FileTypeManager.getInstance()),
         null);
   } finally {
     if (jar != null) jar.close();
   }
   final String jarName =
       getLibraryJarName(
           virtualFile.getName() + JAR_EXTENSION,
           usedJarNames,
           preferredName == null ? null : preferredName + JAR_EXTENSION);
   ZipUtil.addFileOrDirRecursively(
       zos,
       zipFile,
       libraryJar,
       getZipPath(pluginName, jarName),
       createFilter(progressIndicator, null),
       null);
 }
  public void testKotlinJavaScriptProjectWithDirectoryAsStdlib() {
    initProject();
    File jslibJar = PathUtil.getKotlinPathsForDistDirectory().getJsStdLibJarPath();
    File jslibDir = new File(workDir, "KotlinJavaScript");
    try {
      ZipUtil.extract(jslibJar, jslibDir, null);
    } catch (IOException ex) {
      throw new IllegalStateException(ex.getMessage());
    }
    addKotlinJavaScriptDependency("KotlinJavaScript", jslibDir);
    makeAll().assertSuccessful();

    assertEquals(EXPECTED_JS_FILES_IN_OUTPUT_FOR_STDLIB_ONLY, contentOfOutputDir(PROJECT_NAME));
    checkWhen(touch("src/test1.kt"), null, k2jsOutput(PROJECT_NAME));
  }
  public static void assertJarFilesEqual(File file1, File file2) throws IOException {
    final File tempDirectory1;
    final File tempDirectory2;

    final JarFile jarFile1 = new JarFile(file1);
    try {
      final JarFile jarFile2 = new JarFile(file2);
      try {
        tempDirectory1 = PlatformTestCase.createTempDir("tmp1");
        tempDirectory2 = PlatformTestCase.createTempDir("tmp2");
        ZipUtil.extract(jarFile1, tempDirectory1, null);
        ZipUtil.extract(jarFile2, tempDirectory2, null);
      } finally {
        jarFile2.close();
      }
    } finally {
      jarFile1.close();
    }

    final VirtualFile dirAfter =
        LocalFileSystem.getInstance().refreshAndFindFileByIoFile(tempDirectory1);
    assertNotNull(tempDirectory1.toString(), dirAfter);
    final VirtualFile dirBefore =
        LocalFileSystem.getInstance().refreshAndFindFileByIoFile(tempDirectory2);
    assertNotNull(tempDirectory2.toString(), dirBefore);
    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              @Override
              public void run() {
                dirAfter.refresh(false, true);
                dirBefore.refresh(false, true);
              }
            });
    assertDirectoriesEqual(dirAfter, dirBefore);
  }
 private static void addLibraryJar(
     final VirtualFile virtualFile,
     final File zipFile,
     final String pluginName,
     final ZipOutputStream zos,
     final Set<String> usedJarNames,
     final ProgressIndicator progressIndicator)
     throws IOException {
   File ioFile = VfsUtil.virtualToIoFile(virtualFile);
   final String jarName = getLibraryJarName(ioFile.getName(), usedJarNames, null);
   ZipUtil.addFileOrDirRecursively(
       zos,
       zipFile,
       ioFile,
       getZipPath(pluginName, jarName),
       createFilter(progressIndicator, null),
       null);
 }