Search in sources :

Example 1 with GradleSettingsFile

use of com.android.tools.idea.gradle.parser.GradleSettingsFile in project android by JetBrains.

the class KeystoreUtils method getGradleDebugKeystore.

/**
   * Gets a custom debug keystore defined in the build.gradle file for this module
   *
   * @return null if there is no custom debug keystore configured, or if the project is not a Gradle project.
   */
@Nullable
private static File getGradleDebugKeystore(@NotNull AndroidFacet facet) {
    GradleSettingsFile gradleSettingsFile = GradleSettingsFile.get(facet.getModule().getProject());
    if (gradleSettingsFile == null) {
        return null;
    }
    String modulePath = GradleSettingsFile.getModuleGradlePath(facet.getModule());
    if (modulePath == null) {
        return null;
    }
    final GradleBuildFile moduleBuildFile = gradleSettingsFile.getModuleBuildFile(modulePath);
    if (moduleBuildFile == null) {
        return null;
    }
    Iterable<NamedObject> signingConfigs = ApplicationManager.getApplication().runReadAction(new Computable<Iterable<NamedObject>>() {

        @Override
        @SuppressWarnings("unchecked")
        public Iterable<NamedObject> compute() {
            return (Iterable<NamedObject>) moduleBuildFile.getValue(BuildFileKey.SIGNING_CONFIGS);
        }
    });
    if (signingConfigs == null) {
        return null;
    }
    for (NamedObject namedObject : signingConfigs) {
        if (!"debug".equals(namedObject.getName())) {
            continue;
        }
        File debugKey = (File) namedObject.getValue(BuildFileKey.STORE_FILE);
        if (debugKey == null) {
            continue;
        }
        // NOTE: debugKey.getParent() is the current working directory.
        return new File(ModuleUtilCore.getModuleDirPath(facet.getModule()), debugKey.getPath());
    }
    return null;
}
Also used : GradleSettingsFile(com.android.tools.idea.gradle.parser.GradleSettingsFile) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) NamedObject(com.android.tools.idea.gradle.parser.NamedObject) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) File(java.io.File) GradleSettingsFile(com.android.tools.idea.gradle.parser.GradleSettingsFile) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with GradleSettingsFile

use of com.android.tools.idea.gradle.parser.GradleSettingsFile in project android by JetBrains.

the class GradleModuleImporter method getSubProjects.

@NotNull
public static Map<String, VirtualFile> getSubProjects(@NotNull final VirtualFile settingsGradle, Project destinationProject) {
    GradleSettingsFile settingsFile = new GradleSettingsFile(settingsGradle, destinationProject);
    Map<String, File> allProjects = settingsFile.getModulesWithLocation();
    return Maps.transformValues(allProjects, new ResolvePath(virtualToIoFile(settingsGradle.getParent())));
}
Also used : GradleSettingsFile(com.android.tools.idea.gradle.parser.GradleSettingsFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) File(java.io.File) GradleSettingsFile(com.android.tools.idea.gradle.parser.GradleSettingsFile) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GradleSettingsFile

use of com.android.tools.idea.gradle.parser.GradleSettingsFile in project android by JetBrains.

the class GradleModuleImporter method copyAndRegisterModule.

/**
   * Copy modules and adds it to settings.gradle
   */
private static void copyAndRegisterModule(@NotNull Object requestor, @NotNull Map<String, VirtualFile> modules, @NotNull Project project, @Nullable GradleSyncListener listener) throws IOException, ConfigurationException {
    VirtualFile projectRoot = project.getBaseDir();
    if (projectRoot.findChild(SdkConstants.FN_SETTINGS_GRADLE) == null) {
        projectRoot.createChildData(requestor, SdkConstants.FN_SETTINGS_GRADLE);
    }
    GradleSettingsFile gradleSettingsFile = GradleSettingsFile.get(project);
    assert gradleSettingsFile != null : "File should have been created";
    for (Map.Entry<String, VirtualFile> module : modules.entrySet()) {
        String name = module.getKey();
        File targetFile = GradleUtil.getModuleDefaultPath(projectRoot, name);
        VirtualFile moduleSource = module.getValue();
        if (moduleSource != null) {
            if (!isAncestor(projectRoot, moduleSource, true)) {
                VirtualFile target = createDirectoryIfMissing(targetFile.getAbsolutePath());
                if (target == null) {
                    throw new IOException(String.format("Unable to create directory %1$s", targetFile));
                }
                if (target.exists()) {
                    target.delete(requestor);
                }
                moduleSource.copy(requestor, target.getParent(), target.getName());
            } else {
                targetFile = virtualToIoFile(moduleSource);
            }
        }
        gradleSettingsFile.addModule(name, targetFile);
    }
    GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setGenerateSourcesOnSuccess(false);
    GradleSyncInvoker.getInstance().requestProjectSync(project, request, listener);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleSettingsFile(com.android.tools.idea.gradle.parser.GradleSettingsFile) GradleSyncInvoker(com.android.tools.idea.gradle.project.sync.GradleSyncInvoker) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) File(java.io.File) GradleSettingsFile(com.android.tools.idea.gradle.parser.GradleSettingsFile)

Example 4 with GradleSettingsFile

use of com.android.tools.idea.gradle.parser.GradleSettingsFile in project android by JetBrains.

the class GradleModuleImportTest method assertModuleInSettingsFile.

private static void assertModuleInSettingsFile(Project project, String name) throws IOException {
    GradleSettingsFile settingsFile = GradleSettingsFile.get(project);
    assertNotNull("Missing " + SdkConstants.FN_SETTINGS_GRADLE, settingsFile);
    Iterable<String> modules = settingsFile.getModules();
    if (!contains(modules, name)) {
        fail(String.format("Subproject %s is not in %s. Found subprojects: %s", name, SdkConstants.FN_SETTINGS_GRADLE, Joiner.on(", ").join(modules)));
    }
}
Also used : GradleSettingsFile(com.android.tools.idea.gradle.parser.GradleSettingsFile)

Example 5 with GradleSettingsFile

use of com.android.tools.idea.gradle.parser.GradleSettingsFile in project android by JetBrains.

the class ArchiveToGradleModuleModelTest method assertArchiveImportedCorrectly.

private void assertArchiveImportedCorrectly(@NotNull String newModuleGradlePath, @NotNull File archiveToImport) throws IOException {
    Project project = getProject();
    File defaultSubprojectLocation = GradleUtil.getModuleDefaultPath(project.getBaseDir(), newModuleGradlePath);
    File importedArchive = new File(defaultSubprojectLocation, archiveToImport.getName());
    assertWithMessage(String.format("File %s does not exist", importedArchive)).that(importedArchive.exists()).isTrue();
    File buildGradle = new File(defaultSubprojectLocation, SdkConstants.FN_BUILD_GRADLE);
    assertWithMessage(String.format("File %s does not exist", buildGradle)).that(buildGradle.exists()).isTrue();
    VirtualFile vFile = VfsUtil.findFileByIoFile(buildGradle, true);
    assertThat(vFile).isNotNull();
    assertThat(CreateModuleFromArchiveAction.getBuildGradleText(archiveToImport)).isEqualTo(VfsUtilCore.loadText(vFile));
    GradleSettingsFile settingsFile = GradleSettingsFile.get(project);
    assertThat(settingsFile).isNotNull();
    Iterable<String> modules = settingsFile.getModules();
    assertWithMessage("{ " + Joiner.on(", ").join(modules) + " }").that(modules).contains(newModuleGradlePath);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleSettingsFile(com.android.tools.idea.gradle.parser.GradleSettingsFile) Project(com.intellij.openapi.project.Project) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) GradleSettingsFile(com.android.tools.idea.gradle.parser.GradleSettingsFile)

Aggregations

GradleSettingsFile (com.android.tools.idea.gradle.parser.GradleSettingsFile)5 File (java.io.File)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 GradleBuildFile (com.android.tools.idea.gradle.parser.GradleBuildFile)2 VfsUtil.findFileByIoFile (com.intellij.openapi.vfs.VfsUtil.findFileByIoFile)2 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)2 NamedObject (com.android.tools.idea.gradle.parser.NamedObject)1 GradleSyncInvoker (com.android.tools.idea.gradle.project.sync.GradleSyncInvoker)1 Project (com.intellij.openapi.project.Project)1 IOException (java.io.IOException)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1