Skip to content

Commit 3435479

Browse files
authored
Merge pull request #61 from SWAT-engineering/feat/use-rust-and-jni-on-mac-wip
Using JNI+Rust instead of JNA on macOS
2 parents c47f6a0 + 9ee1391 commit 3435479

File tree

20 files changed

+983
-754
lines changed

20 files changed

+983
-754
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ end_of_line = lf
1515
[*.java]
1616
indent_size = 4
1717
max_line_length = 120
18+
19+
[*.rs]
20+
indent_size = 4

.github/workflows/build.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ jobs:
3232
java-version: ${{ matrix.jdk }}
3333
distribution: 'temurin'
3434
cache: 'maven'
35+
#- uses: actions-rust-lang/setup-rust-toolchain@v1
36+
- run: ./update-rust-jni-libs.sh -r
37+
if: ${{ matrix.os.image == 'macos-latest' }}
3538

3639
- name: test
3740
run: mvn -B clean test "-Dwatch.mac.backend=${{ matrix.os.mac-backend }}"

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ replay_pid*
2727

2828
# release plugin state files
2929
/pom.xml.releaseBackup
30-
/release.properties
30+
/release.properties

pom.xml

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,20 @@
7474
<checkerframework.version>3.49.5</checkerframework.version>
7575
<junit.version>5.13.4</junit.version>
7676
<log4j.version>2.25.1</log4j.version>
77-
<jna.version>5.17.0</jna.version>
7877
<maven.compiler.source>11</maven.compiler.source>
7978
<maven.compiler.target>11</maven.compiler.target>
8079
<watch.mac.backend>fsevents</watch.mac.backend>
8180
</properties>
8281

8382
<build>
83+
<resources>
84+
<resource>
85+
<directory>src/main/resources</directory>
86+
<excludes>
87+
<exclude>src/main/rust/**/*.*</exclude>
88+
</excludes>
89+
</resource>
90+
</resources>
8491
<plugins>
8592
<plugin> <!-- configure java compiler -->
8693
<groupId>org.apache.maven.plugins</groupId>
@@ -147,6 +154,9 @@
147154
</excludes>
148155
</licenseSet>
149156
</licenseSets>
157+
<mapping>
158+
<rs>DOUBLESLASH_STYLE</rs> <!-- Use same style for license headers in Rust files as in https://github.com/rust-lang/style-team/blob/main/example/lists.rs -->
159+
</mapping>
150160
</configuration>
151161
<executions>
152162
<execution>
@@ -170,6 +180,11 @@
170180
</goals>
171181
</execution>
172182
</executions>
183+
<configuration>
184+
<excludes>
185+
<exclude>src/main/resources/**</exclude>
186+
</excludes>
187+
</configuration>
173188
</plugin>
174189
<plugin> <!-- use a new version of maven -->
175190
<groupId>org.apache.maven.plugins</groupId>
@@ -229,16 +244,6 @@
229244
<version>${log4j.version}</version>
230245
<scope>test</scope>
231246
</dependency>
232-
<dependency>
233-
<groupId>net.java.dev.jna</groupId>
234-
<artifactId>jna</artifactId>
235-
<version>${jna.version}</version>
236-
</dependency>
237-
<dependency>
238-
<groupId>net.java.dev.jna</groupId>
239-
<artifactId>jna-platform</artifactId>
240-
<version>${jna.version}</version>
241-
</dependency>
242247
</dependencies>
243248

244249
<profiles>

src/main/java/engineering/swat/watch/impl/jdk/JDKPoller.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757
import engineering.swat.watch.DaemonThreadPool;
5858
import engineering.swat.watch.impl.mac.MacWatchService;
59+
import engineering.swat.watch.impl.mac.NativeLibrary;
5960
import engineering.swat.watch.impl.util.SubscriptionKey;
6061

6162
/**
@@ -189,7 +190,7 @@ public Watchable newWatchable(Path path) {
189190
static final Platform CURRENT = current(); // Assumption: the platform doesn't change
190191

191192
private static Platform current() {
192-
if (com.sun.jna.Platform.isMac()) {
193+
if (NativeLibrary.isMac()) {
193194
var key = "engineering.swat.java-watch.mac";
194195
var val = System.getProperty(key);
195196
if (val != null) {

src/main/java/engineering/swat/watch/impl/mac/NativeEventHandler.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@
2626
*/
2727
package engineering.swat.watch.impl.mac;
2828

29+
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
30+
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
31+
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
32+
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;
33+
34+
import java.nio.file.Path;
2935
import java.nio.file.WatchEvent;
30-
import java.nio.file.WatchEvent.Kind;
3136

3237
import org.checkerframework.checker.nullness.qual.Nullable;
3338

@@ -51,5 +56,36 @@
5156
*/
5257
@FunctionalInterface
5358
interface NativeEventHandler {
54-
<T> void handle(Kind<T> kind, @Nullable T context);
59+
<T> void handle(java.nio.file.WatchEvent.Kind<T> kind, @Nullable T context);
60+
61+
default void handle(int kindOrdinal, String rootPath, String relativePath) {
62+
if (kindOrdinal < Kind.values().length) {
63+
var kind = Kind.values()[kindOrdinal];
64+
switch (kind) {
65+
case CREATE:
66+
handle(ENTRY_CREATE, toContext(rootPath, relativePath));
67+
break;
68+
case MODIFY:
69+
handle(ENTRY_MODIFY, toContext(rootPath, relativePath));
70+
break;
71+
case DELETE:
72+
handle(ENTRY_DELETE, toContext(rootPath, relativePath));
73+
break;
74+
case OVERFLOW:
75+
handle(OVERFLOW, null);
76+
break;
77+
}
78+
}
79+
}
80+
81+
static Path toContext(String rootPath, String relativePath) {
82+
return Path.of(rootPath).relativize(Path.of(relativePath));
83+
}
84+
}
85+
86+
enum Kind { // Order of values needs to be consistent with enum `Kind` in Rust
87+
OVERFLOW,
88+
CREATE,
89+
DELETE,
90+
MODIFY;
5591
}

0 commit comments

Comments
 (0)