Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit 481c0cb

Browse files
committed
use Java 22's foreign function API for Windows
1 parent c123afd commit 481c0cb

15 files changed

+548
-496
lines changed

.appveyor.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ on:
88

99
jobs:
1010
build:
11+
strategy:
12+
matrix:
13+
os:
14+
- windows-latest
15+
- ubuntu-latest
1116

12-
runs-on: ubuntu-latest
17+
runs-on: ${{ matrix.os }}
1318

1419
steps:
15-
- uses: actions/checkout@v2
16-
- name: Set up JDK 11
20+
- uses: actions/checkout@v4
21+
- name: Set up JDK 22
1722
uses: actions/setup-java@v2
1823
with:
19-
java-version: '11'
20-
distribution: 'adopt'
24+
java-version: '22'
25+
distribution: 'temurin'
2126
- name: Run tests
2227
run: sbt test

build.sbt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ lazy val root = (project in file("."))
2222
else Some("releases" at nexus + "service/local/staging/deploy/maven2")
2323
},
2424
*/
25-
Compile / packageBin / packageOptions += {
26-
import java.util.jar.{Attributes, Manifest}
27-
val manifest = new Manifest
28-
manifest.getMainAttributes.put(new Attributes.Name("Automatic-Module-Name"), "dev.dirs")
29-
Package.JarManifest(manifest)
30-
},
3125
pomIncludeRepository := { _ => false },
3226
pomExtra :=
3327
<scm>

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.9.8
1+
sbt.version=1.10.1

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

Lines changed: 21 additions & 20 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,25 +274,24 @@ 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-
homeDir = winDirs[0];
278-
dataDir = winDirs[1];
279-
dataLocalDir = winDirs[2];
277+
case Constants.WIN:
278+
homeDir = Windows.getProfileDir();;
279+
dataDir = Windows.getRoamingAppDataDir();;
280+
dataLocalDir = Windows.getLocalAppDataDir();;
280281
configDir = dataDir;
281282
cacheDir = dataLocalDir;
282283
executableDir = null;
283284
preferenceDir = configDir;
284285
runtimeDir = null;
285286
break;
286287
default:
287-
throw new UnsupportedOperatingSystemException("Base directories are not supported on " + operatingSystemName);
288+
throw new UnsupportedOperatingSystemException("Base directories are not supported on " + Constants.operatingSystemName);
288289
}
289290
}
290291

291292
@Override
292293
public String toString() {
293-
return "BaseDirectories (" + operatingSystemName + "):\n" +
294+
return "BaseDirectories (" + Constants.operatingSystemName + "):\n" +
294295
" homeDir = '" + homeDir + "'\n" +
295296
" cacheDir = '" + cacheDir + "'\n" +
296297
" configDir = '" + configDir + "'\n" +

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
public static final String UTF8_BOM = "\ufeff";
18+
19+
static {
20+
final String os = operatingSystemName.toLowerCase(Locale.ROOT);
21+
if (os.contains("linux"))
22+
operatingSystem = LIN;
23+
else if (os.contains("mac"))
24+
operatingSystem = MAC;
25+
else if (os.contains("windows"))
26+
operatingSystem = WIN;
27+
else if (os.contains("bsd"))
28+
operatingSystem = BSD;
29+
else if (os.contains("sunos"))
30+
operatingSystem = SOLARIS;
31+
else if (os.contains("os/400") || os.contains("os400"))
32+
operatingSystem = IBMI;
33+
else if (os.contains("aix"))
34+
operatingSystem = AIX;
35+
else
36+
throw new UnsupportedOperatingSystemException("directories are not supported on " + operatingSystemName);
37+
}
38+
39+
}

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

Lines changed: 36 additions & 32 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;
@@ -230,40 +235,39 @@ public static ProjectDirectories fromPath(String path) {
230235
String dataLocalDir;
231236
String preferenceDir;
232237
String runtimeDir = null;
233-
switch (operatingSystem) {
234-
case LIN:
235-
case BSD:
236-
case SOLARIS:
237-
case IBMI:
238-
case AIX:
238+
switch (Constants.operatingSystem) {
239+
case Constants.LIN:
240+
case Constants.BSD:
241+
case Constants.SOLARIS:
242+
case Constants.IBMI:
243+
case Constants.AIX:
239244
homeDir = System.getProperty("user.home");
240-
cacheDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_CACHE_HOME"), path, homeDir + "/.cache/", path);
241-
configDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_CONFIG_HOME"), path, homeDir + "/.config/", path);
242-
dataDir = defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), path, homeDir + "/.local/share/", path);
245+
cacheDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_CACHE_HOME"), path, homeDir + "/.cache/", path);
246+
configDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_CONFIG_HOME"), path, homeDir + "/.config/", path);
247+
dataDir = Util.defaultIfNullOrEmptyExtended(System.getenv("XDG_DATA_HOME"), path, homeDir + "/.local/share/", path);
243248
dataLocalDir = dataDir;
244249
preferenceDir = configDir;
245-
runtimeDir = linuxRuntimeDir(path);
250+
runtimeDir = Linux.runtimeDir(path);
246251
break;
247-
case MAC:
252+
case Constants.MAC:
248253
homeDir = System.getProperty("user.home");
249254
cacheDir = homeDir + "/Library/Caches/" + path;
250255
configDir = homeDir + "/Library/Application Support/" + path;
251256
dataDir = homeDir + "/Library/Application Support/" + path;
252257
dataLocalDir = dataDir;
253258
preferenceDir = homeDir + "/Library/Preferences/" + path;
254259
break;
255-
case WIN:
256-
String[] winDirs = getWinDirs("3EB685DB-65F9-4CF6-A03A-E3EF65729F3D", "F1B32785-6FBA-4FCF-9D55-7B8E7F157091");
257-
String appDataRoaming = winDirs[0] + '\\' + path;
258-
String appDataLocal = winDirs[1] + '\\' + path;
260+
case Constants.WIN:
261+
String appDataRoaming = Windows.getRoamingAppDataDir() + '\\' + path;
262+
String appDataLocal = Windows.getLocalAppDataDir() + '\\' + path;
259263
dataDir = appDataRoaming + "\\data";
260264
dataLocalDir = appDataLocal + "\\data";
261265
configDir = appDataRoaming + "\\config";
262266
cacheDir = appDataLocal + "\\cache";
263267
preferenceDir = configDir;
264268
break;
265269
default:
266-
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + operatingSystemName);
270+
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + Constants.operatingSystemName);
267271
}
268272
return new ProjectDirectories(path, cacheDir, configDir, dataDir, dataLocalDir, preferenceDir, runtimeDir);
269273
}
@@ -289,32 +293,32 @@ public static ProjectDirectories fromPath(String path) {
289293
* {@code qualifier}, {@code organization} and {@code application} arguments.
290294
*/
291295
public static ProjectDirectories from(String qualifier, String organization, String application) {
292-
if (isNullOrEmpty(organization) && isNullOrEmpty(application))
296+
if (Util.isNullOrEmpty(organization) && Util.isNullOrEmpty(application))
293297
throw new UnsupportedOperationException("organization and application arguments cannot both be null/empty");
294298
String path;
295-
switch (operatingSystem) {
296-
case LIN:
297-
case BSD:
298-
case SOLARIS:
299-
case IBMI:
300-
case AIX:
301-
path = trimLowercaseReplaceWhitespace(application, "", true);
299+
switch (Constants.operatingSystem) {
300+
case Constants.LIN:
301+
case Constants.BSD:
302+
case Constants.SOLARIS:
303+
case Constants.IBMI:
304+
case Constants.AIX:
305+
path = Util.trimLowercaseReplaceWhitespace(application, "", true);
302306
break;
303-
case MAC:
304-
path = macOSApplicationPath(qualifier, organization, application);
307+
case Constants.MAC:
308+
path = MacOs.applicationPath(qualifier, organization, application);
305309
break;
306-
case WIN:
307-
path = windowsApplicationPath(qualifier, organization, application);
310+
case Constants.WIN:
311+
path = Windows.applicationPath(qualifier, organization, application);
308312
break;
309313
default:
310-
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + operatingSystemName);
314+
throw new UnsupportedOperatingSystemException("Project directories are not supported on " + Constants.operatingSystemName);
311315
}
312316
return fromPath(path);
313317
}
314318

315319
@Override
316320
public String toString() {
317-
return "ProjectDirectories (" + operatingSystemName + "):\n" +
321+
return "ProjectDirectories (" + Constants.operatingSystemName + "):\n" +
318322
" projectPath = '" + projectPath + "'\n" +
319323
" cacheDir = '" + cacheDir + "'\n" +
320324
" configDir = '" + configDir + "'\n" +

0 commit comments

Comments
 (0)