-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add list-files task to stager-maven-plugin
- Add a variant of FileUtils.walk that can follow symlinks (with FileVisitOption) - Fix StagingElementFactory/ConfigReader to preserve order
1 parent
db58709
commit 3916dea
Showing
21 changed files
with
701 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
common/common/src/main/java/io/helidon/build/common/Patterns.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2023, 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.build.common; | ||
|
||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* {@link Pattern} utility. | ||
*/ | ||
public final class Patterns { | ||
|
||
private static final Pattern NAMED_GROUP_PATTERN = Pattern.compile("\\(\\?<([^!=].*?)>"); | ||
|
||
private Patterns() { | ||
// cannot be instanciated | ||
} | ||
|
||
/** | ||
* Get the group names defined in a regular expression. | ||
* | ||
* @param regex regular expression | ||
* @return group names | ||
*/ | ||
public static Set<String> groupNames(String regex) { | ||
return NAMED_GROUP_PATTERN.matcher(regex) | ||
.results() | ||
.map(r -> r.group(1)) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
maven-plugins/stager-maven-plugin/src/main/java/io/helidon/build/maven/stager/Exclude.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2023, 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.build.maven.stager; | ||
|
||
/** | ||
* Exclude. | ||
*/ | ||
record Exclude(String value) implements StagingElement { | ||
|
||
static final String ELEMENT_NAME = "exclude"; | ||
|
||
@Override | ||
public String elementName() { | ||
return ELEMENT_NAME; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
maven-plugins/stager-maven-plugin/src/main/java/io/helidon/build/maven/stager/Include.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2023, 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.build.maven.stager; | ||
|
||
/** | ||
* Include. | ||
*/ | ||
record Include(String value) implements StagingElement { | ||
|
||
static final String ELEMENT_NAME = "include"; | ||
|
||
@Override | ||
public String elementName() { | ||
return ELEMENT_NAME; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...lugins/stager-maven-plugin/src/main/java/io/helidon/build/maven/stager/ListFilesTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright (c) 2023, 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.build.maven.stager; | ||
|
||
import java.nio.file.FileVisitOption; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.BiFunction; | ||
|
||
import io.helidon.build.common.Lists; | ||
import io.helidon.build.common.SourcePath; | ||
|
||
import static io.helidon.build.common.FileUtils.walk; | ||
import static io.helidon.build.common.Strings.normalizePath; | ||
|
||
/** | ||
* List files in a directory. | ||
*/ | ||
final class ListFilesTask extends StagingTask implements TextAction { | ||
|
||
private static final Set<FileVisitOption> FILE_VISIT_OPTIONS = Set.of(FileVisitOption.FOLLOW_LINKS); | ||
|
||
static final String ELEMENT_NAME = "list-files"; | ||
|
||
private final List<String> includes; | ||
private final List<String> excludes; | ||
private final List<Substitution> substitutions; | ||
private final List<BiFunction<String, Map<String, String>, String>> chain; | ||
private final String dirName; | ||
private final StringBuilder buf = new StringBuilder(); | ||
|
||
ListFilesTask(ActionIterators iterators, | ||
List<Include> includes, | ||
List<Exclude> excludes, | ||
List<Substitution> substitutions, | ||
Map<String, String> attrs) { | ||
|
||
super(ELEMENT_NAME, null, iterators, attrs); | ||
this.includes = Lists.map(includes, Include::value); | ||
this.excludes = Lists.map(excludes, Exclude::value); | ||
this.substitutions = substitutions; | ||
this.chain = Lists.map(substitutions, Substitution::function); | ||
this.dirName = attrs.getOrDefault("dir", "."); | ||
} | ||
|
||
@Override | ||
public String text() { | ||
return buf.toString(); | ||
} | ||
|
||
List<String> includes() { | ||
return includes; | ||
} | ||
|
||
List<String> excludes() { | ||
return excludes; | ||
} | ||
|
||
List<Substitution> substitutions() { | ||
return substitutions; | ||
} | ||
|
||
@Override | ||
protected void doExecute(StagingContext ctx, Path dir, Map<String, String> vars) { | ||
Path resolved = dir.resolve(dirName); | ||
List<Path> files = walk(resolved, FILE_VISIT_OPTIONS, this::filter); | ||
for (Path file : files) { | ||
String entry = normalizePath(dir.relativize(file)); | ||
for (var function : chain) { | ||
entry = function.apply(entry, vars); | ||
} | ||
buf.append(entry).append("\n"); | ||
} | ||
} | ||
|
||
private boolean filter(Path p, BasicFileAttributes attrs) { | ||
return attrs.isDirectory() || new SourcePath(p).matches(includes, excludes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...gins/stager-maven-plugin/src/main/java/io/helidon/build/maven/stager/StagingElements.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 2022, 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.build.maven.stager; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Container of {@link StagingElement}. | ||
*/ | ||
record StagingElements(String elementName, List<StagingElement> nested) implements StagingElement { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...plugins/stager-maven-plugin/src/main/java/io/helidon/build/maven/stager/Substitution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2023, 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.build.maven.stager; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.BiFunction; | ||
|
||
import io.helidon.build.common.Patterns; | ||
import io.helidon.build.common.Strings; | ||
|
||
/** | ||
* Substitution. | ||
*/ | ||
final class Substitution implements StagingElement { | ||
|
||
static final String ELEMENT_NAME = "substitution"; | ||
|
||
private final String match; | ||
private final String replace; | ||
private final boolean regex; | ||
|
||
Substitution(Map<String, String> attrs) { | ||
match = Strings.requireValid(attrs.get("match"), "match is required"); | ||
replace = Strings.requireValid(attrs.get("replace"), "replace is required"); | ||
regex = Boolean.parseBoolean(attrs.getOrDefault("regex", "true")); | ||
} | ||
|
||
String match() { | ||
return match; | ||
} | ||
|
||
String replace() { | ||
return replace; | ||
} | ||
|
||
boolean isRegex() { | ||
return regex; | ||
} | ||
|
||
@Override | ||
public String elementName() { | ||
return ELEMENT_NAME; | ||
} | ||
|
||
BiFunction<String, Map<String, String>, String> function() { | ||
return this::substitute; | ||
} | ||
|
||
String substitute(String str, Map<String, String> vars) { | ||
String value = str; | ||
String resolvedMatch = StagingTask.resolveVar(match, vars); | ||
String resolvedReplace = StagingTask.resolveVar(replace, vars); | ||
if (regex) { | ||
// replace {group} with ${group} | ||
Set<String> groups = Patterns.groupNames(resolvedMatch); | ||
for (String group : groups) { | ||
String token = "{" + group + "}"; | ||
resolvedReplace = resolvedReplace.replace(token, "$" + token); | ||
} | ||
value = value.replaceAll(resolvedMatch, resolvedReplace); | ||
} else { | ||
value = value.replace(resolvedMatch, resolvedReplace); | ||
} | ||
return value; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...n-plugins/stager-maven-plugin/src/main/java/io/helidon/build/maven/stager/TextAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2023, 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.build.maven.stager; | ||
|
||
/** | ||
* A {@link StagingAction} that produces text. | ||
*/ | ||
interface TextAction extends StagingAction { | ||
|
||
/** | ||
* Get the computed text. | ||
* | ||
* @return text | ||
*/ | ||
String text(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters