Skip to content

Commit

Permalink
fix: don't require appmap.configFile
Browse files Browse the repository at this point in the history
Only pass appmap.config.file to the agent if appmap.configFile is set in
build.gradle. Otherwise, let the agent take care of it (e.g. by
generating a default config if appmap.yml is missing).
  • Loading branch information
apotterri authored and dustinbyrne committed Sep 12, 2023
1 parent fa11246 commit eaa87c0
Show file tree
Hide file tree
Showing 22 changed files with 633 additions and 115 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea/
build
.gradle
/bin/
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java temurin-11.0.20+8
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ cache:
- '$HOME/.gradle/wrapper/'

script:
- ./gradlew -is check
- test/bin/smoketest || travis_terminate 1
- ./gradlew -is check

before_deploy:
- |
Expand Down
11 changes: 9 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def versionLikeRegexp = /^\d+\.\d+.*/
def travisVersionOK = travisVersion && (travisVersion ==~ versionLikeRegexp)

paramVersion = paramVersion ?: (travisVersionOK ? travisVersion : defVersion)
if (findProperty('snapshot') != null) {
paramVersion += '-SNAPSHOT'
}

println "version:$paramVersion"

version = paramVersion
group = paramGroupId

Expand All @@ -49,8 +55,9 @@ repositories {
dependencies {
gradleApi()
localGroovy()
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.2'
testImplementation(enforcedPlatform("org.junit:junit-bom:5.10.0")) // JUnit 5 BOM
testImplementation("org.junit.jupiter:junit-jupiter")

implementation 'com.appland:appmap-agent:[1.3, 2.0)'
implementation 'commons-lang:commons-lang:2.6'
implementation 'com.google.guava:guava:30.1.1-jre'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import static java.lang.String.format;

import com.google.common.base.Joiner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.gradle.api.GradleException;
import org.gradle.api.Named;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.Internal;
import org.gradle.process.CommandLineArgumentProvider;
import org.gradle.util.RelativePathUtil;

import com.google.common.base.Joiner;

/**
* This class is the actual responsible of building the JVM args to run the AppMap Agent.
*/
Expand Down Expand Up @@ -51,6 +54,8 @@ public String getName() {
*/
@Internal
public List<String> getAsJvmArg() {
RegularFileProperty configFile = appmap.getConfigFile();

if (!appmap.isConfigFileValid()) {
appmap.setSkip(true);
throw new GradleException("Configuration file must exist and be readable: "
Expand All @@ -69,7 +74,7 @@ public List<String> getAsJvmArg() {
List<String> argumentLn = new ArrayList<>();
argumentLn.add(javaAgentArg);

if ( appmap.getConfigFile().isPresent() ) {
if (configFile != null && configFile.isPresent()) {
argumentLn.add("-Dappmap.config.file=" + appmap.getConfigFilePath());
}
argumentLn.add("-Dappmap.output.directory=" + appmap.getOutputDirectoryPath());
Expand Down
76 changes: 36 additions & 40 deletions src/main/java/com/appland/appmap/gradle/AppMapPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ public void apply(Project project) {
config.setTransitive(true);
config.setDescription("AppMap agent to generate app map data.");
config.defaultDependencies(dependencies ->
dependencies.add(
project.getDependencies().create("com.appland:appmap-agent:" + DEFAULT_AGENT_VERSION)
)
);
dependencies.add(
project.getDependencies().create(
"com.appland:appmap-agent:" + findProperty("appmapAgentVersion", DEFAULT_AGENT_VERSION))));
AppMapPluginExtension extension = project.getExtensions()
.create(PLUGIN_EXTENSION_NAME, AppMapPluginExtension.class, project, config);
//extension.setAgentVersion(DEFAULT_AGENT_VERSION);*/
Expand All @@ -54,44 +53,41 @@ private void addAppMapGradleTasks(AppMapPluginExtension extension) {
* @param extension holds the config parameters for the plugin.
*/
private void addAppMapTasks(AppMapPluginExtension extension) {
project.getTasks().register(
"appmap",
AppMapTask.class,
prepareAgentTask -> {
prepareAgentTask.doFirst(
new ValidateConfigAction(extension.getConfigFile().getAsFile())
);
prepareAgentTask.doLast(new LoadAppMapAgentAction(project, extension));
prepareAgentTask.setGroup(LifecycleBasePlugin.BUILD_GROUP);
prepareAgentTask.setDescription(
String.format("Injects AppMap Agent JVM settings to the 'test' task")
);
});
project.getTasks().register(
"appmap",
AppMapTask.class,
prepareAgentTask -> {
prepareAgentTask.doFirst(
new ValidateConfigAction(extension.getConfigFile()));
prepareAgentTask.doLast(new LoadAppMapAgentAction(project, extension));
prepareAgentTask.setGroup(LifecycleBasePlugin.BUILD_GROUP);
prepareAgentTask.setDescription(
String.format("Injects AppMap Agent JVM settings to the 'test' task"));
});

project.getTasks().register(
"appmap-validate-config",
validateConfigTask -> {
validateConfigTask.doFirst(
new ValidateConfigAction(extension.getConfigFile().getAsFile())
);
validateConfigTask.setGroup(LifecycleBasePlugin.BUILD_GROUP);
validateConfigTask.setDescription(
String.format("Validates the AppMap Agent configuration")
);
}
);
project.getTasks().register(
"appmap-validate-config",
validateConfigTask -> {
validateConfigTask.doFirst(
new ValidateConfigAction(extension.getConfigFile()));
validateConfigTask.setGroup(LifecycleBasePlugin.BUILD_GROUP);
validateConfigTask.setDescription(
String.format("Validates the AppMap Agent configuration"));
});

project.getTasks().register(
"appmap-print-jar-path",
agentJarPathTask -> {
agentJarPathTask.doFirst(
new PrintJarPathAction(extension)
);
agentJarPathTask.setGroup(LifecycleBasePlugin.BUILD_GROUP);
agentJarPathTask.setDescription(
String.format("Prints the file path of the AppMap Agent JAR")
);
}
);
"appmap-print-jar-path",
agentJarPathTask -> {
agentJarPathTask.doFirst(
new PrintJarPathAction(extension));
agentJarPathTask.setGroup(LifecycleBasePlugin.BUILD_GROUP);
agentJarPathTask.setDescription(
String.format("Prints the file path of the AppMap Agent JAR"));
});
}

String findProperty(String name, String defaultValue) {
String value = (String) project.findProperty(name);
return value != null ? value : defaultValue;
}
}
29 changes: 21 additions & 8 deletions src/main/java/com/appland/appmap/gradle/AppMapPluginExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.file.Directory;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.file.RegularFile;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.Input;

/**
Expand All @@ -29,21 +33,24 @@ public class AppMapPluginExtension {
private int eventValueSize = 1024;

/**
* Constructor method, receives the project, configuration and fork options, read and provide the
* Constructor method, receives the project, configuration and fork options,
* read and provide the
* rest of the configuration to the AppMapPlugin class.
*
* @param project Actual project object representation.
* @param project Actual project object representation.
* @param agentConf Holder of the project configuration.
*/
public AppMapPluginExtension(Project project, Configuration agentConf) {
this.project = project;
this.agentConf = agentConf;
this.configFile = project.getObjects().fileProperty()
.value(project.getLayout().getProjectDirectory().file("appmap.yml"));
this.outputDirectory = project.getObjects().directoryProperty()
.value(project.getLayout().getBuildDirectory().dir(DEFAULT_OUTPUT_DIRECTORY).get());
this.debugFile = project.getObjects().fileProperty()
.value(project.getLayout().getBuildDirectory().file("appmap/agent.log").get());
ProjectLayout projectLayout = project.getLayout();
Directory projectDirectory = projectLayout.getProjectDirectory();
ObjectFactory projectObjects = project.getObjects();
this.configFile = projectObjects.fileProperty();
this.debugFile = projectObjects.fileProperty()
.value(projectLayout.getBuildDirectory().file("appmap/agent.log").get());
logger.info("AppMap Plugin Initialized.");
}

Expand Down Expand Up @@ -117,10 +124,16 @@ public boolean isSkip() {
}

public boolean isConfigFileValid() {
return AppMapPluginExtension.isConfigFileValid(configFile.get().getAsFile());
return AppMapPluginExtension.isConfigFileValid(configFile.getOrNull());
}

public static boolean isConfigFileValid(File configFile) {
public static boolean isConfigFileValid(RegularFile configFileProperty) {
if (configFileProperty == null) {
// If there's no config file specified, let the agent deal with it.
return true;
}

File configFile = configFileProperty.getAsFile();
return configFile.exists() && Files.isReadable(configFile.toPath());
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package com.appland.appmap.gradle;

import java.io.File;

import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Task;
import org.gradle.api.file.RegularFile;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.Provider;

/**
* Action to validates the configuration for appmap exists and is readable.
*/
public class ValidateConfigAction implements Action<Task> {

private final Provider<File> configFile;
private final Provider<RegularFile> configFile;

public ValidateConfigAction(Provider<File> configFile) {
this.configFile = configFile;
public ValidateConfigAction(RegularFileProperty regularFileProperty) {
this.configFile = regularFileProperty;
}

@Override
public void execute(Task task) {
if (!isConfigFileValid()) {
throw new GradleException(
"Config file " + configFile.get().getPath() + " not found or not readable."
"Config file " + configFile.get().getAsFile().getPath() + " not found or not readable."
);
}
}

protected boolean isConfigFileValid() {
return AppMapPluginExtension.isConfigFileValid(configFile.get());
return AppMapPluginExtension.isConfigFileValid(configFile.getOrNull());
}
}
Loading

0 comments on commit eaa87c0

Please sign in to comment.