Avoid duplication of code
parent
14b7a2b814
commit
0e1a451214
@ -0,0 +1,40 @@
|
||||
package tools;
|
||||
|
||||
public class Directories {
|
||||
private String projectName;
|
||||
private String dataHomeDirectory;
|
||||
|
||||
public Directories(String projectName) {
|
||||
this.projectName = projectName;
|
||||
setDataHomeDirectory();
|
||||
}
|
||||
private void setDataHomeDirectory() {
|
||||
/* Follow XDG Base Directory Specification
|
||||
* https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
||||
*/
|
||||
String os = System.getProperty("os.name");
|
||||
if (os.equals("Linux")) {
|
||||
d = System.getenv().get("XDG_DATA_HOME");
|
||||
if (d == null || d.equals("")) {
|
||||
d = System.getProperty("user.home") + "/.local/share";
|
||||
}
|
||||
} else if (os.equals("Mac")||os.equals("Mac OS X") {
|
||||
/* Apple MacOS X User Data Directory
|
||||
* https://developer.apple.com/library/archive/qa/qa1170/_index.html */
|
||||
d = System.getProperty("user.home") + "/Library";
|
||||
} else {
|
||||
d = ".";
|
||||
}
|
||||
d += "/" + projectName + "/";
|
||||
// create directory if not already exists
|
||||
new File(d).mkdirs();
|
||||
dataHomeDirectory = d;
|
||||
}
|
||||
|
||||
public String getDataHomeDirectory() {
|
||||
return dataHomeDirectory();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue