Skip to content

Commit

Permalink
Merge pull request #84 from kuzzleio/1.3.3-proposal
Browse files Browse the repository at this point in the history
Release 1.3.3
  • Loading branch information
Shiranuit authored Jan 2, 2023
2 parents 2b40f9b + 9fed0aa commit c7b46de
Show file tree
Hide file tree
Showing 42 changed files with 680 additions and 501 deletions.
8 changes: 8 additions & 0 deletions .ci/doc/builder/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:14.18.2

COPY . /var/app
WORKDIR /var/app

RUN npm install

CMD [ "node /var/app/builder.js" ]
90 changes: 90 additions & 0 deletions .ci/doc/builder/builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const yaml = require('js-yaml');
const fs = require('fs');
const Path = require('path');
const glob = require('glob');

const javaSnippetPath = '/mnt/doc/**/snippets/*-java.test.yml';
const kotlinSnippetPath = '/mnt/doc/**/snippets/*-kotlin.test.yml';
const snippetTemplatesPath = '/mnt/snippet-templates/*';
const buildDir = '/mnt/build';

/**
* Extract code from balises @start and @stop
* If one of the balise is missing, the whole file is returned
*
* @param {String} code
* @returns
*/
function extractCode(code) {
const lines = code.split('\n');
const start = lines.findIndex(line => line.includes('// @start'));
const end = lines.findIndex(line => line.includes('// @end'));

if (start === -1 || end === -1) {
return code;
}

return lines.slice(start, end).join('\n');
}

/**
* Go over each file and build the snippet with the right template
* then build the method that encapsulate the snippet
*
* @param {String[]} paths
* @param {String} ext
* @param {*} templates
* @returns
*/
function buildSnippetMethods(paths, ext, templates) {
const methodTemplate = fs.readFileSync(`./templates/method.${ext}`, 'utf-8');

const methods = {};
for (const path of paths) {
const testConfig = yaml.load(fs.readFileSync(path, 'utf8'));

const template = templates[`${testConfig.template}.tpl.${ext}`];

const snippetName = testConfig.name.toLowerCase().replace(/ /g, '_').replace(/-/g, '_').replace(/#/g,'');

const snippetCode = extractCode(fs.readFileSync(path.replace('.test.yml', `.${ext}`), 'utf8'));

const snippet = template.replace('[snippet-code]', snippetCode);
methods[snippetName] = methodTemplate.replace('[snippet-name]', snippetName).replace('[snippet-code]', snippet);
}

return methods;
}

/**
* Build each snippet in a static method and bundle every method in a single file
* ready to be compiled
*
* @param {*} paths
* @param {*} ext
* @param {*} templates
*/
function bundleAndBuildTests(paths, ext, templates) {
const methods = buildSnippetMethods(paths, ext, templates);

const mainTemplate = fs.readFileSync(`./templates/main.${ext}`, 'utf8');

const buildedSnippets = Object.keys(methods).map(key => methods[key]).join('\n');


const program = mainTemplate.replace('[builded-snippets]', buildedSnippets);
fs.writeFileSync(`${buildDir}/SnippetTest.${ext}`, program);
}

async function main() {
const templatesPath = glob.sync(snippetTemplatesPath);
const templates = {}
for (const path of templatesPath) {
templates[Path.basename(path)] = fs.readFileSync(path, 'utf8');
}

bundleAndBuildTests(glob.sync(javaSnippetPath), 'java', templates);
bundleAndBuildTests(glob.sync(kotlinSnippetPath), 'kt', templates);
}

main();
98 changes: 98 additions & 0 deletions .ci/doc/builder/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .ci/doc/builder/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "builder",
"version": "1.0.0",
"description": "",
"main": "builder.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"glob": "^7.2.0",
"js-yaml": "^4.1.0"
}
}
39 changes: 39 additions & 0 deletions .ci/doc/builder/templates/main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import io.kuzzle.sdk.Kuzzle;
import io.kuzzle.sdk.protocol.*;
import io.kuzzle.sdk.coreClasses.responses.Response;
import io.kuzzle.sdk.coreClasses.SearchResult;
import io.kuzzle.sdk.coreClasses.lang.Lang;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

public class SnippetTest {

[builded-snippets]

public static void main(String[] args) {
if (args.length < 1) {
System.err.println("Missing snippet name to execute");
System.exit(1);
}

// Format the snippet name into the method name
// Convert "java-searchresultscroll_3e8f25b" into "java_searchresultscroll"
String methodName = args[0].toLowerCase().replace("-","_").substring(0, args[0].length() - 8);

try {
// Find the static method that has the given name using reflection
Method method = SnippetTest.class.getMethod(methodName);
// Execute the static method
method.invoke(null);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

}
30 changes: 30 additions & 0 deletions .ci/doc/builder/templates/main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import io.kuzzle.sdk.Kuzzle
import io.kuzzle.sdk.protocol.*
import io.kuzzle.sdk.coreClasses.responses.Response
import io.kuzzle.sdk.coreClasses.SearchResult
import io.kuzzle.sdk.coreClasses.lang.Lang
import io.kuzzle.sdk.coreClasses.maps.KuzzleMap
import java.lang.reflect.Method
import java.util.*;
import kotlin.system.exitProcess

public class Snippets {
[builded-snippets]
}

fun main(args: Array<String>) {
if (args.isEmpty()) {
println("Missing snippet name to execute")
exitProcess(1)
}

// Format the snippet name into the method name
// Convert "kotlin-searchresultscroll_3e8f25b" into "kotlin_searchresultscroll"
val methodName: String = args[0].lowercase().replace('-', '_').substring(0, args[0].length - 8)

var snippets: Snippets = Snippets()
// Find the method that has the given name using reflection
var method: Method = snippets.javaClass.getMethod(methodName)
// execute method
method.invoke(snippets)
}
4 changes: 4 additions & 0 deletions .ci/doc/builder/templates/method.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public static void [snippet-name]() {
Kuzzle kuzzle = null;
[snippet-code]
}
3 changes: 3 additions & 0 deletions .ci/doc/builder/templates/method.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun [snippet-name]() {
[snippet-code]
}
8 changes: 4 additions & 4 deletions .ci/doc/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ runners:
default: java

java:
debugStdout: false
debugStderr: false
service: doc-runner-java
path: /var/snippets/java
build:
cmd: cd /mnt && ./gradlew jar
afterInit: /mnt/.ci/doc/snippet-build.sh
run:
before: timeout 600 bash -c 'until stat /tmp/runner_is_ready && curl -f -s -o /dev/null http://kuzzle:7512/_now; do sleep 1; done'
cmd: SNIPPET_PROTOCOL="{{ snippet.protocol }}" /mnt/.ci/doc/test-snippet.sh {{ snippet.source }}
cmd: SNIPPET_PROTOCOL="{{ snippet.protocol }}" /mnt/.ci/doc/test-snippet.sh "{{ snippet.source }}" "{{ snippet.name }}"
19 changes: 18 additions & 1 deletion .ci/doc/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,21 @@ services:
ulimits:
nofile: 65536

snippet-builder:
build: ./builder
command: >
sh -c '
rm /mnt/build/SnippetTest.java;
rm /mnt/build/SnippetTest.kt;
node /var/app/builder.js
'
volumes:
- ../..:/mnt
- build:/mnt/build
- ./templates:/mnt/snippet-templates

doc-tests:
image: kuzzleio/snippets-tests
image: kuzzleio/snippets-tests:latest
privileged: true
ports:
- '9229:9229'
Expand All @@ -48,11 +61,15 @@ services:
touch /tmp/runner_is_ready;
tail -f /dev/null
'
depends_on:
- snippet-builder
links:
- kuzzle
volumes:
- ../..:/mnt
- build:/mnt/build
- snippets:/var/snippets

volumes:
snippets:
build:
21 changes: 21 additions & 0 deletions .ci/doc/snippet-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

set -e

echo "Wait for Snippets to be built and bundled"
timeout 600 bash -c 'until stat /mnt/build/SnippetTest.java > /dev/null && stat /mnt/build/SnippetTest.kt > /dev/null; do sleep 1; done'
echo "Building SDK"
cd /mnt && ./gradlew jar
echo "Copying SDK"
cp /mnt/build/libs/sdk-jvm-[0-9+].[0-9+].[0-9+]-without-dependencies.jar /mnt/.ci/doc/java-project/libs/sdk-jvm-without-dependencies.jar
cp /mnt/build/libs/sdk-jvm-[0-9+].[0-9+].[0-9+]-without-dependencies.jar /mnt/.ci/doc/kotlin-project/libs/sdk-jvm-without-dependencies.jar
echo "Copying Java Snippets"
cp /mnt/build/SnippetTest.java /mnt/.ci/doc/java-project/src/main/java/SnippetTest.java
echo "Copying Kotlin Snippets"
cp /mnt/build/SnippetTest.kt /mnt/.ci/doc/kotlin-project/src/main/java/SnippetTest.kt
echo "Building Java executable"
cd /mnt/.ci/doc/java-project/ && ./gradlew build
echo "Building Kotlin executable"
cd /mnt/.ci/doc/kotlin-project/ && ./gradlew build
echo "Wait for Kuzzle to be ready"
timeout 600 bash -c 'until stat /tmp/runner_is_ready && curl -f -s -o /dev/null http://kuzzle:7512/_now; do sleep 1; done'
Loading

0 comments on commit c7b46de

Please sign in to comment.