Search in sources :

Example 1 with AndroidSourceSet

use of com.android.tools.idea.npw.project.AndroidSourceSet in project android by JetBrains.

the class ConfigureTemplateParametersStep method onProceeding.

/**
   * When finished with this step, calculate and install a bunch of values that will be used in our
   * template's <a href="http://freemarker.incubator.apache.org/docs/dgui_quickstart_basics.html">data model.</a>
   */
@Override
protected void onProceeding() {
    // canGoForward guarantees this optional value is present
    AndroidSourceSet sourceSet = getModel().getSourceSet().get();
    AndroidProjectPaths paths = sourceSet.getPaths();
    File moduleRoot = paths.getModuleRoot();
    if (moduleRoot == null) {
        getLog().error(String.format("%s failure: can't create files because module root is not found. Please report this error.", getTitle()));
        return;
    }
    // Some parameter values should be saved for later runs through this wizard, so do that first.
    for (RowEntry rowEntry : myParameterRows.values()) {
        rowEntry.accept();
    }
    // Prepare the template data-model, starting from scratch and filling in all values we know
    Map<String, Object> templateValues = getModel().getTemplateValues();
    templateValues.clear();
    for (Parameter parameter : myParameterRows.keySet()) {
        ObservableValue<?> property = myParameterRows.get(parameter).getProperty();
        if (property != null) {
            templateValues.put(parameter.id, property.get());
        }
    }
    templateValues.put(ATTR_PACKAGE_NAME, myPackageName.get());
    templateValues.put(ATTR_SOURCE_PROVIDER_NAME, sourceSet.getName());
    // Android Modules are called Gradle Projects
    templateValues.put(ATTR_IS_NEW_PROJECT, isNewModule());
    if (isNewModule()) {
        templateValues.put(ATTR_IS_LAUNCHER, true);
    }
    try {
        File sha1File = myFacet == null ? getOrCreateDefaultDebugKeystore() : getDebugKeystore(myFacet);
        templateValues.put(ATTR_DEBUG_KEYSTORE_SHA1, KeystoreUtils.sha1(sha1File));
    } catch (Exception e) {
        getLog().info("Could not compute SHA1 hash of debug keystore.", e);
        templateValues.put(ATTR_DEBUG_KEYSTORE_SHA1, "");
    }
    if (myFacet == null) {
        // If we don't have an AndroidFacet, we must have the Android Sdk info
        AndroidVersionsInfo.VersionItem buildVersion = getModel().androidSdkInfo().getValue();
        templateValues.put(ATTR_MIN_API_LEVEL, buildVersion.getApiLevel());
        templateValues.put(ATTR_MIN_API, buildVersion.getApiLevelStr());
        templateValues.put(ATTR_BUILD_API, buildVersion.getBuildApiLevel());
        templateValues.put(ATTR_BUILD_API_STRING, buildVersion.getBuildApiLevelStr());
        templateValues.put(ATTR_TARGET_API, buildVersion.getTargetApiLevel());
        templateValues.put(ATTR_TARGET_API_STRING, buildVersion.getTargetApiLevelStr());
    } else {
        AndroidPlatform platform = AndroidPlatform.getInstance(myFacet.getModule());
        if (platform != null) {
            templateValues.put(ATTR_BUILD_API, platform.getTarget().getVersion().getFeatureLevel());
            templateValues.put(ATTR_BUILD_API_STRING, getBuildApiString(platform.getTarget().getVersion()));
        }
        AndroidModuleInfo moduleInfo = AndroidModuleInfo.get(myFacet);
        AndroidVersion minSdkVersion = moduleInfo.getMinSdkVersion();
        String minSdkName = minSdkVersion.getApiString();
        templateValues.put(ATTR_MIN_API, minSdkName);
        templateValues.put(ATTR_TARGET_API, moduleInfo.getTargetSdkVersion().getApiLevel());
        templateValues.put(ATTR_MIN_API_LEVEL, minSdkVersion.getFeatureLevel());
        templateValues.put(ATTR_IS_LIBRARY_MODULE, myFacet.isLibraryProject());
        // Register application-wide settings
        String applicationPackage = AndroidPackageUtils.getPackageForApplication(myFacet);
        if (!myPackageName.get().equals(applicationPackage)) {
            templateValues.put(ATTR_APPLICATION_PACKAGE, AndroidPackageUtils.getPackageForApplication(myFacet));
        }
    }
    // Register the resource directories associated with the active source provider
    templateValues.put(ATTR_PROJECT_OUT, FileUtil.toSystemIndependentName(moduleRoot.getAbsolutePath()));
    String packageAsDir = myPackageName.get().replace('.', File.separatorChar);
    File srcDir = paths.getSrcDirectory();
    if (srcDir != null) {
        srcDir = new File(srcDir, packageAsDir);
        templateValues.put(ATTR_SRC_DIR, getRelativePath(moduleRoot, srcDir));
        templateValues.put(ATTR_SRC_OUT, FileUtil.toSystemIndependentName(srcDir.getAbsolutePath()));
    }
    File testDir = paths.getTestDirectory();
    if (testDir != null) {
        testDir = new File(testDir, packageAsDir);
        templateValues.put(ATTR_TEST_DIR, getRelativePath(moduleRoot, testDir));
        templateValues.put(ATTR_TEST_OUT, FileUtil.toSystemIndependentName(testDir.getAbsolutePath()));
    }
    File resDir = paths.getResDirectory();
    if (resDir != null) {
        templateValues.put(ATTR_RES_DIR, getRelativePath(moduleRoot, resDir));
        templateValues.put(ATTR_RES_OUT, FileUtil.toSystemIndependentName(resDir.getPath()));
    }
    File manifestDir = paths.getManifestDirectory();
    if (manifestDir != null) {
        templateValues.put(ATTR_MANIFEST_DIR, getRelativePath(moduleRoot, manifestDir));
        templateValues.put(ATTR_MANIFEST_OUT, FileUtil.toSystemIndependentName(manifestDir.getPath()));
    }
    File aidlDir = paths.getAidlDirectory();
    if (aidlDir != null) {
        templateValues.put(ATTR_AIDL_DIR, getRelativePath(moduleRoot, aidlDir));
        templateValues.put(ATTR_AIDL_OUT, FileUtil.toSystemIndependentName(aidlDir.getPath()));
    }
    templateValues.put(PROJECT_LOCATION_ID, moduleRoot.getParent());
    // We're really interested in the directory name on disk, not the module name. These will be different if you give a module the same
    // name as its containing project.
    String moduleName = moduleRoot.getName();
    templateValues.put(ATTR_MODULE_NAME, moduleName);
}
Also used : AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) AndroidVersion(com.android.sdklib.AndroidVersion) AndroidModuleInfo(com.android.tools.idea.model.AndroidModuleInfo) AndroidVersionsInfo(com.android.tools.idea.npw.platform.AndroidVersionsInfo) File(java.io.File) AndroidSourceSet(com.android.tools.idea.npw.project.AndroidSourceSet) AndroidProjectPaths(com.android.tools.idea.npw.project.AndroidProjectPaths)

Example 2 with AndroidSourceSet

use of com.android.tools.idea.npw.project.AndroidSourceSet in project android by JetBrains.

the class TemplateManager method fillCategory.

private void fillCategory(NonEmptyActionGroup categoryGroup, final String category, ActionManager am) {
    Map<String, File> categoryRow = myCategoryTable.row(category);
    if (CATEGORY_ACTIVITY.equals(category)) {
        AnAction galleryAction = new AnAction() {

            @Override
            public void update(AnActionEvent e) {
                updateAction(e, "Gallery...", true);
            }

            @Override
            public void actionPerformed(AnActionEvent e) {
                // TODO: before submitting this code, change this to only use the new wizard
                if (Boolean.getBoolean("use.npw.modelwizard") && (e.getModifiers() & InputEvent.SHIFT_MASK) == 0) {
                    DataContext dataContext = e.getDataContext();
                    Module module = LangDataKeys.MODULE.getData(dataContext);
                    assert module != null;
                    VirtualFile targetFile = CommonDataKeys.VIRTUAL_FILE.getData(dataContext);
                    assert targetFile != null;
                    VirtualFile targetDirectory = targetFile;
                    if (!targetDirectory.isDirectory()) {
                        targetDirectory = targetFile.getParent();
                        assert targetDirectory != null;
                    }
                    AndroidFacet facet = AndroidFacet.getInstance(module);
                    assert facet != null && facet.getAndroidModel() != null;
                    List<TemplateHandle> templateList = getTemplateList(FormFactor.MOBILE);
                    List<AndroidSourceSet> sourceSets = AndroidSourceSet.getSourceSets(facet, targetDirectory);
                    assert (sourceSets.size() > 0);
                    String initialPackageSuggestion = AndroidPackageUtils.getPackageForPath(facet, sourceSets, targetDirectory);
                    Project project = facet.getModule().getProject();
                    // TODO: Missing logic to select the default template
                    RenderTemplateModel renderModel = new RenderTemplateModel(project, templateList.get(0), initialPackageSuggestion, sourceSets.get(0), AndroidBundle.message("android.wizard.activity.add"));
                    NewModuleModel moduleModel = new NewModuleModel(project);
                    ChooseActivityTypeStep chooseActivityTypeStep = new ChooseActivityTypeStep(moduleModel, renderModel, facet, templateList, targetDirectory);
                    ModelWizard wizard = new ModelWizard.Builder().addStep(chooseActivityTypeStep).build();
                    new StudioWizardDialogBuilder(wizard, "New Android Activity").build().show();
                } else {
                    DataContext dataContext = e.getDataContext();
                    final Module module = LangDataKeys.MODULE.getData(dataContext);
                    VirtualFile targetFile = CommonDataKeys.VIRTUAL_FILE.getData(dataContext);
                    NewAndroidActivityWizard wizard = new NewAndroidActivityWizard(module, targetFile, null);
                    wizard.init();
                    wizard.show();
                }
            }
        };
        categoryGroup.add(galleryAction);
        categoryGroup.addSeparator();
        setPresentation(category, galleryAction);
    }
    for (String templateName : categoryRow.keySet()) {
        if (EXCLUDED_TEMPLATES.contains(templateName)) {
            continue;
        }
        TemplateMetadata metadata = getTemplateMetadata(myCategoryTable.get(category, templateName));
        NewAndroidComponentAction templateAction = new NewAndroidComponentAction(category, templateName, metadata);
        String actionId = ACTION_ID_PREFIX + category + templateName;
        am.unregisterAction(actionId);
        am.registerAction(actionId, templateAction);
        categoryGroup.add(templateAction);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NewAndroidActivityWizard(com.android.tools.idea.npw.NewAndroidActivityWizard) StudioWizardDialogBuilder(com.android.tools.idea.ui.wizard.StudioWizardDialogBuilder) RenderTemplateModel(com.android.tools.idea.npw.template.RenderTemplateModel) NewAndroidComponentAction(com.android.tools.idea.actions.NewAndroidComponentAction) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) NewModuleModel(com.android.tools.idea.npw.module.NewModuleModel) StudioWizardDialogBuilder(com.android.tools.idea.ui.wizard.StudioWizardDialogBuilder) TemplateHandle(com.android.tools.idea.npw.template.TemplateHandle) ChooseActivityTypeStep(com.android.tools.idea.npw.template.ChooseActivityTypeStep) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) AndroidSourceSet(com.android.tools.idea.npw.project.AndroidSourceSet) ModelWizard(com.android.tools.idea.wizard.model.ModelWizard)

Example 3 with AndroidSourceSet

use of com.android.tools.idea.npw.project.AndroidSourceSet in project android by JetBrains.

the class SourceSetComboProvider method createComponent.

@NotNull
@Override
public JComboBox createComponent() {
    DefaultComboBoxModel comboBoxModel = new DefaultComboBoxModel();
    for (AndroidSourceSet sourceSet : mySourceSets) {
        comboBoxModel.addElement(sourceSet);
    }
    JComboBox sourceSetCombo = new JComboBox(comboBoxModel);
    sourceSetCombo.setRenderer(new ListCellRendererWrapper() {

        @Override
        public void customize(JList list, Object value, int index, boolean selected, boolean hasFocus) {
            setText(((AndroidSourceSet) value).getName());
        }
    });
    sourceSetCombo.setToolTipText("<html>The source set within which to generate new project files.<br>" + "If you specify a source set that does not yet exist on disk, a folder will be created for it.</html>");
    return sourceSetCombo;
}
Also used : ListCellRendererWrapper(com.intellij.ui.ListCellRendererWrapper) AndroidSourceSet(com.android.tools.idea.npw.project.AndroidSourceSet) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with AndroidSourceSet

use of com.android.tools.idea.npw.project.AndroidSourceSet in project intellij by bazelbuild.

the class BlazeAndroidProjectPathsTest method getResourceSourceSetsWithNoTargetDirectory.

/**
 * If no target directory is given, but we have a resource module, we can still figure out some
 * paths.
 */
@Test
public void getResourceSourceSetsWithNoTargetDirectory() {
    AndroidFacet facet = mockResourceFacet();
    File resourceFile = VfsUtilCore.virtualToIoFile(resource);
    List<AndroidSourceSet> sourceSets = AndroidSourceSet.getSourceSets(facet, null);
    assertThat(sourceSets).hasSize(1);
    AndroidSourceSet sourceSet = sourceSets.get(0);
    AndroidProjectPaths paths = sourceSet.getPaths();
    assertThat(sourceSet.getName()).isEqualTo("com.google.resource");
    assertThat(paths.getModuleRoot()).isEqualTo(resourceFile);
    assertThat(paths.getSrcDirectory(null)).isEqualTo(resourceFile);
    assertThat(paths.getTestDirectory(null)).isEqualTo(resourceFile);
    assertThat(paths.getResDirectory()).isEqualTo(new File(resourceFile, "res"));
    assertThat(paths.getAidlDirectory(null)).isEqualTo(resourceFile);
    assertThat(paths.getManifestDirectory()).isEqualTo(resourceFile);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) File(java.io.File) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) AndroidSourceSet(com.android.tools.idea.npw.project.AndroidSourceSet) AndroidProjectPaths(com.android.tools.idea.npw.project.AndroidProjectPaths) Test(org.junit.Test)

Example 5 with AndroidSourceSet

use of com.android.tools.idea.npw.project.AndroidSourceSet in project intellij by bazelbuild.

the class BlazeAndroidProjectPaths method getSourceSets.

public static List<AndroidSourceSet> getSourceSets(AndroidFacet androidFacet, @Nullable VirtualFile targetDirectory) {
    Module module = androidFacet.getModule();
    BlazeAndroidProjectPaths paths = new BlazeAndroidProjectPaths();
    VirtualFile[] roots = ModuleRootManager.getInstance(module).getContentRoots();
    if (roots.length > 0) {
        paths.moduleRoot = VfsUtilCore.virtualToIoFile(roots[0]);
    }
    // We have a res dir if this happens to be a resource module.
    SourceProvider sourceProvider = androidFacet.getMainSourceProvider();
    paths.resDirectory = Iterables.getFirst(sourceProvider.getResDirectories(), null);
    // If this happens to be a resource package,
    // the module name (resource package) would be more descriptive than the facet name (Android).
    // Otherwise, .workspace is still better than (Android).
    String name = androidFacet.getModule().getName();
    if (targetDirectory != null) {
        String packageName = getPackageName(module.getProject(), targetDirectory);
        if (packageName != null) {
            name = packageName;
        }
        paths.srcDirectory = VfsUtilCore.virtualToIoFile(targetDirectory);
    } else {
        // People usually put the manifest file with their sources.
        paths.srcDirectory = sourceProvider.getManifestFile().getParentFile();
    }
    if (paths.resDirectory == null) {
        paths.resDirectory = new File(paths.srcDirectory, SdkConstants.FD_RES);
    }
    return Collections.singletonList(new AndroidSourceSet(name, paths));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SourceProvider(com.android.builder.model.SourceProvider) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) AndroidSourceSet(com.android.tools.idea.npw.project.AndroidSourceSet)

Aggregations

AndroidSourceSet (com.android.tools.idea.npw.project.AndroidSourceSet)11 File (java.io.File)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)7 AndroidProjectPaths (com.android.tools.idea.npw.project.AndroidProjectPaths)6 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)6 MockVirtualFile (com.intellij.mock.MockVirtualFile)4 Module (com.intellij.openapi.module.Module)4 Test (org.junit.Test)4 RenderTemplateModel (com.android.tools.idea.npw.template.RenderTemplateModel)3 TemplateHandle (com.android.tools.idea.npw.template.TemplateHandle)3 Project (com.intellij.openapi.project.Project)3 NotNull (org.jetbrains.annotations.NotNull)3 ChooseActivityTypeStep (com.android.tools.idea.npw.template.ChooseActivityTypeStep)2 StudioWizardDialogBuilder (com.android.tools.idea.ui.wizard.StudioWizardDialogBuilder)2 ModelWizard (com.android.tools.idea.wizard.model.ModelWizard)2 SourceProvider (com.android.builder.model.SourceProvider)1 AndroidVersion (com.android.sdklib.AndroidVersion)1 NewAndroidComponentAction (com.android.tools.idea.actions.NewAndroidComponentAction)1 AndroidModuleInfo (com.android.tools.idea.model.AndroidModuleInfo)1 NewAndroidActivityWizard (com.android.tools.idea.npw.NewAndroidActivityWizard)1