Configure Kotlin for Android: add kotlin plugin to project build.gradle file.
Do not add mavenCentral dependency if jcenter is present
This commit is contained in:
@@ -33,5 +33,6 @@
|
||||
<orderEntry type="module" module-name="container" scope="TEST" />
|
||||
<orderEntry type="library" scope="TEST" name="kotlin-test" level="project" />
|
||||
<orderEntry type="module" module-name="build-common" scope="TEST" />
|
||||
<orderEntry type="module" module-name="kotlin-android-plugin" scope="TEST" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.android.AbstractAndroidCompletionTest
|
||||
import org.jetbrains.kotlin.android.AbstractAndroidFindUsagesTest
|
||||
import org.jetbrains.kotlin.android.AbstractAndroidGotoTest
|
||||
import org.jetbrains.kotlin.android.AbstractAndroidRenameTest
|
||||
import org.jetbrains.kotlin.android.configure.AbstractConfigureProjectTest
|
||||
import org.jetbrains.kotlin.annotation.AbstractAnnotationProcessorBoxTest
|
||||
import org.jetbrains.kotlin.asJava.AbstractCompilerLightClassTest
|
||||
import org.jetbrains.kotlin.cfg.AbstractControlFlowTest
|
||||
@@ -568,7 +569,6 @@ fun main(args: Array<String>) {
|
||||
}
|
||||
|
||||
testClass<AbstractConfigureProjectByChangingFileTest>() {
|
||||
model("configuration/android-gradle", pattern = """(\w+)_before\.gradle$""", testMethod = "doTestAndroidGradle")
|
||||
model("configuration/gradle", pattern = """(\w+)_before\.gradle$""", testMethod = "doTestGradle")
|
||||
model("configuration/maven", extension = null, recursive = false, testMethod = "doTestWithMaven")
|
||||
model("configuration/js-maven", extension = null, recursive = false, testMethod = "doTestWithJSMaven")
|
||||
@@ -974,6 +974,12 @@ fun main(args: Array<String>) {
|
||||
}
|
||||
}
|
||||
|
||||
testGroup("idea/kotlin-android-plugin/tests", "idea/testData") {
|
||||
testClass<AbstractConfigureProjectTest>() {
|
||||
model("configuration/android-gradle", pattern = """(\w+)_before\.gradle$""", testMethod = "doTestAndroidGradle")
|
||||
}
|
||||
}
|
||||
|
||||
testGroup("plugins/android-jps-plugin/tests", "plugins/android-jps-plugin/testData") {
|
||||
testClass<AbstractAndroidJpsTestCase>() {
|
||||
model("android", recursive = false, extension = null)
|
||||
|
||||
@@ -38,7 +38,6 @@
|
||||
<orderEntry type="module" module-name="eval4j" />
|
||||
<orderEntry type="module" module-name="idea-core" />
|
||||
<orderEntry type="module" module-name="idea-analysis" exported="" />
|
||||
<orderEntry type="module" module-name="kotlin-android-plugin" />
|
||||
<orderEntry type="module" module-name="js.frontend" />
|
||||
<orderEntry type="library" scope="PROVIDED" name="coverage-plugin" level="project" />
|
||||
<orderEntry type="module" module-name="util" />
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
@@ -13,5 +14,8 @@
|
||||
<orderEntry type="module" module-name="util" />
|
||||
<orderEntry type="module" module-name="idea-analysis" />
|
||||
<orderEntry type="module" module-name="idea" />
|
||||
<orderEntry type="module" module-name="frontend.java" />
|
||||
<orderEntry type="library" name="gradle-and-groovy-plugin" level="project" />
|
||||
<orderEntry type="module" module-name="compiler-tests" scope="TEST" />
|
||||
</component>
|
||||
</module>
|
||||
+14
-2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
* Copyright 2010-2016 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.
|
||||
@@ -14,11 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.configuration;
|
||||
package org.jetbrains.kotlin.android.configure;
|
||||
|
||||
import com.intellij.openapi.module.Module;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.idea.KotlinPluginUtil;
|
||||
import org.jetbrains.kotlin.idea.configuration.KotlinWithGradleConfigurator;
|
||||
import org.jetbrains.kotlin.resolve.TargetPlatform;
|
||||
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatform;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
|
||||
@@ -63,6 +64,17 @@ public class KotlinAndroidGradleModuleConfigurator extends KotlinWithGradleConfi
|
||||
addLastExpressionInBlockIfNeeded(SOURCE_SET, getSourceSetsBlock(androidBlock));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean addElementsToFile(@NotNull GroovyFile groovyFile, boolean isTopLevelProjectFile, @NotNull String version) {
|
||||
if (isTopLevelProjectFile) {
|
||||
addElementsToProjectFile(groovyFile, version);
|
||||
}
|
||||
else {
|
||||
addElementsToModuleFile(groovyFile, version);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static GrClosableBlock getAndroidBlock(@NotNull GroovyFile file) {
|
||||
return getBlockOrCreate(file, "android");
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.android.configure;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.idea.configuration.AbstractConfigureProjectByChangingFileTest;
|
||||
|
||||
public abstract class AbstractConfigureProjectTest extends AbstractConfigureProjectByChangingFileTest {
|
||||
public void doTestAndroidGradle(@NotNull String path) throws Exception {
|
||||
doTest(path, path.replace("before", "after"), new KotlinAndroidGradleModuleConfigurator());
|
||||
}
|
||||
}
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.android.configure;
|
||||
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.test.TestMetadata;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
||||
@SuppressWarnings("all")
|
||||
@TestMetadata("idea/testData/configuration/android-gradle")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public class ConfigureProjectTestGenerated extends AbstractConfigureProjectTest {
|
||||
public void testAllFilesPresentInAndroid_gradle() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/configuration/android-gradle"), Pattern.compile("(\\w+)_before\\.gradle$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("androidStudioDefault_before.gradle")
|
||||
public void testAndroidStudioDefault() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/androidStudioDefault_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("androidStudioDefaultShapshot_before.gradle")
|
||||
public void testAndroidStudioDefaultShapshot() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/androidStudioDefaultShapshot_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("buildConfigs_before.gradle")
|
||||
public void testBuildConfigs() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/buildConfigs_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("emptyDependencyList_before.gradle")
|
||||
public void testEmptyDependencyList() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/emptyDependencyList_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("emptyFile_before.gradle")
|
||||
public void testEmptyFile() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/emptyFile_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("helloWorld_before.gradle")
|
||||
public void testHelloWorld() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/helloWorld_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("libraryFile_before.gradle")
|
||||
public void testLibraryFile() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/libraryFile_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("missedApplyAndroidStatement_before.gradle")
|
||||
public void testMissedApplyAndroidStatement() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/missedApplyAndroidStatement_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("missedBuildscriptBlock_before.gradle")
|
||||
public void testMissedBuildscriptBlock() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/missedBuildscriptBlock_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("missedRepositoriesInBuildscriptBlock_before.gradle")
|
||||
public void testMissedRepositoriesInBuildscriptBlock() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/missedRepositoriesInBuildscriptBlock_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("productFlavor_before.gradle")
|
||||
public void testProductFlavor() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/productFlavor_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/configuration/android-gradle/gradleExamples")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class GradleExamples extends AbstractConfigureProjectTest {
|
||||
public void testAllFilesPresentInGradleExamples() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/configuration/android-gradle/gradleExamples"), Pattern.compile("(\\w+)_before\\.gradle$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample0_before.gradle")
|
||||
public void testGradleExample0() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample0_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample18_before.gradle")
|
||||
public void testGradleExample18() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample18_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample22_before.gradle")
|
||||
public void testGradleExample22() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample22_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample44_before.gradle")
|
||||
public void testGradleExample44() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample44_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample5_before.gradle")
|
||||
public void testGradleExample5() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample5_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample50_before.gradle")
|
||||
public void testGradleExample50() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample50_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample58_before.gradle")
|
||||
public void testGradleExample58() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample58_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample65_before.gradle")
|
||||
public void testGradleExample65() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample65_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample8_before.gradle")
|
||||
public void testGradleExample8() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample8_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,8 @@
|
||||
<packageFragmentProviderExtension implementation="org.jetbrains.kotlin.android.synthetic.idea.res.IDEAndroidPackageFragmentProviderExtension"/>
|
||||
<simpleNameReferenceExtension implementation="org.jetbrains.kotlin.android.synthetic.idea.AndroidSimpleNameReferenceExtension"/>
|
||||
<kotlinIndicesHelperExtension implementation="org.jetbrains.kotlin.android.synthetic.idea.AndroidIndicesHelperExtension"/>
|
||||
|
||||
<projectConfigurator implementation="org.jetbrains.kotlin.android.configure.KotlinAndroidGradleModuleConfigurator"/>
|
||||
</extensions>
|
||||
|
||||
<project-components>
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<idea-plugin>
|
||||
<extensions defaultExtensionNs="org.jetbrains.kotlin">
|
||||
<projectConfigurator implementation="org.jetbrains.kotlin.idea.configuration.KotlinAndroidGradleModuleConfigurator"/>
|
||||
<projectConfigurator implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleModuleConfigurator"/>
|
||||
</extensions>
|
||||
<extensions defaultExtensionNs="org.jetbrains.plugins.gradle">
|
||||
|
||||
@@ -64,6 +64,16 @@ public class KotlinGradleModuleConfigurator extends KotlinWithGradleConfigurator
|
||||
addLastExpressionInBlockIfNeeded(SOURCE_SET, sourceSetBlock);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean addElementsToFile(@NotNull GroovyFile groovyFile, boolean isTopLevelProjectFile, @NotNull String version) {
|
||||
if (!isTopLevelProjectFile) {
|
||||
addElementsToProjectFile(groovyFile, version);
|
||||
addElementsToModuleFile(groovyFile, version);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
KotlinGradleModuleConfigurator() {
|
||||
}
|
||||
}
|
||||
|
||||
+63
-19
@@ -49,13 +49,16 @@ import org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class KotlinWithGradleConfigurator implements KotlinProjectConfigurator {
|
||||
protected static final String VERSION_TEMPLATE = "$VERSION$";
|
||||
|
||||
protected static final String CLASSPATH = "classpath \"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version\"";
|
||||
protected static final String SNAPSHOT_REPOSITORY = "maven {\nurl 'http://oss.sonatype.org/content/repositories/snapshots'\n}";
|
||||
public static final String REPOSITORY = "mavenCentral()\n";
|
||||
public static final String MAVEN_CENTRAL = "mavenCentral()\n";
|
||||
public static final String JCENTER = "jcenter()\n";
|
||||
public static final String LIBRARY = "compile \"org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version\"";
|
||||
protected static final String SOURCE_SET = "main.java.srcDirs += 'src/main/kotlin'\n";
|
||||
protected static final String VERSION = String.format("ext.kotlin_version = '%s'", VERSION_TEMPLATE);
|
||||
@@ -66,11 +69,11 @@ public abstract class KotlinWithGradleConfigurator implements KotlinProjectConfi
|
||||
return true;
|
||||
}
|
||||
|
||||
GroovyFile moduleGradleFile = getBuildGradleFile(module.getProject(), getDefaultPathToBuildGradleFile(module));
|
||||
GroovyFile moduleGradleFile = getBuildGradleFile(module.getProject(), getModuleFilePath(module));
|
||||
if (moduleGradleFile != null && isFileConfigured(moduleGradleFile)) {
|
||||
return true;
|
||||
}
|
||||
GroovyFile projectGradleFile = getBuildGradleFile(module.getProject(), getDefaultPathToBuildGradleFile(module.getProject()));
|
||||
GroovyFile projectGradleFile = getBuildGradleFile(module.getProject(), getTopLevelProjectFilePath(module.getProject()));
|
||||
return projectGradleFile != null && isFileConfigured(projectGradleFile);
|
||||
}
|
||||
|
||||
@@ -88,22 +91,36 @@ public abstract class KotlinWithGradleConfigurator implements KotlinProjectConfi
|
||||
if (!dialog.isOK()) return;
|
||||
|
||||
NotificationMessageCollector collector = NotificationMessageCollectorKt.createConfigureKotlinNotificationCollector(project);
|
||||
Set<GroovyFile> changedFiles = new HashSet<GroovyFile>();
|
||||
GroovyFile projectGradleFile = getBuildGradleFile(project, getTopLevelProjectFilePath(project));
|
||||
if (projectGradleFile != null && canConfigureFile(projectGradleFile)) {
|
||||
boolean isModified = changeGradleFile(projectGradleFile, true, dialog.getKotlinVersion(), collector);
|
||||
if (isModified) {
|
||||
changedFiles.add(projectGradleFile);
|
||||
}
|
||||
}
|
||||
|
||||
for (Module module : dialog.getModulesToConfigure()) {
|
||||
String gradleFilePath = getDefaultPathToBuildGradleFile(module);
|
||||
GroovyFile file = getBuildGradleFile(project, gradleFilePath);
|
||||
GroovyFile file = getBuildGradleFile(project, getModuleFilePath(module));
|
||||
if (file != null && canConfigureFile(file)) {
|
||||
changeGradleFile(file, dialog.getKotlinVersion(), collector);
|
||||
OpenFileAction.openFile(gradleFilePath, project);
|
||||
boolean isModified = changeGradleFile(file, false, dialog.getKotlinVersion(), collector);
|
||||
if (isModified) {
|
||||
changedFiles.add(file);
|
||||
}
|
||||
}
|
||||
else {
|
||||
showErrorMessage(project, "Cannot find build.gradle file for module " + module.getName());
|
||||
}
|
||||
}
|
||||
|
||||
for (GroovyFile file : changedFiles) {
|
||||
OpenFileAction.openFile(file.getVirtualFile(), project);
|
||||
}
|
||||
collector.showNotification();
|
||||
}
|
||||
|
||||
public static void addKotlinLibraryToModule(final Module module, final DependencyScope scope, final ExternalLibraryDescriptor libraryDescriptor) {
|
||||
String gradleFilePath = getDefaultPathToBuildGradleFile(module);
|
||||
String gradleFilePath = getModuleFilePath(module);
|
||||
final GroovyFile gradleFile = getBuildGradleFile(module.getProject(), gradleFilePath);
|
||||
|
||||
if (gradleFile != null && canConfigureFile(gradleFile)) {
|
||||
@@ -156,7 +173,7 @@ public abstract class KotlinWithGradleConfigurator implements KotlinProjectConfi
|
||||
|
||||
@Nullable
|
||||
public static String getKotlinStdlibVersion(@NotNull Module module) {
|
||||
String gradleFilePath = getDefaultPathToBuildGradleFile(module);
|
||||
String gradleFilePath = getModuleFilePath(module);
|
||||
GroovyFile gradleFile = getBuildGradleFile(module.getProject(), gradleFilePath);
|
||||
|
||||
if (gradleFile == null) return null;
|
||||
@@ -181,7 +198,7 @@ public abstract class KotlinWithGradleConfigurator implements KotlinProjectConfi
|
||||
return null;
|
||||
}
|
||||
|
||||
private void addElements(@NotNull GroovyFile file, @NotNull String version) {
|
||||
protected void addElementsToProjectFile(@NotNull GroovyFile file, @NotNull String version) {
|
||||
GrClosableBlock buildScriptBlock = getBuildScriptBlock(file);
|
||||
addFirstExpressionInBlockIfNeeded(VERSION.replace(VERSION_TEMPLATE, version), buildScriptBlock);
|
||||
|
||||
@@ -189,13 +206,15 @@ public abstract class KotlinWithGradleConfigurator implements KotlinProjectConfi
|
||||
if (isSnapshot(version)) {
|
||||
addLastExpressionInBlockIfNeeded(SNAPSHOT_REPOSITORY, buildScriptRepositoriesBlock);
|
||||
}
|
||||
else if (!buildScriptRepositoriesBlock.getText().contains(REPOSITORY)) {
|
||||
addLastExpressionInBlockIfNeeded(REPOSITORY, buildScriptRepositoriesBlock);
|
||||
else if (!isRepositoryConfigured(buildScriptRepositoriesBlock)) {
|
||||
addLastExpressionInBlockIfNeeded(MAVEN_CENTRAL, buildScriptRepositoriesBlock);
|
||||
}
|
||||
|
||||
GrClosableBlock buildScriptDependenciesBlock = getBuildScriptDependenciesBlock(file);
|
||||
addLastExpressionInBlockIfNeeded(CLASSPATH, buildScriptDependenciesBlock);
|
||||
}
|
||||
|
||||
protected void addElementsToModuleFile(@NotNull GroovyFile file, @NotNull String version) {
|
||||
if (!file.getText().contains(getApplyPluginDirective())) {
|
||||
GrExpression apply = GroovyPsiElementFactory.getInstance(file.getProject()).createExpressionFromText(getApplyPluginDirective());
|
||||
GrApplicationStatement applyStatement = getApplyStatement(file);
|
||||
@@ -203,7 +222,19 @@ public abstract class KotlinWithGradleConfigurator implements KotlinProjectConfi
|
||||
file.addAfter(apply, applyStatement);
|
||||
}
|
||||
else {
|
||||
file.addAfter(apply, buildScriptBlock.getParent());
|
||||
GrClosableBlock buildScript = getBlockByName(file, "buildscript");
|
||||
if (buildScript != null) {
|
||||
file.addAfter(apply, buildScript.getParent());
|
||||
}
|
||||
else {
|
||||
GrStatement[] statements = file.getStatements();
|
||||
if (statements.length > 0) {
|
||||
file.addAfter(apply, statements[statements.length - 1]);
|
||||
}
|
||||
else {
|
||||
file.addAfter(apply, file.getFirstChild());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,8 +242,8 @@ public abstract class KotlinWithGradleConfigurator implements KotlinProjectConfi
|
||||
if (isSnapshot(version)) {
|
||||
addLastExpressionInBlockIfNeeded(SNAPSHOT_REPOSITORY, repositoriesBlock);
|
||||
}
|
||||
else if (!repositoriesBlock.getText().contains(REPOSITORY)) {
|
||||
addLastExpressionInBlockIfNeeded(REPOSITORY, repositoriesBlock);
|
||||
else if (!isRepositoryConfigured(repositoriesBlock)) {
|
||||
addLastExpressionInBlockIfNeeded(MAVEN_CENTRAL, repositoriesBlock);
|
||||
}
|
||||
|
||||
GrClosableBlock dependenciesBlock = getDependenciesBlock(file);
|
||||
@@ -221,10 +252,20 @@ public abstract class KotlinWithGradleConfigurator implements KotlinProjectConfi
|
||||
addSourceSetsBlock(file);
|
||||
}
|
||||
|
||||
private boolean isRepositoryConfigured(GrClosableBlock repositoriesBlock) {
|
||||
return repositoriesBlock.getText().contains(MAVEN_CENTRAL) || repositoriesBlock.getText().contains(JCENTER);
|
||||
}
|
||||
|
||||
protected abstract String getApplyPluginDirective();
|
||||
|
||||
protected abstract void addSourceSetsBlock(@NotNull GroovyFile file);
|
||||
|
||||
protected abstract boolean addElementsToFile(
|
||||
@NotNull GroovyFile groovyFile,
|
||||
boolean isTopLevelProjectFile,
|
||||
@NotNull String version
|
||||
);
|
||||
|
||||
protected static boolean canConfigureFile(@NotNull GroovyFile file) {
|
||||
return WritingAccessProvider.isPotentiallyWritable(file.getVirtualFile(), null);
|
||||
}
|
||||
@@ -241,12 +282,12 @@ public abstract class KotlinWithGradleConfigurator implements KotlinProjectConfi
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static String getDefaultPathToBuildGradleFile(@NotNull Project project) {
|
||||
protected static String getTopLevelProjectFilePath(@NotNull Project project) {
|
||||
return project.getBasePath() + "/" + GradleConstants.DEFAULT_SCRIPT_NAME;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static String getDefaultPathToBuildGradleFile(@NotNull Module module) {
|
||||
protected static String getModuleFilePath(@NotNull Module module) {
|
||||
return new File(module.getModuleFilePath()).getParent() + "/" + GradleConstants.DEFAULT_SCRIPT_NAME;
|
||||
}
|
||||
|
||||
@@ -254,15 +295,17 @@ public abstract class KotlinWithGradleConfigurator implements KotlinProjectConfi
|
||||
return version.contains("SNAPSHOT");
|
||||
}
|
||||
|
||||
protected void changeGradleFile(
|
||||
protected boolean changeGradleFile(
|
||||
@NotNull final GroovyFile groovyFile,
|
||||
final boolean isTopLevelProjectFile,
|
||||
@NotNull final String version,
|
||||
@NotNull NotificationMessageCollector collector
|
||||
) {
|
||||
final boolean[] isModified = {false};
|
||||
new WriteCommandAction(groovyFile.getProject()) {
|
||||
@Override
|
||||
protected void run(@NotNull Result result) {
|
||||
addElements(groovyFile, version);
|
||||
isModified[0] = addElementsToFile(groovyFile, isTopLevelProjectFile, version);
|
||||
|
||||
CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(groovyFile);
|
||||
}
|
||||
@@ -272,6 +315,7 @@ public abstract class KotlinWithGradleConfigurator implements KotlinProjectConfi
|
||||
if (virtualFile != null) {
|
||||
collector.addMessage(virtualFile.getPath() + " was modified");
|
||||
}
|
||||
return isModified[0];
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
+3
-6
@@ -38,15 +38,11 @@ public abstract class AbstractConfigureProjectByChangingFileTest extends LightCo
|
||||
doTest(pathWithFile, pathWithFile.replace("pom", "pom_after"), new KotlinJavascriptMavenConfigurator());
|
||||
}
|
||||
|
||||
public void doTestAndroidGradle(@NotNull String path) throws Exception {
|
||||
doTest(path, path.replace("before", "after"), new KotlinAndroidGradleModuleConfigurator());
|
||||
}
|
||||
|
||||
public void doTestGradle(@NotNull String path) throws Exception {
|
||||
doTest(path, path.replace("before", "after"), new KotlinGradleModuleConfigurator());
|
||||
}
|
||||
|
||||
private void doTest(@NotNull String beforeFile, @NotNull String afterFile, @NotNull KotlinProjectConfigurator configurator) throws Exception {
|
||||
protected void doTest(@NotNull String beforeFile, @NotNull String afterFile, @NotNull KotlinProjectConfigurator configurator) throws Exception {
|
||||
configureByFile(beforeFile);
|
||||
|
||||
String versionFromFile = InTextDirectivesUtils.findStringWithPrefixes(getFile().getText(), "// VERSION:");
|
||||
@@ -54,7 +50,8 @@ public abstract class AbstractConfigureProjectByChangingFileTest extends LightCo
|
||||
|
||||
NotificationMessageCollector collector = NotificationMessageCollectorKt.createConfigureKotlinNotificationCollector(getProject());
|
||||
if (configurator instanceof KotlinWithGradleConfigurator) {
|
||||
((KotlinWithGradleConfigurator) configurator).changeGradleFile((GroovyFile) getFile(), version, collector);
|
||||
((KotlinWithGradleConfigurator) configurator).changeGradleFile((GroovyFile) getFile(), true, version, collector);
|
||||
((KotlinWithGradleConfigurator) configurator).changeGradleFile((GroovyFile) getFile(), false, version, collector);
|
||||
}
|
||||
else if (configurator instanceof KotlinMavenConfigurator) {
|
||||
((KotlinMavenConfigurator) configurator).changePomFile(getModule(), getFile(), version, collector);
|
||||
|
||||
-138
@@ -29,144 +29,6 @@ import java.util.regex.Pattern;
|
||||
@SuppressWarnings("all")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public class ConfigureProjectByChangingFileTestGenerated extends AbstractConfigureProjectByChangingFileTest {
|
||||
@TestMetadata("idea/testData/configuration/android-gradle")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Android_gradle extends AbstractConfigureProjectByChangingFileTest {
|
||||
public void testAllFilesPresentInAndroid_gradle() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/configuration/android-gradle"), Pattern.compile("(\\w+)_before\\.gradle$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("androidStudioDefault_before.gradle")
|
||||
public void testAndroidStudioDefault() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/androidStudioDefault_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("androidStudioDefaultShapshot_before.gradle")
|
||||
public void testAndroidStudioDefaultShapshot() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/androidStudioDefaultShapshot_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("buildConfigs_before.gradle")
|
||||
public void testBuildConfigs() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/buildConfigs_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("emptyDependencyList_before.gradle")
|
||||
public void testEmptyDependencyList() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/emptyDependencyList_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("emptyFile_before.gradle")
|
||||
public void testEmptyFile() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/emptyFile_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("helloWorld_before.gradle")
|
||||
public void testHelloWorld() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/helloWorld_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("libraryFile_before.gradle")
|
||||
public void testLibraryFile() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/libraryFile_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("missedApplyAndroidStatement_before.gradle")
|
||||
public void testMissedApplyAndroidStatement() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/missedApplyAndroidStatement_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("missedBuildscriptBlock_before.gradle")
|
||||
public void testMissedBuildscriptBlock() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/missedBuildscriptBlock_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("missedRepositoriesInBuildscriptBlock_before.gradle")
|
||||
public void testMissedRepositoriesInBuildscriptBlock() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/missedRepositoriesInBuildscriptBlock_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("productFlavor_before.gradle")
|
||||
public void testProductFlavor() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/productFlavor_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/configuration/android-gradle/gradleExamples")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class GradleExamples extends AbstractConfigureProjectByChangingFileTest {
|
||||
public void testAllFilesPresentInGradleExamples() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/configuration/android-gradle/gradleExamples"), Pattern.compile("(\\w+)_before\\.gradle$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample0_before.gradle")
|
||||
public void testGradleExample0() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample0_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample18_before.gradle")
|
||||
public void testGradleExample18() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample18_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample22_before.gradle")
|
||||
public void testGradleExample22() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample22_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample44_before.gradle")
|
||||
public void testGradleExample44() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample44_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample5_before.gradle")
|
||||
public void testGradleExample5() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample5_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample50_before.gradle")
|
||||
public void testGradleExample50() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample50_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample58_before.gradle")
|
||||
public void testGradleExample58() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample58_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample65_before.gradle")
|
||||
public void testGradleExample65() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample65_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("gradleExample8_before.gradle")
|
||||
public void testGradleExample8() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/configuration/android-gradle/gradleExamples/gradleExample8_before.gradle");
|
||||
doTestAndroidGradle(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/configuration/gradle")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user