Skip to content

Commit eb8aa07

Browse files
committed
Initial dev commit
1 parent de5dd7b commit eb8aa07

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2325
-0
lines changed

.gitattributes

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# These are explicitly windows files and should use crlf
5+
*.bat text eol=crlf
6+

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.gradle
2+
.idea
3+
4+
build
5+
6+
output
7+
project

app/app.iml

+191
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module relativePaths="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true" LANGUAGE_LEVEL="JDK_14">
4+
<exclude-output/>
5+
<orderEntry type="inheritedJdk"/>
6+
<content url="file://$MODULE_DIR$/">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false"/>
8+
<sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true"/>
9+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource"/>
10+
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource"/>
11+
<excludeFolder url="file://$MODULE_DIR$/.gradle"/>
12+
<excludeFolder url="file://$MODULE_DIR$/build"/>
13+
</content>
14+
<orderEntry type="sourceFolder" forTests="false"/>
15+
<orderEntry type="module-library">
16+
<library>
17+
<CLASSES>
18+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk8/1.5.31/ff5d99aecd328872494e8921b72bf6e3af97af3e/kotlin-stdlib-jdk8-1.5.31.jar!/"/>
19+
</CLASSES>
20+
<JAVADOC/>
21+
<SOURCES>
22+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk8/1.5.31/171265ccd1f7a4307c3cb8eedd9115e662013190/kotlin-stdlib-jdk8-1.5.31-sources.jar!/"/>
23+
</SOURCES>
24+
</library>
25+
</orderEntry>
26+
<orderEntry type="module-library">
27+
<library>
28+
<CLASSES>
29+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/30.1.1-jre/87e0fd1df874ea3cbe577702fe6f17068b790fd8/guava-30.1.1-jre.jar!/"/>
30+
</CLASSES>
31+
<JAVADOC/>
32+
<SOURCES>
33+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/30.1.1-jre/fac882f35fd501581954dcaffa410ee4b5cd8d1b/guava-30.1.1-jre-sources.jar!/"/>
34+
</SOURCES>
35+
</library>
36+
</orderEntry>
37+
<orderEntry type="module-library">
38+
<library>
39+
<CLASSES>
40+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.5.31/77e0f2568912e45d26c31fd417a332458508acdf/kotlin-stdlib-jdk7-1.5.31.jar!/"/>
41+
</CLASSES>
42+
<JAVADOC/>
43+
<SOURCES>
44+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.5.31/ccdfdab71f0f3ba104191d8f673d0620e7ed2e46/kotlin-stdlib-jdk7-1.5.31-sources.jar!/"/>
45+
</SOURCES>
46+
</library>
47+
</orderEntry>
48+
<orderEntry type="module-library">
49+
<library>
50+
<CLASSES>
51+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.5.31/6628d61d0f5603568e72d2d5915d2c034b4f1c55/kotlin-stdlib-1.5.31.jar!/"/>
52+
</CLASSES>
53+
<JAVADOC/>
54+
<SOURCES>
55+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.5.31/70f4e47092d0158a39aafc634ad60b6320c3d6ce/kotlin-stdlib-1.5.31-sources.jar!/"/>
56+
</SOURCES>
57+
</library>
58+
</orderEntry>
59+
<orderEntry type="module-library">
60+
<library>
61+
<CLASSES>
62+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.5.31/43331609c7de811fed085e0dfd150874b157c32/kotlin-stdlib-common-1.5.31.jar!/"/>
63+
</CLASSES>
64+
<JAVADOC/>
65+
<SOURCES>
66+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.5.31/ee22598c6d20e94e0fd08cc696f09f83e41b3935/kotlin-stdlib-common-1.5.31-sources.jar!/"/>
67+
</SOURCES>
68+
</library>
69+
</orderEntry>
70+
<orderEntry type="module-library">
71+
<library>
72+
<CLASSES>
73+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/com.google.guava/failureaccess/1.0.1/1dcf1de382a0bf95a3d8b0849546c88bac1292c9/failureaccess-1.0.1.jar!/"/>
74+
</CLASSES>
75+
<JAVADOC/>
76+
<SOURCES>
77+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/com.google.guava/failureaccess/1.0.1/1d064e61aad6c51cc77f9b59dc2cccc78e792f5a/failureaccess-1.0.1-sources.jar!/"/>
78+
</SOURCES>
79+
</library>
80+
</orderEntry>
81+
<orderEntry type="module-library">
82+
<library>
83+
<CLASSES>
84+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/com.google.guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/b421526c5f297295adef1c886e5246c39d4ac629/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar!/"/>
85+
</CLASSES>
86+
<JAVADOC/>
87+
<SOURCES/>
88+
</library>
89+
</orderEntry>
90+
<orderEntry type="module-library">
91+
<library>
92+
<CLASSES>
93+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/com.google.code.findbugs/jsr305/3.0.2/25ea2e8b0c338a877313bd4672d3fe056ea78f0d/jsr305-3.0.2.jar!/"/>
94+
</CLASSES>
95+
<JAVADOC/>
96+
<SOURCES>
97+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/com.google.code.findbugs/jsr305/3.0.2/b19b5927c2c25b6c70f093767041e641ae0b1b35/jsr305-3.0.2-sources.jar!/"/>
98+
</SOURCES>
99+
</library>
100+
</orderEntry>
101+
<orderEntry type="module-library">
102+
<library>
103+
<CLASSES>
104+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.checkerframework/checker-qual/3.8.0/6b83e4a33220272c3a08991498ba9dc09519f190/checker-qual-3.8.0.jar!/"/>
105+
</CLASSES>
106+
<JAVADOC/>
107+
<SOURCES>
108+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.checkerframework/checker-qual/3.8.0/e2256c40ffa8bbca3a82727dd1e262904cc2f922/checker-qual-3.8.0-sources.jar!/"/>
109+
</SOURCES>
110+
</library>
111+
</orderEntry>
112+
<orderEntry type="module-library">
113+
<library>
114+
<CLASSES>
115+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/com.google.errorprone/error_prone_annotations/2.5.1/562d366678b89ce5d6b6b82c1a073880341e3fba/error_prone_annotations-2.5.1.jar!/"/>
116+
</CLASSES>
117+
<JAVADOC/>
118+
<SOURCES>
119+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/com.google.errorprone/error_prone_annotations/2.5.1/600a12b3f1ec5aab8f10aff4e11cd28c6ee52697/error_prone_annotations-2.5.1-sources.jar!/"/>
120+
</SOURCES>
121+
</library>
122+
</orderEntry>
123+
<orderEntry type="module-library">
124+
<library>
125+
<CLASSES>
126+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/com.google.j2objc/j2objc-annotations/1.3/ba035118bc8bac37d7eff77700720999acd9986d/j2objc-annotations-1.3.jar!/"/>
127+
</CLASSES>
128+
<JAVADOC/>
129+
<SOURCES>
130+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/com.google.j2objc/j2objc-annotations/1.3/d26c56180205cbb50447c3eca98ecb617cf9f58b/j2objc-annotations-1.3-sources.jar!/"/>
131+
</SOURCES>
132+
</library>
133+
</orderEntry>
134+
<orderEntry type="module-library">
135+
<library>
136+
<CLASSES>
137+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.jetbrains/annotations/13.0/919f0dfe192fb4e063e7dacadee7f8bb9a2672a9/annotations-13.0.jar!/"/>
138+
</CLASSES>
139+
<JAVADOC/>
140+
<SOURCES>
141+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.jetbrains/annotations/13.0/5991ca87ef1fb5544943d9abc5a9a37583fabe03/annotations-13.0-sources.jar!/"/>
142+
</SOURCES>
143+
</library>
144+
</orderEntry>
145+
<orderEntry type="module-library" scope="TEST">
146+
<library>
147+
<CLASSES>
148+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-test-junit/1.5.31/eccef3973cd6e54820d992c6b58b51922c7cf557/kotlin-test-junit-1.5.31.jar!/"/>
149+
</CLASSES>
150+
<JAVADOC/>
151+
<SOURCES>
152+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-test-junit/1.5.31/cd79773b5e950e75857393dcd2530c5cbc74bfb9/kotlin-test-junit-1.5.31-sources.jar!/"/>
153+
</SOURCES>
154+
</library>
155+
</orderEntry>
156+
<orderEntry type="module-library" scope="TEST">
157+
<library>
158+
<CLASSES>
159+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-test/1.5.31/a2fdf12f8d0013331e28ced69286fde14b93eed9/kotlin-test-1.5.31.jar!/"/>
160+
</CLASSES>
161+
<JAVADOC/>
162+
<SOURCES>
163+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-test/1.5.31/62bed326a5c862f757501efbb7c34d2b7dd26006/kotlin-test-1.5.31-sources.jar!/"/>
164+
</SOURCES>
165+
</library>
166+
</orderEntry>
167+
<orderEntry type="module-library" scope="TEST">
168+
<library>
169+
<CLASSES>
170+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/junit/junit/4.12/2973d150c0dc1fefe998f834810d68f278ea58ec/junit-4.12.jar!/"/>
171+
</CLASSES>
172+
<JAVADOC/>
173+
<SOURCES>
174+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/junit/junit/4.12/a6c32b40bf3d76eca54e3c601e5d1470c86fcdfa/junit-4.12-sources.jar!/"/>
175+
</SOURCES>
176+
</library>
177+
</orderEntry>
178+
<orderEntry type="module-library" scope="TEST">
179+
<library>
180+
<CLASSES>
181+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar!/"/>
182+
</CLASSES>
183+
<JAVADOC/>
184+
<SOURCES>
185+
<root url="jar:///home/anthony/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/1dc37250fbc78e23a65a67fbbaf71d2e9cbc3c0b/hamcrest-core-1.3-sources.jar!/"/>
186+
</SOURCES>
187+
</library>
188+
</orderEntry>
189+
</component>
190+
<component name="ModuleRootManager"/>
191+
</module>

app/bin/main/shorthandcommands/App.kt

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* Copyright © 2022 antD97
3+
* Licensed under the MIT License https://antD.mit-license.org/
4+
*/
5+
package shorthandcommands
6+
7+
import java.io.File
8+
import java.io.IOException
9+
import java.io.StringReader
10+
import java.util.*
11+
import kotlin.system.exitProcess
12+
13+
fun main() {
14+
15+
// load from properties file
16+
val properties = Properties()
17+
// replace \ with \\ before loading the configuration file
18+
File("shorthand.conf").readText()
19+
.replace("\\", "\\\\")
20+
.let { StringReader(it).use { sr -> properties.load(sr) } }
21+
22+
val projDir = File(properties.getProperty("project") ?: badConfig("project"))
23+
24+
// project directory check
25+
if (!projDir.isDirectory) {
26+
println("Project directory `${projDir.path}` from `shorthand.conf` is not a directory." +
27+
"\nExiting...")
28+
exitProcess(-1)
29+
}
30+
31+
val buildDir = File(properties.getProperty("build") ?: badConfig("build"))
32+
33+
// build directory check
34+
if (!buildDir.isDirectory) {
35+
println("Build directory `${buildDir.path}` from `shorthand.conf` is not a directory." +
36+
"\nExiting...")
37+
exitProcess(-1)
38+
}
39+
40+
val transformedProjDir = buildDir.resolve(projDir.name)
41+
42+
// transformed project directory check
43+
if (transformedProjDir.isFile) {
44+
println("The save location for the converted project `${transformedProjDir.path}` is a " +
45+
"file and will not be overwritten. Please move or delete this file and run the " +
46+
"tool again." +
47+
"\nExiting...")
48+
exitProcess(-1)
49+
}
50+
51+
// transformed project directory = build directory check
52+
if (projDir.canonicalFile == transformedProjDir.canonicalFile) {
53+
println("The save location for the converted project is the same as the project " +
54+
"directory `${projDir.path}`. Please change the build location in " +
55+
"`shorthand.conf` file and run the tool again." +
56+
"\nExiting...")
57+
exitProcess(-1)
58+
}
59+
60+
// data directory & `pack.mcmeta` check
61+
val projDataDir = projDir.listFiles().find { it.name == "data" && it.isDirectory }
62+
val projPackFile = projDir.listFiles().find { it.name == "pack.mcmeta" && it.isFile }
63+
if (projDataDir == null || projPackFile == null) {
64+
println("The project directory `${projDir.path}` needs to contain a data directory and " +
65+
"`pack.mcmeta` file." +
66+
"\nExiting...")
67+
exitProcess(-1)
68+
}
69+
70+
var headerFile: File? = File("header.txt")
71+
72+
// header file check
73+
if (!headerFile!!.isFile) {
74+
println("Could not locate a `header.txt` file. No header will be added for newly created " +
75+
".mcfunction files.")
76+
headerFile = null
77+
}
78+
79+
// delete old transformed project directory
80+
if (transformedProjDir.exists()) {
81+
82+
val transformedProjPackFile = transformedProjDir.listFiles()
83+
.find { it.name == "pack.mcmeta" && it.isFile }
84+
85+
// directory to overwrite is not a datapack
86+
if (transformedProjDir.listFiles().none { it.name == "data" && it.isDirectory }
87+
|| transformedProjPackFile == null) {
88+
println("The save location for the converted project `${transformedProjDir.path}` is " +
89+
"not a datapack and will not be overwritten. Please move or delete this " +
90+
"directory and run the tool again." +
91+
"\nExiting...")
92+
exitProcess(-1)
93+
}
94+
95+
// directory to overwrite does not use the same `pack.mcmeta` file
96+
else if (projPackFile.readText() != transformedProjPackFile.readText()) {
97+
println("The save location for the converted project `${transformedProjDir.path}` " +
98+
"has a different `pack.mcmeta` file than the source project " +
99+
"`${projDir.path}` and will not be overwritten. Please move or delete this " +
100+
"project directory and run the tool again." +
101+
"\nExiting...")
102+
exitProcess(-1)
103+
}
104+
105+
// directory to overwrite is the same datapack and is safe to overwrite
106+
else {
107+
print("Okay to delete the previously converted project " +
108+
"`${transformedProjDir.path}`? (y/n): ")
109+
if (readLine()!!.lowercase() in listOf("y", "yes")) {
110+
111+
println("Deleting `$transformedProjDir`...")
112+
if (!transformedProjDir.deleteRecursively()) {
113+
println("Failed to delete `${transformedProjDir.path}`\nExiting...")
114+
exitProcess(-1)
115+
}
116+
} else {
117+
println("Exiting...")
118+
exitProcess(-1)
119+
}
120+
}
121+
}
122+
123+
// find each functions directory
124+
val projFunctionsDirs = projDataDir.listFiles()
125+
.filter { it.name != "minecraft" && it.isDirectory }
126+
.filter {
127+
val containsFunctionsDir = it.listFiles().any {
128+
it.name == "functions" && it.isDirectory
129+
}
130+
if (!containsFunctionsDir) println("Namespace directory `${it.path}` does not " +
131+
"contain a `function` directory. Skipping...")
132+
containsFunctionsDir
133+
}
134+
.map { it.resolve("functions") }
135+
136+
// copy the source project directory
137+
println("Copying source project...")
138+
projDir.copyDir(transformedProjDir, projFunctionsDirs)
139+
140+
// transform function directories
141+
for (functionDir in projFunctionsDirs) {
142+
Transformer.transformFunctionDir(
143+
functionDir,
144+
transformedProjDir.resolve(functionDir.relativeTo(projDir)),
145+
headerFile
146+
)
147+
}
148+
149+
println("Done.")
150+
}
151+
152+
fun badConfig(property: String): String {
153+
println("\"$property\" missing from `shorthand.conf`.\nExiting...")
154+
exitProcess(-1)
155+
}
156+
157+
/** Recursively copies [this] file/directory to [targetLoc] while skipping [skipFiles]. */
158+
fun File.copyDir(targetLoc: File, skipFiles: List<File>) {
159+
160+
// skip
161+
if (this in skipFiles) return
162+
163+
// copy directory
164+
if (isDirectory) {
165+
try {
166+
if (!targetLoc.mkdir()) {
167+
println("Failed to create directory: `${targetLoc.path}`\nExiting...")
168+
exitProcess(-1)
169+
}
170+
} catch (e: IOException) {
171+
println("Failed to create directory: `${targetLoc.path}`\nExiting...")
172+
exitProcess(-1)
173+
}
174+
175+
this.listFiles().forEach { it.copyDir(targetLoc.resolve(it.name), skipFiles) }
176+
}
177+
178+
// copy file
179+
else {
180+
try {
181+
this.copyTo(targetLoc)
182+
} catch (e: IOException) {
183+
println("Failed to create file: `${targetLoc.name}`\nExiting...")
184+
exitProcess(-1)
185+
}
186+
}
187+
}

0 commit comments

Comments
 (0)