Améliorations du code en ajoutant des patterns/regex + Amélioration du mode debug
This commit is contained in:
parent
d8cd0c785b
commit
238a02796b
15
Makefile
15
Makefile
@ -7,6 +7,7 @@ JARNAME = bakefile.jar
|
|||||||
CLASSP = ./build
|
CLASSP = ./build
|
||||||
MANIFESTPATH = Manifest.MF
|
MANIFESTPATH = Manifest.MF
|
||||||
SOURCEDIR = ./src/
|
SOURCEDIR = ./src/
|
||||||
|
TESTDIR = ./tests/
|
||||||
|
|
||||||
SOURCES := $(shell find $(SOURCEDIR) -name '*.java')
|
SOURCES := $(shell find $(SOURCEDIR) -name '*.java')
|
||||||
|
|
||||||
@ -14,21 +15,17 @@ all:
|
|||||||
@make clean
|
@make clean
|
||||||
@make compile
|
@make compile
|
||||||
@make jar
|
@make jar
|
||||||
@make run
|
|
||||||
|
|
||||||
compile:
|
compile:
|
||||||
@echo "Compiling..."
|
@echo "Compiling..."
|
||||||
javac -cp $(CLASSP) -d $(BUILDDIR) $(SOURCES) -Xlint:unchecked -Xlint:deprecation
|
javac -cp $(CLASSP) -d $(BUILDDIR) $(SOURCES) -Xlint:unchecked -Xlint:deprecation
|
||||||
@echo "Done."
|
@echo "Done."
|
||||||
|
|
||||||
run:
|
|
||||||
@echo "Running..."
|
|
||||||
java -cp $(CLASSP):$(JARNAME) fr.monlouyan.bakefile.Main
|
|
||||||
@echo "Done."
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@echo "Cleaning up..."
|
@echo "Cleaning up..."
|
||||||
@rm -rf $(BUILDDIR)* $(DOCDIR)*
|
@rm -rf $(BUILDDIR)* $(DOCDIR)*
|
||||||
|
@rm -f $(JARNAME)
|
||||||
|
@find $(TESTDIR) -name "$(JARNAME)" -delete
|
||||||
@echo "Done."
|
@echo "Done."
|
||||||
|
|
||||||
javadoc:
|
javadoc:
|
||||||
@ -39,4 +36,10 @@ javadoc:
|
|||||||
jar:
|
jar:
|
||||||
@echo "Creating jar..."
|
@echo "Creating jar..."
|
||||||
@jar cfm $(JARNAME) $(MANIFESTPATH) -C $(BUILDDIR) fr/monlouyan/bakefile
|
@jar cfm $(JARNAME) $(MANIFESTPATH) -C $(BUILDDIR) fr/monlouyan/bakefile
|
||||||
|
@echo "Done."
|
||||||
|
@make deploy-tests
|
||||||
|
|
||||||
|
deploy-tests:
|
||||||
|
@echo "Deploying JAR to test directories..."
|
||||||
|
@find $(TESTDIR) -type d -exec cp $(JARNAME) {} \;
|
||||||
@echo "Done."
|
@echo "Done."
|
@ -12,7 +12,7 @@ public class BakeEngine {
|
|||||||
public BakeEngine(BakeCLI cli) {
|
public BakeEngine(BakeCLI cli) {
|
||||||
this.cli = cli;
|
this.cli = cli;
|
||||||
this.parser = new BakefileParser("Bakefile");
|
this.parser = new BakefileParser("Bakefile");
|
||||||
this.resolver = new DependencyResolver();
|
this.resolver = new DependencyResolver(cli.isDebug());
|
||||||
this.executor = new CommandExecutor(cli.isDebug());
|
this.executor = new CommandExecutor(cli.isDebug());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,39 +3,71 @@ package fr.monlouyan.bakefile;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Arrays;
|
import java.util.regex.Matcher;
|
||||||
import java.util.List;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class BakefileParser {
|
public class BakefileParser {
|
||||||
private String filename;
|
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) {
|
public BakefileParser(String filename) {
|
||||||
this.filename = filename;
|
this.filename = filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Target> parse() {
|
public List<Target> parse() {
|
||||||
List<Target> targets = new ArrayList<>();
|
List<Target> targets = new ArrayList<>();
|
||||||
if (!Files.exists(Paths.get(filename))) {
|
if (!Files.exists(Paths.get(filename))) {
|
||||||
System.out.println("*** No targets specified and no makefile found. Stop.");
|
System.out.println("*** No targets specified and no makefile found. Stop.");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
List<String> lines = Files.readAllLines(Paths.get(filename));
|
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) {
|
for (String line : lines) {
|
||||||
if (line.contains(":")) {
|
Matcher targetMatcher = TARGET_PATTERN.matcher(line);
|
||||||
String[] parts = line.split(":");
|
Matcher commandMatcher = COMMAND_PATTERN.matcher(line);
|
||||||
String name = parts[0].trim();
|
|
||||||
String[] deps = parts[1].trim().split(" ");
|
if (targetMatcher.matches()) {
|
||||||
System.out.println("Target found: " + name + " with dependencies " + Arrays.toString(deps));
|
// Sauvegarde de la précédente target si elle existe
|
||||||
targets.add(new Target(name, Arrays.asList(deps), "gcc -o " + name + " " + name + ".c"));
|
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) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return targets;
|
return targets;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
@ -10,9 +10,12 @@ public class CommandExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void execute(Target target) {
|
public void execute(Target target) {
|
||||||
if (!target.needsUpdate()) return;
|
if (!target.needsUpdate()){
|
||||||
|
System.out.println("bake: '" + target.getName() + "' is up to date.");
|
||||||
|
return;
|
||||||
|
};
|
||||||
try {
|
try {
|
||||||
System.out.println("Executing: " + target.getCommand());
|
System.out.println(target.getCommand());
|
||||||
ProcessBuilder pb = new ProcessBuilder("sh", "-c", target.getCommand());
|
ProcessBuilder pb = new ProcessBuilder("sh", "-c", target.getCommand());
|
||||||
Process process = pb.start();
|
Process process = pb.start();
|
||||||
int exitCode = process.waitFor();
|
int exitCode = process.waitFor();
|
||||||
|
@ -4,10 +4,19 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class DependencyResolver {
|
public class DependencyResolver {
|
||||||
|
private boolean debug;
|
||||||
|
|
||||||
|
public DependencyResolver(boolean debug) {
|
||||||
|
this.debug = debug;
|
||||||
|
}
|
||||||
|
|
||||||
public List<Target> resolve(List<Target> allTargets, List<String> requestedTargets) {
|
public List<Target> resolve(List<Target> allTargets, List<String> requestedTargets) {
|
||||||
List<Target> targetsToBuild = new ArrayList<>();
|
List<Target> targetsToBuild = new ArrayList<>();
|
||||||
for (Target target : allTargets) {
|
for (Target target : allTargets) {
|
||||||
if (requestedTargets.isEmpty() || requestedTargets.contains(target.getName())) {
|
if (requestedTargets.isEmpty() || requestedTargets.contains(target.getName())) {
|
||||||
|
if (debug){
|
||||||
|
System.out.println("Target " + target.getName() + " is requested");
|
||||||
|
}
|
||||||
targetsToBuild.add(target);
|
targetsToBuild.add(target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@ public class Main{
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
System.out.println("Bakefile v1.0");
|
|
||||||
BakeCLI cli = new BakeCLI(args);
|
BakeCLI cli = new BakeCLI(args);
|
||||||
BakeEngine engine = new BakeEngine(cli);
|
BakeEngine engine = new BakeEngine(cli);
|
||||||
engine.run();
|
engine.run();
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
main: main.c
|
main: main.c
|
||||||
gcc -o main main.c
|
gcc -o main main.c
|
||||||
|
Binary file not shown.
@ -2,5 +2,6 @@
|
|||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
printf("Ceci est un programme de test du Bakefile !\n");
|
printf("Ceci est un programme de test du Bakefile !\n");
|
||||||
|
printf("test\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user