Skip to content

Commit

Permalink
Implemented No-Op artifact.
Browse files Browse the repository at this point in the history
(cherry picked from commit 876c111)
  • Loading branch information
Aristos Pasalides committed Dec 15, 2021
1 parent dd6833e commit 38b28ff
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
38 changes: 35 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,25 @@ tasks {
}
}

sourceSets {
noop {
java.srcDir 'src/noop/kotlin'
}
}

dependencies {
implementation platform('org.jetbrains.kotlin:kotlin-bom')
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'

implementation 'org.slf4j:slf4j-api:1.7.28'
testImplementation 'org.slf4j:slf4j-simple:1.7.28'
implementation 'org.slf4j:slf4j-api:1.7.32'
testImplementation 'org.slf4j:slf4j-simple:1.7.32'


testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5'

noopImplementation 'org.slf4j:slf4j-nop:1.7.32'
noopImplementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
}

task dokkaJar(type: Jar) {
Expand All @@ -41,16 +51,37 @@ task sourcesJar(type: Jar) {
from(sourceSets.getByName("main").allSource)
}

task noopSources(type: Jar) {
archiveClassifier.set("sources")
from(sourceSets.noop.allSource)
}

task noopBinary(type: Jar) {
archiveClassifier.set("")
from(sourceSets.noop.output)
}

publishing {
publications {
noop(MavenPublication) {
artifact noopSources
artifact noopBinary

pom {
name = "ktee-noop"
artifactId = "ktee-noop"
}
}

mavenJava(MavenPublication) {
from components.java
artifact sourcesJar
artifact dokkaJar

pom {
name = "ktee"
description = "KTee is Tee for Kotlin code pipelines. If you love the unix command line tee, you know what we mean."
description = "KTee is Tee for Kotlin code pipelines. " +
"If you love the unix command line tee, you know what we mean."
url.set("https://github.com/medly/ktee")
licenses {
license {
Expand Down Expand Up @@ -103,4 +134,5 @@ signing {
def signingPassword = System.getenv("OSSRH_SIGNING_PASSPHRASE")
useInMemoryPgpKeys(signingKey, signingPassword)
sign publishing.publications.mavenJava
sign publishing.publications.noop
}
35 changes: 8 additions & 27 deletions src/main/kotlin/ktee/KTee.kt
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
package ktee

import ktee.KTee.Companion.debug
import org.slf4j.Logger

class KTee {
companion object {
var debugChanged = false
private set

/**
* If debug is set to false, all tee
* functions won't output anything.
*
* Variable can be set only once!
*/
var debug = true
@Synchronized set(value) {
if(!debugChanged) { debugChanged = true; field = value }
else throw IllegalStateException("Variable debug has already been set once.")
}
} }

/**
* Prints the value to the stdout and returns the same value. Useful when chaining
* methods. For example:
Expand All @@ -30,53 +11,53 @@ class KTee {
* myList.map(fn).tee(">>> ").reduce(fn)
*
*/
fun <T> T.tee(marker: String = "") = apply { if (debug) println(marker + this) }
fun <T> T.tee(marker: String = "") = apply { println(marker + this) }

/**
*
* executes the lambda with the value of the chain and writes the
*
*/
inline fun <T> T.tee(fn: (T) -> String) = apply { if (debug) println(fn(this)) }
inline fun <T> T.tee(fn: (T) -> String) = apply { println(fn(this)) }

/**
* logs the value to the given logger at info level. Message can be customized using message parameter
*/
fun <T> T.teeToInfo(logger: Logger, message: String = "{}")
= apply { if (debug) logger.info(message, this) }
= apply { logger.info(message, this) }

/**
* Evaluates the lambda and logs the result (of evaluation) to the given logger at info level
*
*/
inline fun <T> T.teeToInfo(logger: Logger, fn: (T) -> String)
= apply { if (debug) logger.info(fn(this), this) }
= apply { logger.info(fn(this), this) }

/**
* logs the value to the given logger at info level. Message can be customized using message parameter
*/
fun <T> T.teeToDebug(logger: Logger, message: String = "{}")
= apply { if (debug) logger.debug(message, this) }
= apply { logger.debug(message, this) }

/**
* Evaluates the lambda and logs the result (of evaluation) to the given logger at debug level
*
*/
inline fun <T> T.teeToDebug(logger: Logger, fn: (T) -> String)
= apply { if (debug) logger.debug(fn(this), this) }
= apply { logger.debug(fn(this), this) }

/**
* logs the value to the given logger at trace level. Message can be customized using message parameter
*/
fun <T> T.teeToTrace(logger: Logger, message: String = "{}")
= apply { if (debug) logger.trace(message, this) }
= apply { logger.trace(message, this) }

/**
* Evaluates the lambda and logs the result (of evaluation) to the given logger at trace level
*
*/
inline fun <T> T.teeToTrace(logger: Logger, fn: (T) -> String)
= apply { if (debug) logger.trace(fn(this), this) }
= apply { logger.trace(fn(this), this) }



Expand Down
16 changes: 16 additions & 0 deletions src/noop/kotlin/ktee/KTee.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ktee

import org.slf4j.Logger

inline fun <T> T.tee(fn: (T) -> String) = this
inline fun <T> T.tee(marker: String = "") = this
inline fun <T> T.teeToInfo(logger: Logger, fn: (T) -> String) = this
inline fun <T> T.teeToDebug(logger: Logger, fn: (T) -> String) = this
inline fun <T> T.teeToTrace(logger: Logger, fn: (T) -> String) = this
inline fun <T> T.teeToInfo(logger: Logger, message: String = "{}") = this
inline fun <T> T.teeToDebug(logger: Logger, message: String = "{}") = this
inline fun <T> T.teeToTrace(logger: Logger, message: String = "{}") = this




0 comments on commit 38b28ff

Please sign in to comment.