1 /* <lambda>null2 * Copyright 2021 Google LLC 3 * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package com.google.devtools.ksp.gradle.testing 18 19 import java.io.File 20 21 /** 22 * Simpler wrapper that represents a module in a test project. 23 * 24 * It has helper methods to add plugins, dependencies or sources to the project. 25 * 26 * It is loosely structured, which means it won't check its integrity. (e.g. you can add android 27 * sources w/o adding android plugin). It is meant to serve as a convenient way to configure 28 * modules. 29 */ 30 class TestModule( 31 val moduleRoot: File, 32 plugins: List<PluginDeclaration> = emptyList(), 33 dependencies: List<DependencyDeclaration> = emptyList() 34 ) { 35 val plugins = LinkedHashSet(plugins) 36 val dependencies = LinkedHashSet(dependencies) 37 val buildFileAdditions = LinkedHashSet<String>() 38 val name = moduleRoot.name 39 40 init { 41 moduleRoot.mkdirs() 42 } 43 44 /** 45 * Adds the given source file to the main source set. 46 */ 47 fun addSource(name: String, contents: String) { 48 val srcDir = when { 49 name.endsWith(".kt") -> kotlinSourceDir 50 name.endsWith(".java") -> javaSourceDir 51 else -> error("must provide java or kotlin file") 52 } 53 srcDir.resolve(name).writeText(contents) 54 } 55 56 /** 57 * Adds the given source file to the test source set. 58 */ 59 fun addTestSource(name: String, contents: String) { 60 val srcDir = when { 61 name.endsWith(".kt") -> kotlinTestSourceDir 62 name.endsWith(".java") -> javaTestSourceDir 63 else -> error("must provide java or kotlin file") 64 } 65 srcDir.resolve(name).writeText(contents) 66 } 67 68 /** 69 * Adds the given source file to the AndroidTest source set. 70 */ 71 fun addAndroidTestSource(name: String, contents: String) { 72 val srcDir = when { 73 name.endsWith(".kt") -> kotlinAndroidTestSourceDir 74 name.endsWith(".java") -> javaAndroidTestSourceDir 75 else -> error("must provide java or kotlin file") 76 } 77 srcDir.resolve(name).writeText(contents) 78 } 79 80 /** 81 * Adds the given source file to the given KotlinSourceSet in a multiplatform project. 82 */ 83 fun addMultiplatformSource(sourceSet: String, name: String, contents: String) { 84 require(name.endsWith(".kt")) { "multiplatform source extension needs to be *.kt." } 85 val srcDir = multiplatformKotlinSourceDir(sourceSet) 86 srcDir.resolve(name).writeText(contents) 87 } 88 89 private fun multiplatformKotlinSourceDir(sourceSet: String) = moduleRoot.resolve("src/$sourceSet/kotlin").also { 90 it.mkdirs() 91 } 92 93 private val kotlinSourceDir 94 get() = moduleRoot.resolve("src/main/kotlin").also { 95 it.mkdirs() 96 } 97 98 private val javaSourceDir 99 get() = moduleRoot.resolve("src/main/java").also { 100 it.mkdirs() 101 } 102 103 private val kotlinTestSourceDir 104 get() = moduleRoot.resolve("src/test/kotlin").also { 105 it.mkdirs() 106 } 107 108 private val javaTestSourceDir 109 get() = moduleRoot.resolve("src/test/java").also { 110 it.mkdirs() 111 } 112 113 private val kotlinAndroidTestSourceDir 114 get() = moduleRoot.resolve("src/androidTest/kotlin").also { 115 it.mkdirs() 116 } 117 118 private val javaAndroidTestSourceDir 119 get() = moduleRoot.resolve("src/androidTest/java").also { 120 it.mkdirs() 121 } 122 123 private val servicesDir 124 get() = moduleRoot.resolve("src/main/resources/META-INF/services/").also { 125 it.mkdirs() 126 } 127 128 val kspServicesFile 129 get() = servicesDir.resolve("com.google.devtools.ksp.processing.SymbolProcessorProvider") 130 131 private val buildFile 132 get() = moduleRoot.resolve("build.gradle.kts") 133 134 /** 135 * Writes the build file. 136 */ 137 fun writeBuildFile() { 138 val contents = buildString { 139 appendln("plugins {") 140 plugins.forEach { plugin -> 141 appendln(plugin.toCode().prependIndent(" ")) 142 } 143 appendln("}") 144 appendln("dependencies {") 145 dependencies.forEach { dependency -> 146 appendln(dependency.toCode().prependIndent(" ")) 147 } 148 appendln("}") 149 buildFileAdditions.forEach { 150 appendln(it) 151 } 152 } 153 buildFile.writeText(contents) 154 } 155 } 156