Skip to content

Commit

Permalink
ESQL: Only generate syntax diagrams locally
Browse files Browse the repository at this point in the history
CI will skip building them. Lot's of CI machines don't have font support
so they can't generate these. But all local machine have a GUI so they
can.

Also, super-lazy initialize the font so CI don't bump into it by
accident.

Closes elastic#99018
  • Loading branch information
nik9000 committed Aug 30, 2023
1 parent 9653da8 commit 5bf574b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/reference/esql/functions/signature/ceil.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/reference/esql/functions/signature/left.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/reference/esql/functions/types/ceil.asciidoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[%header.monospaced.styled,format=dsv,separator=|]
|===
arg1 | result
n | result
double | double
integer | integer
long | long
Expand Down
5 changes: 5 additions & 0 deletions docs/reference/esql/functions/types/left.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[%header.monospaced.styled,format=dsv,separator=|]
|===
arg1 | arg2 | result
keyword | integer | keyword
|===
6 changes: 5 additions & 1 deletion x-pack/plugin/esql/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ sourceSets.main.java {
}

tasks.getByName('test') {
dependsOn 'cleanGeneratedDocs'
if (BuildParams.isCi() == false) {
dependsOn 'cleanGeneratedDocs'
finalizedBy 'copyGeneratedDocs'
systemProperty 'generateDocs', true
}
}

tasks.register('cleanGeneratedDocs', Delete) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@
* asciidoc ceremony to make the result look right in the rendered docs.
* </li>
* <li>
* Auto-generate a syntax diagram and a table with supported types by running
* {@code ./gradlew x-pack:plugin:esql:copyGeneratedDocs}
* Generate a syntax diagram and a table with supported types by running the tests via
* gradle: {@code ./gradlew x-pack:plugin:esql:test}
* The generated files can be found here
* {@code docs/reference/esql/functions/signature/myfunction.svg }
* and here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,22 @@ private static Stream<DataType> representable() {
return EsqlDataTypes.types().stream().filter(EsqlDataTypes::isRepresentable);
}

@AfterClass
public static void renderSignature() throws IOException {
if (System.getProperty("generateDocs") == null) {
return;
}
FunctionDefinition definition = definition();
if (definition == null) {
LogManager.getLogger(getTestClass()).info("Skipping rendering signature because the function isn't registered");
return;
}

String rendered = RailRoadDiagram.functionSignature(definition);
LogManager.getLogger(getTestClass()).info("Writing function signature");
writeToTempDir("signature", rendered, "svg");
}

/**
* Unique signatures encountered by this test.
* <p>
Expand Down Expand Up @@ -880,6 +896,9 @@ public void trackSignature() {

@AfterClass
public static void renderTypesTable() throws IOException {
if (System.getProperty("generateDocs") == null) {
return;
}
FunctionDefinition definition = definition();
if (definition == null) {
LogManager.getLogger(getTestClass()).info("Skipping rendering types because the function isn't registered");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.nextencia.rrdiagram.grammar.rrdiagram.RRElement;
import net.nextencia.rrdiagram.grammar.rrdiagram.RRText;

import org.elasticsearch.common.util.LazyInitializable;
import org.elasticsearch.xpack.esql.plan.logical.show.ShowFunctions;
import org.elasticsearch.xpack.ql.expression.function.FunctionDefinition;

Expand All @@ -40,9 +41,9 @@ public class RailRoadDiagram {
* on whatever fonts you have installed. And, since the world can't agree
* on fonts, that'd be chaos. So, instead, we load Roboto Mono.
*/
private static final Font FONT = loadFont().deriveFont(20.0F);
private static final LazyInitializable<Font, IOException> FONT = new LazyInitializable<>(() -> loadFont().deriveFont(20.0F));

static String functionSignature(FunctionDefinition definition) {
static String functionSignature(FunctionDefinition definition) throws IOException {
List<Expression> expressions = new ArrayList<>();
expressions.add(new SpecialSequence(definition.name().toUpperCase(Locale.ROOT)));
expressions.add(new Syntax("("));
Expand All @@ -68,12 +69,12 @@ static String functionSignature(FunctionDefinition definition) {

RRDiagramToSVG toSvg = new RRDiagramToSVG();
toSvg.setSpecialSequenceShape(RRDiagramToSVG.BoxShape.RECTANGLE);
toSvg.setSpecialSequenceFont(FONT);
toSvg.setSpecialSequenceFont(FONT.getOrCompute());

toSvg.setLiteralFillColor(toSvg.getSpecialSequenceFillColor());
toSvg.setLiteralFont(FONT);
toSvg.setLiteralFont(FONT.getOrCompute());

toSvg.setRuleFont(FONT);
toSvg.setRuleFont(FONT.getOrCompute());
/*
* "Tighten" the styles in the SVG so they beat the styles sitting in the
* main page. We need this because we're embedding the SVG into the page.
Expand Down Expand Up @@ -143,7 +144,7 @@ public void addElement(String element) {
}
}

private static Font loadFont() {
private static Font loadFont() throws IOException {
try {
InputStream woff = RailRoadDiagram.class.getClassLoader()
.getResourceAsStream("META-INF/resources/webjars/fontsource__roboto-mono/4.5.7/files/roboto-mono-latin-400-normal.woff");
Expand All @@ -152,9 +153,7 @@ private static Font loadFont() {
}
return Font.createFont(Font.TRUETYPE_FONT, new WoffConverter().convertToTTFOutputStream(woff));
} catch (FontFormatException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
throw new IOException(e);
}
}
}

0 comments on commit 5bf574b

Please sign in to comment.