Skip to content

Commit 821d687

Browse files
authored
feat: Java bindings (#481)
1 parent a487b68 commit 821d687

File tree

19 files changed

+1429
-8
lines changed

19 files changed

+1429
-8
lines changed

.github/workflows/build.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ on:
77
- master
88
schedule: [cron: "40 1 * * *"]
99

10+
env:
11+
JAVA_VERSION: "17"
12+
1013
jobs:
1114
commitsar:
1215
name: Verify commit messages
@@ -438,6 +441,35 @@ jobs:
438441
set -e
439442
yarn test
440443
444+
test-java:
445+
strategy:
446+
fail-fast: false
447+
matrix:
448+
os: [ubuntu-22.04, macos-13, windows-2022]
449+
450+
name: Java 17 on ${{ matrix.os }}
451+
runs-on: ${{ matrix.os }}
452+
steps:
453+
- uses: actions/checkout@v4
454+
455+
- name: Build native library
456+
# Build with `--release` as Gradle config expects `target/release`
457+
run: cargo build --release
458+
working-directory: bindings/java
459+
460+
- name: Set up Java
461+
uses: actions/setup-java@v4
462+
with:
463+
distribution: "temurin"
464+
java-version: ${{ env.JAVA_VERSION }}
465+
466+
- name: Setup Gradle
467+
uses: gradle/actions/setup-gradle@v4
468+
469+
- name: Run tests
470+
working-directory: bindings/java
471+
run: gradle clean test
472+
441473
test-python:
442474
strategy:
443475
fail-fast: false

.github/workflows/java-release.yml

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
name: "[Java] Release"
2+
3+
on:
4+
push:
5+
tags:
6+
- java-v*
7+
8+
defaults:
9+
run:
10+
shell: bash
11+
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.ref }}
14+
cancel-in-progress: true
15+
16+
env:
17+
JAVA_VERSION: "17"
18+
19+
jobs:
20+
build-native-libraries:
21+
strategy:
22+
matrix:
23+
include:
24+
- os: ubuntu-22.04
25+
target: x86_64-unknown-linux-gnu
26+
lib: libcss_inline.so
27+
platform: linux-x86_64
28+
29+
- os: macos-13
30+
target: x86_64-apple-darwin
31+
lib: libcss_inline.dylib
32+
platform: darwin-x86_64
33+
- os: macos-14
34+
target: aarch64-apple-darwin
35+
lib: libcss_inline.dylib
36+
platform: darwin-aarch64
37+
38+
- os: windows-2022
39+
target: x86_64-pc-windows-msvc
40+
lib: css_inline.dll
41+
platform: win32-x86_64
42+
43+
name: Build native library for ${{ matrix.target }}
44+
runs-on: ${{ matrix.os }}
45+
steps:
46+
- uses: actions/checkout@v4
47+
48+
- uses: dtolnay/rust-toolchain@stable
49+
with:
50+
targets: ${{ matrix.target }}
51+
52+
- uses: Swatinem/rust-cache@v2
53+
with:
54+
workspaces: |
55+
css-inline
56+
bindings/java
57+
58+
- name: Build native library
59+
working-directory: bindings/java
60+
run: cargo build --release --target ${{ matrix.target }}
61+
62+
- name: Upload native library
63+
uses: actions/upload-artifact@v4
64+
with:
65+
name: native-${{ matrix.platform }}
66+
if-no-files-found: error
67+
path: bindings/java/target/${{ matrix.target }}/release/${{ matrix.lib }}
68+
69+
build-jar:
70+
name: Build JAR file
71+
runs-on: ubuntu-22.04
72+
needs: build-native-libraries
73+
steps:
74+
- uses: actions/checkout@v4
75+
76+
- name: Set up Java
77+
uses: actions/setup-java@v4
78+
with:
79+
distribution: "temurin"
80+
java-version: ${{ env.JAVA_VERSION }}
81+
82+
- name: Download all native libraries
83+
uses: actions/download-artifact@v4
84+
with:
85+
path: native-libs
86+
87+
- name: Assemble JAR with native libraries
88+
working-directory: bindings/java
89+
run: |
90+
mkdir -p src/main/resources/org/cssinline/native/{linux-x86_64,darwin-x86_64,darwin-aarch64,win32-x86_64}
91+
92+
# Copy native libraries to their expected location
93+
cp ../../native-libs/native-linux-x86_64/libcss_inline.so src/main/resources/org/cssinline/native/linux-x86_64/
94+
cp ../../native-libs/native-darwin-x86_64/libcss_inline.dylib src/main/resources/org/cssinline/native/darwin-x86_64/
95+
cp ../../native-libs/native-darwin-aarch64/libcss_inline.dylib src/main/resources/org/cssinline/native/darwin-aarch64/
96+
cp ../../native-libs/native-win32-x86_64/css_inline.dll src/main/resources/org/cssinline/native/win32-x86_64/
97+
98+
gradle build --info
99+
100+
- name: Upload JAR
101+
uses: actions/upload-artifact@v4
102+
with:
103+
name: java-jar
104+
if-no-files-found: error
105+
path: bindings/java/build/libs/*.jar
106+
107+
test-jar:
108+
needs: build-jar
109+
strategy:
110+
matrix:
111+
include:
112+
- os: ubuntu-22.04
113+
platform: linux-x86_64
114+
- os: macos-13
115+
platform: darwin-x86_64
116+
- os: macos-14
117+
platform: darwin-aarch64
118+
- os: windows-2022
119+
platform: win32-x86_64
120+
121+
name: Test JAR on ${{ matrix.platform }}
122+
runs-on: ${{ matrix.os }}
123+
steps:
124+
- name: Set up Java
125+
uses: actions/setup-java@v4
126+
with:
127+
distribution: "temurin"
128+
java-version: ${{ env.JAVA_VERSION }}
129+
130+
- name: Download JAR
131+
uses: actions/download-artifact@v4
132+
with:
133+
name: java-jar
134+
135+
- name: Integration test on ${{ matrix.platform }}
136+
run: |
137+
cat > Test.java << 'EOF'
138+
import org.cssinline.CssInline;
139+
public class Test {
140+
public static void main(String[] args) {
141+
String html = "<html><head><style>h1{color:red}</style></head><body><h1>Test</h1></body></html>";
142+
String result = CssInline.inline(html);
143+
if (!result.contains("style=\"color: red;\"")) {
144+
throw new RuntimeException("Expected inlined style not found in: " + result);
145+
}
146+
System.out.println("✓ Integration test passed on ${{ matrix.platform }}");
147+
}
148+
}
149+
EOF
150+
151+
JAR_FILE=$(ls *.jar)
152+
153+
if [[ "$RUNNER_OS" == "Windows" ]]; then
154+
CLASSPATH_SEP=";"
155+
else
156+
CLASSPATH_SEP=":"
157+
fi
158+
159+
javac -cp "$JAR_FILE" Test.java
160+
java -cp "$JAR_FILE${CLASSPATH_SEP}." Test
161+
162+
publish-github-packages:
163+
name: Publish to GitHub Packages
164+
runs-on: ubuntu-22.04
165+
needs: [build-jar, test-jar]
166+
if: startsWith(github.ref, 'refs/tags/java-v')
167+
permissions:
168+
contents: read
169+
packages: write
170+
steps:
171+
- uses: actions/checkout@v4
172+
173+
- name: Set up Java
174+
uses: actions/setup-java@v4
175+
with:
176+
distribution: "temurin"
177+
java-version: ${{ env.JAVA_VERSION }}
178+
179+
- name: Download JAR artifact
180+
uses: actions/download-artifact@v4
181+
with:
182+
name: java-jar
183+
path: bindings/java/build/libs
184+
185+
- name: Extract version
186+
run: echo "version=${GITHUB_REF#refs/tags/java-v}" >> $GITHUB_ENV
187+
188+
- name: Publish to GitHub Packages
189+
working-directory: bindings/java
190+
run: gradle publish -Pversion=${{ env.version }}
191+
env:
192+
GITHUB_ACTOR: ${{ github.actor }}
193+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
194+
195+
release:
196+
name: Create GitHub Release
197+
runs-on: ubuntu-22.04
198+
needs: [build-jar, test-jar, publish-github-packages]
199+
if: startsWith(github.ref, 'refs/tags/java-v')
200+
steps:
201+
- name: Download JAR
202+
uses: actions/download-artifact@v4
203+
with:
204+
name: java-jar
205+
path: dist
206+
207+
- name: Extract version
208+
run: echo "version=${GITHUB_REF#refs/tags/java-v}" >> $GITHUB_ENV
209+
210+
- name: GitHub Release
211+
uses: softprops/action-gh-release@v2
212+
with:
213+
make_latest: false
214+
draft: true
215+
name: "[Java] Release ${{ env.version }}"
216+
files: dist/*.jar

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)