Améliorations du code en ajoutant des patterns/regex + Amélioration du mode debug
This commit is contained in:
@@ -3,39 +3,71 @@ package fr.monlouyan.bakefile;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class BakefileParser {
|
||||
private String filename;
|
||||
|
||||
/**
|
||||
* Regex pour détecter les targets et leurs dépendances.
|
||||
* Format : "nom : dépendance1 dépendance2"
|
||||
*/
|
||||
private static final Pattern TARGET_PATTERN = Pattern.compile("^(\\S+)\\s*:\\s*(.*)$");
|
||||
|
||||
/**
|
||||
* Regex pour détecter les lignes de commande associées à une target.
|
||||
* Format : " gcc -o program program.c" (ligne indentée)
|
||||
*/
|
||||
private static final Pattern COMMAND_PATTERN = Pattern.compile("^\\s+(.+)$");
|
||||
|
||||
public BakefileParser(String filename) {
|
||||
this.filename = filename;
|
||||
}
|
||||
|
||||
|
||||
public List<Target> parse() {
|
||||
List<Target> targets = new ArrayList<>();
|
||||
if (!Files.exists(Paths.get(filename))) {
|
||||
System.out.println("*** No targets specified and no makefile found. Stop.");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
List<String> lines = Files.readAllLines(Paths.get(filename));
|
||||
String currentTarget = null;
|
||||
List<String> dependencies = new ArrayList<>();
|
||||
List<String> commands = new ArrayList<>();
|
||||
|
||||
for (String line : lines) {
|
||||
if (line.contains(":")) {
|
||||
String[] parts = line.split(":");
|
||||
String name = parts[0].trim();
|
||||
String[] deps = parts[1].trim().split(" ");
|
||||
System.out.println("Target found: " + name + " with dependencies " + Arrays.toString(deps));
|
||||
targets.add(new Target(name, Arrays.asList(deps), "gcc -o " + name + " " + name + ".c"));
|
||||
Matcher targetMatcher = TARGET_PATTERN.matcher(line);
|
||||
Matcher commandMatcher = COMMAND_PATTERN.matcher(line);
|
||||
|
||||
if (targetMatcher.matches()) {
|
||||
// Sauvegarde de la précédente target si elle existe
|
||||
if (currentTarget != null) {
|
||||
targets.add(new Target(currentTarget, dependencies, String.join(" && ", commands)));
|
||||
}
|
||||
|
||||
// Nouvelle target détectée
|
||||
currentTarget = targetMatcher.group(1);
|
||||
dependencies = new ArrayList<>(Arrays.asList(targetMatcher.group(2).trim().split("\\s+")));
|
||||
commands = new ArrayList<>();
|
||||
|
||||
} else if (commandMatcher.matches()) {
|
||||
// Ligne de commande associée à la dernière target trouvée
|
||||
commands.add(commandMatcher.group(1));
|
||||
}
|
||||
}
|
||||
|
||||
// Ajout de la dernière target après la boucle
|
||||
if (currentTarget != null) {
|
||||
targets.add(new Target(currentTarget, dependencies, String.join(" && ", commands)));
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return targets;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user