Files
SAE32_2024/src/fr/monlouyan/bakefile/Rule.java

104 lines
4.2 KiB
Java
Raw Normal View History

2025-02-04 17:31:25 +01:00
package fr.monlouyan.bakefile;
import java.io.File;
import java.util.List;
public class Rule {
private String name;
private List<String> dependencies;
private List<String> commands;
private boolean isPhony;
public Rule(String name, List<String> dependencies, List<String> commands, boolean isPhony) {
this.name = name;
this.dependencies = dependencies;
this.commands = commands;
this.isPhony = isPhony;
}
public String getName() { return name; }
public List<String> getDependencies() { return dependencies; }
public List<String> getCommands() { return commands; }
public boolean isPhony() { return isPhony; }
public boolean isEmpty() { return dependencies.isEmpty() && commands.isEmpty(); }
public boolean needsUpdate() {
if (BakeCLI.isDebug()){
System.out.println("Debug : Checking if rule " + name + " needs update");
}
// Les règles phony sont toujours mises à jour
2025-02-04 17:31:25 +01:00
if (isPhony) {
if (BakeCLI.isDebug()) {
System.out.println("Debug : Rule " + name + " is phony, always needs update");
}
return true;
2025-02-04 17:31:25 +01:00
}
// Si la règle n'a pas de commandes, on vérifie seulement si une dépendance doit être mise à jour
if (commands.isEmpty()) {
for (String dependency : dependencies) {
File depFile = new File(dependency);
if (!depFile.exists() && !dependency.isEmpty() && !BakeEngine.hasRule(dependency)) {
System.out.println("bake: *** No rule to make target '" + dependency + "', needed by '" + name + "'. Stop.");
System.exit(1);
}
Rule depRule = BakeEngine.getRule(dependency);
if (depRule != null && depRule.needsUpdate()) {
if (BakeCLI.isDebug()) {
System.out.println("Debug : Dependency rule " + dependency + " needs update");
}
return true;
}
}
return false;
}
// Pour les règles avec des commandes, on vérifie aussi les timestamps
File targetFile = new File(name);
2025-02-04 17:31:25 +01:00
if (BakeCLI.isDebug()){
System.out.println("Debug : Checking if target file " + name + " exist and is up to date");
}
2025-02-04 17:31:25 +01:00
long targetTimestamp = targetFile.exists() ? TimestampManager.getTimestamp(targetFile) : 0;
if (BakeCLI.isDebug()) {
System.out.println("Debug : Target file '" + name + "' last modified at " + TimestampManager.formatTimestamp(targetTimestamp));
}
2025-02-04 17:31:25 +01:00
for (String dependency : dependencies) {
File depFile = new File(dependency);
long depTimestamp = depFile.exists() ? TimestampManager.getTimestamp(depFile) : 0;
if (BakeCLI.isDebug()) {
System.out.println("Debug : Dependency '" + dependency + "' last modified at " + TimestampManager.formatTimestamp(depTimestamp));
}
2025-02-06 20:49:12 +01:00
// Vérifier si une règle existe pour cette dépendance
2025-02-06 20:49:12 +01:00
boolean hasRule = BakeEngine.hasRule(dependency);
if (!depFile.exists() && !dependency.isEmpty() && !hasRule) {
System.out.println("bake: *** No rule to make target '" + dependency + "', needed by '" + name + "'. Stop.");
System.exit(1);
}
// Si la dépendance est une règle et qu'elle a besoin d'être mise à jour
Rule depRule = BakeEngine.getRule(dependency);
if (depRule != null && depRule.needsUpdate()) {
if (BakeCLI.isDebug()) {
System.out.println("Debug : Dependency rule " + dependency + " needs update");
}
return true;
}
// Vérifier les timestamps seulement si le fichier existe
2025-02-04 17:31:25 +01:00
if (depFile.exists() && TimestampManager.getTimestamp(depFile) > targetTimestamp) {
if (BakeCLI.isDebug()) {
System.out.println("Debug : Dependency " + dependency + " is newer than target file " + name + ", needs update");
}
return true;
}
}
return false;
}
}