dk.nstack.kotlin.plugin.RateReminderActionsGenerator.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of translation Show documentation
Show all versions of translation Show documentation
Gradle extensions for NStack.io translations
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
}
}