Skip to content

Commit aafd944

Browse files
committed
gradle插件
1 parent 5efff45 commit aafd944

File tree

21 files changed

+353
-169
lines changed

21 files changed

+353
-169
lines changed

Example/build.gradle.kts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
1+
import kotlin.io.path.Path
2+
13
plugins {
24
kotlin("jvm") version "1.9.23"
5+
6+
id("top.mcfpp.gradle") version "1.0-SNAPSHOT"
7+
38
}
49

510
group = "org.example"
611
version = "1.0-SNAPSHOT"
712

813
repositories {
914
mavenCentral()
15+
maven {
16+
url = uri("../build/repo")
17+
}
1018
}
1119

1220
dependencies {
1321
testImplementation("org.jetbrains.kotlin:kotlin-test")
14-
implementation(files("G:\\AST\\MCFPP\\build\\libs\\MCFPP-1.0-SNAPSHOT.jar"))
22+
implementation("top.mcfpp:mcfpp-gradle:1.0-SNAPSHOT")
1523
}
1624

1725
tasks.test {
1826
useJUnitPlatform()
1927
}
28+
2029
kotlin {
2130
jvmToolchain(21)
31+
}
32+
33+
mcfpp {
34+
version = "1.21"
35+
description = "qwq"
36+
targetPath = Path("")
2237
}

Example/settings.gradle.kts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
plugins {
2-
id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0"
3-
}
41
rootProject.name = "Example"
52

3+
pluginManagement {
4+
repositories {
5+
maven {
6+
url = uri("../build/repo")
7+
}
8+
gradlePluginPortal()
9+
maven("https://jitpack.io/")
10+
maven("https://libraries.minecraft.net")
11+
}
12+
}
13+
14+
plugins {
15+
id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0"
16+
}

Example/src/main/kotlin/Main.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.example
22

3-
import top.mcfpp.main
3+
import top.mcfpp.MCFPP
44

5-
fun main() {
6-
println("Hello World!")
5+
fun main(){
6+
println(MCFPP.version)
77
}

Example/src/main/mcfpp/hello.mcfpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func test(){
2+
print("qwq");
3+
}

build.gradle.kts

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import org.gradle.kotlin.dsl.cpp
2+
import org.jetbrains.kotlin.fir.declarations.builder.buildScript
23
import java.nio.file.Files
4+
import java.nio.file.Path
5+
import java.nio.file.Paths
36
import java.util.Optional
47

58
plugins {
@@ -10,16 +13,21 @@ plugins {
1013
id("org.jetbrains.dokka") version "1.9.20"
1114
java
1215
cpp
16+
id("java-gradle-plugin")
17+
id("maven-publish")
18+
id("com.gradleup.shadow") version "8.3.5"
1319
}
1420

15-
group = "top.mcfpp"
16-
version = "1.0-SNAPSHOT"
21+
val GROUP = "top.mcfpp"
22+
val VERSION = "1.0-SNAPSHOT"
23+
24+
group = GROUP
25+
version = VERSION
1726

1827
repositories {
1928
mavenCentral()
2029
maven("https://jitpack.io")
2130
maven("https://maven.aliyun.com/nexus/content/groups/public/")
22-
maven("https://jitpack.io/")
2331
maven("https://libraries.minecraft.net")
2432
}
2533

@@ -29,6 +37,7 @@ dependencies {
2937
implementation("com.alibaba.fastjson2:fastjson2:2.0.28")
3038
implementation("org.apache.logging.log4j:log4j-api:2.20.0")
3139
implementation("org.apache.logging.log4j:log4j-core:2.20.0")
40+
implementation("org.apache.logging.log4j:log4j-slf4j-impl:2.20.0")
3241
implementation("org.openjdk.nashorn:nashorn-core:15.4")
3342
implementation("com.github.Querz:NBT:6.1")
3443
implementation("com.mojang:brigadier:1.0.18")
@@ -44,10 +53,43 @@ dependencies {
4453
implementation("com.google.guava:guava:33.2.0-jre")
4554
}
4655

56+
tasks.shadowJar {
57+
minimize()
58+
}
59+
60+
gradlePlugin {
61+
plugins {
62+
create("mcfpp") {
63+
id = "top.mcfpp.gradle"
64+
implementationClass = "top.mcfpp.gradle.MCFPPGradlePlugin"
65+
}
66+
}
67+
}
68+
69+
publishing {
70+
publications {
71+
create<MavenPublication>("mavenJava") {
72+
from(components["java"])
73+
groupId = GROUP
74+
artifactId = "mcfpp-gradle"
75+
version = VERSION
76+
}
77+
}
78+
repositories {
79+
maven {
80+
url = Paths.get("${layout.buildDirectory.get().asFile.absolutePath}/repo").toUri()
81+
}
82+
}
83+
}
84+
4785
tasks.test {
4886
useJUnitPlatform()
4987
}
5088

89+
tasks.compileTestKotlin {
90+
dependsOn("generateTestGrammarSource")
91+
}
92+
5193
tasks.generateGrammarSource {
5294
mkdir("build")
5395
arguments = arguments +
@@ -67,6 +109,10 @@ tasks.jar{
67109
into("native")
68110
include("**/*.dll")
69111
}
112+
113+
exclude("META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA")
114+
115+
isZip64 = true
70116
}
71117

72118
val jniSourceDir = file("src/main/java/top/mcfpp/jni")
@@ -180,4 +226,4 @@ enum class OperatingSystem {
180226
}
181227
}
182228
}
183-
}
229+
}

src/main/kotlin/top/mcfpp/MCFPP.kt

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import top.mcfpp.model.field.GlobalField
77
import top.mcfpp.util.LogProcessor
88
import top.mcfpp.util.UwU
99
import java.io.FileInputStream
10+
import java.nio.file.Files
11+
import kotlin.io.path.Path
12+
import kotlin.io.path.absolutePathString
1013

1114
/**
1215
* 编译器的启动入口
@@ -21,40 +24,60 @@ fun main(args: Array<String>) {
2124
println("Failed to load log4j2.xml")
2225
}
2326
if (args.isNotEmpty()) {
24-
if(args.size > 1){
25-
if(args.contains("debug")){
26-
CompileSettings.isDebug = true
27-
LogProcessor.warn("Compiling in debug mode.")
28-
}
29-
}
30-
val start: Long = System.currentTimeMillis()
27+
parseArgs(args.asList().subList(1, args.size))
28+
3129
LogProcessor.info("Tips: " + UwU.tip) //生成tips
30+
3231
val path = args[0]
33-
Project.stageProcessor[Project.compileStage].forEach { it() }
34-
Project.init() //初始化
35-
Project.readProject(path) //读取配置文件
36-
Project.readLib() //读取引用的库的索引
37-
Project.indexType() //编制类型索引
38-
Project.resolveField() //编制函数索引
39-
Project.runAnnotation() //执行注解
40-
Project.compile() //编译
41-
Project.optimization() //优化
42-
Project.genIndex() //生成索引
43-
Project.ctx = null
44-
if(!Project.config.noDatapack){
45-
Project.compileStage++
46-
try{
47-
DatapackCreator.createDatapack(Project.config.targetPath) //生成数据包
48-
}catch (e: Exception){
49-
LogProcessor.error("Cannot create datapack in path: ${Project.config.targetPath}", e)
50-
}
51-
Project.stageProcessor[Project.compileStage].forEach { it() }
32+
if(!Files.exists(Path(path))){
33+
LogProcessor.error("Cannot find file: $path")
5234
}
53-
LogProcessor.info("Finished in " + (System.currentTimeMillis() - start) + "ms")
35+
compile(Project.readConfig(path)) //读取配置文件
5436
GlobalField.printAll()
5537
}
5638
}
5739

40+
fun compile(config: ProjectConfig){
41+
val start: Long = System.currentTimeMillis()
42+
43+
Project.config = config
44+
Project.compileStage = 0
45+
Project.stageProcessor[0].forEach { it() }
46+
Project.init() //初始化
47+
Project.checkConfig() //检查配置文件
48+
Project.readProject() //读取引用的库的索引
49+
Project.indexType() //编制类型索引
50+
Project.resolveField() //编制函数索引
51+
Project.runAnnotation() //执行注解
52+
Project.compile() //编译
53+
Project.optimization() //优化
54+
Project.genIndex() //生成索引
55+
Project.ctx = null
56+
if(!Project.config.noDatapack){
57+
Project.compileStage++
58+
try{
59+
DatapackCreator.createDatapack(Project.config.targetPath!!.absolutePathString()) //生成数据包
60+
}catch (e: Exception){
61+
LogProcessor.error("Cannot create datapack in path: ${Project.config.targetPath}", e)
62+
}
63+
Project.stageProcessor[Project.compileStage].forEach { it() }
64+
}
65+
66+
LogProcessor.info("Finished in " + (System.currentTimeMillis() - start) + "ms")
67+
}
68+
5869
object MCFPP {
59-
val version = "0.1.0"
70+
const val version = "0.1.0"
71+
}
72+
73+
fun parseArgs(args: List<String>){
74+
75+
for (arg in args){
76+
when(arg){
77+
"-debug" -> CompileSettings.isDebug = true
78+
"-ignoreStdLib" -> CompileSettings.ignoreStdLib = true
79+
"-isLib" -> CompileSettings.isLib = true
80+
}
81+
}
82+
6083
}

0 commit comments

Comments
 (0)