Ajout de la structure de base du projet Bake avec les fichiers principaux.

Première ébauche du projet
This commit is contained in:
2025-02-04 10:18:26 +01:00
parent e466bda000
commit 2d7f69f02e
9 changed files with 265 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
package fr.monlouyan.bakefile;
import java.util.List;
import java.util.ArrayList;
/**
* Gestion des arguments de la ligne de commande
*
* @author Moncef STITI
* @version 1.0
* @date 03/02/2025
* @see Main
*/
public class BakeCLI {
/*
* Mode debug activé ou non
*/
private boolean debug;
/*
* Liste des arguments passés en ligne de commande
*/
private List<String> targets;
/**
* Constructeur de la classe BakeCLI
*
* @param args Les arguments passés en ligne de commande
* @return void
* @see Main
*/
public BakeCLI(String[] args){
this.debug = false;
this.targets = new ArrayList<>();
parseArgs(args);
}
/**
* Permet de parcourir les arguments passés en ligne de commande, d'activer le mode debug et de récupérer les autres arguments
* @param args
* @return void
*/
private void parseArgs(String[] args){
for (String arg : args){
if (arg.equals("-d")){
debug = true;
} else {
targets.add(arg);
}
}
}
/**
* Permet de savoir si le mode debug est activé ou non.
* @return true si le mode debug est activé, false sinon
*/
public boolean isDebug(){ return debug; }
/**
* Permet de récupérer les arguments autres que "-d" passés en ligne de commande
* @return
*/
public List<String> getTargets(){ return targets; }
}

View File

@@ -0,0 +1,26 @@
package fr.monlouyan.bakefile;
import java.util.List;
public class BakeEngine {
private BakeCLI cli;
private BakefileParser parser;
private DependencyResolver resolver;
private CommandExecutor executor;
public BakeEngine(BakeCLI cli) {
this.cli = cli;
this.parser = new BakefileParser("Bakefile");
this.resolver = new DependencyResolver();
this.executor = new CommandExecutor(cli.isDebug());
}
public void run() {
List<Target> targets = parser.parse();
List<Target> targetsToBuild = resolver.resolve(targets, cli.getTargets());
for (Target target : targetsToBuild) {
executor.execute(target);
}
}
}

View File

@@ -0,0 +1,36 @@
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;
public class BakefileParser {
private String filename;
public BakefileParser(String filename) {
this.filename = filename;
}
public List<Target> parse() {
List<Target> targets = new ArrayList<>();
try {
List<String> lines = Files.readAllLines(Paths.get(filename));
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"));
}
}
} catch (IOException e) {
e.printStackTrace();
}
return targets;
}
}

View File

@@ -0,0 +1,25 @@
package fr.monlouyan.bakefile;
import java.io.IOException;
public class CommandExecutor {
private boolean debug;
public CommandExecutor(boolean debug) {
this.debug = debug;
}
public void execute(Target target) {
if (!target.needsUpdate()) return;
try {
System.out.println("Executing: " + target.getCommand());
ProcessBuilder pb = new ProcessBuilder("sh", "-c", target.getCommand());
Process process = pb.start();
int exitCode = process.waitFor();
if (debug) System.out.println("Executed: " + target.getCommand() + " with exit code " + exitCode);
if (exitCode != 0) System.err.println("Error executing " + target.getName());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,16 @@
package fr.monlouyan.bakefile;
import java.util.ArrayList;
import java.util.List;
public class DependencyResolver {
public List<Target> resolve(List<Target> allTargets, List<String> requestedTargets) {
List<Target> targetsToBuild = new ArrayList<>();
for (Target target : allTargets) {
if (requestedTargets.isEmpty() || requestedTargets.contains(target.getName())) {
targetsToBuild.add(target);
}
}
return targetsToBuild;
}
}

View File

@@ -0,0 +1,24 @@
package fr.monlouyan.bakefile;
/**
* Classe principale du programme
*
* @version 1.0
* @author Moncef STITI
* @date 03/02/2025
*/
public class Main{
/**
* Méthode principale du programme
*
* @param args Les arguments passés en ligne de commande
* @return void
*/
public static void main(String[] args){
System.out.println("Bakefile v1.0");
BakeCLI cli = new BakeCLI(args);
BakeEngine engine = new BakeEngine(cli);
engine.run();
}
}

View File

@@ -0,0 +1,30 @@
package fr.monlouyan.bakefile;
import java.io.File;
import java.util.List;
public class Target {
private String name;
private List<String> dependencies;
private String command;
public Target(String name, List<String> dependencies, String command) {
this.name = name;
this.dependencies = dependencies;
this.command = command;
}
public boolean needsUpdate() {
File targetFile = new File(name);
if (!targetFile.exists()) return true;
long lastModified = targetFile.lastModified();
for (String dep : dependencies) {
File depFile = new File(dep);
if (depFile.exists() && depFile.lastModified() > lastModified) return true;
}
return false;
}
public String getCommand() { return command; }
public String getName() { return name; }
}