Ajout de la structure de base du projet Bake avec les fichiers principaux.
Première ébauche du projet
This commit is contained in:
parent
e466bda000
commit
2d7f69f02e
42
Makefile
Normal file
42
Makefile
Normal file
@ -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."
|
2
Manifest.MF
Normal file
2
Manifest.MF
Normal file
@ -0,0 +1,2 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: fr.monlouyan.bakefile.Main
|
64
src/fr/monlouyan/bakefile/BakeCLI.java
Normal file
64
src/fr/monlouyan/bakefile/BakeCLI.java
Normal 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; }
|
||||
}
|
26
src/fr/monlouyan/bakefile/BakeEngine.java
Normal file
26
src/fr/monlouyan/bakefile/BakeEngine.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
36
src/fr/monlouyan/bakefile/BakefileParser.java
Normal file
36
src/fr/monlouyan/bakefile/BakefileParser.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
25
src/fr/monlouyan/bakefile/CommandExecutor.java
Normal file
25
src/fr/monlouyan/bakefile/CommandExecutor.java
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
16
src/fr/monlouyan/bakefile/DependencyResolver.java
Normal file
16
src/fr/monlouyan/bakefile/DependencyResolver.java
Normal 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;
|
||||
}
|
||||
}
|
24
src/fr/monlouyan/bakefile/Main.java
Executable file
24
src/fr/monlouyan/bakefile/Main.java
Executable 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();
|
||||
}
|
||||
}
|
30
src/fr/monlouyan/bakefile/Target.java
Normal file
30
src/fr/monlouyan/bakefile/Target.java
Normal 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; }
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user