All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.nstack.kotlin.plugin.RateReminderActionsGenerator.groovy Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
package dk.nstack.kotlin.plugin

import groovy.json.JsonSlurper

class RateReminderActionsGenerator {

    static String generateActions(String packageName) {
        def url = TranslationPlugin.project.translation.contentUrl + "api/v2/notify/rate_reminder_v2/actions"
        def actionsJson = Util.getTextFromUrl(url)
        if (actionsJson.isEmpty()) {
            return ""
        }
        def actions = new JsonSlurper().parseText(actionsJson).data as List

        def enumString = "package $packageName\n\n"
        enumString += "import dk.nodes.nstack.kotlin.NStack\n\n"
        enumString += "/**\n * generated by nstack gradle plugin on ${new Date()}\n */\n"
        enumString += "enum class RateReminderAction(private val apiName: String) {\n\n"
        for (def i = 0; i < actions.size() - 1; ++i) {
            enumString += actionToEnumName(actions[i])
        }
        enumString += actionToEnumName(actions.last(), true)
        enumString += """
    suspend fun sendToNStack() {
        NStack.RateReminder.action(apiName)
    }
}
"""
        return enumString
    }

    private static String actionToEnumName(String action, Boolean isLast = false) {
        def enumName = action.replace("-", "_").toUpperCase()
        def result = "\t$enumName(\"$action\")"
        if (!isLast) {
            result += ",\n"
        } else {
            result += ";\n"
        }
        return result
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy