Skip to content

Commit

Permalink
Module Manager & Standalone Improvements
Browse files Browse the repository at this point in the history
* Improved dependency load sorter
* Improved folder system for Standalone software
* Fixed modules not working for Standalone software
* Improved gradle-test workflow
  • Loading branch information
Im-Fran committed Apr 14, 2023
1 parent 43f8f8b commit 2fd15f8
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 66 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/gradle-test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: "Test"
# Only execute this workflow when a PR is opened or when something is pushed to the master branch
on: [pull_request]
on:
pull_request:
branches: [ master ]
jobs:
testBuilds:
strategy:
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Temporary Items
build/
bin/
*.pom.xml
modules/
plugins/
./modules/
./plugins/
./SimpleCoreAPI/
!gradle-wrapper.jar
30 changes: 15 additions & 15 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/discord.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 14 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ val env = project.rootProject.file(".env").let { file ->
if(file.exists()) file.readLines().filter { it.isNotBlank() && !it.startsWith("#") && it.split("=").size == 2 }.associate { it.split("=")[0] to it.split("=")[1] } else emptyMap()
}.toMutableMap().apply { putAll(System.getenv()) }

val projectVersion = env["VERSION"] ?: "0.6.2-SNAPSHOT"
val projectVersion = env["VERSION"] ?: "0.6.3-SNAPSHOT"

group = "xyz.theprogramsrc"
version = projectVersion.replaceFirst("v", "").replace("/", "")
Expand Down Expand Up @@ -59,6 +59,10 @@ blossom {

tasks {
named<ShadowJar>("shadowJar") {
manifest {
attributes["Main-Class"] = "xyz.theprogramsrc.simplecoreapi.standalone.StandaloneLoaderKt"
}

relocate("org.apache.commons", "xyz.theprogramsrc.simplecoreapi.libs.apache.commons")
relocate("org.checkerframework", "xyz.theprogramsrc.simplecoreapi.libs.checkerframework")
relocate("org.intellij", "xyz.theprogramsrc.simplecoreapi.libs.intellij")
Expand Down Expand Up @@ -183,14 +187,16 @@ publishing {
}
}

nexusPublishing {
repositories {
sonatype {
nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/"))
snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/"))
if(env["ENV"] == "prod") {
nexusPublishing {
repositories {
sonatype {
nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/"))
snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/"))

username.set(env["SONATYPE_USERNAME"])
password.set(env["SONATYPE_PASSWORD"])
username.set(env["SONATYPE_USERNAME"])
password.set(env["SONATYPE_PASSWORD"])
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import xyz.theprogramsrc.simplecoreapi.global.module.ModuleManager
import xyz.theprogramsrc.simplecoreapi.global.utils.ILogger
import xyz.theprogramsrc.simplecoreapi.global.utils.SoftwareType
import xyz.theprogramsrc.simplecoreapi.global.utils.update.GitHubUpdateChecker
import java.util.*
import xyz.theprogramsrc.simplecoreapi.standalone.StandaloneLoader
import java.io.File

/**
* Class used to initialize SimpleCoreAPI (DO NOT CALL IT FROM EXTERNAL PLUGINS, IT MAY CRASH)
Expand All @@ -19,6 +20,14 @@ class SimpleCoreAPI(logger: ILogger) {
*/
lateinit var instance: SimpleCoreAPI
private set

/**
* Gets a file relative to the data folder.
* If running in standalone mode the data folder will be ./SimpleCoreAPI, otherwise it will be the plugins/SimpleCoreAPI folder
*
* @return The file relative to the data folder
*/
fun dataFolder(path: String = ""): File = File(if (StandaloneLoader.isRunning) "./SimpleCoreAPI" else "plugins/SimpleCoreAPI", path)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package xyz.theprogramsrc.simplecoreapi.global.module

import com.google.gson.JsonObject
import com.google.gson.JsonParser
import xyz.theprogramsrc.simplecoreapi.global.SimpleCoreAPI
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
Expand All @@ -19,10 +20,10 @@ object ModuleHelper {
* Downloads a Module from the database
* @param repository Repository of a module to download. (Must be in GitHub format 'User/Repository'. Example: 'TheProgramSrc/SimpleCore-UIsModule')
* @param fileName The name of the file. This can be fetched using the repository metadata.
* @param downloadLocation Location to download the module. (Defaults to plugins/SimpleCoreAPI/modules/)
* @param downloadLocation Location to download the module. (Defaults to SimpleCoreAPI/modules/)
* @return true if the module was downloaded, false otherwise
*/
fun downloadModule(repository: String, fileName: String, downloadLocation: File = File("plugins/SimpleCoreAPI/modules/")): Boolean{
fun downloadModule(repository: String, fileName: String, downloadLocation: File = SimpleCoreAPI.dataFolder("modules/")): Boolean{
if(!downloadLocation.exists()) downloadLocation.mkdirs()
val releases = JsonParser.parseString(URL("https://api.github.com/repos/$repository/releases").readText()).asJsonArray // Get the repo releases list
if(releases.isEmpty) // If empty stop
Expand All @@ -46,23 +47,27 @@ object ModuleHelper {
* @return List of the sorted modules to load
*/
fun sortModuleDependencies(dependencies: Map<String, Collection<String>>): List<String> {
val sorted = mutableListOf<String>()
// First add the modules that don't have dependencies
sorted.addAll(dependencies.filter { it.value.isEmpty() }.keys)
val visited = mutableSetOf<String>()
val result = mutableListOf<String>()

// Now add the modules that have dependencies
dependencies.filter { !sorted.contains(it.key) }.forEach { (moduleName, moduleDepends) ->
moduleDepends.forEach { dependency ->
if(sorted.contains(dependency) && sorted.indexOf(moduleName) > sorted.indexOf(dependency)) {
sorted.remove(dependency)
sorted.add(sorted.indexOf(moduleName), dependency)
}else if(!sorted.contains(dependency)){
sorted.add(dependency)
fun dfs(node: String) {
visited.add(node)
for (neighbor in dependencies[node] ?: emptySet()) {
if (neighbor !in visited) {
dfs(neighbor)
}
}
if(!sorted.contains(moduleName)) sorted.add(moduleName)

result.add(node)
}
return sorted.filter { it.isNotBlank() && it.isNotEmpty() }

for (node in dependencies.keys) {
if (node !in visited) {
dfs(node)
}
}

return result
}

/**
Expand All @@ -87,7 +92,7 @@ object ModuleHelper {

val now = System.currentTimeMillis()
if(lastRepoUpdate == 0L || (lastRepoUpdate - now) > 30000L){
val file = File("plugins/SimpleCoreAPI/modules-repository.json")
val file = SimpleCoreAPI.dataFolder("modules-repository.json")
val onlineBytes = URL("https://github.com/TheProgramSrc/GlobalDatabase/raw/master/SimpleCoreAPI/modules-repository.json").readBytes() // Get the online version
if(!file.exists()) file.createNewFile() // Create the file
file.writeBytes(onlineBytes) // Overwrite file
Expand All @@ -102,17 +107,17 @@ object ModuleHelper {
*/
fun getModuleMeta(moduleId: String): JsonObject? {
updateRepository() // First we update the repo
val json = JsonParser.parseString(File("plugins/SimpleCoreAPI/modules-repository.json").readText()).asJsonObject
val json = JsonParser.parseString(SimpleCoreAPI.dataFolder("modules-repository.json").readText()).asJsonObject
return if(json.has(moduleId)) json.getAsJsonObject(moduleId) else null
}

/**
* Scans the given [File] for the simplecoreapi.modules
* file and loads the required modules if any
* @param file File to scan.
* @param downloadLocation Location to download the modules. (Defaults to plugins/SimpleCoreAPI/modules/)
* @param downloadLocation Location to download the modules. (Defaults to SimpleCoreAPI/modules/)
*/
fun downloadRequiredModules(file: File, downloadLocation: File = File("plugins/SimpleCoreAPI/modules/")){
fun downloadRequiredModules(file: File, downloadLocation: File = SimpleCoreAPI.dataFolder("modules/")){
updateRepository() // First we update the repository
if(file.extension != "jar") return
try {
Expand Down
Loading

0 comments on commit 2fd15f8

Please sign in to comment.