Skip to content

Commit

Permalink
Draft sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
gianluz committed Nov 5, 2019
1 parent c98912b commit 2e48dc9
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 107 deletions.
1 change: 1 addition & 0 deletions danger-kotlin-library/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apply from: file('../dependencyVersions.gradle')

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation project(":danger-kotlin-sdk")
utilsDependencies()
kotlinDependencies()
testingDependencies()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import systems.danger.kotlin.sdk.DangerContext
import systems.danger.kotlin.sdk.DangerPlugin
import java.io.File
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

private fun FilePath.readText() = File(this).readText()

fun toISO8601UTC(date: Date): String {
val tz = TimeZone.getTimeZone("UTC")
val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
Expand All @@ -31,22 +35,42 @@ fun fromISO8601UTC(dateStr: String): Date? {
return null
}

class Rfc3339DateJsonAdapter: JsonAdapter<Date>() {
class Rfc3339DateJsonAdapter : JsonAdapter<Date>() {
@Synchronized
override fun fromJson(reader: JsonReader): Date {
val string = reader.nextString()
return fromISO8601UTC(string)!!
}

override fun toJson(writer: JsonWriter, value: Date?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
//Implementation not needed right now
}
}

object register {
internal var dangerPlugins = mutableListOf<DangerPlugin>()

infix fun plugin(plugin: DangerPlugin) {
dangerPlugins.add(plugin)
}

fun plugins(vararg pluginArgs: DangerPlugin) {
dangerPlugins.addAll(pluginArgs)
}
}

private class DangerRunner(jsonInputFilePath: FilePath, jsonOutputPath: FilePath) {
inline fun register(block: register.() -> Unit) = register.apply(block)

inline fun danger(args: Array<String>, block: DangerDSL.()->Unit) = Danger(args).apply(block)

internal fun DangerPlugin.withContext(dangerContext: DangerContext) {
context = dangerContext
}

private class DangerRunner(jsonInputFilePath: FilePath, jsonOutputPath: FilePath) : DangerContext {
val jsonOutputFile: File = File(jsonOutputPath)
val danger: DangerDSL
val dangerResults: DangerResults = DangerResults(arrayOf(), arrayOf(),arrayOf(), arrayOf())
val dangerResults: DangerResults = DangerResults(arrayOf(), arrayOf(), arrayOf(), arrayOf())

private val moshi = Moshi.Builder()
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe())
Expand All @@ -55,124 +79,145 @@ private class DangerRunner(jsonInputFilePath: FilePath, jsonOutputPath: FilePath

init {
this.danger = moshi.adapter(DSL::class.java).fromJson(jsonInputFilePath.readText())!!.danger

register.dangerPlugins.forEach {
it.withContext(this)
}

saveDangerResults()
}

fun saveDangerResults() {
val resultsJSON = moshi.adapter(DangerResults::class.java).toJson(dangerResults)
jsonOutputFile.writeText(resultsJSON)
//Api Implementation
/**
* Adds an inline fail message to the Danger report
*/
override fun fail(message: String) {
fail(Violation(message))
}
}

class DangerKotlinAPIProvider {
companion object{
@JvmStatic
fun getApi() = apiImpl
/**
* Adds an inline fail message to the Danger report
*/
override fun fail(message: String, file: FilePath, line: Int) {
fail(Violation(message, file, line))
}
}

private lateinit var dangerRunner: DangerRunner
private lateinit var apiImpl: Unit
/**
* Adds an inline warning message to the Danger report
*/
override fun warn(message: String) {
warn(Violation(message))
}

fun Danger(args: Array<String>): DangerDSL {
val argsCount = args.count()
/**
* Adds an inline warning message to the Danger report
*/
override fun warn(message: String, file: FilePath, line: Int) {
warn(Violation(message, file, line))
}

val jsonInputFilePath = args[argsCount - 2]
val jsonOutputPath = args[argsCount - 1]
/**
* Adds an inline message to the Danger report
*/
override fun message(message: String) {
message(Violation(message))
}

dangerRunner = DangerRunner(jsonInputFilePath, jsonOutputPath)
return dangerRunner.danger
}
/**
* Adds an inline message to the Danger report
*/
override fun message(message: String, file: FilePath, line: Int) {
message(Violation(message, file, line))
}

/**
* Adds an inline fail message to the Danger report
*/
fun fail(message: String) {
fail(Violation(message))
}
/**
* Adds an inline markdown message to the Danger report
*/
override fun markdown(message: String) {
markdown(Violation(message))
}

/**
* Adds an inline fail message to the Danger report
*/
fun fail(message: String, file: FilePath, line: Int) {
fail(Violation(message, file, line))
}
/**
* Adds an inline markdown message to the Danger report
*/
override fun markdown(message: String, file: FilePath, line: Int) {
markdown(Violation(message, file, line))
}

private fun fail(violation: Violation) {
dangerRunner.dangerResults.fails+=violation
dangerRunner.saveDangerResults()
}
/**
* Adds an inline suggest markdown message to the Danger report
*/
override fun suggest(code: String, file: FilePath, line: Int) {
if (dangerRunner.danger.onGitHub) {
val message = "```suggestion\n $code \n```"
markdown(Violation(message, file, line))
} else {
val message = "```\n $code \n```"
message(Violation(message))
}
}

/**
* Adds an inline warning message to the Danger report
*/
fun warn(message: String) {
warn(Violation(message))
}
private fun warn(violation: Violation) {
dangerResults.warnings += (violation)
saveDangerResults()
}

/**
* Adds an inline warning message to the Danger report
*/
fun warn(message: String, file: FilePath, line: Int) {
warn(Violation(message, file, line))
}
private fun fail(violation: Violation) {
dangerResults.fails += violation
saveDangerResults()
}

private fun warn(violation: Violation) {
dangerRunner.dangerResults.warnings+=(violation)
dangerRunner.saveDangerResults()
}
private fun message(violation: Violation) {
dangerResults.messages += violation
saveDangerResults()
}

/**
* Adds an inline message to the Danger report
*/
fun message(message: String) {
message(Violation(message))
}
private fun markdown(violation: Violation) {
dangerResults.markdowns += violation
saveDangerResults()
}

/**
* Adds an inline message to the Danger report
*/
fun message(message: String, file: FilePath, line: Int) {
message(Violation(message, file, line))
private fun saveDangerResults() {
val resultsJSON = moshi.adapter(DangerResults::class.java).toJson(dangerResults)
jsonOutputFile.writeText(resultsJSON)
}
}

private fun message(violation: Violation) {
dangerRunner.dangerResults.messages+=violation
dangerRunner.saveDangerResults()
}
private lateinit var dangerRunner: DangerRunner

/**
* Adds an inline markdown message to the Danger report
*/
fun markdown(message: String) {
markdown(Violation(message))
}
fun Danger(args: Array<String>): DangerDSL {
val argsCount = args.count()

/**
* Adds an inline markdown message to the Danger report
*/
fun markdown(message: String, file: FilePath, line: Int) {
markdown(Violation(message, file, line))
}
val jsonInputFilePath = args[argsCount - 2]
val jsonOutputPath = args[argsCount - 1]

private fun markdown(violation: Violation) {
dangerRunner.dangerResults.markdowns+=violation
dangerRunner.saveDangerResults()
dangerRunner = DangerRunner(jsonInputFilePath, jsonOutputPath)
return dangerRunner.danger
}

fun fail(message: String) =
dangerRunner.fail(message)

/**
* Adds an inline suggest markdown message to the Danger report
*/
fun suggest(code: String, file: FilePath, line: Int) {
if (dangerRunner.danger.onGitHub) {
val message = "```suggestion\n $code \n```"
markdown(Violation(message, file, line))
} else {
val message = "```\n $code \n```"
message(Violation(message))
}
}
fun fail(message: String, file: FilePath, line: Int) =
dangerRunner.fail(message, file, line)

private fun FilePath.readText() = File(this).readText()
fun warn(message: String) =
dangerRunner.warn(message)

fun warn(message: String, file: FilePath, line: Int) =
dangerRunner.warn(message, file, line)

fun message(message: String) =
dangerRunner.message(message)

fun message(message: String, file: FilePath, line: Int) =
dangerRunner.message(message, file, line)

fun markdown(message: String) =
dangerRunner.markdown(message)

fun markdown(message: String, file: FilePath, line: Int) =
dangerRunner.markdown(message, file, line)

fun suggest(code: String, file: FilePath, line: Int) =
dangerRunner.suggest(code, file, line)
13 changes: 10 additions & 3 deletions kotlin-sdk/build.gradle → danger-kotlin-sdk/build.gradle
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'maven-publish'
}

group 'systems.danger'
version '1.0-SNAPSHOT'
version '1.0'

repositories {
mavenCentral()
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation project(":danger-kotlin-library")
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
}
publishing {
publications {
maven(MavenPublication) {
from components.java
}
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package systems.danger.kotlin.sdk

import com.danger.dangerkotlin.*

interface DangerKotlinAPI {
interface DangerContext {
fun message(message: String)
fun message(message: String, file: String, line: Int)
fun markdown(message: String)
Expand All @@ -14,8 +12,16 @@ interface DangerKotlinAPI {
fun suggest(code: String, file: String, line: Int)
}

class DangerKotlinApiGetter {
object Sdk {
const val VERSION_NAME = "1.0"
const val API_VERSION = 1
}

abstract class DangerPlugin {
companion object {
fun get() = DangerKotlinAPIProvider.getApi()
const val DEVELOPED_WITH_API = Sdk.API_VERSION
}

abstract val id: String
lateinit var context: DangerContext
}
4 changes: 2 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ pluginManagement {
}
}

include ':danger-kotlin-library'
include ':danger-kotlin'
include ':kotlin-sdk'
include ':danger-kotlin-library'
include ':danger-kotlin-sdk'

0 comments on commit 2e48dc9

Please sign in to comment.