Skip to content

Commit

Permalink
Merge "Recipe converter clean up" into studio-main
Browse files Browse the repository at this point in the history
  • Loading branch information
ducrohet authored and Android (Google) Code Review committed Jan 2, 2024
2 parents 440d113 + 78e6e99 commit 7635452
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.google.android.gradle_recipe.converter.converters
import com.google.android.gradle_recipe.converter.printErrorAndTerminate
import com.google.android.gradle_recipe.converter.recipe.RecipeData
import com.google.android.gradle_recipe.converter.recipe.toMajorMinor
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.attribute.PosixFilePermission
Expand All @@ -36,13 +37,41 @@ const val GRADLE_RESOURCES_FOLDER = "gradle-resources"
abstract class Converter(
protected val branchRoot: Path
) {
protected val DEFAULT_SKIP_FILENAMES = setOf("gradlew", "gradlew.bat", "local.properties")
protected val DEFAULT_SKIP_FOLDERNAMES = setOf("build", ".idea", ".gradle", "out", "wrapper")

var recipeData: RecipeData? = null
// some converters may need the minimum AGP version supported by the recipe.
var minAgp: String? = null

protected open val skippedFilenames: Set<String>
get() = DEFAULT_SKIP_FILENAMES

/** Can converter convert this recipe
protected open val skippedFoldernames: Set<String>
get() = DEFAULT_SKIP_FOLDERNAMES

/**
* A filter for files and folders during a conversion. Filters out Gradle
* and Android Studio temporary and local files.
*/
abstract fun isConversionCompliant(recipeData: RecipeData): Boolean
fun accept(file: File): Boolean {
if (file.isFile) {
return !skippedFilenames.contains(file.name)
}

if (file.isDirectory) {
return !skippedFoldernames.contains(file.name)
}

return true
}


/**
* Can converter convert this recipe
*/
open fun isConversionCompliant(recipeData: RecipeData): Boolean {
return true
}

/**
* Converts build.gradle
Expand Down Expand Up @@ -102,11 +131,6 @@ abstract class Converter(

open fun processGradleWrapperProperties(file: Path) { }

protected fun getMinAgp(): String = recipeData?.minAgpVersion
// this really should not happen
// TODO(b/317888166) improve this
?: printErrorAndTerminate("recipeData not loaded in Converter")

protected fun getVersionInfoFromAgp(agpVersion: String): VersionInfo {
val agp = agpVersion.toMajorMinor()
return getVersionsFromAgp(branchRoot, agp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import com.google.android.gradle_recipe.converter.deleteNonHiddenRecursively
import com.google.android.gradle_recipe.converter.printErrorAndTerminate
import com.google.android.gradle_recipe.converter.recipe.RecipeData
import com.google.android.gradle_recipe.converter.recipe.toMajorMinor
import java.io.File
import java.lang.System.err
import java.nio.file.FileVisitResult
import java.nio.file.Files
import java.nio.file.Path
Expand Down Expand Up @@ -92,7 +90,11 @@ const val compileSdkVersion = "34"
*/
const val minimumSdkVersion = "21"

data class ConversionResult(val recipeData: RecipeData, val isConversionSuccessful: Boolean)
enum class ResultMode {
SUCCESS, FAILURE, SKIPPED
}

data class ConversionResult(val recipeData: RecipeData, val result: ResultMode)

/**
* Converts the individual recipe, calculation the conversion mode by input parameters
Expand All @@ -112,26 +114,6 @@ class RecipeConverter(
RELEASE, WORKINGCOPY, SOURCE
}

/** A filter for files and folders during a conversion. Filters out Gradle
* and Android Studio temporary and local files.
*/
companion object {
private val skippedFilenames = setOf("gradlew", "gradlew.bat", "local.properties")
private val skippedFoldernames = setOf("build", ".idea", ".gradle", "out", "wrapper")

fun accept(file: File): Boolean {
if (file.isFile) {
return !skippedFilenames.contains(file.name)
}

if (file.isDirectory) {
return !skippedFoldernames.contains(file.name)
}

return true
}
}

init {
converter = when (mode) {
Mode.WORKINGCOPY -> {
Expand Down Expand Up @@ -183,11 +165,13 @@ class RecipeConverter(
}

val success = if (converter.isConversionCompliant(recipeData)) {
converter.recipeData = recipeData
if (mode == Mode.WORKINGCOPY) {
converter.minAgp = recipeData.minAgpVersion
}

Files.walkFileTree(source, object : SimpleFileVisitor<Path>() {
override fun preVisitDirectory(dir: Path, attrs: BasicFileAttributes): FileVisitResult {
if (accept(dir.toFile())) {
if (converter.accept(dir.toFile())) {
Files.createDirectories(recipeDestination.resolve(source.relativize(dir)))
return FileVisitResult.CONTINUE
}
Expand Down Expand Up @@ -225,7 +209,7 @@ class RecipeConverter(
}

else -> {
if (accept(sourceFile.toFile())) {
if (converter.accept(sourceFile.toFile())) {
Files.copy(sourceFile, destinationFile, StandardCopyOption.REPLACE_EXISTING)
}
}
Expand All @@ -239,10 +223,12 @@ class RecipeConverter(
converter.copyGradleFolder(recipeDestination)
}

true
converter.minAgp = null

ResultMode.SUCCESS
} else {
err.println("Couldn't convert $source due to AGP version compliance ")
false
println("Couldn't convert $source due to AGP version compliance ")
ResultMode.SKIPPED
}

return ConversionResult(recipeData, success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class RecursiveConverter(
visitRecipes(sourceAll) { recipeFolder: Path ->
val conversionResult = recipeConverter.convert(recipeFolder, destination)

if (conversionResult.isConversionSuccessful) {
if (conversionResult.result == ResultMode.SUCCESS) {
for (keyword in conversionResult.recipeData.keywords) {
val list = keywordsToRecipePaths.computeIfAbsent(keyword) { mutableListOf() }
list.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package com.google.android.gradle_recipe.converter.converters

import com.google.android.gradle_recipe.converter.printErrorAndTerminate
import com.google.android.gradle_recipe.converter.recipe.RECIPE_METADATA_FILE
import com.google.android.gradle_recipe.converter.recipe.RecipeData
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.writeLines

/** This mode is for a recipe that has no placeholders.
/**
* This mode is for a recipe that has no placeholders.
* This is for releases ando/or tests
*/
class ReleaseConverter(
Expand All @@ -39,6 +41,8 @@ class ReleaseConverter(
private var pluginRepo: List<String> = listOf()
private var dependencyRepo: List<String> = listOf()

private val releaseSkipFilenames = DEFAULT_SKIP_FILENAMES + RECIPE_METADATA_FILE

init {
if (gradleVersion != null) {
// github release
Expand All @@ -54,6 +58,9 @@ class ReleaseConverter(
}
}

override val skippedFilenames: Set<String>
get() = releaseSkipFilenames

override fun isConversionCompliant(recipeData: RecipeData): Boolean {
return recipeData.isCompliantWithAgp(agpVersion)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.android.gradle_recipe.converter.converters

import com.google.android.gradle_recipe.converter.recipe.RecipeData
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.writeLines
Expand All @@ -25,10 +24,6 @@ import kotlin.io.path.writeLines
* this is how we store the recipes in the dev branch
*/
class SourceConverter(branchRoot: Path) : Converter(branchRoot) {
override fun isConversionCompliant(recipeData: RecipeData): Boolean {
return true
}

override fun convertBuildGradle(source: Path, target: Path) {
target.writeLines(Files.readAllLines(source).unwrapGradlePlaceholders(), Charsets.UTF_8)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.google.android.gradle_recipe.converter.converters

import com.google.android.gradle_recipe.converter.recipe.RecipeData
import com.google.android.gradle_recipe.converter.printErrorAndTerminate
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.writeLines
Expand All @@ -27,12 +27,8 @@ import kotlin.io.path.writeLines
*/
class WorkingCopyConverter(branchRoot: Path) : Converter(branchRoot) {

override fun isConversionCompliant(recipeData: RecipeData): Boolean {
return true
}

override fun convertBuildGradle(source: Path, target: Path) {
val agpVersion = getMinAgp()
val agpVersion = minAgp ?: error("Calling WorkingCopyConverter without minAgp value")

val convertedText = Files.readAllLines(source)
.wrapGradlePlaceholdersWithInlineValue(
Expand Down Expand Up @@ -73,7 +69,7 @@ class WorkingCopyConverter(branchRoot: Path) : Converter(branchRoot) {
}

override fun convertVersionCatalog(source: Path, target: Path) {
val agpVersion = getMinAgp()
val agpVersion = minAgp ?: error("Calling WorkingCopyConverter without minAgp value")

val convertedText = Files.readAllLines(source)
.wrapVersionCatalogPlaceholders(
Expand All @@ -88,13 +84,15 @@ class WorkingCopyConverter(branchRoot: Path) : Converter(branchRoot) {
}

override fun processGradleWrapperProperties(file: Path) {
val agpVersion = minAgp ?: error("Calling WorkingCopyConverter without minAgp value")

// building the line
// distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
file.writeLines(
Files.readAllLines(file)
.wrapGradleWrapperPlaceholders(
"\$GRADLE_LOCATION",
"https\\://services.gradle.org/distributions/gradle-${getVersionInfoFromAgp(getMinAgp()).gradle}-bin.zip"
"https\\://services.gradle.org/distributions/gradle-${getVersionInfoFromAgp(agpVersion).gradle}-bin.zip"
),
Charsets.UTF_8
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.google.android.gradle_recipe.converter.validators

import com.google.android.gradle_recipe.converter.converters.RecipeConverter
import com.google.android.gradle_recipe.converter.converters.RecipeConverter.Mode
import com.google.android.gradle_recipe.converter.converters.ResultMode
import com.google.android.gradle_recipe.converter.recipe.visitRecipes
import java.io.IOException
import java.nio.file.Path
Expand Down Expand Up @@ -53,7 +54,7 @@ class InternalCIValidator(
source = recipeFolder, destination = destinationFolder
)

if (conversionResult.isConversionSuccessful) {
if (conversionResult.result == ResultMode.SUCCESS) {
println("Validating: $destinationFolder with AGP: $agpVersion and Gradle: $gradlePath")
val tasksExecutor = GradleTasksExecutor(destinationFolder)
tasksExecutor.executeTasks(conversionResult.recipeData.tasks)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.google.android.gradle_recipe.converter.validators

import com.google.android.gradle_recipe.converter.converters.RecipeConverter
import com.google.android.gradle_recipe.converter.converters.RecipeConverter.Mode
import com.google.android.gradle_recipe.converter.converters.ResultMode
import com.google.android.gradle_recipe.converter.converters.getMaxAgp
import com.google.android.gradle_recipe.converter.converters.getVersionsFromAgp
import com.google.android.gradle_recipe.converter.printErrorAndTerminate
Expand Down Expand Up @@ -68,7 +69,7 @@ class MinMaxCurrentAgpValidator(
source = from, destination = destinationFolder
)

if (conversionResult.isConversionSuccessful) {
if (conversionResult.result == ResultMode.SUCCESS) {
println("Validating: Recipe $name ($destinationFolder) with AGP: $agpVersion and Gradle: $gradleVersion")
val tasksExecutor = GradleTasksExecutor(destinationFolder)
tasksExecutor.executeTasks(conversionResult.recipeData.tasks)
Expand Down

0 comments on commit 7635452

Please sign in to comment.