//usr/bin/env jbang "$0" "$@" ; exit $? | |
//DEPS com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.10.2 | |
//DEPS com.fasterxml.jackson.core:jackson-databind:2.10.2 | |
//DEPS fr.opensagres.js:minimatch.java:1.1.0 | |
//DEPS info.picocli:picocli:4.1.4 | |
import java.io.File; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.concurrent.Callable; | |
import java.util.stream.Collectors; | |
import static java.lang.System.*; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | |
import minimatch.Minimatch; | |
import picocli.CommandLine; | |
import picocli.CommandLine.Command; | |
import picocli.CommandLine.Option; | |
import picocli.CommandLine.Parameters; | |
import picocli.CommandLine.Help.Ansi; | |
@Command(description="Prints matching labels for directories found in folder. Useful to check how labeler will work.") | |
public class checklabeler implements Callable<Integer> { | |
@Parameters(index="0", description="Path to .github/labeler.yml") | |
String yamlFile; | |
@Parameters(index="1", defaultValue = "", description = "Path to dump labels for") | |
String rootDir; | |
@Option(names = "--only-dirs", description = "If set, only print for directories", defaultValue = "false") | |
boolean onlyDirs; | |
@Option(names= "--only-matches", description = "Print only matches") | |
boolean onlyMatches; | |
public static void main(String... args) { | |
System.exit(new CommandLine(new checklabeler()).execute(args)); | |
} | |
private Map<String, List<String>> labelers; | |
List<String> findLabels(Path p) { | |
var result = new ArrayList<String>(); | |
var item = labelers.entrySet().stream().filter(e -> match(p, e.getValue())) | |
.map(x->x.getKey()) | |
.collect(Collectors.joining(",")); | |
result.add(item); | |
if(onlyMatches && item.isEmpty()) { | |
// noop | |
} else { | |
out.println(Ansi.AUTO.string(p + " @|bold,green,underline " + result +"|@")); | |
} | |
return result; | |
} | |
private static boolean match(Path p, List<String> value) { | |
for (String pattern : value) { | |
if(Minimatch.minimatch(p.toFile().getPath(), pattern)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
@SuppressWarnings("unchecked") | |
public Integer call() { | |
var mapper = new ObjectMapper(new YAMLFactory()); | |
try { | |
labelers = mapper.readValue(new File(yamlFile), Map.class); | |
//out.println(labelers); | |
Files.walk(Paths.get(rootDir)).filter(x -> onlyDirs?x.toFile().isDirectory():true).forEach(this::findLabels); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return -1; | |
} | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment