Skip to content

Commit

Permalink
Доработки в презентации
Browse files Browse the repository at this point in the history
  • Loading branch information
LimeBeck committed Apr 11, 2024
1 parent 7be7614 commit afcd06f
Show file tree
Hide file tree
Showing 9 changed files with 451 additions and 259 deletions.
80 changes: 40 additions & 40 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
#name: Build and Deploy
#on:
# # Triggers the workflow on push or pull request events but only for the master branch
# push:
# branches: [ master ]
#jobs:
# jbang:
# runs-on: ubuntu-latest
# name: A job to run jbang
# permissions:
# pages: write # to deploy to Pages
# id-token: write # to verify the deployment originates from an appropriate source
#
# environment:
# name: github-pages
# url: ${{ steps.deployment.outputs.page_url }}
# steps:
# - name: checkout
# uses: actions/checkout@v1
# - uses: actions/cache@v1
# with:
# path: /root/.jbang
# key: $-jbang-$
# restore-keys: |
# $-jbang-
# - name: jbang
# uses: jbangdev/[email protected]
# with:
# script: [email protected]
# scriptargs: render ./presentation/GradlePresentation.reveal.kts
# - name: Setup Pages
# uses: actions/configure-pages@v2
# - name: Upload artifact
# uses: actions/upload-pages-artifact@v1
# with:
# # Upload build dir
# path: './out'
# - name: Deploy to GitHub Pages
# id: deployment
# uses: actions/deploy-pages@v1
name: Build and Deploy
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master ]
jobs:
jbang:
runs-on: ubuntu-latest
name: A job to run jbang
permissions:
pages: write # to deploy to Pages
id-token: write # to verify the deployment originates from an appropriate source

environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: checkout
uses: actions/checkout@v1
- uses: actions/cache@v1
with:
path: /root/.jbang
key: $-jbang-$
restore-keys: |
$-jbang-
- name: jbang
uses: jbangdev/[email protected]
with:
script: [email protected]
scriptargs: render ./presentation/GradlePresentation.reveal.kts
- name: Setup Pages
uses: actions/configure-pages@v2
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
# Upload build dir
path: './out'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package dev.limebeck.ci.gitlab.scripts

import dev.limebeck.ci.gitlab.scripts.tools.Directories
import java.io.File
import java.nio.ByteBuffer
import java.security.MessageDigest
import kotlin.script.experimental.api.ScriptCompilationConfiguration
import kotlin.script.experimental.api.SourceCode


const val COMPILED_SCRIPTS_CACHE_DIR_ENV_VAR = "KOTLIN_MAIN_KTS_COMPILED_SCRIPTS_CACHE_DIR"
const val COMPILED_SCRIPTS_CACHE_DIR_PROPERTY = "kotlin.main.kts.compiled.scripts.cache.dir"
const val COMPILED_SCRIPTS_CACHE_VERSION = 1

internal fun findCacheBaseDir(): File? {
val cacheExtSetting = System.getProperty(COMPILED_SCRIPTS_CACHE_DIR_PROPERTY)
?: System.getenv(COMPILED_SCRIPTS_CACHE_DIR_ENV_VAR)
return when {
cacheExtSetting == null -> Directories(System.getProperties(), System.getenv()).cache
?.takeIf { it.exists() && it.isDirectory }
?.let { File(it, "main.kts.compiled.cache").apply { mkdir() } }

cacheExtSetting.isBlank() -> null
else -> File(cacheExtSetting)
}?.takeIf { it.exists() && it.isDirectory }
}

internal fun compiledScriptUniqueName(
script: SourceCode,
scriptCompilationConfiguration: ScriptCompilationConfiguration
): String {
val digestWrapper = MessageDigest.getInstance("SHA-256")

fun addToDigest(chunk: String) = with(digestWrapper) {
val chunkBytes = chunk.toByteArray()
update(chunkBytes.size.toByteArray())
update(chunkBytes)
}

digestWrapper.update(COMPILED_SCRIPTS_CACHE_VERSION.toByteArray())
addToDigest(script.text)
scriptCompilationConfiguration.notTransientData.entries
.sortedBy { it.key.name }
.forEach {
addToDigest(it.key.name)
addToDigest(it.value.toString())
}
return digestWrapper.digest().toHexString()
}

private fun ByteArray.toHexString(): String = joinToString("", transform = { "%02x".format(it) })

private fun Int.toByteArray() = ByteBuffer.allocate(Int.SIZE_BYTES).also { it.putInt(this) }.array()
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.limebeck.ci.gitlab.scripts

import kotlinx.coroutines.runBlocking
import kotlin.script.experimental.api.*
import kotlin.script.experimental.dependencies.CompoundDependenciesResolver
import kotlin.script.experimental.dependencies.FileSystemDependenciesResolver
import kotlin.script.experimental.dependencies.maven.MavenDependenciesResolver
import kotlin.script.experimental.dependencies.resolveFromScriptSourceAnnotations
import kotlin.script.experimental.jvm.JvmDependency


// Handler that reconfigures the compilation on the fly
fun configureMavenDepsOnAnnotations(
context: ScriptConfigurationRefinementContext
): ResultWithDiagnostics<ScriptCompilationConfiguration> {
val annotations = context.collectedData
?.get(ScriptCollectedData.collectedAnnotations)
?.takeIf { it.isNotEmpty() }
?: return context.compilationConfiguration.asSuccess()
return runBlocking {
resolver.resolveFromScriptSourceAnnotations(annotations)
}.onSuccess {
context.compilationConfiguration.with {
dependencies.append(JvmDependency(it))
}.asSuccess()
}
}

private val resolver = CompoundDependenciesResolver(FileSystemDependenciesResolver(), MavenDependenciesResolver())
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package dev.limebeck.ci.gitlab.scripts

import dev.limebeck.ci.gitlab.scripts.tools.Directories
import dev.otbe.gitlab.ci.dsl.PipelineBuilder
import kotlinx.coroutines.runBlocking
import java.io.File
import java.nio.ByteBuffer
import java.security.MessageDigest
import kotlin.script.experimental.annotations.KotlinScript
import kotlin.script.experimental.api.*
import kotlin.script.experimental.dependencies.*
import kotlin.script.experimental.dependencies.maven.MavenDependenciesResolver
import kotlin.script.experimental.dependencies.DependsOn
import kotlin.script.experimental.dependencies.Repository
import kotlin.script.experimental.host.ScriptingHostConfiguration
import kotlin.script.experimental.jvm.JvmDependency
import kotlin.script.experimental.jvm.compilationCache
import kotlin.script.experimental.jvm.dependenciesFromClassContext
import kotlin.script.experimental.jvm.jvm
Expand All @@ -26,10 +20,6 @@ import kotlin.script.experimental.jvmhost.CompiledScriptJarsCache
)
abstract class GitlabCiKtScript

const val COMPILED_SCRIPTS_CACHE_DIR_ENV_VAR = "KOTLIN_MAIN_KTS_COMPILED_SCRIPTS_CACHE_DIR"
const val COMPILED_SCRIPTS_CACHE_DIR_PROPERTY = "kotlin.main.kts.compiled.scripts.cache.dir"
const val COMPILED_SCRIPTS_CACHE_VERSION = 1

object GitlabCiKtScriptCompilationConfiguration : ScriptCompilationConfiguration({
jvm { dependenciesFromClassContext(PipelineBuilder::class, wholeClasspath = true) }
defaultImports(DependsOn::class, Repository::class)
Expand All @@ -41,29 +31,19 @@ object GitlabCiKtScriptCompilationConfiguration : ScriptCompilationConfiguration
implicitReceivers(PipelineBuilder::class)
ide { acceptedLocations(ScriptAcceptedLocation.Everywhere) }
compilerOptions.append("-Xadd-modules=ALL-MODULE-PATH")

// Callbacks
refineConfiguration {
// Process specified annotations with the provided handler
onAnnotations(DependsOn::class, Repository::class, handler = ::configureMavenDepsOnAnnotations)
}
})

object GitlabCiKtHostConfiguration : ScriptingHostConfiguration({
jvm {
val cacheExtSetting = System.getProperty(COMPILED_SCRIPTS_CACHE_DIR_PROPERTY)
?: System.getenv(COMPILED_SCRIPTS_CACHE_DIR_ENV_VAR)
val cacheBaseDir = when {
cacheExtSetting == null -> Directories(System.getProperties(), System.getenv()).cache
?.takeIf { it.exists() && it.isDirectory }
?.let { File(it, "main.kts.compiled.cache").apply { mkdir() } }
cacheExtSetting.isBlank() -> null
else -> File(cacheExtSetting)
}?.takeIf { it.exists() && it.isDirectory }
val cacheBaseDir = findCacheBaseDir()
if (cacheBaseDir != null)
compilationCache(
CompiledScriptJarsCache { script, scriptCompilationConfiguration ->
File(cacheBaseDir, compiledScriptUniqueName(script, scriptCompilationConfiguration) + ".jar")
cacheBaseDir
.resolve(compiledScriptUniqueName(script, scriptCompilationConfiguration) + ".jar")
}
)
}
Expand All @@ -73,42 +53,3 @@ object GitlabCiKtEvaluationConfiguration : ScriptEvaluationConfiguration({
scriptsInstancesSharing(false)
constructorArgs()
})

// Handler that reconfigures the compilation on the fly
fun configureMavenDepsOnAnnotations(context: ScriptConfigurationRefinementContext): ResultWithDiagnostics<ScriptCompilationConfiguration> {
val annotations = context.collectedData?.get(ScriptCollectedData.collectedAnnotations)?.takeIf { it.isNotEmpty() }
?: return context.compilationConfiguration.asSuccess()
return runBlocking {
resolver.resolveFromScriptSourceAnnotations(annotations)
}.onSuccess {
context.compilationConfiguration.with {
dependencies.append(JvmDependency(it))
}.asSuccess()
}
}

private val resolver = CompoundDependenciesResolver(FileSystemDependenciesResolver(), MavenDependenciesResolver())

private fun compiledScriptUniqueName(script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration): String {
val digestWrapper = MessageDigest.getInstance("SHA-256")

fun addToDigest(chunk: String) = with(digestWrapper) {
val chunkBytes = chunk.toByteArray()
update(chunkBytes.size.toByteArray())
update(chunkBytes)
}

digestWrapper.update(COMPILED_SCRIPTS_CACHE_VERSION.toByteArray())
addToDigest(script.text)
scriptCompilationConfiguration.notTransientData.entries
.sortedBy { it.key.name }
.forEach {
addToDigest(it.key.name)
addToDigest(it.value.toString())
}
return digestWrapper.digest().toHexString()
}

private fun ByteArray.toHexString(): String = joinToString("", transform = { "%02x".format(it) })

private fun Int.toByteArray() = ByteBuffer.allocate(Int.SIZE_BYTES).also { it.putInt(this) }.array()
Loading

0 comments on commit afcd06f

Please sign in to comment.