Check for offline mode before initializing submodules

This commit is contained in:
Jason Penilla 2024-04-27 11:10:48 -07:00
parent cf4fcd8953
commit cfcb6ad8ef
No known key found for this signature in database
GPG key ID: 0E75A301420E48F8
4 changed files with 33 additions and 3 deletions

View file

@ -35,12 +35,17 @@ import io.papermc.paperweight.util.constants.*
import java.io.File import java.io.File
import org.gradle.api.Plugin import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.Delete import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.TaskProvider import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.bundling.Zip import org.gradle.api.tasks.bundling.Zip
import org.gradle.kotlin.dsl.* import org.gradle.kotlin.dsl.*
class PaperweightCore : Plugin<Project> { class PaperweightCore : Plugin<Project> {
companion object {
private val logger = Logging.getLogger(PaperweightCore::class.java)
}
override fun apply(target: Project) { override fun apply(target: Project) {
checkJavaVersion() checkJavaVersion()
Git.checkForGit() Git.checkForGit()
@ -58,7 +63,7 @@ class PaperweightCore : Plugin<Project> {
// Make sure the submodules are initialized, since there are files there // Make sure the submodules are initialized, since there are files there
// which are required for configuration // which are required for configuration
target.layout.initSubmodules() target.layout.maybeInitSubmodules(target.offlineMode(), logger)
target.configurations.create(PARAM_MAPPINGS_CONFIG) target.configurations.create(PARAM_MAPPINGS_CONFIG)
target.configurations.create(REMAPPER_CONFIG) target.configurations.create(REMAPPER_CONFIG)

View file

@ -40,7 +40,9 @@ open class GeneralTasks(
) : InitialTasks(project) { ) : InitialTasks(project) {
// Configuration won't necessarily always run, so do it as the first task when it's needed as well // Configuration won't necessarily always run, so do it as the first task when it's needed as well
val initSubmodules by tasks.registering<InitSubmodules>() val initSubmodules by tasks.registering<InitSubmodules> {
offlineMode.set(project.offlineMode())
}
val buildDataInfo: Provider<BuildDataInfo> = project.contents(extension.craftBukkit.buildDataInfo) { val buildDataInfo: Provider<BuildDataInfo> = project.contents(extension.craftBukkit.buildDataInfo) {
gson.fromJson(it) gson.fromJson(it)

View file

@ -26,17 +26,27 @@ import io.papermc.paperweight.util.*
import javax.inject.Inject import javax.inject.Inject
import org.gradle.api.DefaultTask import org.gradle.api.DefaultTask
import org.gradle.api.file.ProjectLayout import org.gradle.api.file.ProjectLayout
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.UntrackedTask import org.gradle.api.tasks.UntrackedTask
@Suppress("LeakingThis")
@UntrackedTask(because = "Git tracks the state") @UntrackedTask(because = "Git tracks the state")
abstract class InitSubmodules : DefaultTask() { abstract class InitSubmodules : DefaultTask() {
@get:Inject @get:Inject
abstract val layout: ProjectLayout abstract val layout: ProjectLayout
@get:Input
abstract val offlineMode: Property<Boolean>
init {
offlineMode.convention(false)
}
@TaskAction @TaskAction
fun run() { fun run() {
layout.initSubmodules() layout.maybeInitSubmodules(offlineMode.get(), logger)
} }
} }

View file

@ -60,6 +60,7 @@ import org.gradle.api.file.RegularFile
import org.gradle.api.file.RegularFileProperty import org.gradle.api.file.RegularFileProperty
import org.gradle.api.invocation.Gradle import org.gradle.api.invocation.Gradle
import org.gradle.api.logging.LogLevel import org.gradle.api.logging.LogLevel
import org.gradle.api.logging.Logger
import org.gradle.api.plugins.JavaPluginExtension import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.provider.Property import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider import org.gradle.api.provider.Provider
@ -94,11 +95,23 @@ val ProjectLayout.cache: Path
get() = projectDirectory.dir(".gradle/$CACHE_PATH").path get() = projectDirectory.dir(".gradle/$CACHE_PATH").path
fun ProjectLayout.cacheDir(path: String) = projectDirectory.dir(".gradle/$CACHE_PATH").dir(path) fun ProjectLayout.cacheDir(path: String) = projectDirectory.dir(".gradle/$CACHE_PATH").dir(path)
fun ProjectLayout.maybeInitSubmodules(offline: Boolean, logger: Logger) {
if (offline) {
Git.checkForGit()
logger.lifecycle("Offline mode enabled, not initializing submodules. This may cause problems if submodules are not already initialized.")
} else {
initSubmodules()
}
}
fun ProjectLayout.initSubmodules() { fun ProjectLayout.initSubmodules() {
Git.checkForGit() Git.checkForGit()
Git(projectDirectory.path)("submodule", "update", "--init").executeOut() Git(projectDirectory.path)("submodule", "update", "--init").executeOut()
} }
fun Project.offlineMode(): Boolean = gradle.startParameter.isOffline
fun <T : FileSystemLocation> Provider<out T>.fileExists(project: Project): Provider<out T?> { fun <T : FileSystemLocation> Provider<out T>.fileExists(project: Project): Provider<out T?> {
return flatMap { project.provider { it.takeIf { f -> f.path.exists() } } } return flatMap { project.provider { it.takeIf { f -> f.path.exists() } } }
} }