From 2d7f69f02eb34cb0ad715c017e081c81ed9645d2 Mon Sep 17 00:00:00 2001 From: Moncef STITI <moncef.stiti@etu.u-pec.fr> Date: Tue, 4 Feb 2025 10:18:26 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20la=20structure=20de=20base=20du=20?= =?UTF-8?q?projet=20Bake=20avec=20les=20fichiers=20principaux.=20Premi?= =?UTF-8?q?=C3=A8re=20=C3=A9bauche=20du=20projet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 42 ++++++++++++ Manifest.MF | 2 + src/fr/monlouyan/bakefile/BakeCLI.java | 64 +++++++++++++++++++ src/fr/monlouyan/bakefile/BakeEngine.java | 26 ++++++++ src/fr/monlouyan/bakefile/BakefileParser.java | 36 +++++++++++ .../monlouyan/bakefile/CommandExecutor.java | 25 ++++++++ .../bakefile/DependencyResolver.java | 16 +++++ src/fr/monlouyan/bakefile/Main.java | 24 +++++++ src/fr/monlouyan/bakefile/Target.java | 30 +++++++++ 9 files changed, 265 insertions(+) create mode 100644 Makefile create mode 100644 Manifest.MF create mode 100644 src/fr/monlouyan/bakefile/BakeCLI.java create mode 100644 src/fr/monlouyan/bakefile/BakeEngine.java create mode 100644 src/fr/monlouyan/bakefile/BakefileParser.java create mode 100644 src/fr/monlouyan/bakefile/CommandExecutor.java create mode 100644 src/fr/monlouyan/bakefile/DependencyResolver.java create mode 100755 src/fr/monlouyan/bakefile/Main.java create mode 100644 src/fr/monlouyan/bakefile/Target.java diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..26000dc --- /dev/null +++ b/Makefile @@ -0,0 +1,42 @@ +PACKAGE = fr.monlouyan.bakefile +ENTRY = Main +SOURCEDIR = ./src/fr/monlouyan/bakefile/ +BUILDDIR = ./build/ +DOCDIR = ./doc/ +JARNAME = bakefile.jar +CLASSP = ./build +MANIFESTPATH = Manifest.MF +SOURCEDIR = ./src/ + +SOURCES := $(shell find $(SOURCEDIR) -name '*.java') + +all: + @make clean + @make compile + @make jar + @make run + +compile: + @echo "Compiling..." + javac -cp $(CLASSP) -d $(BUILDDIR) $(SOURCES) -Xlint:unchecked -Xlint:deprecation + @echo "Done." + +run: + @echo "Running..." + java -cp $(CLASSP):$(JARNAME) fr.monlouyan.bakefile.Main + @echo "Done." + +clean: + @echo "Cleaning up..." + @rm -rf $(BUILDDIR)* $(DOCDIR)* + @echo "Done." + +javadoc: + @echo "Generating javadoc..." + @javadoc -d $(DOCDIR) -sourcepath src -subpackages $(PACKAGE) + @echo "Done." + +jar: + @echo "Creating jar..." + @jar cfm $(JARNAME) $(MANIFESTPATH) -C $(BUILDDIR) fr/monlouyan/bakefile + @echo "Done." \ No newline at end of file diff --git a/Manifest.MF b/Manifest.MF new file mode 100644 index 0000000..82ace76 --- /dev/null +++ b/Manifest.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 +Main-Class: fr.monlouyan.bakefile.Main \ No newline at end of file diff --git a/src/fr/monlouyan/bakefile/BakeCLI.java b/src/fr/monlouyan/bakefile/BakeCLI.java new file mode 100644 index 0000000..e3462ee --- /dev/null +++ b/src/fr/monlouyan/bakefile/BakeCLI.java @@ -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; } +} diff --git a/src/fr/monlouyan/bakefile/BakeEngine.java b/src/fr/monlouyan/bakefile/BakeEngine.java new file mode 100644 index 0000000..dc3b1ca --- /dev/null +++ b/src/fr/monlouyan/bakefile/BakeEngine.java @@ -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); + } + } +} \ No newline at end of file diff --git a/src/fr/monlouyan/bakefile/BakefileParser.java b/src/fr/monlouyan/bakefile/BakefileParser.java new file mode 100644 index 0000000..6ed681c --- /dev/null +++ b/src/fr/monlouyan/bakefile/BakefileParser.java @@ -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; + } + +} \ No newline at end of file diff --git a/src/fr/monlouyan/bakefile/CommandExecutor.java b/src/fr/monlouyan/bakefile/CommandExecutor.java new file mode 100644 index 0000000..5341282 --- /dev/null +++ b/src/fr/monlouyan/bakefile/CommandExecutor.java @@ -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(); + } + } +} \ No newline at end of file diff --git a/src/fr/monlouyan/bakefile/DependencyResolver.java b/src/fr/monlouyan/bakefile/DependencyResolver.java new file mode 100644 index 0000000..2dfd0ab --- /dev/null +++ b/src/fr/monlouyan/bakefile/DependencyResolver.java @@ -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; + } +} \ No newline at end of file diff --git a/src/fr/monlouyan/bakefile/Main.java b/src/fr/monlouyan/bakefile/Main.java new file mode 100755 index 0000000..8e571f7 --- /dev/null +++ b/src/fr/monlouyan/bakefile/Main.java @@ -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(); + } +} \ No newline at end of file diff --git a/src/fr/monlouyan/bakefile/Target.java b/src/fr/monlouyan/bakefile/Target.java new file mode 100644 index 0000000..29b32ca --- /dev/null +++ b/src/fr/monlouyan/bakefile/Target.java @@ -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; } +} \ No newline at end of file