Skip to content

Commit

Permalink
Default exclude src/main and src/test folders.
Browse files Browse the repository at this point in the history
  • Loading branch information
renelink committed Sep 4, 2024
1 parent 187cca9 commit 5e3dbd9
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 64 deletions.
21 changes: 15 additions & 6 deletions multi-module-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ plugins {
### Exclude Paths

Let's assume you have a project structure like this.

```
my-app/
├─ modules/
Expand All @@ -42,27 +43,31 @@ my-app/
```

You can then exclude specific paths by configuring the `MultiModuleConfig`. Here are some examples:

```kotlin
// settings.gradle.kts
import com.link_intersystems.gradle.plugins.multimodule.MultiModuleConfig

// Exclude only modules/moduleA
configure<MultiModuleConfig> {
excludedPaths = listOf("modules/moduleA")
excludedPaths = listOf("modules/moduleA")
}

// Exclude all modules ending with A using a glob pattern (modules/moduleA, otherModules/moduleA)
configure<MultiModuleConfig> {
excludedPaths = listOf("**/*A")
excludedPaths = listOf("**/*A")
}

// Exclude modules/moduleA and modules/moduleB
configure<MultiModuleConfig> {
excludedPaths = listOf("regex:modules/module?")
excludedPaths = listOf("regex:modules/module?")
}
```
Under the hood the multi-module plugin uses Java's PathMatcher, so you can
configure whatever a [PathMatcher](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/FileSystem.html#getPathMatcher(java.lang.String)) can be configured with.

Under the hood the multi-module plugin uses Java's PathMatcher, so you can
configure whatever
a [PathMatcher](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/FileSystem.html#getPathMatcher(java.lang.String))
can be configured with.

If you do not prefix the exclude path with `glob:` or `regex:`, the plugin assumes
that the path is a glob pattern.
Expand All @@ -72,12 +77,16 @@ that the path is a glob pattern.
Per default the plugin excludes `buildSrc` and any includeBuild that is configured within
a pluginManagement section, since these locations are usually used for convention plugins.

Also `**/src/main/**` and `**/src/test/**` are excluded, because Gradle plugin
developers might put Gradle build scripts in these locations, e.g. for testing.

However, you can turn off the default excludes

```kotlin
// settings.gradle.kts
import com.link_intersystems.gradle.plugins.multimodule.SettingsMultiModuleConfig

configure<MultiModuleConfig> {
isOmitDefaultExcludes = true
isOmitDefaultExcludes = true
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

class DefaultExcludePaths implements Predicate<Path> {
import static java.util.Arrays.asList;

class DefaultExcludePaths extends PatternPathExclude {

private final List<String> excludePaths;

Expand All @@ -16,10 +18,15 @@ public DefaultExcludePaths(Settings settings) {
excludePaths.add("buildSrc");
IncludeBuildsSource includeBuildsSource = new PluginManagementIncludeBuildSource(settings);
excludePaths.addAll(includeBuildsSource.getIncludedBuilds());

setExcludePaths(asList("**/src/test/**", "**/src/main/**"));
}

@Override
public boolean test(Path path) {
if (super.test(path)) {
return true;
}
return excludePaths.contains(path.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ private Predicate<Path> getExcludePaths(Predicate<Path> excludePathsPredicate, C

List<String> excludePaths = multiModuleConfig.getExcludedPaths();
if (!excludePaths.isEmpty()) {
PropertiesExcludePathPredicate propertiesExcludePathPredicate = new PropertiesExcludePathPredicate(excludePaths);
propertiesExcludePathPredicate.getAppliedPaths().forEach(path -> {
PatternPathExclude patternPathExclude = new PatternPathExclude(excludePaths);
patternPathExclude.getAppliedPaths().forEach(path -> {
logger.debug("com.link-intersystems.gradle.multi-module exclude path '{}' added.", path);
});
propertiesExcludePathPredicate.getIgnoredPaths().forEach(path -> {
patternPathExclude.getIgnoredPaths().forEach(path -> {
logger.warn("com.link-intersystems.gradle.multi-module.exclude-paths contains an invalid path that will be ignored: '{}'", path);
});

excludePathsPredicate = excludePathsPredicate.or(propertiesExcludePathPredicate);
excludePathsPredicate = excludePathsPredicate.or(patternPathExclude);
}

return excludePathsPredicate;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.link_intersystems.gradle.plugins.multimodule;

import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

class PatternPathExclude implements Predicate<Path> {

private final List<String> appliedPaths = new ArrayList<>();
private final List<String> ignoredPaths = new ArrayList<>();
private List<String> excludePaths;
private List<PathMatcher> excludedPaths;

public PatternPathExclude() {
excludePaths = new ArrayList<>();
}

public PatternPathExclude(List<String> excludePaths) {
this.excludePaths = excludePaths;
}


public void setExcludePaths(List<String> excludePaths) {
this.excludePaths = new ArrayList<>(excludePaths);
excludedPaths = null;
}

private void ensureInitialized() {
if (excludedPaths == null) {
excludedPaths = new ArrayList<>();
FileSystem fileSystem = FileSystems.getDefault();
for (String excludePath : excludePaths) {
try {
if (!excludePath.startsWith("regex:") && !excludePath.startsWith("glob:")) {
excludePath = "glob:" + excludePath;
}
PathMatcher pathMatcher = fileSystem.getPathMatcher(excludePath);
excludedPaths.add(pathMatcher);
appliedPaths.add(excludePath);
} catch (Exception e) {
ignoredPaths.add(excludePath);
}
}
}
}

public List<String> getAppliedPaths() {
ensureInitialized();
return appliedPaths;
}

public List<String> getIgnoredPaths() {
ensureInitialized();
return ignoredPaths;
}

@Override
public boolean test(Path path) {
ensureInitialized();
for (PathMatcher excludedPath : excludedPaths) {
if (excludedPath.matches(path)) {
return true;
}
}
return false;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.link_intersystems.gradle.api.ExtensionContainerMocking;
import com.link_intersystems.gradle.api.ProviderFactoryMocking;
import com.link_intersystems.gradle.project.builder.GradleProjectBuilder;
import com.link_intersystems.gradle.project.builder.GradleSubprojectBuilder;
import org.gradle.api.initialization.Settings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -15,7 +16,7 @@

import static org.mockito.Mockito.*;

class SettingsMultiModuleConfigPluginTest {
class MultiModulePluginComponentTest {

private MultiModulePlugin multiModulePlugin;
private Settings settings;
Expand Down Expand Up @@ -51,7 +52,8 @@ private void applyMultiModulePlugin() {
void apply() throws IOException {
projectBuilder.createCompositeBuild("buildSrc");
projectBuilder.createCompositeBuild("modules/moduleA").createSubproject("subA");
projectBuilder.createSubproject("modules/moduleB");
GradleSubprojectBuilder moduleB = projectBuilder.createSubproject("modules/moduleB");
moduleB.file("src/test/resources/build.gradle.kts");
projectBuilder.createSubproject("modules/moduleB/moduleC");
projectBuilder.createCompositeBuild("modules/.hiddenModuleA");

Expand Down Expand Up @@ -108,4 +110,28 @@ void dryRun() throws IOException {
verify(settings, never()).include(":modules:moduleB");
verify(settings, never()).include(":modules:moduleB:moduleC");
}

@Test
void defaultExcludeTestResources() throws IOException {
projectBuilder.createSubproject("modules/moduleA");
projectBuilder.createSubproject("modules/moduleA/src/test/resources/test");

applyMultiModulePlugin();

verify(settings, never()).includeBuild("");
verify(settings, never()).include(":modules:moduleA:src:test:resources:test");
verify(settings).include(":modules:moduleA");
}

@Test
void defaultExcludeMainResources() throws IOException {
projectBuilder.createSubproject("modules/moduleA");
projectBuilder.createSubproject("modules/moduleA/src/main/resources/test");

applyMultiModulePlugin();

verify(settings, never()).includeBuild("");
verify(settings, never()).include(":modules:moduleA:src:main:resources:test");
verify(settings).include(":modules:moduleA");
}
}

0 comments on commit 5e3dbd9

Please sign in to comment.