Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds EnvironmentVarsMacro. #23

Open
wants to merge 1 commit into
base: 0.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions gdkexamples/asciidoc/commons/connect-mysql-db.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ Note: If you are using macOS on Apple Silicon (M1, M1 Pro), Docker might fail to

____

Define the database driver URL, username, and password via environment variables:

The Micronaut framework populates the properties `datasources.default.url`, `datasources.default.username` and `datasources.default.password` with those environment variables' values. Learn more about https://micronaut-projects.github.io/micronaut-sql/latest/guide/#jdbc-connection-pools[JDBC Connection Pools].
Define the database driver URL, username, and password via environment variables:
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ include::asciidoc/commons/set-env-vars.adoc[]

include::asciidoc/commons/connect-mysql-db.adoc[]

environment-vars:[DATASOURCES_DEFAULT_URL=jdbc:mysql://localhost:3306/db,DATASOURCES_DEFAULT_USERNAME=sherlock,DATASOURCES_DEFAULT_PASSWORD=elementary]

The Micronaut framework populates the properties `datasources.default.url`, `datasources.default.username` and `datasources.default.password` with those environment variables' values. Learn more about https://micronaut-projects.github.io/micronaut-sql/latest/guide/#jdbc-connection-pools[JDBC Connection Pools].

== Run the Application

Expand Down
36 changes: 36 additions & 0 deletions guides-cli/src/main/java/io/micronaut/guides/cli/ConsoleTabs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.micronaut.guides.cli;

import io.micronaut.context.exceptions.ConfigurationException;
import io.micronaut.core.io.ResourceLoader;
import io.micronaut.guides.core.GuidesTemplatesConfiguration;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.stream.Collectors;

public class ConsoleTabs {
protected String consoleTabsHtml;

protected ConsoleTabs(ResourceLoader resourceLoader, GuidesTemplatesConfiguration guidesTemplatesConfiguration) {
String path = "classpath:" + guidesTemplatesConfiguration.getFolder() + "/consoleTabs.html";
Optional<InputStream> inputStreamOptional = resourceLoader.getResourceAsStream(path);
if (inputStreamOptional.isEmpty()) {
throw new ConfigurationException(path);
}
try (InputStream inputStream = inputStreamOptional.get()) {
this.consoleTabsHtml = readInputStream(inputStream);
} catch (Exception e) {
throw new ConfigurationException("Error loading resource: " + path, e);
}
}

protected static String readInputStream(InputStream inputStream) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.micronaut.guides.cli;

import io.micronaut.core.io.ResourceLoader;
import io.micronaut.guides.core.Guide;
import io.micronaut.guides.core.GuidesOption;
import io.micronaut.guides.core.GuidesTemplatesConfiguration;
import io.micronaut.guides.core.MacroSubstitution;
import io.micronaut.guides.core.asciidoc.AsciidocMacro;
import io.micronaut.guides.core.asciidoc.Attribute;
import jakarta.inject.Singleton;

import java.util.Optional;

import static io.micronaut.guides.core.MacroUtils.findMacroLines;

@Singleton
public class EnvironmentVarsMacro extends ConsoleTabs implements MacroSubstitution {
private final static String MACRO = "environment-vars";

EnvironmentVarsMacro(ResourceLoader resourceLoader, GuidesTemplatesConfiguration guidesTemplatesConfiguration) {
super(resourceLoader, guidesTemplatesConfiguration);
}

@Override
public String substitute(String str, Guide guide, GuidesOption option) {
for (String line : findMacroLines(str, MACRO)) {
Optional<AsciidocMacro> asciidocMacroOptional = AsciidocMacro.of(MACRO, line);
if (asciidocMacroOptional.isEmpty()) {
continue;
}

AsciidocMacro asciidocMacro = asciidocMacroOptional.get();
StringBuilder bashBuilder = new StringBuilder();
StringBuilder windowsBuilder = new StringBuilder();
StringBuilder powershellBuilder = new StringBuilder();

for (Attribute attribute : asciidocMacro.attributes()) {
String name = attribute.key();
String value = attribute.values().get(0);
bashBuilder.append("<span class=\"hljs-built_in\">export</span> ").append(name).append("=").append(value).append("\n");
windowsBuilder.append("<span class=\"hljs-built_in\">set</span> ").append(name).append("=").append(value).append("\n");
powershellBuilder.append("<span class=\"hljs-variable\">$ENV</span> ").append(name).append(" = <span class=\"hljs-string\">\"").append(value).append("\"</span>\n");
}

str = str.replace(line, "++++\n" + consoleTabsHtml.replace("{bash}", bashBuilder.toString()).replace("{windows}", windowsBuilder.toString()).replace("{powershell}", powershellBuilder.toString()) + "\n++++\n");
}
return str;
}
}
11 changes: 11 additions & 0 deletions guides-cli/src/main/resources/gdktemplates/consoleTabs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div class="tabs-doc ui-tabs ui-corner-all ui-widget ui-widget-content" data-name="system">
<div data-value="linux" data-label="Linux &amp; macOS" id="linux" aria-labelledby="ui-id-11" role="tabpanel" class="ui-tabs-panel ui-corner-bottom ui-widget-content" aria-hidden="false" style="">
<pre><code class="language-bash hljs" data-highlighted="yes">{bash}</code><button>Copy</button></pre>
</div>
<div data-value="windows" data-label="Windows" id="windows" aria-labelledby="ui-id-12" role="tabpanel" class="ui-tabs-panel ui-corner-bottom ui-widget-content" aria-hidden="true" style="display: none;">
<pre><code class="language-bash hljs" data-highlighted="yes">{windows}</code><button>Copy</button></pre>
</div>
<div data-value="windows-powershell" data-label="Windows PowerShell" id="windows-powershell" aria-labelledby="ui-id-13" role="tabpanel" class="ui-tabs-panel ui-corner-bottom ui-widget-content" aria-hidden="true" style="display: none;">
<pre><code class="language-bash hljs" data-highlighted="yes">{powershell}</code><button>Copy</button></pre>
</div>
</div>
Loading