Skip to content

Commit 79a6dbb

Browse files
Merge pull request #4 from coursier/merge-upstream
Merge upstream changes
2 parents 7c8dad3 + ca4f793 commit 79a6dbb

File tree

10 files changed

+336
-273
lines changed

10 files changed

+336
-273
lines changed

src/main/java/dev/dirs/BaseDirectories.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dev.dirs;
22

3-
import static dev.dirs.Util.*;
3+
import dev.dirs.impl.Linux;
4+
import dev.dirs.impl.Util;
5+
import dev.dirs.impl.Windows;
46

57
/** {@code BaseDirectories} provides paths of user-invisible standard directories, following the conventions of the operating system the library is running on.
68
* <p>
@@ -247,22 +249,22 @@ public static BaseDirectories get() {
247249
}
248250

249251
private BaseDirectories() {
250-
switch (operatingSystem) {
251-
case LIN:
252-
case BSD:
253-
case SOLARIS:
254-
case IBMI:
255-
case AIX:
252+
switch (Constants.operatingSystem) {
253+
case Constants.LIN:
254+
case Constants.BSD:
255+
case Constants.SOLARIS:
256+
case Constants.IBMI:
257+
case Constants.AIX:
256258
homeDir = System.getProperty("user.home");
257-
cacheDir = defaultIfNullOrEmpty(System.getenv("XDG_CACHE_HOME"), homeDir, "/.cache");
258-
configDir = defaultIfNullOrEmpty(System.getenv("XDG_CONFIG_HOME"), homeDir, "/.config");
259-
dataDir = defaultIfNullOrEmpty(System.getenv("XDG_DATA_HOME"), homeDir, "/.local/share");
259+
cacheDir = Util.defaultIfNullOrEmpty(System.getenv("XDG_CACHE_HOME"), homeDir, "/.cache");
260+
configDir = Util.defaultIfNullOrEmpty(System.getenv("XDG_CONFIG_HOME"), homeDir, "/.config");
261+
dataDir = Util.defaultIfNullOrEmpty(System.getenv("XDG_DATA_HOME"), homeDir, "/.local/share");
260262
dataLocalDir = dataDir;
261-
executableDir = linuxExecutableDir(homeDir, dataDir);
263+
executableDir = Linux.executableDir(homeDir, dataDir);
262264
preferenceDir = configDir;
263-
runtimeDir = linuxRuntimeDir(null);
265+
runtimeDir = Linux.runtimeDir(null);
264266
break;
265-
case MAC:
267+
case Constants.MAC:
266268
homeDir = System.getProperty("user.home");
267269
cacheDir = homeDir + "/Library/Caches/";
268270
configDir = homeDir + "/Library/Application Support/";
@@ -272,8 +274,8 @@ private BaseDirectories() {
272274
preferenceDir = homeDir + "/Library/Preferences/";
273275
runtimeDir = null;
274276
break;
275-
case WIN:
276-
String[] winDirs = getWinDirs("5E6C858F-0E22-4760-9AFE-EA3317B67173", "3EB685DB-65F9-4CF6-A03A-E3EF65729F3D", "F1B32785-6FBA-4FCF-9D55-7B8E7F157091");
277+
case Constants.WIN:
278+
String[] winDirs = Windows.getWinDirs("5E6C858F-0E22-4760-9AFE-EA3317B67173", "3EB685DB-65F9-4CF6-A03A-E3EF65729F3D", "F1B32785-6FBA-4FCF-9D55-7B8E7F157091");
277279
homeDir = winDirs[0];
278280
dataDir = winDirs[1];
279281
dataLocalDir = winDirs[2];
@@ -284,13 +286,13 @@ private BaseDirectories() {
284286
runtimeDir = null;
285287
break;
286288
default:
287-
throw new UnsupportedOperatingSystemException("Base directories are not supported on " + operatingSystemName);
289+
throw new UnsupportedOperatingSystemException("Base directories are not supported on " + Constants.operatingSystemName);
288290
}
289291
}
290292

291293
@Override
292294
public String toString() {
293-
return "BaseDirectories (" + operatingSystemName + "):\n" +
295+
return "BaseDirectories (" + Constants.operatingSystemName + "):\n" +
294296
" homeDir = '" + homeDir + "'\n" +
295297
" cacheDir = '" + cacheDir + "'\n" +
296298
" configDir = '" + configDir + "'\n" +

src/main/java/dev/dirs/Constants.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package dev.dirs;
2+
3+
import java.util.Locale;
4+
5+
public class Constants {
6+
7+
static final String operatingSystemName = System.getProperty("os.name");
8+
public static final char operatingSystem;
9+
static final char LIN = 'l';
10+
static final char MAC = 'm';
11+
static final char WIN = 'w';
12+
static final char BSD = 'b';
13+
static final char SOLARIS = 's';
14+
static final char IBMI = 'i';
15+
static final char AIX = 'a';
16+
17+
static {
18+
final String os = operatingSystemName.toLowerCase(Locale.ROOT);
19+
if (os.contains("linux"))
20+
operatingSystem = LIN;
21+
else if (os.contains("mac"))
22+
operatingSystem = MAC;
23+
else if (os.contains("windows"))
24+
operatingSystem = WIN;
25+
else if (os.contains("bsd"))
26+
operatingSystem = BSD;
27+
else if (os.contains("sunos"))
28+
operatingSystem = SOLARIS;
29+
else if (os.contains("os/400") || os.contains("os400"))
30+
operatingSystem = IBMI;
31+
else if (os.contains("aix"))
32+
operatingSystem = AIX;
33+
else
34+
throw new UnsupportedOperatingSystemException("directories are not supported on " + operatingSystemName);
35+
}
36+
37+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package dev.dirs;
22

3+
import dev.dirs.impl.Windows;
4+
35
public interface GetWinDirs {
46
String[] getWinDirs(String... guids);
57

6-
GetWinDirs powerShellBased = Util::getWinDirs;
8+
GetWinDirs powerShellBased = Windows::getWinDirs;
79
}

src/main/java/dev/dirs/ProjectDirectories.java

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package dev.dirs;
22

3-
import static dev.dirs.Util.*;
3+
import dev.dirs.impl.Linux;
4+
import dev.dirs.impl.MacOs;
5+
import dev.dirs.impl.Util;
6+
import dev.dirs.impl.Windows;
7+
8+
import java.util.Objects;
49

510
/** {@code ProjectDirectories} computes the location of cache, config or data directories for a specific application,
611
* which are derived from the standard directories and the name of the project/organization.
@@ -28,7 +33,7 @@ private ProjectDirectories(
2833
final String preferenceDir,
2934
final String runtimeDir) {
3035

31-
requireNonNull(projectPath);
36+
Objects.requireNonNull(projectPath);
3237

3338
this.projectPath = projectPath;
3439
this.cacheDir = cacheDir;
@@ -234,29 +239,29 @@ public static ProjectDirectories fromPath(String path, GetWinDirs getWinDirs) {
234239
String dataLocalDir;
235240
String preferenceDir;
236241
String runtimeDir = null;
237-
switch (operatingSystem) {
238-
case LIN:
239-
case BSD:
240-
case SOLARIS:
241-
case IBMI:
242-
case AIX:
242+
switch (Constants.operatingSystem) {
243+
case Constants.LIN:
244+
case Constants.BSD:
245+
case Constants.SOLARIS:
246+
case Constants.IBMI:
247+
case Constants.AIX:
243248
homeDir = System.getProperty("user.home");
244-
cacheDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_CACHE_HOME"), path, homeDir + "/.cache/", path);
245-
configDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_CONFIG_HOME"), path, homeDir + "/.config/", path);
246-
dataDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), path, homeDir + "/.local/share/", path);
249+
cacheDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_CACHE_HOME"), path, homeDir + "/.cache/", path);
250+
configDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_CONFIG_HOME"), path, homeDir + "/.config/", path);
251+
dataDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), path, homeDir + "/.local/share/", path);
247252
dataLocalDir = dataDir;
248253
preferenceDir = configDir;
249-
runtimeDir = linuxRuntimeDir(path);
254+
runtimeDir = Linux.runtimeDir(path);
250255
break;
251-
case MAC:
256+
case Constants.MAC:
252257
homeDir = System.getProperty("user.home");
253258
cacheDir = homeDir + "/Library/Caches/" + path;
254259
configDir = homeDir + "/Library/Application Support/" + path;
255260
dataDir = homeDir + "/Library/Application Support/" + path;
256261
dataLocalDir = dataDir;
257262
preferenceDir = homeDir + "/Library/Preferences/" + path;
258263
break;
259-
case WIN:
264+
case Constants.WIN:
260265
String[] winDirs = getWinDirs.getWinDirs("3EB685DB-65F9-4CF6-A03A-E3EF65729F3D", "F1B32785-6FBA-4FCF-9D55-7B8E7F157091");
261266
String appDataRoaming = winDirs[0] + '\\' + path;
262267
String appDataLocal = winDirs[1] + '\\' + path;
@@ -267,7 +272,7 @@ public static ProjectDirectories fromPath(String path, GetWinDirs getWinDirs) {
267272
preferenceDir = configDir;
268273
break;
269274
default:
270-
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + operatingSystemName);
275+
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + Constants.operatingSystemName);
271276
}
272277
return new ProjectDirectories(path, cacheDir, configDir, dataDir, dataLocalDir, preferenceDir, runtimeDir);
273278
}
@@ -297,32 +302,32 @@ public static ProjectDirectories from(String qualifier, String organization, Str
297302
}
298303

299304
public static ProjectDirectories from(String qualifier, String organization, String application, GetWinDirs getWinDirs) {
300-
if (isNullOrEmpty(organization) && isNullOrEmpty(application))
305+
if (Util.isNullOrEmpty(organization) && Util.isNullOrEmpty(application))
301306
throw new UnsupportedOperationException("organization and application arguments cannot both be null/empty");
302307
String path;
303-
switch (operatingSystem) {
304-
case LIN:
305-
case BSD:
306-
case SOLARIS:
307-
case IBMI:
308-
case AIX:
309-
path = trimLowercaseReplaceWhitespace(application, "", true);
308+
switch (Constants.operatingSystem) {
309+
case Constants.LIN:
310+
case Constants.BSD:
311+
case Constants.SOLARIS:
312+
case Constants.IBMI:
313+
case Constants.AIX:
314+
path = Util.trimLowercaseReplaceWhitespace(application, "", true);
310315
break;
311-
case MAC:
312-
path = macOSApplicationPath(qualifier, organization, application);
316+
case Constants.MAC:
317+
path = MacOs.applicationPath(qualifier, organization, application);
313318
break;
314-
case WIN:
315-
path = windowsApplicationPath(qualifier, organization, application);
319+
case Constants.WIN:
320+
path = Windows.applicationPath(qualifier, organization, application);
316321
break;
317322
default:
318-
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + operatingSystemName);
323+
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + Constants.operatingSystemName);
319324
}
320325
return fromPath(path, getWinDirs);
321326
}
322327

323328
@Override
324329
public String toString() {
325-
return "ProjectDirectories (" + operatingSystemName + "):\n" +
330+
return "ProjectDirectories (" + Constants.operatingSystemName + "):\n" +
326331
" projectPath = '" + projectPath + "'\n" +
327332
" cacheDir = '" + cacheDir + "'\n" +
328333
" configDir = '" + configDir + "'\n" +

src/main/java/dev/dirs/UserDirectories.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dev.dirs;
22

3-
import static dev.dirs.Util.*;
3+
import dev.dirs.impl.Linux;
4+
import dev.dirs.impl.Util;
5+
import dev.dirs.impl.Windows;
46

57
/** {@code UserDirectories} provides paths of user-facing standard directories, following the conventions of the operating system the library is running on.
68
*
@@ -299,24 +301,24 @@ public static UserDirectories get() {
299301
}
300302

301303
private UserDirectories() {
302-
switch (operatingSystem) {
303-
case LIN:
304-
case BSD:
305-
case SOLARIS:
306-
case AIX:
307-
String[] userDirs = getXDGUserDirs("MUSIC", "DESKTOP", "DOCUMENTS", "DOWNLOAD", "PICTURES", "PUBLICSHARE", "TEMPLATES", "VIDEOS");
304+
switch (Constants.operatingSystem) {
305+
case Constants.LIN:
306+
case Constants.BSD:
307+
case Constants.SOLARIS:
308+
case Constants.AIX:
309+
String[] userDirs = Linux.getXDGUserDirs("MUSIC", "DESKTOP", "DOCUMENTS", "DOWNLOAD", "PICTURES", "PUBLICSHARE", "TEMPLATES", "VIDEOS");
308310
homeDir = System.getProperty("user.home");
309311
audioDir = userDirs[0];
310312
desktopDir = userDirs[1];
311313
documentDir = userDirs[2];
312314
downloadDir = userDirs[3];
313-
fontDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), "/fonts", homeDir, "/.local/share/fonts");
315+
fontDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), "/fonts", homeDir, "/.local/share/fonts");
314316
pictureDir = userDirs[4];
315317
publicDir = userDirs[5];
316318
templateDir = userDirs[6];
317319
videoDir = userDirs[7];
318320
break;
319-
case MAC:
321+
case Constants.MAC:
320322
homeDir = System.getProperty("user.home");
321323
audioDir = homeDir + "/Music";
322324
desktopDir = homeDir + "/Desktop";
@@ -328,20 +330,20 @@ private UserDirectories() {
328330
templateDir = null;
329331
videoDir = homeDir + "/Movies";
330332
break;
331-
case IBMI:
333+
case Constants.IBMI:
332334
homeDir = System.getProperty("user.home");
333335
audioDir = homeDir + "/Music";
334336
desktopDir = homeDir + "/Desktop";
335337
documentDir = homeDir + "/Documents";
336338
downloadDir = homeDir + "/Downloads";
337-
fontDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), "/fonts", homeDir, "/.local/share/fonts");
339+
fontDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), "/fonts", homeDir, "/.local/share/fonts");
338340
pictureDir = homeDir + "/Pictures";
339341
publicDir = homeDir + "/Public";
340342
templateDir = null;
341343
videoDir = homeDir + "/Movies";
342344
break;
343-
case WIN:
344-
String[] winDirs = getWinDirs(
345+
case Constants.WIN:
346+
String[] winDirs = Windows.getWinDirs(
345347
"5E6C858F-0E22-4760-9AFE-EA3317B67173",
346348
"4BD8D571-6D19-48D3-BE97-422220080E43",
347349
"B4BFCC3A-DB2C-424C-B029-7FE99A87C641",
@@ -363,13 +365,13 @@ private UserDirectories() {
363365
videoDir = winDirs[8];
364366
break;
365367
default:
366-
throw new UnsupportedOperatingSystemException("User directories are not supported on " + operatingSystemName);
368+
throw new UnsupportedOperatingSystemException("User directories are not supported on " + Constants.operatingSystemName);
367369
}
368370
}
369371

370372
@Override
371373
public String toString() {
372-
return "UserDirectories (" + operatingSystemName + "):\n" +
374+
return "UserDirectories (" + Constants.operatingSystemName + "):\n" +
373375
" homeDir = '" + homeDir + "'\n" +
374376
" audioDir = '" + audioDir + "'\n" +
375377
" fontDir = '" + fontDir + "'\n" +

0 commit comments

Comments
 (0)