Search in sources :

Example 1 with GradleImport

use of com.android.tools.idea.gradle.eclipse.GradleImport in project android by JetBrains.

the class GradleImportTest method checkImport.

private static File checkImport(@Nullable File gradleProjectDir, @NonNull File adtProjectDir, @NonNull String expectedSummary, boolean checkBuild, @Nullable ImportCustomizer customizer, @NonNull File destDir, @NonNull File rootDir) throws Exception {
    assertTrue(isAdtProjectDir(adtProjectDir));
    List<File> projects = Collections.singletonList(adtProjectDir);
    GradleImport importer = new GradleImport();
    boolean isImport = gradleProjectDir != null;
    if (isImport) {
        importer.setImportIntoExisting(true);
    } else {
        gradleProjectDir = destDir;
    }
    String sdkPath = getTestSdkPathLocal();
    if (sdkPath != null) {
        importer.setSdkLocation(new File(sdkPath));
    }
    File wrapper = findGradleWrapper();
    if (wrapper != null) {
        importer.setGradleWrapperLocation(wrapper);
    }
    if (customizer != null) {
        customizer.customize(importer);
    }
    importer.importProjects(projects);
    importer.getSummary().setWrapErrorMessages(false);
    if (isImport) {
        Map<File, File> map = Maps.newHashMap();
        map.put(adtProjectDir, destDir);
        importer.exportIntoProject(gradleProjectDir, true, true, map);
    } else {
        importer.exportProject(destDir, false);
    }
    String summary = Files.toString(new File(gradleProjectDir, IMPORT_SUMMARY_TXT), UTF_8);
    summary = summary.replace("\r", "");
    summary = stripOutRiskyPathMessage(summary, rootDir);
    String testSdkPath = getTestSdkPathLocal();
    if (testSdkPath != null) {
        summary = summary.replace(testSdkPath, "$ANDROID_HOME");
    }
    summary = summary.replace(separatorChar, '/');
    summary = summary.replace(adtProjectDir.getPath().replace(separatorChar, '/'), "$ROOT");
    File parentFile = adtProjectDir.getParentFile();
    if (parentFile != null) {
        summary = summary.replace(parentFile.getPath().replace(separatorChar, '/'), "$ROOT_PARENT");
    }
    assertEquals(expectedSummary, summary);
    if (checkBuild) {
        assertBuildsCleanly(gradleProjectDir, true);
    }
    return gradleProjectDir;
}
Also used : GradleImport(com.android.tools.idea.gradle.eclipse.GradleImport) File(java.io.File)

Example 2 with GradleImport

use of com.android.tools.idea.gradle.eclipse.GradleImport in project android by JetBrains.

the class GradleImportTest method testLibraries2.

@SuppressWarnings("ResultOfMethodCallIgnored")
public void testLibraries2() throws Exception {
    Pair<File, File> pair = createLibrary2(new File("Library1"));
    File root = pair.getFirst();
    File app = pair.getSecond();
    // ADT Directory structure created by the above:
    assertEquals("" + "AndroidApp\n" + "  .classpath\n" + "  .gitignore\n" + "  .project\n" + "  AndroidManifest.xml\n" + "  gen\n" + "    test\n" + "      pkg\n" + "        R.java\n" + "  project.properties\n" + "  res\n" + "    drawable\n" + "      ic_launcher.xml\n" + "    values\n" + "      strings.xml\n" + "  src\n" + "    test\n" + "      pkg\n" + "        AppActivity.java\n" + "AndroidLibrary\n" + "  .classpath\n" + "  .project\n" + "  AndroidManifest.xml\n" + "  gen\n" + "    test\n" + "      android\n" + "        lib\n" + "          pkg\n" + "            R.java\n" + "  project.properties\n" + "  src\n" + "    test\n" + "      android\n" + "        lib\n" + "          pkg\n" + "            AndroidLibrary.java\n" + "Library1\n" + "  .classpath\n" + "  .project\n" + "  .settings\n" + "    org.eclipse.jdt.core.prefs\n" + "  src\n" + "    test\n" + "      lib1\n" + "        pkg\n" + "          Library1.java\n" + "Library2\n" + "  .classpath\n" + "  .project\n" + "  .settings\n" + "    org.eclipse.jdt.core.prefs\n" + "  src\n" + "    test\n" + "      lib2\n" + "        pkg\n" + "          Library2.java\n" + "some\n" + "  path\n" + "    guava-13.0.1.jar\n", fileTree(root, true));
    final AtomicReference<GradleImport> importReference = new AtomicReference<>();
    File imported = checkProject(app, "" + MSG_HEADER + MSG_MANIFEST + MSG_UNHANDLED + "* .gitignore\n" + MSG_REPLACED_JARS + "guava-13.0.1.jar => com.google.guava:guava:13.0.1\n" + MSG_GUESSED_VERSIONS + "guava-13.0.1.jar => version 13.0.1 in com.google.guava:guava:13.0.1\n" + MSG_FOLDER_STRUCTURE + "In Library1:\n" + "* src/ => library1/src/main/java/\n" + "In Library2:\n" + "* src/ => library2/src/main/java/\n" + "In AndroidLibrary:\n" + "* AndroidManifest.xml => androidLibrary/src/main/AndroidManifest.xml\n" + "* src/ => androidLibrary/src/main/java/\n" + "In AndroidApp:\n" + "* AndroidManifest.xml => androidApp/src/main/AndroidManifest.xml\n" + "* res/ => androidApp/src/main/res/\n" + "* src/ => androidApp/src/main/java/\n" + MSG_FOOTER, false, /* checkBuild */
    importReference::set);
    assertEquals("{/Library1=" + new File(root, "Library1").getCanonicalPath() + ", /Library2=" + new File(root, "Library2").getCanonicalPath() + "}", describePathMap(importReference.get()));
    // Imported project
    assertEquals("" + "androidApp\n" + "  build.gradle\n" + "  src\n" + "    main\n" + "      AndroidManifest.xml\n" + "      java\n" + "        test\n" + "          pkg\n" + "            AppActivity.java\n" + "      res\n" + "        drawable\n" + "          ic_launcher.xml\n" + "        values\n" + "          strings.xml\n" + "androidLibrary\n" + "  build.gradle\n" + "  src\n" + "    main\n" + "      AndroidManifest.xml\n" + "      java\n" + "        test\n" + "          android\n" + "            lib\n" + "              pkg\n" + "                AndroidLibrary.java\n" + "build.gradle\n" + "import-summary.txt\n" + "library1\n" + "  build.gradle\n" + "  src\n" + "    main\n" + "      java\n" + "        test\n" + "          lib1\n" + "            pkg\n" + "              Library1.java\n" + "library2\n" + "  build.gradle\n" + "  src\n" + "    main\n" + "      java\n" + "        test\n" + "          lib2\n" + "            pkg\n" + "              Library2.java\n" + (getTestSdkPathLocal() != null ? "local.properties\n" : "") + "settings.gradle\n", fileTree(imported, true));
    // Let's peek at some of the key files to make sure we codegen'ed the right thing
    //noinspection PointlessBooleanExpression,ConstantConditions
    assertEquals("" + (!DECLARE_GLOBAL_REPOSITORIES ? "buildscript {\n" + "    repositories {\n" + "        " + MAVEN_REPOSITORY.replace(NL, "\n") + "\n" + "    }\n" + "}\n" : "") + "apply plugin: 'java'\n" + "\n" + "dependencies {\n" + "    compile 'com.google.guava:guava:13.0.1'\n" + "}\n", Files.toString(new File(imported, "library1" + separator + "build.gradle"), UTF_8).replace(NL, "\n"));
    //noinspection PointlessBooleanExpression,ConstantConditions
    assertEquals("" + (!DECLARE_GLOBAL_REPOSITORIES ? "buildscript {\n" + "    repositories {\n" + "        " + MAVEN_REPOSITORY.replace(NL, "\n") + "\n" + "    }\n" + "    dependencies {\n" + "        classpath '" + ANDROID_GRADLE_PLUGIN + "'\n" + "    }\n" + "}\n" : "") + "apply plugin: 'com.android.application'\n" + (!DECLARE_GLOBAL_REPOSITORIES ? "\n" + "repositories {\n" + "    " + MAVEN_REPOSITORY.replace(NL, "\n") + "\n" + "}\n" : "") + "\n" + "android {\n" + "    compileSdkVersion 22\n" + "    buildToolsVersion \"" + BUILD_TOOLS_VERSION + "\"\n" + "\n" + "    defaultConfig {\n" + "        applicationId \"test.pkg\"\n" + "        minSdkVersion 8\n" + "        targetSdkVersion 22\n" + "    }\n" + "\n" + "    buildTypes {\n" + "        release {\n" + "            minifyEnabled false\n" + "            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'\n" + "        }\n" + "    }\n" + "}\n" + "\n" + "dependencies {\n" + "    compile project(':androidLibrary')\n" + "}\n", Files.toString(new File(imported, "androidApp" + separator + "build.gradle"), UTF_8).replace(NL, "\n"));
    //noinspection PointlessBooleanExpression,ConstantConditions
    assertEquals("" + (!DECLARE_GLOBAL_REPOSITORIES ? "buildscript {\n" + "    repositories {\n" + "        " + MAVEN_REPOSITORY.replace(NL, "\n") + "\n" + "    }\n" + "    dependencies {\n" + "        classpath '" + ANDROID_GRADLE_PLUGIN + "'\n" + "    }\n" + "}\n" : "") + "apply plugin: 'com.android.library'\n" + (!DECLARE_GLOBAL_REPOSITORIES ? "\n" + "repositories {\n" + "    " + MAVEN_REPOSITORY.replace(NL, "\n") + "\n" + "}\n" : "") + "\n" + "android {\n" + "    compileSdkVersion 18\n" + "    buildToolsVersion \"" + BUILD_TOOLS_VERSION + "\"\n" + "\n" + "    defaultConfig {\n" + "        minSdkVersion 8\n" + "        targetSdkVersion 8\n" + "    }\n" + "\n" + "    buildTypes {\n" + "        release {\n" + "            minifyEnabled false\n" + "            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'\n" + "        }\n" + "    }\n" + "}\n" + "\n" + "dependencies {\n" + "    compile project(':library1')\n" + "    compile project(':library2')\n" + "}\n", Files.toString(new File(imported, "androidLibrary" + separator + "build.gradle"), UTF_8).replace(NL, "\n"));
    //noinspection PointlessBooleanExpression,ConstantConditions
    assertEquals("" + (!DECLARE_GLOBAL_REPOSITORIES ? "buildscript {\n" + "    repositories {\n" + "        " + MAVEN_REPOSITORY.replace(NL, "\n") + "\n" + "    }\n" + "}\n" : "") + "apply plugin: 'java'\n" + "\n" + "dependencies {\n" + "    compile 'com.google.guava:guava:13.0.1'\n" + "}\n", Files.toString(new File(imported, "library1" + separator + "build.gradle"), UTF_8).replace(NL, "\n"));
    //noinspection PointlessBooleanExpression,ConstantConditions
    assertEquals("" + (!DECLARE_GLOBAL_REPOSITORIES ? "buildscript {\n" + "    repositories {\n" + "        " + MAVEN_REPOSITORY.replace(NL, "\n") + "\n" + "    }\n" + "}\n" : "") + "apply plugin: 'java'\n" + "\n" + "sourceCompatibility = \"1.7\"\n" + "targetCompatibility = \"1.7\"\n" + "\n" + "dependencies {\n" + "    compile project(':library1')\n" + "}\n", Files.toString(new File(imported, "library2" + separator + "build.gradle"), UTF_8).replace(NL, "\n"));
    // TODO: Should this ONLY include the root module?
    assertEquals("" + "include ':library1'\n" + "include ':library2'\n" + "include ':androidLibrary'\n" + "include ':androidApp'\n", Files.toString(new File(imported, "settings.gradle"), UTF_8).replace(NL, "\n"));
    //noinspection ConstantConditions
    assertEquals("" + "// Top-level build file where you can add configuration options common to all sub-projects/modules.\n" + (DECLARE_GLOBAL_REPOSITORIES ? "buildscript {\n" + "    repositories {\n" + "        " + MAVEN_REPOSITORY.replace(NL, "\n") + "\n" + "    }\n" + "    dependencies {\n" + "        classpath '" + ANDROID_GRADLE_PLUGIN + "'\n" + "    }\n" + "}\n" + "\n" + "allprojects {\n" + "    repositories {\n" + "        " + MAVEN_REPOSITORY.replace(NL, "\n") + "\n" + "    }\n" + "}\n" : ""), Files.toString(new File(imported, "build.gradle"), UTF_8).replace(NL, "\n"));
    deleteDir(root);
    deleteDir(imported);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) GradleImport(com.android.tools.idea.gradle.eclipse.GradleImport) File(java.io.File)

Example 3 with GradleImport

use of com.android.tools.idea.gradle.eclipse.GradleImport in project android by JetBrains.

the class GradleImportTest method testSdkNdkSetters.

public void testSdkNdkSetters() {
    GradleImport importer = new GradleImport();
    File ndkLocation = new File("ndk");
    File sdkLocation = new File("sdk");
    importer.setNdkLocation(ndkLocation);
    importer.setSdkLocation(sdkLocation);
    assertSame(sdkLocation, importer.getSdkLocation());
    assertSame(ndkLocation, importer.getNdkLocation());
}
Also used : GradleImport(com.android.tools.idea.gradle.eclipse.GradleImport) File(java.io.File)

Example 4 with GradleImport

use of com.android.tools.idea.gradle.eclipse.GradleImport in project android by JetBrains.

the class AdtModuleImporter method importProjects.

@Override
public void importProjects(Map<String, VirtualFile> projects) {
    Project project = myContext.getProject();
    assert project != null;
    AdtImportBuilder builder = AdtImportBuilder.getBuilder(myContext);
    assert builder != null;
    GradleImport importer = getGradleImport();
    ImmutableMap.Builder<File, String> modules = ImmutableMap.builder();
    for (Map.Entry<String, VirtualFile> entry : projects.entrySet()) {
        modules.put(virtualToIoFile(entry.getValue()), getDefaultPhysicalPathFromGradlePath(entry.getKey()));
    }
    importer.setImportModuleNames(modules.build());
    if (builder.validate(null, project)) {
        builder.commit(project, null, ModulesProvider.EMPTY_MODULES_PROVIDER, null);
        builder.cleanup();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) AdtImportBuilder(com.android.tools.idea.gradle.eclipse.AdtImportBuilder) GradleImport(com.android.tools.idea.gradle.eclipse.GradleImport) 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) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 5 with GradleImport

use of com.android.tools.idea.gradle.eclipse.GradleImport in project android by JetBrains.

the class AdtModuleImporter method findModules.

@Override
@NotNull
public Set<ModuleToImport> findModules(@NotNull VirtualFile importSource) throws IOException {
    final AdtImportBuilder builder = (AdtImportBuilder) myContext.getProjectBuilder();
    assert builder != null;
    builder.setSelectedProject(virtualToIoFile(importSource));
    final GradleImport gradleImport = getGradleImport();
    gradleImport.importProjects(Collections.singletonList(virtualToIoFile(importSource)));
    Map<String, File> adtProjects = gradleImport.getDetectedModuleLocations();
    Set<ModuleToImport> modules = Sets.newHashSet();
    for (final Map.Entry<String, File> entry : adtProjects.entrySet()) {
        VirtualFile location = findFileByIoFile(entry.getValue(), false);
        modules.add(new ModuleToImport(entry.getKey(), location, new Supplier<Iterable<String>>() {

            @Override
            public Iterable<String> get() {
                return gradleImport.getProjectDependencies(entry.getKey());
            }
        }));
    }
    return modules;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AdtImportBuilder(com.android.tools.idea.gradle.eclipse.AdtImportBuilder) GradleImport(com.android.tools.idea.gradle.eclipse.GradleImport) Supplier(com.google.common.base.Supplier) 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) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GradleImport (com.android.tools.idea.gradle.eclipse.GradleImport)15 File (java.io.File)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 AdtImportBuilder (com.android.tools.idea.gradle.eclipse.AdtImportBuilder)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 VfsUtil.findFileByIoFile (com.intellij.openapi.vfs.VfsUtil.findFileByIoFile)2 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 Map (java.util.Map)2 NotNull (org.jetbrains.annotations.NotNull)2 Supplier (com.google.common.base.Supplier)1 Project (com.intellij.openapi.project.Project)1