Commit 01f39e58 authored by DELEFOSSE Alexandre's avatar DELEFOSSE Alexandre
Browse files

TODO: gerer dependances

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
.settings
bin
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>MyAnt</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
src = ./src/fr/isima/myant
bin = ./bin
file = myant.jar
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project name="build-MyAnt" default="all">
<property file = "build-myant.properties"/>
<target name="all" depends="init,clean,compile,package,run" />
<condition property="varNotSet">
<or>
<equals arg1="${bin}" arg2=""/>
<equals arg1="${src}" arg2=""/>
<equals arg1="${file}" arg2=""/>
</or>
</condition>
<target name="init" if="varNotSet">
<echo>WARNING: some variables are not set!</echo>
<property name="bin" value="./bin"/>
<property name="src" value="./src/fr/isima/myant"/>
<property name="file" value="myant.jar"/>
</target>
<target name="clean" depends="init">
<delete dir="${bin}" />
<mkdir dir="${bin}" />
<echo>Cleaning Done </echo>
</target>
<target name="compile" depends="init">
<echo>Compiling files... </echo>
<javac destdir="${bin}" srcdir="${src}" />
<echo>Compiling Done </echo>
</target>
<target name="package" depends="compile">
<jar destfile="${file}" basedir="${bin}" excludes="**/*Test.class">
<manifest>
<attribute name="Main-Class" value="fr.isima.myant.Main"/>
</manifest>
</jar>
</target>
<target name="run" depends="init">
<java jar="${file}" fork="true"/>
</target>
</project>
\ No newline at end of file
use fr.isima.myant.CopyTask as cptask
use fr.isima.myant.EchoTask as echotask
use fr.isima.myant.MkDirTask as mkdirtask
-
default:other,!
echo[message:"Starting... "]
echotask[message:"value1"]
mkdirtask[dir:"blabla"]
echo[message:"Completed! "]
-
other:!
echo[message:"Starting... "]
echotask[message:"un"]
echotask[message:"autre"]
echotask[message:"message"]
cptask[from:"mybuild.txt", to:"testLALALA.txt"]
echo[message:"Completed! "]
\ No newline at end of file
package fr.isima.myant;
import java.nio.file.Files;
import java.nio.file.Paths;
public class CopyTask extends Task {
private String from;
private String to;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public CopyTask() {
}
public void execute() {
try {
Files.copy(Paths.get(from), Paths.get(to));
} catch (Exception e) {
System.out.println("\tErreur: copytask");
}
}
}
package fr.isima.myant;
public class EchoTask extends Task {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public EchoTask() {
}
public EchoTask(String message) {
this.message = message;
}
public void execute() {
System.out.println(message);
}
}
package fr.isima.myant;
public class Main {
public static void main(String[] args) {
System.out.println("Init...");
Project p = new Project("test");
p.init();
p.loadFromFile();
System.out.println("Init Done \n");
System.out.println("Output:");
p.execute();
}
}
package fr.isima.myant;
import java.io.File;
public class MkDirTask extends Task {
private String dir;
public String getDir() {
return dir;
}
public void setDir(String dir) {
this.dir = dir;
}
public MkDirTask() {
}
public void execute() {
File theDir = new File(dir);
if (!theDir.exists()) {
System.out.println("creating directory: " + theDir.getName());
try{
theDir.mkdir();
}
catch(SecurityException se){
System.out.println("Permission Denied");
}
} else {
System.out.println("Dir already exist.");
}
}
}
package fr.isima.myant;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class Project {
private String name;
private List<Target> targets;
public List<Target> getTargets() {
return targets;
}
public void setTargets(List<Target> targets) {
this.targets = targets;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void init() {
Target t = new Target("default");
t.addTask(new EchoTask("message"));
this.addTarget(t);
}
private boolean targetExist(String name) {
boolean bool=false;
for(Target t : targets) {
if (t.getName().equals(name))
bool = true;
}
return bool;
}
public void loadFromFile() {
String fileName = "mybuild.txt";
String line = null;
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String[]> aliases = new ArrayList<String[]>();
String targetName = "";
while((line = bufferedReader.readLine()) != null) {
while (line != null && line.split(" ")[0] != null && line.split(" ")[0].equals("use")) {
aliases.add(new String[] {line.split(" ")[1], line.split(" ")[3]});
line = bufferedReader.readLine();
}
int nTask=1;
while((line = bufferedReader.readLine()) != null) {
if(!line.equals("-")) {
targetName = line.split(":")[0];
List<String> dependencies = new ArrayList<String>();
System.out.println("Starting task " + nTask);
System.out.println(targetName);
int indice=0;
while(!line.split(":")[1].split(",")[indice].equals("!")) {
dependencies.add(line.split(":")[1].split(",")[indice]);
System.out.println("Nouvelle Dependence: -" + line.split(":")[1].split(",")[indice] + "-");
indice++;
}
if(!targetExist(targetName))
targets.add(new Target(targetName));
line = bufferedReader.readLine();
line = bufferedReader.readLine();
while(!line.split("\\[")[0].equals("echo")) {
Class<?> clazz;
List<String> attributs = new ArrayList<String>();
List<String> fieldName = new ArrayList<String>();
for(String[] nameDuo : aliases) {
if(nameDuo[1].equals(line.split("\\[")[0])) {
try {
clazz = Class.forName(nameDuo[0]);
for(int i=1;i<line.split("\"").length;i+=2) {
attributs.add(line.split("\"")[i]);
}
fieldName.add(line.split(":")[0].split("\\[")[1]);
for(int i=1;i<line.split(":").length-1;i++) {
fieldName.add(line.split(":")[i].split(" ")[1]);
}
for(Target t : targets) {
if (t.getName().equals(targetName)) {
try {
Task instance = (Task) clazz.newInstance();
Field fields [] = clazz.getDeclaredFields();
for(int j=0; j<fieldName.size(); j++) {
for(int i=0; i<fields.length; i++) {
if (fieldName.get(j).equals(fields[i].getName())) {
fields[i].setAccessible(true);
fields[i].set(instance, attributs.get(j));
}
}
}
t.addTask(instance);
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (ClassNotFoundException e) {
System.out.println("Class not found : " + nameDuo[0]);
}
}
}
line = bufferedReader.readLine();
}
System.out.println("Completed task " + nTask++);
}
}
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
ex.printStackTrace();
}
catch(IOException ex) {
ex.printStackTrace();
}
}
public void execute() {
System.out.println("nbTargets: " + targets.size());
for(Target t : targets) {
t.execute();
}
}
public void addTarget(Target t) {
targets.add(t);
}
public Project(String name) {
this.name = name;
this.targets = new ArrayList<Target>();
}
}
package fr.isima.myant;
import java.util.ArrayList;
import java.util.List;
public class Target {
private String name;
private List<Task> tasks;
private List<Target> dependencies;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void execute() {
System.out.println("Target: -" + name + "-" + " with dependencies: -" + dependencies.size() + "-");
for(Target dep : dependencies) {
dep.execute();
}
for(Task task : tasks) {
task.execute();
}
}
public void addTask(Task t) {
tasks.add(t);
}
public void addDependency(Target t) {
dependencies.add(t);
}
public Target(String name) {
this.name = name;
this.tasks = new ArrayList<Task>();
this.dependencies = new ArrayList<Target>();
}
}
package fr.isima.myant;
public class Task {
public void execute() {
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment