This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.gradle.kts
233 lines (201 loc) · 7.25 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import org.apache.tools.ant.taskdefs.condition.Os
import java.security.MessageDigest
plugins {
java
`maven-publish`
signing
}
group = "org.polystat"
version = "0.0.3"
val mvnUsername: String? by project
val mvnPassword: String? by project
val mvnPublicationVersion: String? by project
val candidates: String? by project
// The Java grammar source file for Bison
val javaGrammarFilePath = "src/main/resources/Java_16_Grammar.y"
// Where to put Bison compilation report
val reportFilePath = "out/Java_16_Grammar.report"
// Where to put generated parser
val javaParserFilePath = "src/main/java/parser/JavaParser.java"
// MD5 of the latest generated grammar file is stored here
val latestGrammarMD5FilePath = "out/latestGrammarMD5"
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
withSourcesJar()
}
repositories {
mavenCentral()
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
// Library for command-line arguments support
implementation("commons-cli:commons-cli:1.5.0")
}
tasks.getByName<Test>("test") {
useJUnitPlatform()
}
tasks.getByName("build") {
createOutDirs()
// Only run Bison build if grammar file changed
if (readMD5FromFile(latestGrammarMD5FilePath) != grammarFileMD5()) {
println("Grammar file changed since last build; Rebuilding parser with Bison...")
runBison()
writeMD5ToFile(grammarFileMD5(), latestGrammarMD5FilePath)
} else {
println("Grammar file didn't change; Skipping parser build.")
}
}
publishing {
publications {
create<MavenPublication>("mavenJava") {
groupId = "org.polystat"
artifactId = "j2ast"
version = listOfNotNull(mvnPublicationVersion, project.version as String).first()
from(components["java"])
pom {
name.set("j2ast")
description.set("Java parser")
url.set("https://github.com/polystat/j2ast")
// properties.set(mapOf(
// "myProp" to "value",
// "prop.with.dots" to "anotherValue"
// ))
licenses {
license {
name.set("MIT")
url.set("https://github.com/polystat/polystat/blob/master/LICENSE.txt")
}
}
developers {
developer {
id.set("zouev")
name.set("Eugene Zouev")
email.set("e.zuev@innopolis.ru")
}
developer {
id.set("IamMaxim58")
name.set("Maxim Stepanov")
email.set("m.stepanov@innopolis.university")
}
developer {
id.set("Ilia_Mil")
name.set("Ilya Milyoshin")
email.set("i.mileshin@innopolis.university")
}
developer {
id.set("egorklementev")
name.set("Egor Klementev")
email.set("e.klementev@innopolis.ru")
}
}
scm {
connection.set("scm:git:git@github.com:polystat/j2ast.git")
developerConnection.set("scm:git:git@github.com:polystat/j2ast.git")
url.set("https://github.com/polystat/j2ast")
}
}
repositories {
maven {
credentials {
println("Applying Maven credentials")
username = mvnUsername
password = mvnPassword
}
url = uri("https://s01.oss.sonatype.org/content/repositories/releases/")
}
}
}
}
}
/**
* Creates directories for all Bison output files.
*/
fun createOutDirs() {
// Create out directory if it doesn't exist, so report may be placed inside
val outPath = reportFilePath.substring(0, reportFilePath.lastIndexOf("/"))
file(outPath).mkdirs()
// Create parser directory if it doesn't exist, so parser may be placed inside
val parserPath = javaParserFilePath.substring(0, javaParserFilePath.lastIndexOf("/"))
file(parserPath).mkdirs()
}
/**
* Runs Bison using OS-specific shell command.
*/
fun runBison() =
try {
when {
Os.isFamily(Os.FAMILY_WINDOWS) ->
exec {
workingDir = File(".")
executable = "bin/win_bison.exe"
args = mutableListOf(
"--report=states,lookaheads",
// "-r", "all",
// "--debug", "--help", "--stacktrace",
"--report-file=${reportFilePath}",
"--output=${javaParserFilePath}",
javaGrammarFilePath
)
}
Os.isFamily(Os.FAMILY_MAC) ->
exec {
workingDir = File(".")
executable = "bin/bison_mac"
args = mutableListOf(
"--report=states,lookaheads",
"--report-file=${reportFilePath}",
"--output=${javaParserFilePath}",
javaGrammarFilePath
)
}
Os.isFamily(Os.FAMILY_UNIX) ->
exec {
workingDir = File(".")
executable = "bison"
args = mutableListOf(
"--report=states,lookaheads",
"--report-file=${reportFilePath}",
"--output=${javaParserFilePath}",
javaGrammarFilePath
)
}
else ->
throw UnsupportedOperationException(
"Your OS is not yet supported. File a GitHub or issue or " +
"provide a Pull Request with support for Bison execution for your OS."
)
}
} catch (e: Exception) {
e.printStackTrace()
}
/**
* Returns MD5 string for a given file.
*/
fun generateMD5(filepath: String): String {
val digest: MessageDigest = MessageDigest.getInstance("MD5")
println("Working Directory = " + System.getProperty("user.dir"))
File(filepath).inputStream().use { inputStream ->
val buffer = ByteArray(8192)
var read = 0
do {
digest.update(buffer, 0, read)
read = inputStream.read(buffer)
} while (read > 0)
}
val md5sum: ByteArray = digest.digest()
val bigInt = BigInteger(1, md5sum)
return bigInt.toString(16).padStart(32, '0')
}
fun grammarFileMD5(): String =
generateMD5(javaGrammarFilePath)
fun readMD5FromFile(filepath: String): String =
File(filepath).let { f ->
if (f.exists())
File(filepath).readText()
else
""
}
fun writeMD5ToFile(md5: String, filepath: String) =
File(filepath).writeText(md5)