island/src/org/atriasoft/island/model/ConfigManifest.java

205 lines
5.7 KiB
Java

package org.atriasoft.island.model;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import org.atriasoft.exml.Exml;
import org.atriasoft.exml.exception.ExmlBuilderException;
import org.atriasoft.island.Env;
import org.atriasoft.island.internal.Log;
public class ConfigManifest {
private String repo = "";
private String branch = "master";
private String manifestName = "default.xml";
private List<Volatile> volatiles = new ArrayList<>();
private List<Link> curentLink = new ArrayList<>();
public ConfigManifest() {
}
public String getRepo() {
return this.repo;
}
public void setRepo(final String repo) {
this.repo = repo;
}
public String getBranch() {
return this.branch;
}
public void setBranch(final String branch) {
this.branch = branch;
}
public String getManifestName() {
return this.manifestName;
}
public void setManifestName(final String manifestName) {
this.manifestName = manifestName;
}
public List<Volatile> getVolatiles() {
return this.volatiles;
}
public void setVolatiles(final List<Volatile> volatiles) {
this.volatiles = volatiles;
}
public void addVolatile(final String gitAddress, final String path) {
rmVolatile(path);
this.volatiles.add(new Volatile(gitAddress, path));
}
private void rmVolatile(final String path) {
ListIterator<Volatile> it = this.volatiles.listIterator();
while (it.hasNext()) {
Volatile elem = it.next();
if (elem.path.equals(path)) {
it.remove();
}
}
}
public List<Link> getCurentLink() {
return this.curentLink;
}
public void setCurentLink(final List<Link> curentLink) {
this.curentLink = curentLink;
}
public void addLink(final String source, final String destination) {
rmLink(destination);
this.curentLink.add(new Link(source, destination));
}
private void rmLink(final String destination) {
ListIterator<Link> it = this.curentLink.listIterator();
while (it.hasNext()) {
Link elem = it.next();
if (elem.getDestination().equals(destination)) {
it.remove();
}
}
}
public static ConfigManifest load() {
return ConfigManifest.load(Env.get_island_path_config());
}
public static ConfigManifest load(final Path path) {
ConfigManifest[] root = null;
try {
root = Exml.parse(path, ConfigManifest.class, "config-island");
} catch (ExmlBuilderException e) {
Log.error("Can not parse the file.1. " + path);
e.printStackTrace();
}
if (root.length != 1) {
Log.error("Can not parse the file.2. " + path);
}
return root[0];
}
public void store() {
store(Env.get_island_path_config());
}
public void store(final Path path) {
Log.todo("unimplemented model...");
}
}
// public void load():
// // transform the old format of configuration (use json now ==> simple
// if os.path.exists(Env.get_island_path_config_old()) == true:
// self.convert_config_file()
// if os.path.exists(Env.get_island_path_config()) == false:
// return true
// this.volatiles = []
// this.curentLink = []
// with open(Env.get_island_path_config()) as json_file:
// data = json.load(json_file)
// if "repo" in data.keys():
// this.repo = data["repo"]
// if "branch" in data.keys():
// this.branch = data["branch"]
// if "manifest_name" in data.keys():
// this.manifestName = data["manifest_name"]
// if "volatiles" in data.keys():
// for elem in data["
// self.add_volatile(elem["git_address"], elem["path"])
// if "link" in data.keys():
// for elem in data["link"]:
// if "source" in elem.keys() and "destination" in elem.keys():
// self.add_link(elem["source"], elem["destination"])
// return true
// return false
//
// public void store(self):
// data = {}
// data["repo"] = this.repo
// data["branch"] = this.branch
// data["manifest_name"] = this.manifestName
// data["volatiles"] = this.volatiles
// data["link"] = this.curentLink
// with open(Env.get_island_path_config(), 'w') as outfile:
// json.dump(data, outfile, indent=4)
// return true
// return false
//
// public void add_volatile(self, git_adress, local_path):
// for elem in this.volatiles:
// if elem["path"] == local_path:
// Log.error("can not have multiple local repositoty on the same PATH", crash=false)
// return false
// this.volatiles.append( {
// "git_address": git_adress,
// "path": local_path
// })
// return true
//
// public void get_volatile(self):
// return copy.deepcopy(this.volatiles)
//
//
// public void get_links(self):
// return this.curentLink
//
// public void add_link(self, source, destination):
// for elem in this.curentLink:
// if elem["destination"] == destination:
// Log.error("can not have multiple destination folder in link " + destination, crash=false)
// return false
// this.curentLink.append( {
// "source": source,
// "destination": destination
// })
// return true
//
// public void remove_link(self, destination):
// for elem in this.curentLink:
// if elem["destination"] == destination:
// del this.curentLink[elem]
// return
// Log.warning("Request remove link that does not exist")
//
// public void clear_links(self):
// this.curentLink = []
//
//
// public void get_manifest_config(self):
// conf = repo_config.RepoConfig()
// base_volatile, repo_volatile = repo_config.split_repo(self.get_manifest())
// conf.name = repo_volatile
// conf.path = new Path("." + Env.get_system_base_name(), "manifest") //Env.get_island_path_manifest()
// conf.branch = "master"
// conf.volatile = false
// conf.remotes = [
// {
// 'name': 'origin',
// 'fetch': base_volatile,
// 'mirror': []
// }
// ]
// conf.select_remote = {
// 'name': 'origin',
// 'fetch': base_volatile,
// 'sync': false,
// 'mirror': []
// }
// return conf