-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
181 lines (157 loc) · 5.49 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import com.google.protobuf.gradle.GenerateProtoTask
import com.google.protobuf.gradle.id
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.merge.MergeStrategy
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.io.ByteArrayOutputStream
plugins {
kotlin("jvm") version "1.8.21"
id("com.google.protobuf") version "0.9.3"
id("maven-publish")
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.eclipse.jgit:org.eclipse.jgit:6.6.0.202305301015-r")
}
}
group = "com.github.moodletracker"
version = "1.0.0"
repositories {
mavenCentral()
}
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.8.21")
api("com.google.protobuf:protobuf-java:3.23.0")
api("com.google.protobuf:protobuf-kotlin:3.23.0")
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "17"
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.23.0"
}
plugins {
id("kotlin")
}
generateProtoTasks {
all().configureEach {
builtins {
kotlin {}
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
// check if protoc-gen-go is installed
if (exec {
commandLine("bash", "-c", "command -v protoc-gen-go")
isIgnoreExitValue = true
standardOutput = ByteArrayOutputStream()
}.exitValue == 0) {
logger.info("protoc-gen-go found, generating Go sources")
create("go")
} else {
logger.warn("protoc-gen-go not found, skipping generation of Go sources")
}
}
plugins {
id("kotlin")
}
}
}
}
// create task that checks if protoc-gen-go is installed
val checkGo = project.tasks.register("checkProtocGenGo", Exec::class.java) {
commandLine("protoc-gen-go", "--version")
isIgnoreExitValue = true
doLast {
if (executionResult.get().exitValue != 0) {
throw Exception("protoc-gen-go not found")
}
}
}
publishing {
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/MoodleTracker/Protocol")
credentials {
username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME")
password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN")
}
}
}
publications {
register<MavenPublication>("gpr") {
from(components["java"])
}
}
}
tasks.register("publishGo") {
val generateProtoTask = tasks.getByName<GenerateProtoTask>("generateProto")
dependsOn(checkGo, generateProtoTask)
doLast {
val goOutputPath = generateProtoTask.getOutputDir(GenerateProtoTask.PluginOptions("go"))
?.let { "$it/github.com/moodletracker" }
?: throw Exception("Go output path not found")
val path = project.findProperty("publish.go.repopath") ?: (project.rootDir.parent + "/go")
val gitDirectory = file(path)
if (gitDirectory.exists()) {
gitDirectory.deleteRecursively()
}
val url =
project.findProperty("publish.go.giturl")?.toString() ?: "https://github.com/MoodleTracker/Protocol-Go.git"
logger.debug("Cloning Go repository from {} to {}", url, gitDirectory)
val git = Git.cloneRepository()
.setDirectory(gitDirectory)
.setURI(url)
.call()
logger.debug("Cloned Go repository to {}", gitDirectory)
logger.debug("Copying Go sources from {} to {}", goOutputPath, gitDirectory)
file(goOutputPath).copyRecursively(gitDirectory, true)
logger.debug("Copied Go sources to {}", gitDirectory)
git.add().addFilepattern(".").call()
val commitHash = project.findProperty("publish.go.commit")?.toString()?.let { " (${it})" } ?: ""
val commit = git.commit()
.setAuthor("MoodleTracker Bot", "")
.setSign(false)
.setMessage("Update Go sources for version ${project.version}$commitHash")
.call()
git.checkout().setName("main").call()
git.merge()
.include(commit.id)
.setSquash(true)
.setStrategy(MergeStrategy.THEIRS)
.setCommit(false)
.call()
git.tag()
.setName("v${project.version}")
.setForceUpdate(true)
.call()
val credentials = UsernamePasswordCredentialsProvider(
project.findProperty("publish.go.git-user")?.toString() ?: System.getenv("GIT_USER") ?: throw Exception(
"No GIT_USER set"
),
project.findProperty("publish.go.git-password")?.toString() ?: System.getenv("GIT_PASSWORD")
?: throw Exception(
"No GIT_PASSWORD set"
)
)
git.push()
.setCredentialsProvider(credentials)
.setPushAll()
.call()
git.push()
.setCredentialsProvider(credentials)
.setPushTags()
.call()
}
}