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

#2348 updates dep macro to do groovy+kotlin compatible gradle, handle… #18

Open
wants to merge 1 commit into
base: master
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ class BuildDependencyMacro extends InlineMacroProcessor implements ValueAtAttrib
static final String DEPENDENCY_PREFIX = 'micronaut-'
static final String GROUPID = 'io.micronaut'
static final String MULTILANGUAGECSSCLASS = 'multi-language-sample'
static final String BUILD_GRADLE = 'gradle-groovy'
static final String BUILD_GRADLE = 'gradle'
static final String BUILD_MAVEN = 'maven'
static final String BUILD_GRADLE_KOTLIN = 'gradle-kotlin'
public static final String SCOPE_COMPILE = 'compile'

BuildDependencyMacro(String macroName, Map<String, Object> config) {
Expand Down Expand Up @@ -98,8 +97,7 @@ class BuildDependencyMacro extends InlineMacroProcessor implements ValueAtAttrib
String gradleScope = valueAtAttributes('gradleScope', attributes) ?: toGradleScope(attributes) ?: SCOPE_COMPILE
String mavenScope = valueAtAttributes('mavenScope', attributes) ?: toMavenScope(attributes) ?: SCOPE_COMPILE
String title = valueAtAttributes('title', attributes) ?: ""
String content = gradleDependency(BUILD_GRADLE, groupId, artifactId, version, classifier, gradleScope, MULTILANGUAGECSSCLASS, title, verbose)
content += gradleKotlinDependency(BUILD_GRADLE_KOTLIN, groupId, artifactId, version, classifier, mavenScope, MULTILANGUAGECSSCLASS, title)
String content = gradleKotlinAndGroovyDependency(BUILD_GRADLE, groupId, artifactId, version, classifier, gradleScope, MULTILANGUAGECSSCLASS, title)
content += mavenDependency(BUILD_MAVEN, groupId, artifactId, version, classifier, mavenScope, MULTILANGUAGECSSCLASS, title)
createBlock(parent, "pass", [content], attributes, config).convert()
}
Expand Down Expand Up @@ -133,48 +131,7 @@ class BuildDependencyMacro extends InlineMacroProcessor implements ValueAtAttrib
}
}



String gradleDependency(String build,
String groupId,
String artifactId,
String version,
String classifier,
String scope,
String multilanguageCssClass,
String title,
boolean verbose) {
String html = """\
<div class=\"listingblock ${multilanguageCssClass}\">
<div class=\"title\">$title</div>
<div class=\"content\">
<pre class=\"highlightjs highlight\"><code class=\"language-groovy hljs" data-lang="${build}">"""
if (verbose) {
html += "${scope} <span class=\"hljs-string\">group:</span> <span class=\"hljs-string\">'${groupId}'</span>, <span class=\"hljs-string\">name:</span> <span class=\"hljs-string\">'${artifactId}'</span>"
if (version) {
html +=", <span class=\"hljs-string\">version:</span> <span class=\"hljs-string\">'${version}'</span>"
}
if (classifier) {
html +=", <span class=\"hljs-string\">classifier:</span> <span class=\"hljs-string\">'${classifier}'</span>"
}
} else {
html += "${scope} <span class=\"hljs-string\">'${groupId}:${artifactId}"
if (version) {
html += ":${version}"
}
if (classifier) {
html += ":${classifier}"
}
html += "'</span>"
}
html += """</code></pre>
</div>
</div>
"""
html
}

String gradleKotlinDependency(String build,
String gradleKotlinAndGroovyDependency(String build,
String groupId,
String artifactId,
String version,
Expand Down
125 changes: 91 additions & 34 deletions src/main/docs/resources/js/multi-language-sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ var LANG_JAVA = "java";
var LANG_GROOVY = "groovy";
var LANG_KOTLIN = "kotlin";
var MICRONAUT_SUPPORTED_BUILDS = [BUILD_GRADLE, BUILD_GRADLE_GROOVY, BUILD_GRADLE_KOTLIN, BUILD_MAVEN];
var MICRONAUT_BUILD_HAS_SUBSET = [BUILD_GRADLE];
var MICRONAUT_BUILD_IS_SUBSET = [BUILD_GRADLE_GROOVY, BUILD_GRADLE_KOTLIN];
var MICRONAUT_BUILD_SUBSET_LOOKUP = {};
MICRONAUT_BUILD_SUBSET_LOOKUP[BUILD_GRADLE_GROOVY] = BUILD_GRADLE;
MICRONAUT_BUILD_SUBSET_LOOKUP[BUILD_GRADLE_KOTLIN] = BUILD_GRADLE;
var MICRONAUT_SUPPORTED_LANGS = [LANG_JAVA, LANG_GROOVY, LANG_KOTLIN];
var DEFAULT_SUPPORTED_LANG = LANG_JAVA;
var DEFAULT_BUILD = BUILD_GRADLE;
var DEFAULT_BUILD_SUBSET = BUILD_GRADLE_GROOVY;
var LOCALSTORAGE_KEY_LANG = "preferred-micronaut-language";
var LOCALSTORAGE_KEY_BUILD = "preferred-micronaut-build";
var LOCALSTORAGE_KEY_BUILD_SUBSET = "preferred-micronaut-build-subset";


function addCopyToClipboardButtons() {
Expand All @@ -25,22 +32,33 @@ function postProcessCodeBlocks() {
// Assumptions:
// 1) All siblings that are marked with class="multi-language-sample" should be grouped
// 2) Only one language can be selected per domain (to allow selection to persist across all docs pages)
// 3) There is exactly 1 small set of languages to choose from. This does not allow for multiple language preferences. For example, users cannot prefer both Kotlin and ZSH.
// 3) There is exactly 1 small set of languages to choose from. This does not allow for multiple language preferences. For example, users cannot prefer both Kotlin and Groovy.
// (Exception: With the build languages, we can have common gradle, which works w/ the groovy and kotlin DSLs, as well as DSL preferences.)
// 4) Only 1 sample of each language can exist in the same collection.


var preferredLanguage = initPreferredLanguage();
var preferredBuild = initPreferredBuild();
var preferredLanguage = getOrSetDefaultPreferredLanguage();
var preferredBuild = getOrSetDefaultPreferredBuild();
var preferredBuildSubset = getOrSetDefaultPreferredBuildSubset();

function isBuild(optionId) {
return MICRONAUT_SUPPORTED_BUILDS.indexOf(optionId) > -1
return MICRONAUT_SUPPORTED_BUILDS.indexOf(optionId) > -1;
}

function isBuildSubset(optionId) {
return MICRONAUT_BUILD_IS_SUBSET.indexOf(optionId) > -1;
}

function isBuildSupertype(optionId) {
return MICRONAUT_BUILD_HAS_SUBSET.indexOf(optionId) > -1;
}

function isLang(optionId) {
return MICRONAUT_SUPPORTED_LANGS.indexOf(optionId) > -1
return MICRONAUT_SUPPORTED_LANGS.indexOf(optionId) > -1;
}

// Ensure preferred Language is valid, defaulting to JAVA
function initPreferredLanguage() {
function getOrSetDefaultPreferredLanguage() {
var lang = window.localStorage.getItem(LOCALSTORAGE_KEY_LANG);
if (MICRONAUT_SUPPORTED_LANGS.indexOf(lang) === -1) {
window.localStorage.setItem(LOCALSTORAGE_KEY_LANG, DEFAULT_SUPPORTED_LANG);
Expand All @@ -50,7 +68,7 @@ function postProcessCodeBlocks() {
}

// Ensure preferred build is valid, defaulting to GRADLE
function initPreferredBuild() {
function getOrSetDefaultPreferredBuild() {
var build = window.localStorage.getItem(LOCALSTORAGE_KEY_BUILD);
if (MICRONAUT_SUPPORTED_BUILDS.indexOf(build) === -1) {
window.localStorage.setItem(LOCALSTORAGE_KEY_BUILD, DEFAULT_BUILD);
Expand All @@ -59,6 +77,15 @@ function postProcessCodeBlocks() {
return build;
}

function getOrSetDefaultPreferredBuildSubset() {
var buildSubset = window.localStorage.getItem(LOCALSTORAGE_KEY_BUILD_SUBSET);
if (MICRONAUT_BUILD_IS_SUBSET.indexOf(buildSubset) === -1) {
window.localStorage.setItem(LOCALSTORAGE_KEY_BUILD_SUBSET, DEFAULT_BUILD_SUBSET);
buildSubset = DEFAULT_BUILD_SUBSET;
}
return buildSubset;
}

// This makes the dash separated sub-langs display better
function makeTitleForSnippetSelector(string) {
var langSlices = string.split("-");
Expand All @@ -74,11 +101,27 @@ function postProcessCodeBlocks() {
return string.charAt(0).toUpperCase() + string.slice(1);
}

function processSampleEl(sampleEl, prefLangId, prefBuildId) {
function shouldHideCodeEl(codeEl, prefLangId, prefBuildId, prefBuildSubsetId) {
var doesElementMatchLangPref = codeEl.getAttribute("data-lang") === prefLangId;
var doesElementMatchBuildPref = codeEl.getAttribute("data-lang") === prefBuildId;
var doesElementMatchBuildSubsetPref = codeEl.getAttribute("data-lang") === prefBuildSubsetId && MICRONAUT_BUILD_SUBSET_LOOKUP[prefBuildSubsetId] === prefBuildId;
var isElementMatch = doesElementMatchLangPref || doesElementMatchBuildPref || doesElementMatchBuildSubsetPref;
return !isElementMatch
}

function shouldAddSelectedToOptionEl(optionEl, prefLangId, prefBuildId, prefBuildSubsetId) {
var doesElementMatchLangPref = optionEl.getAttribute("data-lang") === prefLangId;
var doesElementMatchBuildPref = optionEl.getAttribute("data-lang") === prefBuildId;
var doesElementMatchBuildSubsetPref = optionEl.getAttribute("data-lang") === prefBuildSubsetId && MICRONAUT_BUILD_SUBSET_LOOKUP[prefBuildSubsetId] === prefBuildId;
var isElementMatch = doesElementMatchLangPref || doesElementMatchBuildPref || doesElementMatchBuildSubsetPref;
return isElementMatch;
}

function processSampleEl(sampleEl, prefLangId, prefBuildId, prefBuildSubsetId) {
var codeEl = sampleEl.querySelector("code[data-lang]");
if (codeEl != null) {
sampleEl.setAttribute("data-lang", codeEl.getAttribute("data-lang"));
if (codeEl.getAttribute("data-lang") !== prefLangId && codeEl.getAttribute("data-lang") !== prefBuildId) {
if (shouldHideCodeEl(codeEl, prefLangId, prefBuildId, prefBuildSubsetId)) {
sampleEl.classList.add("hidden");
} else {
sampleEl.classList.remove("hidden");
Expand All @@ -99,19 +142,20 @@ function postProcessCodeBlocks() {
}
}

function switchSampleLanguage(languageId, buildId) {
function switchSampleLanguage(languageId, buildId, buildSubsetId) {

// First make sure all the code sample sections are created
ensureMultiLanguageSampleSectionsHydrated(languageId, buildId);
ensureMultiLanguageSampleSectionsHydrated(languageId, buildId, buildSubsetId);

[].slice.call(document.querySelectorAll(".multi-language-selector .language-option")).forEach(function (optionEl) {
if (optionEl.getAttribute("data-lang") === languageId || optionEl.getAttribute("data-lang") === buildId) {
if (shouldAddSelectedToOptionEl(optionEl, languageId, buildId, buildSubsetId)) {
optionEl.classList.add("selected");
} else {
optionEl.classList.remove("selected");
}
});

// What project is this used in? The core docs don't use this...
[].slice.call(document.querySelectorAll(".multi-language-text")).forEach(function (el) {
if (!el.classList.contains("lang-" + languageId) && !el.classList.contains("lang-" + buildId)) {
el.classList.add("hidden");
Expand All @@ -121,18 +165,47 @@ function postProcessCodeBlocks() {
});
}

function ensureMultiLanguageSampleSectionsHydrated(languageId, buildId) {
function updatePreferredLanguage(event) {
var optionId = event.target.getAttribute("data-lang");
var isOptionBuild = isBuild(optionId);
var isOptionBuildSubset = isBuildSubset(optionId);
var isOptionLang = isLang(optionId);
if (isOptionBuild) {
if(isOptionBuildSubset) {
window.localStorage.setItem(LOCALSTORAGE_KEY_BUILD_SUBSET, optionId);
window.localStorage.setItem(LOCALSTORAGE_KEY_BUILD, MICRONAUT_BUILD_SUBSET_LOOKUP[optionId]);
} else {
window.localStorage.setItem(LOCALSTORAGE_KEY_BUILD, optionId);
}
}
if (isOptionLang) {
window.localStorage.setItem(LOCALSTORAGE_KEY_LANG, optionId);
}

switchSampleLanguage(
isOptionLang ? optionId : getOrSetDefaultPreferredLanguage(),
isOptionBuild ? optionId : getOrSetDefaultPreferredBuild(),
isOptionBuildSubset ? optionId : getOrSetDefaultPreferredBuildSubset()
);

// scroll to multi-lange selector. Offset the scroll a little bit to focus.
event.target.scrollIntoView();
var offset = 150;
window.scrollBy(0, -offset);
}

function ensureMultiLanguageSampleSectionsHydrated(languageId, buildId, buildSubsetId) {
var multiLanguageSampleElements = [].slice.call(document.querySelectorAll(".multi-language-sample"));
// Array of Arrays, each top-level array representing a single collection of samples
var multiLanguageSets = [];
for (var i = 0; i < multiLanguageSampleElements.length; i++) {
var currentCollection = [multiLanguageSampleElements[i]];
var currentSampleElement = multiLanguageSampleElements[i];
processSampleEl(currentSampleElement, languageId, buildId);
processSampleEl(currentSampleElement, languageId, buildId, buildSubsetId);
while (currentSampleElement.nextElementSibling != null && currentSampleElement.nextElementSibling.classList.contains("multi-language-sample")) {
currentCollection.push(currentSampleElement.nextElementSibling);
currentSampleElement = currentSampleElement.nextElementSibling;
processSampleEl(currentSampleElement, languageId, buildId);
processSampleEl(currentSampleElement, languageId, buildId, buildSubsetId);
i++;
}

Expand Down Expand Up @@ -167,24 +240,7 @@ function postProcessCodeBlocks() {

optionEl.innerText = makeTitleForSnippetSelector(sampleLanguage);

optionEl.addEventListener("click", function updatePreferredLanguage(evt) {
var optionId = optionEl.getAttribute("data-lang");
var isOptionBuild = isBuild(optionId);
var isOptionLang = isLang(optionId);
if (isOptionBuild) {
window.localStorage.setItem(LOCALSTORAGE_KEY_BUILD, optionId);
}
if (isOptionLang) {
window.localStorage.setItem(LOCALSTORAGE_KEY_LANG, optionId);
}

switchSampleLanguage(isOptionLang ? optionId : initPreferredLanguage(), isOptionBuild ? optionId : initPreferredBuild());

// scroll to multi-lange selector. Offset the scroll a little bit to focus.
optionEl.scrollIntoView();
var offset = 150;
window.scrollBy(0, -offset);
});
optionEl.addEventListener("click", updatePreferredLanguage);
multiLanguageSelectorElement.appendChild(optionEl);
});
sampleCollection[0].parentNode.insertBefore(languageSelectorFragment, sampleCollection[0]);
Expand All @@ -211,7 +267,7 @@ function postProcessCodeBlocks() {
});
}

switchSampleLanguage(preferredLanguage, preferredBuild);
switchSampleLanguage(preferredLanguage, preferredBuild, preferredBuildSubset);
}

function createCopyToClipboardElement() {
Expand Down Expand Up @@ -242,5 +298,6 @@ function postProcessCodeCallouts() {
document.addEventListener("DOMContentLoaded", function(event) {
addCopyToClipboardButtons();
postProcessCodeBlocks();
// What project uses these "code callouts"? They don't seem to be in the core docs
postProcessCodeCallouts();
});