Skip to content

Instantly share code, notes, and snippets.

@keima
Created May 18, 2018 11:51
Show Gist options
  • Save keima/f75de57df4941d5f8334d239771657e8 to your computer and use it in GitHub Desktop.
Save keima/f75de57df4941d5f8334d239771657e8 to your computer and use it in GitHub Desktop.
import net.yslibrary.licenseadapter.Library
import net.yslibrary.licenseadapter.Licenses
fun licenseList(setup: LicenseListBuilder.() -> Unit) =
LicenseListBuilder().let {
it.setup()
it.build()
}
class LicenseListBuilder {
private val list = mutableListOf<LicenseBuilder>()
fun github(shortUrl: String): LicenseBuilder {
val item = LicenseBuilder(SourceType.GITHUB, shortUrl)
list.add(item)
return item
}
fun String.hostedByGitHub(): LicenseBuilder {
return github(this)
}
infix fun String.licensedBy(def: LicenseDef): LicenseBuilder {
return github(this).apply {
licensedBy(def)
}
}
fun build() =
list.map { it.build() }
}
class LicenseBuilder(
private val sourceType: SourceType,
private val shortUrl: String,
private var def: LicenseDef = ApacheV2()
) {
infix fun licensedBy(def: LicenseDef) {
this.def = def
}
fun build(): Library = when (sourceType) {
SourceType.GITHUB -> {
when (def) {
is ApacheV2 -> {
Licenses.fromGitHubApacheV2(shortUrl)
}
is MIT -> TODO()
is BSD -> TODO()
is UNKNOWN -> TODO()
}
}
SourceType.NO_SOURCE -> TODO()
SourceType.NO_LINK -> TODO()
}
}
enum class SourceType {
GITHUB, NO_SOURCE, NO_LINK;
}
// Licenses Definitions
sealed class LicenseDef(val licenseName: String)
class ApacheV2 : LicenseDef(Licenses.NAME_APACHE_V2)
class MIT : LicenseDef(Licenses.NAME_MIT)
class BSD : LicenseDef(Licenses.NAME_BSD)
class UNKNOWN : LicenseDef(Licenses.NAME_UNKNOWN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment