-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace git submodules for test projects with custom GitCheckoutTask (#…
…3483) Replace git submodules for test projects with custom GitCheckoutTask This improves the experience when running Dokka tests. External projects used for testing can be managed using Gradle. Because the commitId is defined in code it's easier to see what version the external projects are used. * remove submodules checkout from GitHub actions * gitignore downloaded projects * move `fs.sync` outside of `use { git -> ... }` so it's easier to view & understand the steps * add more comments to explain git checkout process
- Loading branch information
Showing
11 changed files
with
164 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
build-logic/src/main/kotlin/dokkabuild/tasks/GitCheckoutTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
package dokkabuild.tasks | ||
|
||
import org.eclipse.jgit.api.Git | ||
import org.eclipse.jgit.api.ResetCommand.ResetType.HARD | ||
import org.eclipse.jgit.lib.NullProgressMonitor | ||
import org.eclipse.jgit.lib.ProgressMonitor | ||
import org.eclipse.jgit.lib.RepositoryCache | ||
import org.eclipse.jgit.lib.TextProgressMonitor | ||
import org.eclipse.jgit.util.FS | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.file.FileSystemOperations | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.CacheableTask | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.OutputDirectory | ||
import org.gradle.api.tasks.TaskAction | ||
import java.io.File | ||
import javax.inject.Inject | ||
|
||
/** | ||
* Clones a remote Git repository into [GitCheckoutTask.destination]. | ||
*/ | ||
@CacheableTask | ||
abstract class GitCheckoutTask @Inject constructor( | ||
private val fs: FileSystemOperations | ||
) : DefaultTask() { | ||
|
||
@get:OutputDirectory | ||
abstract val destination: DirectoryProperty | ||
|
||
/** Public HTTP URI of the Git repo. */ | ||
@get:Input | ||
abstract val uri: Property<String> | ||
|
||
/** Specific git hash to checkout. */ | ||
@get:Input | ||
abstract val commitId: Property<String> | ||
|
||
private val localRepoDir: File | ||
get() = temporaryDir.resolve("repo") | ||
|
||
private val gitOperationsPrinter: ProgressMonitor = | ||
if (logger.isInfoEnabled) { | ||
TextProgressMonitor() | ||
} else { | ||
NullProgressMonitor.INSTANCE | ||
} | ||
|
||
init { | ||
group = "git checkout" | ||
} | ||
|
||
@TaskAction | ||
fun action() { | ||
initializeRepo() | ||
|
||
fs.sync { | ||
from(localRepoDir) | ||
into(destination) | ||
// exclude the .git dir: | ||
// - prevent the root git repo getting confused with a nested git repo | ||
// - improves Gradle caching | ||
exclude(".git/") | ||
} | ||
|
||
logger.lifecycle("initialized git repo $uri in ${destination.asFile.orNull}") | ||
} | ||
|
||
/** | ||
* Initialize [uri] in [localRepoDir]. | ||
* | ||
* If a git repo already exists in [localRepoDir], try to re-use it. | ||
* | ||
* Any changes to tracked or untracked files will be forcibly removed. | ||
*/ | ||
private fun initializeRepo() { | ||
val uri = uri.get() | ||
val commitId = commitId.get() | ||
|
||
// Check if the repo is already cloned. If yes, then we can re-use it to save time. | ||
val gitRepoInitialized = RepositoryCache.FileKey.isGitRepository(localRepoDir, FS.DETECTED) | ||
|
||
val repo = if (gitRepoInitialized) { | ||
// re-use existing cloned repo | ||
Git.open(localRepoDir) | ||
} else { | ||
// repo is either not cloned or is not recognizable, so delete it and make a fresh clone | ||
fs.delete { delete(localRepoDir) } | ||
|
||
Git.cloneRepository() | ||
.setProgressMonitor(gitOperationsPrinter) | ||
.setNoCheckout(true) | ||
.setURI(uri) | ||
.setDirectory(localRepoDir) | ||
.call() | ||
} | ||
|
||
repo.use { git -> | ||
// checkout the specific commitId specified in the task input | ||
git.checkout() | ||
.setProgressMonitor(gitOperationsPrinter) | ||
.setForced(true) | ||
.setName(commitId) | ||
.call() | ||
|
||
// git reset --hard (wipe changes to tracked files, if any) | ||
git.reset() | ||
.setProgressMonitor(gitOperationsPrinter) | ||
.setMode(HARD) | ||
.call() | ||
|
||
// git clean -fdx (remove any changes in untracked files) | ||
git.clean() | ||
.setForce(true) | ||
.setCleanDirectories(true) | ||
.setIgnore(false) | ||
.call() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
dokka-integration-tests/gradle/projects/coroutines/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# kotlinx-coroutines is automatically downloaded with a Gradle task | ||
kotlinx-coroutines/ |
2 changes: 2 additions & 0 deletions
2
dokka-integration-tests/gradle/projects/serialization/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# kotlinx-serialization is automatically downloaded with a Gradle task | ||
kotlinx-serialization/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# biojava is automatically downloaded with a Gradle task | ||
biojava/ |