Skip to content

Commit

Permalink
Buildscript improvements (#182)
Browse files Browse the repository at this point in the history
* Fix possible crash when switching between branches with different modules set

* Improve buildscript

* build.gradle: fix publishing
  • Loading branch information
thecatcore authored Dec 6, 2024
1 parent 2c3f108 commit a99bee8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 53 deletions.
101 changes: 49 additions & 52 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,31 @@ import net.fabricmc.loom.task.RemapJarTask
import net.legacyfabric.VersionHelper
import org.apache.commons.codec.digest.DigestUtils

static def getMCVersion(Project project) {
def projectFullName = project.name
static def parseProjectName(String name, Project project) {
def parts = name.split("_")

if (!projectFullName.contains("_")) return "1.8.9"
if (parts.length < 2) parts = [*parts, "1.8.9"]

def mcVersion = projectFullName.split("_")[1]
def (moduleName, mcVersion) = parts

if (mcVersion == "common") return "1.8.9"
def isCommon = mcVersion == "common"

return mcVersion
}
def mavenName = moduleName.trim()

static def getProjectSimpleName(Project project) {
def projectFullName = project.name
if (isCommon) {
mcVersion = "1.8.9"

if (!projectFullName.contains("_")) return projectFullName
if (project != null && project.hasProperty("artifactSuffix")) {
mavenName += project["artifactSuffix"]
} else {
mavenName += "-common"
}
}

return projectFullName.split("_")[0].trim()
return [moduleName.trim(), mcVersion, isCommon, mavenName.trim()]
}

def getProjectMavenName(projectLike) {
def projectInfoFromName(projectLike) {
Project project
if (!(projectLike instanceof Project)) {
if (projectLike instanceof BasePluginExtension) {
Expand All @@ -66,21 +70,9 @@ def getProjectMavenName(projectLike) {
}
else project = projectLike

def simpleName = getProjectSimpleName(project)

if (project.name.endsWith("_common")) {
if (project.hasProperty("artifactSuffix")) {
simpleName += project["artifactSuffix"]
} else {
simpleName += "-common"
}
}

return simpleName.trim()
return parseProjectName(project.name, project)
}



static def getMCRange(Project project) {
def predicates = []

Expand Down Expand Up @@ -114,18 +106,16 @@ static def getMCRange(Project project) {
def getSubprojectVersion(Project project) {
def version

def nameParts = project.name.split("_")
def projectName = nameParts[0]
def mcVersion = nameParts[1]
def (moduleName, mcVersion, isCommon) = projectInfoFromName(project)

if (projectName == "legacyfabric-api") version = rootProject.version
else version = rootProject[projectName + ".version"]
if (moduleName == "legacyfabric-api") version = rootProject.version
else version = rootProject[moduleName + ".version"]

if (mcVersion != "common") {
if (!isCommon) {
version = version + "+" + mcVersion
}

if (projectName == "legacyfabric-api") return version
if (moduleName == "legacyfabric-api") return version

if (grgit == null) {
return version + "+nogit"
Expand Down Expand Up @@ -158,7 +148,7 @@ def static setupRepositories(RepositoryHandler repositories) {
def ENV = System.getenv()
if (ENV.MAVEN_PUBLISH_CREDENTIALS) {
repositories.maven {
url "https://maven.legacyfabric.net/"
url "https://repo.legacyfabric.net/legacyfabric/"
credentials {
username ENV.MAVEN_PUBLISH_CREDENTIALS.split(":")[0]
password ENV.MAVEN_PUBLISH_CREDENTIALS.split(":")[1]
Expand All @@ -173,22 +163,22 @@ def static setupRepositories(RepositoryHandler repositories) {
def moduleDependencies(Project project, List<String> projectNames) {
def depNames = []

def projectMCVersion = VersionHelper.parseVersion(getMCVersion(project))
def (moduleName, mcVersion, isCommon) = projectInfoFromName(project)

def isCommon = project.name.endsWith("_common")
def projectMCVersion = VersionHelper.parseVersion(mcVersion)

rootProject.subprojects {
def subProjectName = getProjectSimpleName(it)
def (subModuleName) = projectInfoFromName(it)

if (projectNames.contains(subProjectName)) {
if (projectNames.contains(subModuleName)) {
def predicate = VersionHelper.parsePredicate(getMCRange(it))

if (predicate.test(projectMCVersion)) depNames.add(it.name)
}
}

if (!isCommon) {
def commonProject = findProject(":${getProjectSimpleName(project)}_common")
def commonProject = findProject(":${moduleName}_common")

if (commonProject != null) {
depNames.add(0, commonProject.name)
Expand All @@ -210,7 +200,7 @@ def moduleDependencies(Project project, List<String> projectNames) {
}
}

if (getProjectSimpleName(project) != "legacy-fabric-logger-api-v1") {
if (moduleName != "legacy-fabric-logger-api-v1") {
deps.add(project.dependencies.project(path: ":legacy-fabric-logger-api-v1_common", configuration: "namedElements"))
}

Expand All @@ -221,9 +211,11 @@ def moduleDependencies(Project project, List<String> projectNames) {
pom.withXml {
def depsNode = asNode().appendNode("dependencies")
deps.each {
def (_, _1, _2, mavenName) = projectInfoFromName(it)

def depNode = depsNode.appendNode("dependency")
depNode.appendNode("groupId", it.group)
depNode.appendNode("artifactId", getProjectMavenName(it))
depNode.appendNode("artifactId", mavenName)
depNode.appendNode("version", it.version)
depNode.appendNode("scope", "compile")

Expand Down Expand Up @@ -281,8 +273,7 @@ allprojects {
setVersion(getSubprojectVersion(it))
}

def mcVersion = getMCVersion(it)
String projectName = getProjectSimpleName(it)
def (moduleName, mcVersion, _) = projectInfoFromName(it)
def hasTest = it.file("src/testmod").exists()

loom {
Expand All @@ -293,7 +284,7 @@ allprojects {
runs {
"testModClient$mcVersion" {
client()
ideConfigGenerated projectName == "legacyfabric-api"
ideConfigGenerated moduleName == "legacyfabric-api"
name = "$mcVersion Testmod Client"
source sourceSets.testmod

Expand All @@ -304,7 +295,7 @@ allprojects {

"testModServer$mcVersion" {
server()
ideConfigGenerated projectName == "legacyfabric-api"
ideConfigGenerated moduleName == "legacyfabric-api"
name = "$mcVersion Testmod Server"
source sourceSets.testmod
}
Expand Down Expand Up @@ -482,25 +473,27 @@ def addPomMetadataInformation(Project project, MavenPom pom) {
}

subprojects {
def (moduleName, _, _1, mavenName) = projectInfoFromName(project)

base {
archivesName = getProjectMavenName(it)
archivesName = mavenName
}

dependencies {
testmodImplementation sourceSets.main.output

if (getProjectSimpleName(project) != "legacy-fabric-logger-api-v1") {
if (moduleName != "legacy-fabric-logger-api-v1") {
api project(path: ":legacy-fabric-logger-api-v1_common", configuration: "namedElements")
}
}

publishing {
publications {
mavenJava(MavenPublication) {
if (getProjectSimpleName(project) == "legacyfabric-api") {
if (moduleName == "legacyfabric-api") {
artifactId = "legacy-fabric-api"
} else {
artifactId = getProjectMavenName(project)
artifactId = mavenName
}

pom {
Expand All @@ -522,9 +515,9 @@ subprojects {

javadoc.enabled = false

def scriptPath = rootProject.file("${getProjectSimpleName(project)}/common.gradle")
def scriptPath = rootProject.file("${moduleName}/common.gradle")
if (scriptPath.exists()) {
apply from: rootProject.file("${getProjectSimpleName(project)}/common.gradle")
apply from: rootProject.file("${moduleName}/common.gradle")
}

afterEvaluate {
Expand Down Expand Up @@ -561,7 +554,9 @@ sourceSets {
dependencies {
afterEvaluate {
subprojects.each {
if (getProjectSimpleName(it) == "legacyfabric-api") return
def (moduleName) = projectInfoFromName(it)

if (moduleName == "legacyfabric-api") return

if (it.file("src/main").exists()) api project(path: "${it.path}", configuration: "namedElements")
if (it.file("src/testmod").exists()) testmodImplementation project("${it.path}").sourceSets.testmod.output
Expand All @@ -572,7 +567,9 @@ dependencies {
remapJar {
afterEvaluate {
subprojects.each {
if (getProjectSimpleName(it) == "legacyfabric-api") return
def (moduleName) = projectInfoFromName(it)

if (moduleName == "legacyfabric-api") return
// Include the signed or none signed jar from the sub project.
if (it.file("src/main").exists()) nestedJars.from project("${it.path}:").tasks.getByName("remapJar")
}
Expand Down
5 changes: 4 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ rootProject.name = "legacy-fabric-api"

def loadProject(String path) {
file(path).listFiles().each {
if (it.isDirectory()) {
if (it.isDirectory() && (
file("$it/src/main/resources/fabric.mod.json").exists() ||
file("$it/src/testmod/resources/fabric.mod.json").exists()
)) {
include("${path}_$it.name")

project(":${path}_$it.name").projectDir = it
Expand Down

0 comments on commit a99bee8

Please sign in to comment.