Ejemplo n.º 1
0
  @Override
  public void _init() {
    if (myInited) return;
    Module module = myWizard.getFacet().getModule();

    PropertiesComponent properties = PropertiesComponent.getInstance(module.getProject());
    String lastModule = properties.getValue(ChooseModuleStep.MODULE_PROPERTY);
    String lastApkPath = properties.getValue(APK_PATH_PROPERTY);
    if (lastApkPath != null && module.getName().equals(lastModule)) {
      myApkPathField.setText(FileUtil.toSystemDependentName(lastApkPath));
    } else {
      String contentRootPath = getContentRootPath(module);
      if (contentRootPath != null) {
        String defaultPath =
            FileUtil.toSystemDependentName(contentRootPath + "/" + module.getName() + ".apk");
        myApkPathField.setText(defaultPath);
      }
    }

    final String runProguardPropValue = properties.getValue(RUN_PROGUARD_PROPERTY);
    boolean selected;

    if (runProguardPropValue != null) {
      selected = Boolean.parseBoolean(runProguardPropValue);
    } else {
      selected = false;
    }
    myProguardCheckBox.setSelected(selected);
    myProguardConfigFilePathLabel.setEnabled(selected);
    myProguardConfigFilePathField.setEnabled(selected);

    final String proguardCfgPath = properties.getValue(PROGUARD_CFG_PATH_PROPERTY);
    if (proguardCfgPath != null
        && LocalFileSystem.getInstance().refreshAndFindFileByPath(proguardCfgPath) != null) {
      myProguardConfigFilePathField.setText(FileUtil.toSystemDependentName(proguardCfgPath));
    } else {
      final VirtualFile proguardConfigFile =
          AndroidCompileUtil.getProguardConfigFile(myWizard.getFacet());
      if (proguardConfigFile != null) {
        myProguardConfigFilePathField.setText(
            FileUtil.toSystemDependentName(proguardConfigFile.getPath()));
      }
    }

    myInited = true;
  }
  private static GenerationItem[] doGenerate(
      @NotNull final CompileContext context,
      @NotNull final GenerationItem[] items,
      VirtualFile outputRootDirectory) {
    if (context.getProject().isDisposed()) {
      return EMPTY_GENERATION_ITEM_ARRAY;
    }

    // we have one item per module there, so clear output directory
    final String genRootPath = FileUtil.toSystemDependentName(outputRootDirectory.getPath());
    final File genRootDir = new File(genRootPath);
    if (genRootDir.exists()) {
      if (!FileUtil.delete(genRootDir)) {
        context.addMessage(
            CompilerMessageCategory.ERROR, "Cannot delete directory " + genRootPath, null, -1, -1);
        return EMPTY_GENERATION_ITEM_ARRAY;
      }
      if (!genRootDir.mkdir()) {
        context.addMessage(
            CompilerMessageCategory.ERROR, "Cannot create directory " + genRootPath, null, -1, -1);
        return EMPTY_GENERATION_ITEM_ARRAY;
      }
    }

    final List<GenerationItem> results = new ArrayList<GenerationItem>(items.length);
    for (final GenerationItem item : items) {
      if (item instanceof MyGenerationItem) {
        final MyGenerationItem genItem = (MyGenerationItem) item;

        if (!AndroidCompileUtil.isModuleAffected(context, genItem.myModule)) {
          continue;
        }

        boolean success = true;

        for (final VirtualFile sourceFile : genItem.myFiles) {
          final String depFolderOsPath =
              getDependencyFolder(context.getProject(), sourceFile, outputRootDirectory);

          try {
            final Map<CompilerMessageCategory, List<String>> messages =
                AndroidCompileUtil.toCompilerMessageCategoryKeys(
                    AndroidRenderscript.execute(
                        genItem.mySdkLocation,
                        genItem.myAndroidTarget,
                        sourceFile.getPath(),
                        genRootPath,
                        depFolderOsPath,
                        genItem.myRawDirPath));

            ApplicationManager.getApplication()
                .runReadAction(
                    new Runnable() {
                      public void run() {
                        if (context.getProject().isDisposed()) {
                          return;
                        }
                        addMessages(context, messages, sourceFile.getUrl());
                      }
                    });

            if (messages.get(CompilerMessageCategory.ERROR).size() > 0) {
              success = false;
            }
          } catch (final IOException e) {
            ApplicationManager.getApplication()
                .runReadAction(
                    new Runnable() {
                      public void run() {
                        if (context.getProject().isDisposed()) return;
                        context.addMessage(
                            CompilerMessageCategory.ERROR,
                            e.getMessage(),
                            sourceFile.getUrl(),
                            -1,
                            -1);
                      }
                    });
            success = false;
          }
        }

        if (success) {
          results.add(genItem);
        }
      }
    }
    outputRootDirectory.refresh(false, true);
    return results.toArray(new GenerationItem[results.size()]);
  }
Ejemplo n.º 3
0
  @Override
  protected void commitForNext() throws CommitStepException {
    final String apkPath = myApkPathField.getText().trim();
    if (apkPath.length() == 0) {
      throw new CommitStepException(
          AndroidBundle.message("android.extract.package.specify.apk.path.error"));
    }

    AndroidFacet facet = myWizard.getFacet();
    PropertiesComponent properties = PropertiesComponent.getInstance(myWizard.getProject());
    properties.setValue(
        ChooseModuleStep.MODULE_PROPERTY, facet != null ? facet.getModule().getName() : "");
    properties.setValue(APK_PATH_PROPERTY, apkPath);

    File folder = new File(apkPath).getParentFile();
    if (folder == null) {
      throw new CommitStepException(
          AndroidBundle.message("android.cannot.create.file.error", apkPath));
    }
    try {
      if (!folder.exists()) {
        folder.mkdirs();
      }
    } catch (Exception e) {
      throw new CommitStepException(e.getMessage());
    }

    final CompilerManager manager = CompilerManager.getInstance(myWizard.getProject());
    final CompileScope compileScope = manager.createModuleCompileScope(facet.getModule(), true);
    AndroidCompileUtil.setReleaseBuild(compileScope);

    properties.setValue(RUN_PROGUARD_PROPERTY, Boolean.toString(myProguardCheckBox.isSelected()));

    if (myProguardCheckBox.isSelected()) {
      final String proguardCfgPath = myProguardConfigFilePathField.getText().trim();
      if (proguardCfgPath.length() == 0) {
        throw new CommitStepException(
            AndroidBundle.message("android.extract.package.specify.proguard.cfg.path.error"));
      }
      properties.setValue(PROGUARD_CFG_PATH_PROPERTY, proguardCfgPath);

      if (!new File(proguardCfgPath).isFile()) {
        throw new CommitStepException("Cannot find file " + proguardCfgPath);
      }

      compileScope.putUserData(AndroidProguardCompiler.PROGUARD_CFG_PATH_KEY, proguardCfgPath);
    }

    manager.make(
        compileScope,
        new CompileStatusNotification() {
          public void finished(
              boolean aborted, int errors, int warnings, CompileContext compileContext) {
            if (aborted || errors != 0) {
              return;
            }

            final String title = AndroidBundle.message("android.extract.package.task.title");
            ProgressManager.getInstance()
                .run(
                    new Task.Backgroundable(myWizard.getProject(), title, true, null) {
                      public void run(@NotNull ProgressIndicator indicator) {
                        createAndAlignApk(apkPath);
                      }
                    });
          }
        });
  }