Files
kotlin-fork/idea/tests/org/jetbrains/kotlin/test/ConfigLibraryUtil.java
T

157 lines
6.2 KiB
Java

/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.test;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.*;
import com.intellij.openapi.roots.impl.libraries.LibraryEx;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.ui.configuration.libraryEditor.NewLibraryEditor;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VfsUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.utils.PathUtil;
import java.io.File;
/**
* Helper for configuring kotlin runtime in tested project.
*/
public class ConfigLibraryUtil {
private static final String DEFAULT_JAVA_RUNTIME_LIB_NAME = "JAVA_RUNTIME_LIB_NAME";
private static final String DEFAULT_KOTLIN_JS_STDLIB_NAME = "KOTLIN_JS_STDLIB_NAME";
private ConfigLibraryUtil() {
}
private static NewLibraryEditor getKotlinRuntimeLibEditor(String libName, File library) {
NewLibraryEditor editor = new NewLibraryEditor();
editor.setName(libName);
editor.addRoot(VfsUtil.getUrlForLibraryRoot(library), OrderRootType.CLASSES);
return editor;
}
public static void configureKotlinRuntime(Module module, Sdk sdk) {
configureLibrary(module, sdk, getKotlinRuntimeLibEditor(DEFAULT_JAVA_RUNTIME_LIB_NAME, PathUtil.getKotlinPathsForDistDirectory().getRuntimePath()));
}
public static void configureKotlinJsRuntime(Module module, Sdk sdk) {
configureLibrary(module, sdk, getKotlinRuntimeLibEditor(DEFAULT_KOTLIN_JS_STDLIB_NAME, PathUtil.getKotlinPathsForDistDirectory().getJsStdLibJarPath()));
}
public static void unConfigureKotlinRuntime(Module module, Sdk sdk) {
unConfigureLibrary(module, sdk, DEFAULT_JAVA_RUNTIME_LIB_NAME);
}
public static void unConfigureKotlinJsRuntime(Module module, Sdk sdk) {
unConfigureLibrary(module, sdk, DEFAULT_KOTLIN_JS_STDLIB_NAME);
}
public static void configureLibrary(Module module, Sdk sdk, NewLibraryEditor libraryEditor) {
configureSdk(module, sdk);
addLibrary(libraryEditor, module);
}
public static void unConfigureLibrary(Module module, Sdk sdk, String libraryName) {
configureSdk(module, sdk);
removeLibrary(module, libraryName);
}
public static void configureSdk(@NotNull final Module module, @NotNull final Sdk sdk) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
ModifiableRootModel rootModel = rootManager.getModifiableModel();
rootModel.setSdk(sdk);
rootModel.commit();
}
});
}
public static Library addLibrary(final NewLibraryEditor editor, final Module module) {
return ApplicationManager.getApplication().runWriteAction(new Computable<Library>() {
@Override
public Library compute() {
ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
ModifiableRootModel model = rootManager.getModifiableModel();
Library library = addLibrary(editor, model);
model.commit();
return library;
}
});
}
public static Library addLibrary(NewLibraryEditor editor, ModifiableRootModel model) {
Library library = model.getModuleLibraryTable().createLibrary(editor.getName());
Library.ModifiableModel libModel = library.getModifiableModel();
editor.applyTo((LibraryEx.ModifiableModelEx) libModel);
libModel.commit();
return library;
}
public static void removeLibrary(@NotNull final Module module, @NotNull final String libraryName) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
ModifiableRootModel model = rootManager.getModifiableModel();
for (OrderEntry orderEntry : model.getOrderEntries()) {
if (orderEntry instanceof LibraryOrderEntry) {
LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) orderEntry;
Library library = libraryOrderEntry.getLibrary();
if (library != null) {
String name = library.getName();
if (name != null && name.equals(libraryName)) {
// Dispose attached roots
Library.ModifiableModel modifiableModel = library.getModifiableModel();
for (String rootUrl : library.getRootProvider().getUrls(OrderRootType.CLASSES)) {
modifiableModel.removeRoot(rootUrl, OrderRootType.CLASSES);
}
for (String rootUrl : library.getRootProvider().getUrls(OrderRootType.SOURCES)) {
modifiableModel.removeRoot(rootUrl, OrderRootType.SOURCES);
}
modifiableModel.commit();
model.getModuleLibraryTable().removeLibrary(library);
break;
}
}
}
}
model.commit();
}
});
}
}