|
|
|
@ -12,10 +12,13 @@ import kotlin.io.path.relativeTo
|
|
|
|
|
/** |
|
|
|
|
* This task should be FAST and SAFE! Because it is being run during IDE import. |
|
|
|
|
*/ |
|
|
|
|
abstract class GenerateResClassTask : DefaultTask() { |
|
|
|
|
internal abstract class GenerateResClassTask : DefaultTask() { |
|
|
|
|
@get:Input |
|
|
|
|
abstract val packageName: Property<String> |
|
|
|
|
|
|
|
|
|
@get:Input |
|
|
|
|
abstract val shouldGenerateResClass: Property<Boolean> |
|
|
|
|
|
|
|
|
|
@get:InputFiles |
|
|
|
|
@get:PathSensitive(PathSensitivity.RELATIVE) |
|
|
|
|
abstract val resDir: Property<File> |
|
|
|
@ -26,32 +29,37 @@ abstract class GenerateResClassTask : DefaultTask() {
|
|
|
|
|
@TaskAction |
|
|
|
|
fun generate() { |
|
|
|
|
try { |
|
|
|
|
val rootResDir = resDir.get() |
|
|
|
|
logger.info("Generate resources for $rootResDir") |
|
|
|
|
val kotlinDir = codeDir.get().asFile |
|
|
|
|
logger.info("Clean directory $kotlinDir") |
|
|
|
|
kotlinDir.deleteRecursively() |
|
|
|
|
kotlinDir.mkdirs() |
|
|
|
|
|
|
|
|
|
//get first level dirs |
|
|
|
|
val dirs = rootResDir.listNotHiddenFiles() |
|
|
|
|
if (shouldGenerateResClass.get()) { |
|
|
|
|
val rootResDir = resDir.get() |
|
|
|
|
logger.info("Generate resources for $rootResDir") |
|
|
|
|
|
|
|
|
|
dirs.forEach { f -> |
|
|
|
|
if (!f.isDirectory) { |
|
|
|
|
error("${f.name} is not directory! Raw files should be placed in '${rootResDir.name}/files' directory.") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//get first level dirs |
|
|
|
|
val dirs = rootResDir.listNotHiddenFiles() |
|
|
|
|
|
|
|
|
|
//type -> id -> resource item |
|
|
|
|
val resources: Map<ResourceType, Map<String, List<ResourceItem>>> = dirs |
|
|
|
|
.flatMap { dir -> |
|
|
|
|
dir.listNotHiddenFiles() |
|
|
|
|
.mapNotNull { it.fileToResourceItems(rootResDir.toPath()) } |
|
|
|
|
.flatten() |
|
|
|
|
dirs.forEach { f -> |
|
|
|
|
if (!f.isDirectory) { |
|
|
|
|
error("${f.name} is not directory! Raw files should be placed in '${rootResDir.name}/files' directory.") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
.groupBy { it.type } |
|
|
|
|
.mapValues { (_, items) -> items.groupBy { it.name } } |
|
|
|
|
|
|
|
|
|
val kotlinDir = codeDir.get().asFile |
|
|
|
|
kotlinDir.deleteRecursively() |
|
|
|
|
kotlinDir.mkdirs() |
|
|
|
|
getResFileSpec(resources, packageName.get()).writeTo(kotlinDir) |
|
|
|
|
//type -> id -> resource item |
|
|
|
|
val resources: Map<ResourceType, Map<String, List<ResourceItem>>> = dirs |
|
|
|
|
.flatMap { dir -> |
|
|
|
|
dir.listNotHiddenFiles() |
|
|
|
|
.mapNotNull { it.fileToResourceItems(rootResDir.toPath()) } |
|
|
|
|
.flatten() |
|
|
|
|
} |
|
|
|
|
.groupBy { it.type } |
|
|
|
|
.mapValues { (_, items) -> items.groupBy { it.name } } |
|
|
|
|
getResFileSpec(resources, packageName.get()).writeTo(kotlinDir) |
|
|
|
|
} else { |
|
|
|
|
logger.info("Generation Res class is disabled") |
|
|
|
|
} |
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
//message must contain two ':' symbols to be parsed by IDE UI! |
|
|
|
|
logger.error("e: GenerateResClassTask was failed:", e) |
|
|
|
|