Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Implement GradleProjectFlattenerCli #65

Merged
merged 6 commits into from
Nov 22, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/main/kotlin/slack/cli/gradle/GradleProjectFlattener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (C) 2023 Slack Technologies, LLC
*
* 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
*
* https://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 slack.cli.gradle

import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.defaultLazy
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.path
import java.io.File
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.appendLines
import kotlin.io.path.appendText
import kotlin.io.path.copyToRecursively
import kotlin.io.path.exists
import kotlin.io.path.isDirectory
import kotlin.io.path.readText
import kotlin.io.path.relativeTo
import slack.cli.dryRunOption
import slack.cli.projectDirOption

/**
* A CLI that flattens all gradle projects in a given directory to be top level while preserving
* their original project paths.
*
* This is useful for flattening nested projects that use Dokka, which does not currently support
* easy doc gen for nested projects and end up with colliding names.
*
* It's recommended to run `./gradlew clean` first before running this script to minimize work.
*/
public class GradleProjectFlattenerCli :
CliktCommand(
help =
"A CLI that flattens all gradle projects in a given directory to be top level while preserving their original project paths."
) {

private val projectDir by projectDirOption()

private val settingsFile by
option(
"--settings-file",
"-s",
help =
"The settings.gradle file to use. Defaults to settings.gradle.kts in the current directory. Note this file _must_ only have a single, top-level `include()` call with vararg project args."
)
.path(mustExist = true, canBeDir = false)
.defaultLazy { projectDir.resolve("settings.gradle.kts") }

private val projectDelimiter: String by option().default("--")

private val dryRun by dryRunOption()

@ExperimentalPathApi
override fun run() {
val projectPaths =
settingsFile.readText().trim().removePrefix("include(").removeSuffix(")").split(",").map {
it.trim().removeSurrounding("\"")
}

val newPathMapping = mutableMapOf<String, String>()
for (path in projectPaths) {
val realPath = projectDir.resolve(path.removePrefix(":").replace(":", File.separator))
check(realPath.exists()) { "Expected $realPath to exist." }
check(realPath.isDirectory()) { "Expected $realPath to be a directory." }
val newPath = projectDir.resolve(path.removePrefix(":").replace(":", projectDelimiter))
if (newPath == realPath) {
// Already top-level, move on
continue
}
newPathMapping[path] = newPath.relativeTo(projectDir).toString()
echo("Flattening $realPath to $newPath")
if (!dryRun) {
realPath.copyToRecursively(newPath, followLinks = false, overwrite = false)
}
}

echo("Finished flattening projects. Updating settings file")
val newPaths =
projectPaths.mapNotNull { path ->
// Point at their new paths
// Example:
// project(":libraries:compose-extensions:pull-refresh").projectDir =
// file("libraries--compose-extensions--pull-refresh")
val newPath = newPathMapping[path] ?: return@mapNotNull null
"project(\"$path\").projectDir = file(\"$newPath\")".also { echo("+ $it") }
}

if (!dryRun) {
settingsFile.appendText("\n\n")
settingsFile.appendLines(newPaths)
}
}
}
Loading