|
| 1 | +/* |
| 2 | + * Copyright 2020 MovingBlocks |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import groovy.json.JsonSlurper |
| 18 | + |
| 19 | +class module { |
| 20 | + def excludedItems = ["engine", "Index", "out", "build"] |
| 21 | + |
| 22 | + def getGithubDefaultHome(Properties properties) { |
| 23 | + return properties.alternativeGithubHome ?: "Terasology" |
| 24 | + } |
| 25 | + |
| 26 | + File targetDirectory = new File("modules") |
| 27 | + def itemType = "module" |
| 28 | + |
| 29 | + String[] findDependencies(File targetDir, boolean respectExcludedItems = true) { |
| 30 | + def foundDependencies = readModuleDependencies(new File(targetDir, "module.txt"), respectExcludedItems) |
| 31 | + println "Looked for dependencies, found: " + foundDependencies |
| 32 | + return foundDependencies |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Reads a given module info file to figure out which if any dependencies it has. Filters out any already retrieved. |
| 37 | + * This method is only for modules. |
| 38 | + * @param targetModuleInfo the target file to check (a module.txt file or similar) |
| 39 | + * @return a String[] containing the next level of dependencies, if any |
| 40 | + */ |
| 41 | + String[] readModuleDependencies(File targetModuleInfo, boolean respectExcludedItems = true) { |
| 42 | + def qualifiedDependencies = [] |
| 43 | + if (!targetModuleInfo.exists()) { |
| 44 | + println "The module info file did not appear to exist - can't calculate dependencies" |
| 45 | + return qualifiedDependencies |
| 46 | + } |
| 47 | + def slurper = new JsonSlurper() |
| 48 | + def moduleConfig = slurper.parseText(targetModuleInfo.text) |
| 49 | + for (dependency in moduleConfig.dependencies) { |
| 50 | + if (respectExcludedItems && excludedItems.contains(dependency.id)) { |
| 51 | + println "Skipping listed dependency $dependency.id as it is in the exclude list (shipped with primary project)" |
| 52 | + } else { |
| 53 | + println "Accepting listed dependency $dependency.id" |
| 54 | + qualifiedDependencies << dependency.id |
| 55 | + } |
| 56 | + } |
| 57 | + return qualifiedDependencies |
| 58 | + } |
| 59 | + |
| 60 | + def copyInTemplateFiles(File targetDir) { |
| 61 | + // Copy in the template build.gradle for modules |
| 62 | + println "In copyInTemplateFiles for module $targetDir.name - copying in a build.gradle then next checking for module.txt" |
| 63 | + File targetBuildGradle = new File(targetDir, 'build.gradle') |
| 64 | + targetBuildGradle.delete() |
| 65 | + targetBuildGradle << new File('templates/build.gradle').text |
| 66 | + |
| 67 | + // Copy in the template module.txt for modules (if one doesn't exist yet) |
| 68 | + File moduleManifest = new File(targetDir, 'module.txt') |
| 69 | + if (!moduleManifest.exists()) { |
| 70 | + def moduleText = new File("templates/module.txt").text |
| 71 | + |
| 72 | + moduleManifest << moduleText.replaceAll('MODULENAME', targetDir.name) |
| 73 | + println "WARNING: the module ${targetDir.name} did not have a module.txt! One was created, please review and submit to GitHub" |
| 74 | + } |
| 75 | + |
| 76 | + // TODO: Copy in a module readme template soon |
| 77 | + // TODO : Add in the logback.groovy from engine\src\test\resources\logback.groovy ? Local dev only, Jenkins will use the one inside engine-tests.jar. Also add to .gitignore |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Filters the given items based on this item type's preferences |
| 82 | + * @param possibleItems A map of repos (possible items) and their descriptions (potential filter data) |
| 83 | + * @return A list containing only the items this type cares about |
| 84 | + */ |
| 85 | + List filterItemsFromApi(Map possibleItems) { |
| 86 | + List itemList = [] |
| 87 | + |
| 88 | + // Modules just consider the item name and excludes those in a specific list |
| 89 | + itemList = possibleItems.findAll { |
| 90 | + !excludedItems.contains(it.key) |
| 91 | + }.collect { it.key } |
| 92 | + |
| 93 | + return itemList |
| 94 | + } |
| 95 | + |
| 96 | + def refreshGradle(File targetDir) { |
| 97 | + // Copy in the template build.gradle for modules |
| 98 | + if (!new File(targetDir, "module.txt").exists()) { |
| 99 | + println "$targetDir has no module.txt, it must not want a fresh build.gradle" |
| 100 | + return |
| 101 | + } |
| 102 | + println "In refreshGradle for module $targetDir - copying in a fresh build.gradle" |
| 103 | + File targetBuildGradle = new File(targetDir, 'build.gradle') |
| 104 | + targetBuildGradle.delete() |
| 105 | + targetBuildGradle << new File('templates/build.gradle').text |
| 106 | + } |
| 107 | +} |
0 commit comments