Search in sources :

Example 21 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.

the class ExternalAnnotationsSupport method addAnnotations.

public static void addAnnotations(@NotNull Sdk sdk) {
    SdkModificator modifier = sdk.getSdkModificator();
    attachJdkAnnotations(modifier);
    modifier.commitChanges();
}
Also used : SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 22 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.

the class GradleSpecificInitializer method checkAndSetSources.

private static void checkAndSetSources(@NotNull Sdk sdk) {
    VirtualFile[] storedSources = sdk.getRootProvider().getFiles(OrderRootType.SOURCES);
    if (storedSources.length > 0) {
        return;
    }
    AndroidPlatform platform = AndroidPlatform.getInstance(sdk);
    if (platform != null) {
        SdkModificator sdkModificator = sdk.getSdkModificator();
        IAndroidTarget target = platform.getTarget();
        AndroidSdks.getInstance().findAndSetPlatformSources(target, sdkModificator);
        sdkModificator.commitChanges();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) AndroidSdkUtils.createNewAndroidPlatform(org.jetbrains.android.sdk.AndroidSdkUtils.createNewAndroidPlatform) IAndroidTarget(com.android.sdklib.IAndroidTarget) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 23 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.

the class GradleSpecificInitializer method setupSdks.

private static void setupSdks() {
    IdeSdks ideSdks = IdeSdks.getInstance();
    File androidHome = ideSdks.getAndroidSdkPath();
    if (androidHome != null) {
        String androidHomePath = androidHome.getAbsolutePath();
        ValidationResult result = validateLocation(androidHomePath, "Android SDK location", false, WritableCheckMode.DO_NOT_CHECK);
        if (result.isError()) {
            notifyInvalidSdk();
        }
        // Do not prompt user to select SDK path (we have one already.) Instead, check SDK compatibility when a project is opened.
        return;
    }
    // If running in a GUI test we don't want the "Select SDK" dialog to show up when running GUI tests.
    if (AndroidPlugin.isGuiTestingMode()) {
        // This is good enough. Later on in the GUI test we'll validate the given SDK path.
        return;
    }
    Sdk sdk = findFirstCompatibleAndroidSdk();
    if (sdk != null) {
        String sdkHomePath = sdk.getHomePath();
        assert sdkHomePath != null;
        ideSdks.createAndroidSdkPerAndroidTarget(new File(toSystemDependentName(sdkHomePath)));
        return;
    }
    // Called in a 'invokeLater' block, otherwise file chooser will hang forever.
    ApplicationManager.getApplication().invokeLater(() -> {
        File androidSdkPath = getAndroidSdkPath();
        if (androidSdkPath == null) {
            return;
        }
        FirstRunWizardMode wizardMode = AndroidStudioWelcomeScreenProvider.getWizardMode();
        // Only show "Select SDK" dialog if the "First Run" wizard is not displayed.
        boolean promptSdkSelection = wizardMode == null;
        Sdk sdk1 = createNewAndroidPlatform(androidSdkPath.getPath(), promptSdkSelection);
        if (sdk1 != null) {
            // Rename the SDK to fit our default naming convention.
            String sdkNamePrefix = AndroidSdks.SDK_NAME_PREFIX;
            if (sdk1.getName().startsWith(sdkNamePrefix)) {
                SdkModificator sdkModificator = sdk1.getSdkModificator();
                sdkModificator.setName(sdkNamePrefix + sdk1.getName().substring(sdkNamePrefix.length()));
                sdkModificator.commitChanges();
                // Rename the JDK that goes along with this SDK.
                AndroidSdkAdditionalData additionalData = AndroidSdks.getInstance().getAndroidSdkAdditionalData(sdk1);
                if (additionalData != null) {
                    Sdk jdk = additionalData.getJavaSdk();
                    if (jdk != null) {
                        sdkModificator = jdk.getSdkModificator();
                        sdkModificator.setName(DEFAULT_JDK_NAME);
                        sdkModificator.commitChanges();
                    }
                }
                // Fill out any missing build APIs for this new SDK.
                ideSdks.createAndroidSdkPerAndroidTarget(androidSdkPath);
            }
        }
    });
}
Also used : IdeSdks(com.android.tools.idea.sdk.IdeSdks) AndroidSdkAdditionalData(org.jetbrains.android.sdk.AndroidSdkAdditionalData) FirstRunWizardMode(com.android.tools.idea.welcome.config.FirstRunWizardMode) Sdk(com.intellij.openapi.projectRoots.Sdk) ValidationResult(com.android.tools.idea.npw.WizardUtils.ValidationResult) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 24 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.

the class AndroidSdkSourcesBrowsingTest method initializeDummyProject.

/**
   * This method prepares a simple project which can be used by tests to confirm that the project
   * was hooked up in expected ways. When finished, it returns the full path to the source
   * directory under the project, which can be useful for finding source files in the project.
   *
   * <p>If a test only cares about a single source file that references the SDK directly, then this
   * method is not needed.
   */
private String initializeDummyProject() {
    VirtualFile sourcesDir = myFixture.copyDirectoryToProject('/' + DUMMY_PROJECT_PATH, "/src");
    VirtualFile classesJar = JarFileSystem.getInstance().findFileByPath(sourcesDir.getPath() + "/" + DUMMY_CLASSES_JAR + "!/");
    Sdk sdk = ModuleRootManager.getInstance(myFixture.getModule()).getSdk();
    SdkModificator modificator = sdk.getSdkModificator();
    modificator.addRoot(sourcesDir, OrderRootType.SOURCES);
    modificator.addRoot(classesJar, OrderRootType.CLASSES);
    modificator.commitChanges();
    return sourcesDir.getPath();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Sdk(com.intellij.openapi.projectRoots.Sdk) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 25 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project intellij-plugins by JetBrains.

the class BndProjectImporter method addEntry.

private void addEntry(ModifiableModuleModel moduleModel, LibraryTable.ModifiableModel libraryModel, ModifiableRootModel rootModel, Container dependency, DependencyScope scope) throws IllegalArgumentException {
    File file = dependency.getFile();
    String bsn = dependency.getBundleSymbolicName();
    String version = dependency.getVersion();
    String path = file.getPath();
    if (path.contains(": ")) {
        throw new IllegalArgumentException("Cannot resolve " + bsn + ":" + version + ": " + path);
    }
    if (JDK_DEPENDENCY.equals(bsn)) {
        String name = BND_LIB_PREFIX + bsn + ":" + version;
        if (FileUtil.isAncestor(myWorkspace.getBase(), file, true)) {
            name += "-" + myProject.getName();
        }
        ProjectJdkTable jdkTable = ProjectJdkTable.getInstance();
        Sdk jdk = jdkTable.findJdk(name);
        if (jdk == null) {
            jdk = jdkTable.createSdk(name, JavaSdk.getInstance());
            SdkModificator jdkModel = jdk.getSdkModificator();
            jdkModel.setHomePath(file.getParent());
            jdkModel.setVersionString(version);
            VirtualFile root = VirtualFileManager.getInstance().findFileByUrl(url(file));
            assert root != null : file + " " + file.exists();
            jdkModel.addRoot(root, OrderRootType.CLASSES);
            VirtualFile srcRoot = VirtualFileManager.getInstance().findFileByUrl(url(file) + SRC_ROOT);
            if (srcRoot != null)
                jdkModel.addRoot(srcRoot, OrderRootType.SOURCES);
            jdkModel.commitChanges();
            jdkTable.addJdk(jdk);
        }
        rootModel.setSdk(jdk);
        return;
    }
    ExportableOrderEntry entry;
    switch(dependency.getType()) {
        case PROJECT:
            {
                String name = dependency.getProject().getName();
                Module module = moduleModel.findModuleByName(name);
                if (module == null) {
                    throw new IllegalArgumentException("Unknown module '" + name + "'");
                }
                entry = (ModuleOrderEntry) ContainerUtil.find(rootModel.getOrderEntries(), e -> e instanceof ModuleOrderEntry && ((ModuleOrderEntry) e).getModule() == module);
                if (entry == null) {
                    entry = rootModel.addModuleOrderEntry(module);
                }
                break;
            }
        case REPO:
            {
                String name = BND_LIB_PREFIX + bsn + ":" + version;
                Library library = libraryModel.getLibraryByName(name);
                if (library == null) {
                    library = libraryModel.createLibrary(name);
                }
                Library.ModifiableModel model = library.getModifiableModel();
                for (String url : model.getUrls(OrderRootType.CLASSES)) model.removeRoot(url, OrderRootType.CLASSES);
                for (String url : model.getUrls(OrderRootType.SOURCES)) model.removeRoot(url, OrderRootType.SOURCES);
                model.addRoot(url(file), OrderRootType.CLASSES);
                String srcRoot = mySourcesMap.get(path);
                if (srcRoot != null) {
                    model.addRoot(url(file) + srcRoot, OrderRootType.SOURCES);
                }
                model.commit();
                entry = rootModel.addLibraryEntry(library);
                break;
            }
        case EXTERNAL:
            {
                Library library = rootModel.getModuleLibraryTable().createLibrary(file.getName());
                Library.ModifiableModel model = library.getModifiableModel();
                model.addRoot(url(file), OrderRootType.CLASSES);
                String srcRoot = mySourcesMap.get(path);
                if (srcRoot != null) {
                    model.addRoot(url(file) + srcRoot, OrderRootType.SOURCES);
                }
                model.commit();
                entry = rootModel.findLibraryOrderEntry(library);
                assert entry != null : library;
                break;
            }
        default:
            throw new IllegalArgumentException("Unknown dependency '" + dependency + "' of type " + dependency.getType());
    }
    entry.setScope(scope);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileUtilRt(com.intellij.openapi.util.io.FileUtilRt) OsmorcFacetType(org.osmorc.facet.OsmorcFacetType) CompilerConfiguration(com.intellij.compiler.CompilerConfiguration) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) FacetUtil(com.intellij.facet.impl.FacetUtil) Refreshable(aQute.bnd.service.Refreshable) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) VirtualFileManager(com.intellij.openapi.vfs.VirtualFileManager) Library(com.intellij.openapi.roots.libraries.Library) Task(com.intellij.openapi.progress.Task) OsmorcFacet(org.osmorc.facet.OsmorcFacet) ZipFile(java.util.zip.ZipFile) FileUtil(com.intellij.openapi.util.io.FileUtil) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) ZipEntry(java.util.zip.ZipEntry) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) JpsJavaCompilerOptions(org.jetbrains.jps.model.java.compiler.JpsJavaCompilerOptions) LanguageLevel(com.intellij.pom.java.LanguageLevel) StdModuleTypes(com.intellij.openapi.module.StdModuleTypes) ModifiableModelCommitter(com.intellij.openapi.roots.impl.ModifiableModelCommitter) ModifiableModuleModel(com.intellij.openapi.module.ModifiableModuleModel) OutputPathType(org.jetbrains.osgi.jps.model.OutputPathType) NotificationType(com.intellij.notification.NotificationType) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProjectJdkTable(com.intellij.openapi.projectRoots.ProjectJdkTable) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) NotificationDisplayType(com.intellij.notification.NotificationDisplayType) java.util(java.util) OsmorcFacetConfiguration(org.osmorc.facet.OsmorcFacetConfiguration) ModuleManager(com.intellij.openapi.module.ModuleManager) ContainerUtil(com.intellij.util.containers.ContainerUtil) Container(aQute.bnd.build.Container) com.intellij.openapi.roots(com.intellij.openapi.roots) Workspace(aQute.bnd.build.Workspace) NotificationGroup(com.intellij.notification.NotificationGroup) ManifestGenerationMode(org.jetbrains.osgi.jps.model.ManifestGenerationMode) OsmorcBundle.message(org.osmorc.i18n.OsmorcBundle.message) Project(aQute.bnd.build.Project) StringUtil(com.intellij.openapi.util.text.StringUtil) JavacConfiguration(com.intellij.compiler.impl.javaCompiler.javac.JavacConfiguration) Key(com.intellij.openapi.util.Key) IOException(java.io.IOException) Sdk(com.intellij.openapi.projectRoots.Sdk) File(java.io.File) Attrs(aQute.bnd.header.Attrs) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) VfsUtil(com.intellij.openapi.vfs.VfsUtil) ObjectUtils(com.intellij.util.ObjectUtils) PathUtil(com.intellij.util.PathUtil) ModuleFileType(com.intellij.ide.highlighter.ModuleFileType) Condition(com.intellij.openapi.util.Condition) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) ProjectJdkTable(com.intellij.openapi.projectRoots.ProjectJdkTable) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)51 Sdk (com.intellij.openapi.projectRoots.Sdk)32 VirtualFile (com.intellij.openapi.vfs.VirtualFile)30 File (java.io.File)8 Module (com.intellij.openapi.module.Module)7 IAndroidTarget (com.android.sdklib.IAndroidTarget)6 AndroidSdkAdditionalData (org.jetbrains.android.sdk.AndroidSdkAdditionalData)5 NotNull (org.jetbrains.annotations.NotNull)5 ProjectJdkTable (com.intellij.openapi.projectRoots.ProjectJdkTable)4 Library (com.intellij.openapi.roots.libraries.Library)4 Nullable (org.jetbrains.annotations.Nullable)4 JSTestOptions (com.intellij.lang.javascript.JSTestOptions)3 Project (com.intellij.openapi.project.Project)3 ProjectJdkImpl (com.intellij.openapi.projectRoots.impl.ProjectJdkImpl)3 FirstRunWizardMode (com.android.tools.idea.welcome.config.FirstRunWizardMode)2 FlexSdkType2 (com.intellij.lang.javascript.flex.sdk.FlexSdkType2)2 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)2 IOException (java.io.IOException)2 AndroidSdkData (org.jetbrains.android.sdk.AndroidSdkData)2 Contract (org.jetbrains.annotations.Contract)2