Skip to content

Commit

Permalink
feat: Added Flank Wrapper (#2064)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotradamczyk5 committed Jul 6, 2021
1 parent 7c36d01 commit 1474048
Show file tree
Hide file tree
Showing 22 changed files with 363 additions and 26 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ jobs:
if: startsWith(github.ref, 'refs/tags/v')
run: flankScripts github make_release --input-file=./test_runner/build/libs/flank.jar --git-tag=$RELEASE_TAG --token=${{ secrets.GITHUB_TOKEN }}

- name: Append checksum to release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
sha256sum ./test_runner/build/libs/flank.jar > flank.sha256
gh release upload $RELEASE_TAG flank.sha256
- name: Gradle Upload to MavenCentral
uses: eskatos/gradle-command-action@v1
env:
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/release_flank_wrapper.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Release Flank Wrapper

on:
push:
paths:
- 'flank_wrapper/**'
branches:
- 'master'

jobs:
release:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- uses: gradle/wrapper-validation-action@v1

- name: Gradle Upload to Github packages and Github release
uses: eskatos/gradle-command-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
arguments: "flank_wrapper:releaseFlankWrapper -PGITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}"
25 changes: 25 additions & 0 deletions buildSrc/src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import org.gradle.api.Project
import java.io.ByteArrayOutputStream

fun Project.execAndGetStdout(vararg args: String): String {
val stdout = ByteArrayOutputStream()
exec {
commandLine(*args)
standardOutput = stdout
workingDir = projectDir
}
return stdout.toString().trimEnd()
}

// TODO replace with plugin in #2063
fun Project.isVersionChangedInBuildGradle(): Boolean {

val localResultsStream = execAndGetStdout("git", "diff", "origin/master", "HEAD", "--", "build.gradle.kts")
.split("\n")
val commitedResultsStream = execAndGetStdout("git", "diff", "origin/master", "--", "build.gradle.kts")
.split("\n")
return (commitedResultsStream + localResultsStream)
.filter { it.startsWith("-version = ") || it.startsWith("+version = ") }
.isNotEmpty()
}

2 changes: 2 additions & 0 deletions common/src/main/kotlin/flank/common/Files.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ val appDataDirectory: String by lazy {
if (isWindows) System.getenv("APPDATA") else System.getProperty("user.home")
}

val dotFlank = Paths.get(userHome, ".flank")

fun linkFiles(
link: String,
target: String
Expand Down
8 changes: 8 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,14 @@ flank:
# custom-sharding-json: ./custom_sharding.json
```

## Flank Wrapper

Flank wrapper is a solution to always run the latest version of Flank.
It will download the latest version of Flank itself always when it changed.
Using Flank wrapper is similar to using Flank, all options provided to Flank wrapper will be passed to Flank itself.
To download the latest version of Flank wrapper, please visit [GitHub releases](https://github.com/Flank/flank/releases)
and search for tag `flank_wrapper-XXX`. There are also shell and a batch wrapper over `.jar` file included.

## Android code coverage

<details>
Expand Down
27 changes: 3 additions & 24 deletions flank-scripts/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import java.io.ByteArrayOutputStream
import java.nio.file.Files
import java.nio.file.Paths

Expand All @@ -26,7 +25,7 @@ shadowJar.apply {
}
}
// <breaking change>.<feature added>.<fix/minor change>
version = "1.9.21"
version = "1.9.22"
group = "com.github.flank"

application {
Expand Down Expand Up @@ -128,6 +127,7 @@ val download by tasks.registering(Exec::class) {
}
}

// TODO replace with plugin in #2063
val checkIfVersionUpdated by tasks.registering(Exec::class) {
group = "verification"
commandLine("git", "fetch", "--no-tags")
Expand Down Expand Up @@ -160,25 +160,4 @@ val releaseFlankScripts by tasks.registering(Exec::class) {
)
}

fun isVersionChangedInBuildGradle(): Boolean {

val localResultsStream = execAndGetStdout("git", "diff", "origin/master", "HEAD", "--", "build.gradle.kts")
.split("\n")
val commitedResultsStream = execAndGetStdout("git", "diff", "origin/master", "--", "build.gradle.kts")
.split("\n")
return (commitedResultsStream + localResultsStream)
.filter { it.startsWith("-version = ") || it.startsWith("+version = ") }
.size >= 2
}

fun execAndGetStdout(vararg args: String): String {
val stdout = ByteArrayOutputStream()
exec {
commandLine(*args)
standardOutput = stdout
workingDir = projectDir
}
return stdout.toString().trimEnd()
}

tasks["lintKotlin"].dependsOn(tasks["checkIfVersionUpdated"])
tasks["lintKotlin"].dependsOn(checkIfVersionUpdated)
5 changes: 5 additions & 0 deletions flank_wrapper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
132 changes: 132 additions & 0 deletions flank_wrapper/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

plugins {
application
kotlin(Plugins.Kotlin.PLUGIN_JVM)
id(Plugins.PLUGIN_SHADOW_JAR) version Versions.SHADOW
id(Plugins.MAVEN_PUBLISH)
}

val artifactID = "flank_wrapper"
val runnerClass = "com.github.flank.wrapper.MainKt"
val shadowJar: ShadowJar by tasks

shadowJar.apply {
archiveClassifier.set("")
archiveFileName.set("$artifactID.jar")
mergeServiceFiles()

@Suppress("UnstableApiUsage")
manifest {
attributes(mapOf("Main-Class" to runnerClass))
}
}
// <breaking change>.<feature added>.<fix/minor change>
version = "1.0.0"
group = "com.github.flank"

repositories {
mavenCentral()
}

publishing {
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/Flank/flank")
credentials {
username = System.getenv("GITHUB_ACTOR") ?: properties["GITHUB_ACTOR"].toString()
password = System.getenv("GITHUB_TOKEN") ?: properties["GITHUB_TOKEN"].toString()
}
}
}
publications {
create<MavenPublication>("mavenJava") {
groupId = group.toString()
artifactId = artifactID
version = version

artifact(shadowJar)

pom {
name.set("Flank-wrapper")
description.set("Wrapper to run latest version of Flank")
url.set("https://github.com/Flank/flank")

licenses {
license {
name.set("The Apache License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
}
}
}

pom.withXml {
// Remove deps since we're publishing a fat jar
val pom = asNode()
val depNode: groovy.util.NodeList = pom.get("dependencies") as groovy.util.NodeList
for (child in depNode) {
pom.remove(child as groovy.util.Node)
}
}
}
}
}

dependencies {
implementation(project(":common"))
implementation(Dependencies.Fuel.CORE)
testImplementation(Dependencies.JUNIT)
testImplementation(Dependencies.TRUTH)
}

val releaseFlankWrapper by tasks.registering(Exec::class) {
dependsOn(":flank_wrapper:publish")
commandLine(
"gh", "release", "create",
"flank-wrapper-$version",
"$buildDir/libs/$artifactID.jar",
"flankw",
"flankw.bat",
"-t", "Flank Wrapper $version",
"-p"
)
}


application {
mainClass.set(runnerClass)
}

// TODO replace with plugin in #2063
val checkIfVersionUpdated by tasks.registering(Exec::class) {
group = "verification"
commandLine("git", "fetch", "--no-tags")

doLast {
val changedFiles = execAndGetStdout("git", "diff", "origin/master", "HEAD", "--name-only").split("\n") +
execAndGetStdout("git", "diff", "origin/master", "--name-only").split("\n")
val isVersionChanged = changedFiles.any { it.startsWith("flank_wrapper") }.not() ||
(changedFiles.contains("flank_wrapper/build.gradle.kts") && isVersionChangedInBuildGradle())

if (isVersionChanged.not()) {
throw GradleException(
"""
Flank wrapper version is not updated, but files changed.
Please update version according to schema: <breaking change>.<feature added>.<fix/minor change>
""".trimIndent()

)
}
}
}

val prepareJar by tasks.registering(Copy::class) {
dependsOn("shadowJar")
from("$buildDir/libs")
include("flank_wrapper.jar")
into("$projectDir")
}


tasks["lintKotlin"].dependsOn(checkIfVersionUpdated)
5 changes: 5 additions & 0 deletions flank_wrapper/flankw
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

DIR=`dirname "$BASH_SOURCE"`

/usr/bin/env java -jar "$DIR/flank_wrapper.jar" "$@"
1 change: 1 addition & 0 deletions flank_wrapper/flankw.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java -jar "flank_wrapper.jar" %*
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.flank.wrapper

import com.github.flank.wrapper.internal.runFlankOnLatestVersion

fun main(vararg args: String) {
runFlankOnLatestVersion(args)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.flank.wrapper.internal

import java.io.File
import java.security.MessageDigest

internal fun File.sha256(): String = readBytes().sha256()

private fun ByteArray.sha256(): String {
return hashString(this, "SHA-256")
}

private fun hashString(input: ByteArray, algorithm: String): String {
return MessageDigest
.getInstance(algorithm)
.digest(input)
.fold("") { str, it -> str + "%02x".format(it) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.flank.wrapper.internal

import flank.common.downloadFile
import java.io.File
import kotlin.io.path.createTempFile
import kotlin.io.path.readText

internal fun compareLatestVersionCheckSumWithCurrent(): Boolean = getFlankLocalChecksum() == getFlankRemoteChecksum()

private fun getFlankLocalChecksum(): String {
val flankFile = File(flankRunnerPath)
return flankFile.takeIf(File::exists)?.let(File::sha256).orEmpty()
}

private fun getFlankRemoteChecksum(): String = with(createTempFile("flank", "sha256")) {
downloadFile(
sourceUrl = "https://github.com/Flank/flank/releases/latest/download/flank.sha256",
destinationPath = toAbsolutePath()
)
readText().split("\\s+".toRegex()).first()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.flank.wrapper.internal

import flank.common.downloadFile

internal fun downloadLatestFlankVersion() {
downloadFile(
sourceUrl = FLANK_JAR_REMOTE_PATH,
destination = flankRunnerPath
)
}

private const val FLANK_JAR_REMOTE_PATH = "https://github.com/Flank/flank/releases/latest/download/flank.jar"
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.flank.wrapper.internal

import kotlin.system.exitProcess

internal fun executeFlank(args: Array<out String>) {
ProcessBuilder()
.command(buildRunCommand(args))
.inheritIO()
.start()
.waitFor()
.let { flankExitCode -> exitProcess(flankExitCode) }
}

internal fun buildRunCommand(args: Array<out String>): List<String> = listOf(
"java",
"-jar",
flankRunnerPath
) + args.asList()
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.flank.wrapper.internal

import flank.common.dotFlank

internal val flankRunnerPath: String
get() = "$dotFlank/$FLANK_JAR_NAME"

private const val FLANK_JAR_NAME = "flank.jar"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.flank.wrapper.internal

internal fun runFlankOnLatestVersion(args: Array<out String>) {
if (!compareLatestVersionCheckSumWithCurrent()) {
println("The new Flank version is available. Downloading new version...")
downloadLatestFlankVersion()
println("The new Flank version is ready to use")
}
executeFlank(args)
}
Loading

0 comments on commit 1474048

Please sign in to comment.