Skip to content

Commit cd43979

Browse files
committed
feat: Java bindings
Signed-off-by: Dmitry Dygalo <[email protected]>
1 parent a487b68 commit cd43979

File tree

18 files changed

+1252
-8
lines changed

18 files changed

+1252
-8
lines changed

.github/workflows/build.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,37 @@ jobs:
438438
set -e
439439
yarn test
440440
441+
test-java:
442+
env:
443+
JAVA_VERSION: "21"
444+
strategy:
445+
fail-fast: false
446+
matrix:
447+
os: [ubuntu-22.04, macos-13, windows-2022]
448+
449+
name: Java ${{ env.JAVA_VERSION }} on ${{ matrix.os }}
450+
runs-on: ${{ matrix.os }}
451+
steps:
452+
- uses: actions/checkout@v4
453+
454+
- name: Build native library
455+
# Build with `--release` as Gradle config expects `target/release`
456+
run: cargo build --release
457+
working-directory: bindings/java
458+
459+
- name: Set up Java
460+
uses: actions/setup-java@v4
461+
with:
462+
distribution: "temurin"
463+
java-version: ${{ env.JAVA_VERSION }}
464+
465+
- name: Setup Gradle
466+
uses: gradle/actions/setup-gradle@v4
467+
468+
- name: Run tests
469+
working-directory: bindings/java
470+
run: gradle clean test
471+
441472
test-python:
442473
strategy:
443474
fail-fast: false

.github/workflows/java-release.yml

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
name: "[Java] Release"
2+
3+
on:
4+
pull_request: {}
5+
push:
6+
tags:
7+
- java-v*
8+
9+
defaults:
10+
run:
11+
shell: bash
12+
13+
concurrency:
14+
group: ${{ github.workflow }}-${{ github.ref }}
15+
cancel-in-progress: true
16+
17+
env:
18+
JAVA_VERSION: "21"
19+
20+
jobs:
21+
build-native-libraries:
22+
strategy:
23+
matrix:
24+
include:
25+
- os: ubuntu-22.04
26+
target: x86_64-unknown-linux-gnu
27+
lib: libcss_inline.so
28+
platform: linux-x86_64
29+
30+
- os: macos-13
31+
target: x86_64-apple-darwin
32+
lib: libcss_inline.dylib
33+
platform: darwin-x86_64
34+
- os: macos-14
35+
target: aarch64-apple-darwin
36+
lib: libcss_inline.dylib
37+
platform: darwin-aarch64
38+
39+
- os: windows-2022
40+
target: x86_64-pc-windows-msvc
41+
lib: css_inline.dll
42+
platform: win32-x86_64
43+
44+
name: Build native library for ${{ matrix.target }}
45+
runs-on: ${{ matrix.os }}
46+
steps:
47+
- uses: actions/checkout@v4
48+
49+
- uses: dtolnay/rust-toolchain@stable
50+
with:
51+
targets: ${{ matrix.target }}
52+
53+
- name: Build native library
54+
working-directory: bindings/java
55+
run: cargo build --release --target ${{ matrix.target }}
56+
57+
- name: Upload native library
58+
uses: actions/upload-artifact@v4
59+
with:
60+
name: native-${{ matrix.platform }}
61+
if-no-files-found: error
62+
path: bindings/java/target/${{ matrix.target }}/release/${{ matrix.lib }}
63+
64+
build-jar:
65+
name: Build JAR file
66+
runs-on: ubuntu-22.04
67+
needs: build-native-libraries
68+
steps:
69+
- uses: actions/checkout@v4
70+
71+
- name: Set up Java
72+
uses: actions/setup-java@v4
73+
with:
74+
distribution: "temurin"
75+
java-version: ${{ env.JAVA_VERSION }}
76+
77+
- name: Download all native libraries
78+
uses: actions/download-artifact@v4
79+
with:
80+
path: native-libs
81+
82+
- name: Assemble JAR with native libraries
83+
working-directory: bindings/java
84+
run: |
85+
mkdir -p src/main/resources/org/cssinline/native/{linux-x86_64,darwin-x86_64,darwin-aarch64,win32-x86_64}
86+
87+
# Copy native libraries to their expected location
88+
cp ../../native-libs/native-linux-x86_64/libcss_inline.so src/main/resources/org/cssinline/native/linux-x86_64/
89+
cp ../../native-libs/native-darwin-x86_64/libcss_inline.dylib src/main/resources/org/cssinline/native/darwin-x86_64/
90+
cp ../../native-libs/native-darwin-aarch64/libcss_inline.dylib src/main/resources/org/cssinline/native/darwin-aarch64/
91+
cp ../../native-libs/native-win32-x86_64/css_inline.dll src/main/resources/org/cssinline/native/win32-x86_64/
92+
93+
# This one will also run tests
94+
gradle build
95+
96+
- name: Upload JAR
97+
uses: actions/upload-artifact@v4
98+
with:
99+
name: java-jar
100+
if-no-files-found: error
101+
path: bindings/java/build/libs/*.jar
102+
103+
test-jar:
104+
needs: build-jar
105+
strategy:
106+
matrix:
107+
include:
108+
- os: ubuntu-22.04
109+
platform: linux-x86_64
110+
- os: macos-13
111+
platform: darwin-x86_64
112+
- os: macos-14
113+
platform: darwin-aarch64
114+
- os: windows-2022
115+
platform: win32-x86_64
116+
117+
name: Test JAR on ${{ matrix.platform }}
118+
runs-on: ${{ matrix.os }}
119+
steps:
120+
- name: Set up Java
121+
uses: actions/setup-java@v4
122+
with:
123+
distribution: "temurin"
124+
java-version: ${{ env.JAVA_VERSION }}
125+
126+
- name: Download JAR
127+
uses: actions/download-artifact@v4
128+
with:
129+
name: java-jar
130+
131+
- name: Integration test on ${{ matrix.platform }}
132+
run: |
133+
cat > Test.java << 'EOF'
134+
import org.cssinline.CssInline;
135+
public class Test {
136+
public static void main(String[] args) {
137+
try {
138+
String html = "<html><head><style>h1{color:red}</style></head><body><h1>Test</h1></body></html>";
139+
String result = CssInline.inline(html);
140+
if (!result.contains("style=\"color: red;\"")) {
141+
System.err.println("Expected inlined style not found in: " + result);
142+
System.exit(1);
143+
}
144+
System.out.println("✓ Integration test passed on ${{ matrix.platform }}");
145+
} catch (Exception e) {
146+
System.err.println("Integration test failed: " + e.getMessage());
147+
e.printStackTrace();
148+
System.exit(1);
149+
}
150+
}
151+
}
152+
EOF
153+
154+
JAR_FILE=$(ls *.jar)
155+
156+
if [[ "$RUNNER_OS" == "Windows" ]]; then
157+
CLASSPATH_SEP=";"
158+
else
159+
CLASSPATH_SEP=":"
160+
fi
161+
162+
javac -cp "$JAR_FILE" Test.java
163+
java -cp "$JAR_FILE${CLASSPATH_SEP}." Test

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ into:
4141
- Optionally caches external stylesheets
4242
- Works on Linux, Windows, and macOS
4343
- Supports HTML5 & CSS3
44-
- Bindings for [Python](https://github.com/Stranger6667/css-inline/tree/master/bindings/python), [Ruby](https://github.com/Stranger6667/css-inline/tree/master/bindings/ruby), [JavaScript](https://github.com/Stranger6667/css-inline/tree/master/bindings/javascript), [C](https://github.com/Stranger6667/css-inline/tree/master/bindings/c), and a [WebAssembly](https://github.com/Stranger6667/css-inline/tree/master/bindings/javascript/wasm) module to run in browsers.
44+
- Bindings for [Python](https://github.com/Stranger6667/css-inline/tree/master/bindings/python), [Ruby](https://github.com/Stranger6667/css-inline/tree/master/bindings/ruby), [JavaScript](https://github.com/Stranger6667/css-inline/tree/master/bindings/javascript), [Java](https://github.com/Stranger6667/css-inline/tree/master/bindings/java), [C](https://github.com/Stranger6667/css-inline/tree/master/bindings/c), and a [WebAssembly](https://github.com/Stranger6667/css-inline/tree/master/bindings/javascript/wasm) module to run in browsers.
4545
- Command Line Interface
4646

4747
## Playground

bindings/java/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.gradle/
2+
build/

bindings/java/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## Unreleased
4+
5+
- Initial public release

bindings/java/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "css_inline"
3+
version = "0.15.0"
4+
edition = "2024"
5+
authors = ["Dmitry Dygalo <[email protected]>"]
6+
7+
[lib]
8+
crate-type = ["cdylib"]
9+
path = "src/main/rust/lib.rs"
10+
11+
[dependencies]
12+
jni = "0.21.1"
13+
14+
[dependencies.css-inline]
15+
path = "../../css-inline"
16+
version = "*"
17+
default-features = false
18+
features = ["http", "file", "stylesheet-cache"]

0 commit comments

Comments
 (0)