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

1533 move dependency and dependencies macros to core #1534

Merged
merged 27 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9e05a85
Adds rawTest macro and tests. Fix #1524.
AndreaLaGrotteria Oct 23, 2024
356ae11
Fixes spaces.
AndreaLaGrotteria Oct 23, 2024
cdff572
Fixes spaces.
AndreaLaGrotteria Oct 23, 2024
e72201d
Adds resource macro and tests. Fix #1525.
AndreaLaGrotteria Oct 23, 2024
e0ed37e
Apply requested changes.
AndreaLaGrotteria Oct 23, 2024
0633bde
Apply requested changes.
AndreaLaGrotteria Oct 23, 2024
c830ca7
Apply requested changes.
AndreaLaGrotteria Oct 23, 2024
f72d259
Apply requested changes.
AndreaLaGrotteria Oct 23, 2024
857651f
Fix for header lines offset.
AndreaLaGrotteria Oct 23, 2024
0a6b4c1
Implements suggestions.
AndreaLaGrotteria Oct 23, 2024
e972324
fix tests
sdelamo Oct 24, 2024
6a8e709
add @Nullability annotation
sdelamo Oct 24, 2024
adc0585
license lines is 0 don’t add lines attributes
sdelamo Oct 24, 2024
58fe4fb
Adds testResource macro to core. Fix #1526.
AndreaLaGrotteria Oct 24, 2024
d8c08e6
Adds supports for multi-line inputs. Makes code more modular.
AndreaLaGrotteria Oct 24, 2024
c645194
Adds supports for multi-line inputs. Makes code more modular.
AndreaLaGrotteria Oct 24, 2024
eb82a83
Adds supports for multi-line inputs. Makes code more modular.
AndreaLaGrotteria Oct 24, 2024
b295ee6
Improves path display.
AndreaLaGrotteria Oct 24, 2024
e9d59d2
Adds annotations.
AndreaLaGrotteria Oct 24, 2024
c0f3f79
Adds annotations.
AndreaLaGrotteria Oct 24, 2024
9d500ea
Adds resourceMacro and tests. Fix #1525.
AndreaLaGrotteria Oct 24, 2024
2329684
Merge branch '1525-move-resource-macro-to-core' into 1526-move-testre…
AndreaLaGrotteria Oct 24, 2024
60426d9
Adapts code to merged changes.
AndreaLaGrotteria Oct 24, 2024
df22d23
Fixes code warnings.
AndreaLaGrotteria Oct 24, 2024
0d725e1
Merge branch 'master' into 1526-move-testresource-macro-to-core
AndreaLaGrotteria Oct 25, 2024
f2a0a68
Bug fix.
AndreaLaGrotteria Oct 25, 2024
349c43d
Merge branch '1526-move-testresource-macro-to-core' into 1533-move-de…
AndreaLaGrotteria Oct 25, 2024
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 @@ -7,6 +7,7 @@ import io.micronaut.context.ApplicationContext
import io.micronaut.core.annotation.NonNull
import io.micronaut.core.util.StringUtils
import io.micronaut.guides.core.App
import io.micronaut.guides.core.DependencyLines
import io.micronaut.guides.core.Guide
import io.micronaut.guides.core.GuideUtils
import io.micronaut.guides.core.GuidesOption
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micronaut.guides;
package io.micronaut.guides.core;

import io.micronaut.core.util.StringUtils;
import io.micronaut.starter.options.BuildTool;
Expand Down Expand Up @@ -28,22 +28,13 @@ private static String toMavenScope(Map<String, String> attributes) {
if (s == null) {
return null;
}
switch (s) {
case "api":
case "implementation":
case "annotationProcessor":
return "compile";
case "testCompile":
case "testRuntimeOnly":
case "testImplementation":
return "test";
case "compileOnly":
return "provided";
case "runtimeOnly":
return "runtime";
default:
return s;
}
return switch (s) {
case "api", "implementation", "annotationProcessor" -> "compile";
case "testCompile", "testRuntimeOnly", "testImplementation" -> "test";
case "compileOnly" -> "provided";
case "runtimeOnly" -> "runtime";
default -> s;
};
}

private static String toGradleScope(Map<String, String> attributes, Language language) {
Expand Down Expand Up @@ -170,10 +161,10 @@ private static String extractCallout(Map<String, String> attributes) {
}

private static String getMavenAnnotationScopeXMLPath(Language language) {
switch (language) {
case JAVA: return "path";
case KOTLIN: return "annotationProcessorPath";
default: return ""; // not used for Groovy
}
return switch (language) {
case JAVA -> "path";
case KOTLIN -> "annotationProcessorPath";
default -> ""; // not used for Groovy
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.micronaut.guides.core;

import jakarta.inject.Singleton;
import java.util.List;

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

@Singleton
public class DependencyMacroSubstitution implements MacroSubstitution{
@Override
public String substitute(String str, String slug, GuidesOption option) {
for(String block : findMacroGroups(str, "dependencies")){
List<String> lines = DependencyLines.asciidoc(block.replace(":dependencies:","").strip().lines().toList(), option.getBuildTool(), option.getLanguage());
str = str.replace(block,String.join("\n", lines));
}
for(String line : findMacroLines(str, "dependency")){
List<String> lines = DependencyLines.asciidoc(line, option.getBuildTool(), option.getLanguage());
str = str.replace(line,String.join("\n", lines));
}
return str;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.micronaut.guides.core;

import java.util.List;

public interface GuidesConfiguration {
String getHomePageUrl();
String getTitle();
String getLicensePath();
String getPackageName();
List<String> getFilesWithHeader();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.micronaut.context.annotation.ConfigurationProperties;

import java.util.List;

@ConfigurationProperties(GuidesConfigurationProperties.PREFIX)
public class GuidesConfigurationProperties implements GuidesConfiguration {
public static final String PREFIX = "guides";
Expand All @@ -12,6 +14,7 @@ public class GuidesConfigurationProperties implements GuidesConfiguration {
private static final String DEFAULT_PACKAGE_NAME = "example.micronaut";
private String licensePath = DEFAULT_LICENSEHEADER;
private String packageName = DEFAULT_PACKAGE_NAME;
private List<String> sourceFilesExtensions = List.of("java", "kotlin", "groovy");

@Override
public String getPackageName() {
Expand Down Expand Up @@ -48,4 +51,13 @@ public String getTitle() {
public void setTitle(String title) {
this.title = title;
}

@Override
public List<String> getFilesWithHeader() {
return sourceFilesExtensions;
}

public void setFilesWithHeader(List<String> sourceFilesExtensions) {
this.sourceFilesExtensions = sourceFilesExtensions;
}
}
145 changes: 96 additions & 49 deletions buildSrc/src/main/java/io/micronaut/guides/core/MacroUtils.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
package io.micronaut.guides.core;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.util.StringUtils;

import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static io.micronaut.starter.api.TestFramework.SPOCK;

public final class MacroUtils {
private MacroUtils() {
}

@NonNull
public static String getSourceDir(@NonNull String slug, @NonNull GuidesOption option) {
return slug + "-" + option.getBuildTool() + "-" + option.getLanguage();
}

/**
* Adds include directives to the list of lines for the given source path and tags.
*
* @param option The {@link GuidesOption} containing the build tool and language.
* @param slug The slug representing the guide, which will be used as root folder.
* @param sourcePath The path to the source file from the guide's project root.
* @param licenseLoader The {@link LicenseLoader} to calculate the line offset. If null no line offset is added.
* @param extension The file extension of the source file.
* @param indent The indent value for the include directive. If empty no indent is added.
* @param tags The list of tags for the include directive.
* @return A list of lines with include directives.
*/
@NonNull
public static List<String> addIncludes(@NonNull GuidesOption option,
@NonNull LicenseLoader licenseLoader,
@NonNull GuidesConfiguration configuration,
@NonNull String slug,
@NonNull String sourcePath,
@NonNull LicenseLoader licenseLoader,
String indent,
@NonNull String extension,
@Nullable String indent,
@NonNull List<String> tags) {
String sourceDir = getSourceDir(slug, option);
List<String> lines = new ArrayList<>();
lines.add("[source," + option.getLanguage().toString() + "]");
String normalizedSourcePath = Paths.get(sourcePath).normalize().toString();
lines.add("." + normalizedSourcePath);
lines.add("[source," + extension + "]");
Path path = Path.of(slug, sourceDir, sourcePath);
lines.add("." + path.normalize().toString().replace(slug+"/"+sourceDir+"/", ""));
lines.add("----");

if (!tags.isEmpty()) {
Expand All @@ -40,20 +48,49 @@ public static List<String> addIncludes(@NonNull GuidesOption option,
if (StringUtils.isNotEmpty(indent)) {
attrs += "," + indent;
}
lines.add("include::{sourceDir}/" + slug + "/" + sourceDir + "/" + sourcePath + "[" + attrs + "]\n");
lines.add("include::{sourceDir}/" + path + "[" + attrs + "]\n");
}
} else {
List<String> attributes = new ArrayList<>();
attributes.add("lines=" + licenseLoader.getNumberOfLines() + "..-1");
if (fileContainsHeader(extension, configuration) && licenseLoader.getNumberOfLines() > 0) {
attributes.add("lines=" + licenseLoader.getNumberOfLines() + "..-1");
}
if (StringUtils.isNotEmpty(indent)) {
attributes.add(indent);
}
lines.add("include::{sourceDir}/" + slug + "/"+sourceDir+"/" + sourcePath + "[" + String.join(";", attributes) + "]");
lines.add("include::{sourceDir}/" + path + "[" + String.join(";", attributes) + "]");
}
lines.add("----\n");
lines.add("----");
return lines;
}

/**
* Adds include directives to the list of lines for the given source path and tags.
*
* @param option The {@link GuidesOption} containing the build tool and language.
* @param slug The slug representing the guide, which will be used as root folder.
* @param sourcePath The path to the source file from the guide's project root.
* @param licenseLoader The {@link LicenseLoader} to calculate the line offset.
* @param indent The indent value for the include directive. If empty no indent is added.
* @param tags The list of tags for the include directive.
* @return A list of lines with include directives.
*/
@NonNull
public static List<String> addIncludes(@NonNull GuidesOption option,
@NonNull LicenseLoader licenseLoader,
@NonNull GuidesConfiguration configuration,
@NonNull String slug,
@NonNull String sourcePath,
@NonNull String indent,
@NonNull List<String> tags) {
return addIncludes(option, licenseLoader, configuration, slug, sourcePath, option.getLanguage().toString(), indent, tags);
}

@NonNull
static String getSourceDir(@NonNull String slug, @NonNull GuidesOption option) {
return slug + "-" + option.getBuildTool() + "-" + option.getLanguage();
}

@NonNull
public static String extractName(@NonNull String line, @NonNull String macro) {
return line.substring(macro.length() + 1, line.indexOf('['));
Expand All @@ -68,49 +105,28 @@ public static String extractAppName(@NonNull String line) {
public static List<String> extractTags(@NonNull String line) {
String attributeValue = extractFromParametersLine(line, "tags");
if (StringUtils.isNotEmpty(attributeValue)) {
return Arrays.asList(attributeValue.split("\\|"));
return Arrays.stream(attributeValue.split("\\|")).map(it -> "tag=" + it).toList();
}

return extractTagName(line).isEmpty() ? Collections.emptyList() : Collections.singletonList(extractTagName(line));
attributeValue = extractTagName(line);
return attributeValue.isEmpty() ? Collections.emptyList() : Collections.singletonList("tag="+attributeValue);
}

@NonNull
public static String mainPath(@NonNull GuidesConfiguration guidesConfiguration,
@NonNull String appName,
@NonNull String fileName,
GuidesOption option) {
return pathByFolder(guidesConfiguration, appName, fileName, "main", option);
}

@NonNull
static String testPath(@NonNull GuidesConfiguration guidesConfiguration,
@NonNull String appName,
@NonNull String name,
GuidesOption option) {
String fileName = name;

if (name.endsWith("Test")) {
fileName = name.substring(0, name.indexOf("Test"));
fileName += option.getTestFramework() == SPOCK ? "Spec" : "Test";
}

return pathByFolder(guidesConfiguration, appName, fileName, "test", option);
}

@NonNull
private static String pathByFolder(@NonNull GuidesConfiguration guidesConfiguration,
@NonNull String appName,
@NonNull String fileName,
String folder,
GuidesOption option) {
String module = !appName.isEmpty() ? appName + "/" : "";
return module+"src/"+folder+"/"+option.getLanguage().toString()+"/" + guidesConfiguration.getPackageName().replace(".", "/") + "/"+fileName+"."+option.getLanguage().getExtension();
static String pathByFolder(@NonNull GuidesConfiguration guidesConfiguration,
@NonNull String appName,
@NonNull String fileName,
@NonNull String folder,
@NonNull GuidesOption option) {
String module = appName.isEmpty() ? "" : appName + "/";
return module + "src/" + folder + "/" + option.getLanguage().toString() + "/" + guidesConfiguration.getPackageName().replace(".", "/") + "/" +fileName + "." + option.getLanguage().getExtension();
}

@NonNull
public static String extractIndent(@NonNull String line) {
String indentValue = extractFromParametersLine(line, "indent");
return !indentValue.isEmpty() ? "indent="+indentValue : "";
return !indentValue.isEmpty() ? "indent=" + indentValue : "";
}

@NonNull
Expand All @@ -129,4 +145,35 @@ static String extractFromParametersLine(@NonNull String line, @NonNull String at
.findFirst()
.orElse("");
}

static boolean fileContainsHeader(@NonNull String extension, @NonNull GuidesConfiguration configuration) {
return configuration.getFilesWithHeader().contains(extension);
}

static List<String> findMacroLines(@NonNull String str, @NonNull String macro) {
return str.lines()
.filter(line -> line.startsWith(macro+":"))
.toList();
}

static List<String> findMacroGroups(@NonNull String str, @NonNull String macro) {
List<String> matches = new ArrayList<>();
String pattern = ":"+macro+":";
int startIndex = 0;

while (true) {
startIndex = str.indexOf(pattern, startIndex);
if (startIndex == -1) break;

int endIndex = str.indexOf(pattern, startIndex + pattern.length());
if (endIndex == -1) break;

String match = str.substring(startIndex, endIndex + pattern.length());
matches.add(match);

startIndex = endIndex + pattern.length();
}

return matches;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.micronaut.guides.core;

import io.micronaut.core.annotation.NonNull;
import jakarta.inject.Singleton;
import java.util.List;

import static io.micronaut.guides.core.MacroUtils.*;
import static io.micronaut.guides.core.MacroUtils.addIncludes;

@Singleton
public class RawTestMacroSubstitution implements MacroSubstitution{

private final GuidesConfiguration guidesConfiguration;
private final LicenseLoader licenseLoader;

public RawTestMacroSubstitution(GuidesConfiguration guidesConfiguration, LicenseLoader licenseLoader) {
this.guidesConfiguration = guidesConfiguration;
this.licenseLoader = licenseLoader;
}

@Override
public String substitute(String str, String slug, GuidesOption option) {
for(String line : findMacroLines(str, "rawTest")) {

String name = extractName(line, "rawTest");
String appName = extractAppName(line);
List<String> tags = extractTags(line);
String indent = extractIndent(line);
String sourcePath = rawTestPath(guidesConfiguration, appName, name, option);

List<String> lines = addIncludes(option, licenseLoader, guidesConfiguration, slug, sourcePath, option.getTestFramework().toTestFramework().getDefaultLanguage().getExtension(), indent, tags);

str = str.replace(line,String.join("\n", lines));
}
return str;
}

@NonNull
private static String rawTestPath(@NonNull GuidesConfiguration guidesConfiguration,
@NonNull String appName,
@NonNull String name,
@NonNull GuidesOption option) {
String module = appName.isEmpty() ? "" : appName + "/";
String fileExtension = option.getTestFramework().toTestFramework().getDefaultLanguage().getExtension();
String langTestFolder = option.getTestFramework().toTestFramework().getDefaultLanguage().getTestSrcDir();
return module+langTestFolder + "/" + guidesConfiguration.getPackageName().replace(".", "/") + "/" + name + "." + fileExtension;
}
}
Loading
Loading