Skip to content

Commit

Permalink
refactor: Create module with shared utilities (#1418)
Browse files Browse the repository at this point in the history
Fixes #1402

`:common` modules with shared utils created. Many files has been changed in this PR, however they are mostly import changes

## Test Plan
> How do we know the code works?

`:common` is available, all tests passed
  • Loading branch information
piotradamczyk5 authored Dec 22, 2020
1 parent a4553ae commit f3f2455
Show file tree
Hide file tree
Showing 155 changed files with 457 additions and 441 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tasks {
exclude(
"**/*Generated.kt",
"**/*Test.kt",
"**/Test*.kt" //we can expand this list
"**/Test*.kt" // we can expand this list
)
}
}
Expand Down
2 changes: 0 additions & 2 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,3 @@ object Dependencies {
const val GLASSFISH_JSON = "org.glassfish:javax.json:${Versions.GLASSFISH_JSON}"
//endregion
}


14 changes: 14 additions & 0 deletions common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Compiled class file
*.class

# Log file
*.log

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
*.iml
.idea/
build/
out/
.gradle/
local.properties
41 changes: 41 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
application
kotlin(Plugins.Kotlin.PLUGIN_JVM)
}

tasks.test {
maxHeapSize = "2048m"
minHeapSize = "512m"
}

tasks.withType<KotlinCompile> { kotlinOptions.jvmTarget = "1.8" }

dependencies {
implementation(kotlin("stdlib", org.jetbrains.kotlin.config.KotlinCompilerVersion.VERSION)) // or "stdlib-jdk8"
// Fuel
api(Dependencies.Fuel.CORE)
api(Dependencies.Fuel.KOTLINX_SERIALIZATION)
api(Dependencies.Fuel.COROUTINES)
// Archive
api(Dependencies.ARCHIVE_LIB)
api(Dependencies.TUKAANI_XZ)

testImplementation(Dependencies.JUNIT)
testImplementation(Dependencies.MOCKK)
testImplementation(Dependencies.TRUTH)

testImplementation(Dependencies.SYSTEM_RULES)
}
repositories {
mavenCentral()
}
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "1.8"
}
val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
jvmTarget = "1.8"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flank.scripts.utils
package flank.common

import org.rauschig.jarchivelib.ArchiveFormat
import org.rauschig.jarchivelib.ArchiverFactory
Expand Down
50 changes: 50 additions & 0 deletions common/src/main/kotlin/flank.common/Files.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package flank.common

import com.github.kittinunf.fuel.Fuel
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

val userHome: String by lazy {
if (isWindows) System.getenv("HOMEPATH") else System.getProperty("user.home")
}

fun createSymbolicLink(
link: String,
target: String
) {
Files.createSymbolicLink(
Paths.get(link)
.also { linkPath -> if (Files.isSymbolicLink(linkPath)) Files.delete(linkPath) }
.toAbsolutePath().normalize(),

Paths.get(target)
.toAbsolutePath().normalize()
)
}

fun createSymbolicLinkToFile(link: Path, target: Path) {
Files.createSymbolicLink(link, target.fileName)
}

fun downloadFile(sourceUrl: String, destination: String) {
Fuel.download(sourceUrl)
.fileDestination { _, _ -> File(destination) }
.responseString()
}

fun downloadFile(sourceUrl: String, destinationPath: Path) {
Fuel.download(sourceUrl)
.fileDestination { _, _ -> destinationPath.toFile() }
.responseString()
}

fun createDirectoryIfNotExist(path: Path) {
if (Files.notExists(path)) Files.createDirectory(path)
}

fun File.hasAllFiles(fileList: List<String>): Boolean {
val directoryFiles = list() ?: emptyArray()
return fileList.all { it in directoryFiles }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flank.scripts.utils
package flank.common

import java.net.InetAddress
import java.net.InetSocketAddress
Expand Down
13 changes: 13 additions & 0 deletions common/src/main/kotlin/flank.common/Os.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package flank.common

private val osName = System.getProperty("os.name")?.toLowerCase() ?: ""

val isMacOS: Boolean by lazy {
val isMacOS = osName.indexOf("mac") >= 0
logLn("isMacOS = $isMacOS ($osName)")
isMacOS
}

val isWindows: Boolean by lazy {
osName.indexOf("win") >= 0
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ftl.log
package flank.common

fun setLogLevel(logLevel: OutputLogLevel) {
minimumLogLevel = logLevel
Expand Down
33 changes: 33 additions & 0 deletions common/src/main/kotlin/flank.common/Strings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package flank.common

import java.io.StringWriter
import java.nio.file.Files
import java.nio.file.Paths

fun String.withNewLineAtTheEnd() = plus(System.lineSeparator())

fun String.normalizeLineEnding(): String {
// required for tests to pass on Windows
return this.replace("\r\n", "\n")
}

fun String.trimStartLine(): String {
return this.split("\n").drop(1).joinToString("\n")
}

fun StringWriter.println(msg: String = "") {
appendLine(msg)
}

fun String.write(data: String) {
Files.write(Paths.get(this), data.toByteArray())
}

fun join(first: String, vararg more: String): String {
// Note: Paths.get(...) does not work for joining because the path separator
// will be '\' on Windows which is invalid for a URI
return listOf(first, *more)
.joinToString("/")
.replace("\\", "/")
.replace(regex = Regex("/+"), replacement = "/")
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
package flank.scripts.utils
package flank.common

import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import java.io.OutputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import java.util.zip.ZipOutputStream

fun unzip(src: File, dst: File = src.parentFile) {
print("* Unzipping: $src to $dst - ")
ZipFile(src).use { zipFile ->
zipFile.entries().asSequence().forEach { zipEntry ->
val outputFile = File(dst, zipEntry.name)

if (zipEntry.isDirectory) {
outputFile.mkdirs()
return@forEach
}

outputFile.parentFile.mkdirs()
fun unzipFile(zipFileName: File, unzipPath: String): List<File> {
println("Unzipping: ${zipFileName.absolutePath} to $unzipPath")
return ZipFile(zipFileName).unzipTo(unzipPath)
}

zipFile.getInputStream(zipEntry) useCopyTo outputFile.outputStream()
private fun ZipFile.unzipTo(unzipPath: String) = use { zipFile ->
zipFile.entries()
.asSequence()
.fold(listOf<File>()) { unzippedFiles, zipFileEntry ->
unzippedFiles + zipFileEntry.saveToFile(zipFile, unzipPath)
}
}

private fun ZipEntry.saveToFile(zipFile: ZipFile, unzipPath: String): File {
val outputFile = File(unzipPath, name)
if (isDirectory) {
outputFile.mkdirs()
} else {
zipFile.getInputStream(this).use { zipEntryInput -> zipEntryInput.toFile(outputFile) }
}
println("OK")

return outputFile
}

private infix fun InputStream.useCopyTo(output: OutputStream) =
use { zipEntryInput -> output.use { output -> zipEntryInput.copyTo(output) } }
private fun InputStream.toFile(destinationFile: File) {
destinationFile.outputStream().use { output -> copyTo(output) }
}

fun zip(src: File, dst: File) {
print("* Zipping: $src to $dst - ")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flank.scripts.utils
package flank.common

import org.junit.After
import org.junit.Assert.assertTrue
Expand All @@ -8,7 +8,7 @@ import java.io.File
import java.nio.file.Files
import java.nio.file.Paths

class FileKtTest {
class FilesTest {

private val linkName = "tmp"
private val targetName = "../"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ftl.log
package flank.common

import com.google.common.truth.Truth
import ftl.test.util.TestHelper.normalizeLineEnding
import org.junit.After
import org.junit.Rule
import org.junit.Test
Expand All @@ -11,7 +10,8 @@ class OutputLoggerTest {

@Rule
@JvmField
val systemOutRule: SystemOutRule = SystemOutRule().enableLog().muteForSuccessfulTests()
val systemOutRule: SystemOutRule = SystemOutRule()
.enableLog().muteForSuccessfulTests()

private val simpleMessage = "print for simple"
private val detailedMessage = "print for detailed"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package flank.scripts.utils
package flank.common

import org.junit.Assert
import org.junit.Test
import java.io.File

internal class ZipKtTest {
internal class ArchiveKtTest {

@Test
fun zipTest() {
val src = File("./src")
val src2 = File("./src2")
val srcZip = File("./src.zip")
zip(src, srcZip)
unzip(srcZip, src2)
unzipFile(srcZip, src2.absolutePath)
Assert.assertEquals(
src.calculateSize(),
src2.calculateSize()
Expand Down
12 changes: 12 additions & 0 deletions firebase_apis/test_api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
`java-library`
kotlin(Plugins.Kotlin.PLUGIN_JVM)
}

java {
Expand All @@ -13,4 +16,13 @@ repositories {

dependencies {
implementation(Dependencies.GOOGLE_API_CLIENT)
implementation(kotlin("stdlib-jdk8"))
}
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "1.8"
}
val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
jvmTarget = "1.8"
}
2 changes: 1 addition & 1 deletion flank-actions/common.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
object Common {
const val EXIT_CODE_SUCCESS = 0
const val EXIT_CODE_FAILURE = -1
const val URL_SLACK ="https://slack.com/api/chat.postMessage"
const val URL_SLACK = "https://slack.com/api/chat.postMessage"
const val ARGS_TOKEN = 0
const val ARGS_CHANNEL = 1
const val ARGS_MESSAGE = 2
Expand Down
9 changes: 4 additions & 5 deletions flank-actions/sendMessage.kts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import kotlin.system.exitProcess

// DEPS com.github.kittinunf.fuel:fuel:2.3.0

//DEPS com.github.kittinunf.fuel:fuel:2.3.0

//INCLUDE slackService.kt
//INCLUDE common.kt
// INCLUDE slackService.kt
// INCLUDE common.kt

val result = sendMessage(args)

println("Message has been sent with result $result")

if (result != 0){
if (result != 0) {
exitProcess(result)
}
9 changes: 4 additions & 5 deletions flank-actions/slackService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.Headers

fun sendMessage(args: Array<String>): Int{
fun sendMessage(args: Array<String>): Int {
val token = args[Common.ARGS_TOKEN]
val channel = args[Common.ARGS_CHANNEL]
val message = args[Common.ARGS_MESSAGE]
val cookie = args[Common.ARGS_COOKIE]

return try {
val (req, rep, res) = Fuel.get(Common.URL_SLACK,
listOf("token" to token,"channel" to channel,"text" to message))
val (req, rep, res) = Fuel.get(Common.URL_SLACK,
listOf("token" to token, "channel" to channel, "text" to message))
.header(Headers.COOKIE to cookie)
.responseString()

Common.EXIT_CODE_SUCCESS
}
catch(e: Exception) {
} catch (e: Exception) {
Common.EXIT_CODE_FAILURE
}
}
Loading

0 comments on commit f3f2455

Please sign in to comment.