blob: f4abe6385e5bce8db9d76ba4b66474df9aff569e [file] [log] [blame]
/*
* Copyright (C) 2019 The Android Open Source Project
*
* 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 com.android.tools.idea.wizard.template
import java.io.File
/** Execution engine for the instructions in a recipe. */
interface RecipeExecutor {
/**
* Copies the given source file into the given destination file (where the
* source is allowed to be a directory, in which case the whole directory is copied recursively).
*/
fun copy(from: File, to: File)
/**
* Writes text into the given output file.
* @param source the source of the text
* @param to the location of the output file
* @param trimVertical trim the leading and trailing white spaces if set to true
* @param squishEmptyLines squish the empty lines if set to true
* @param commitDocument commit the saved file if set to true. This can be usually false. This is useful when the saved file needs to be
* modified in the same recipe file. E.g. in a case where a build.gradle file is saved and a dependency is added to
* the build.gradle file
*/
fun save(source: String, to: File, trimVertical: Boolean = true, squishEmptyLines: Boolean = true, commitDocument: Boolean = false)
/** Merges the given XML source into the given destination file (or just writes it if the destination file does not exist). */
fun mergeXml(source: String, to: File)
/**
* Creates a directory at the specified location (if not already present).
* This will also create any parent directories that don't exist, as well.
*/
fun createDirectory(at: File)
/** Records that this file should be opened in Studio. */
fun open(file: File)
/** Adds "apply plugin: '`plugin`'" statement to the module build.gradle file. */
fun applyPlugin(plugin: String)
/** Records a classpath dependency. */
fun addClasspathDependency(mavenCoordinate: String, minRev: String? = null)
/**
* Determines if a module/project already have a dependency.
* @param moduleDir determines a module to check. The current module will be used if it is null.
*/
fun hasDependency(mavenCoordinate: String, moduleDir: File? = null): Boolean
/**
* Records a library dependency
* Old [configuration]s such as "compile" will be converted to new ones ("implementation") in later stages if Gradle supports it.
*
* @param mavenCoordinate coordinate of dependency to be added in Maven format (e.g androidx.appcompat:appcompat:1.1.0).
* @param configuration Gradle configuration to use.
* @param minRev If [minRev] is present, [minRev] or a higher number is used as the version of the dependency.
* @param moduleDir determines a module to add the dependency. The current module will be used if it is null.
* @param toBase If true the dependency will be added to a base module if it exists.
* Example: Libraries with manifest resources added to a Dynamic Feature Module need to be added to the base module.
*/
fun addDependency(
mavenCoordinate: String,
configuration: String = "compile",
minRev: String? = null,
moduleDir: File? = null,
toBase: Boolean = false
)
/**
* Records a module dependency.
* Old [configuration]s such as "compile" will be converted to new ones ("implementation") in later stages if Gradle supports it.
*
* @param configuration Gradle configuration to use.
* @param moduleName name of a module on which something depends. Should not start with ':'.
* @param toModule path to the module which depends on [moduleName].
*/
fun addModuleDependency(configuration: String, moduleName: String, toModule: File)
/**
* Adds a new entry to 'sourceSets' block of Gradle build file.
*
* @param type type of the source set.
* @param name source set name that is created/modified.
* @param dir path to the source set folder (or file if [type] is [SourceSetType.MANIFEST]).
* */
fun addSourceSet(type: SourceSetType, name: String, dir: File)
/** Initializes the variable with [name] to [value] in the ext block of global Gradle build file. */
fun setExtVar(name: String, value: String)
/**
* Looks for the given classpath dependency coordinate, in the project base, and returns the version variable name on it.
* For example if the base project has a classpath dependency of 'org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version'
* this function returns 'kotlin_version'.
* If the dependency has no variable, or the variable can't be determined, it returns the specified default value.
*
* @param mavenCoordinate coordinate of class path dependency to be added in Maven format (e.g androidx.appcompat:appcompat).
* @param valueIfNotFound value to return if the dependency has no variable, or the variable can't be determined.
*/
fun getClasspathDependencyVarName(mavenCoordinate: String, valueIfNotFound: String): String
/**
* Looks for the given dependency coordinate and returns the version variable name on it.
* For example if the module has a dependency of 'androidx.appcompat:appcompat:$appcompat_version'
* this function returns 'appcompat_version'.
* If the dependency has no variable, or the variable can't be determined, it returns the specified default value.
*
* @param mavenCoordinate coordinate of the dependency to be added in Maven format (e.g androidx.appcompat:appcompat).
* @param valueIfNotFound value to return if the dependency has no variable, or the variable can't be determined.
*/
fun getDependencyVarName(mavenCoordinate: String, valueIfNotFound: String): String
/**
* Adds a module dependency to global settings.gradle[.kts] file.
*/
fun addIncludeToSettings(moduleName: String)
/**
* Adds a new build feature to android block. For example, may enable compose.
*/
fun setBuildFeature(name: String, value: Boolean)
/**
* Sets sourceCompatibility and targetCompatibility in compileOptions and (if needed) jvmTarget in kotlinOptions.
*/
fun requireJavaVersion(version: String, kotlinSupport: Boolean = false)
/**
* Adds a dynamic feature [name] to [toModule]'s build.gradle[.kts].
*
* @param name name of a dynamic feature which should be added.
* @param toModule path to base feature module dir
*/
fun addDynamicFeature(name: String, toModule: File)
}
enum class SourceSetType {
AIDL,
ASSETS,
JAVA,
JNI,
MANIFEST,
RENDERSCRIPT,
RES,
RESOURCES
}