Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Experimental version of using GraalVM to statically compile the XMLValidator to native binaries for macOS, Linux and Windows #40

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/build-with-graalvm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: GraalVM Native Image builds
on: [push, pull_request]
jobs:
build:
name: XMLValidator on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
steps:
- uses: actions/checkout@v4

- uses: graalvm/setup-graalvm@v1
with:
java-version: '21'
distribution: 'graalvm'
github-token: ${{ secrets.GITHUB_TOKEN }}
native-image-job-reports: 'true'

- name: Build and run XMLValidator.java
run: |
cd support
javac XMLValidator.java
native-image -march=compatibility XMLValidator -o dist/XMLValidator-${{ matrix.os }}
stat dist/XMLValidator

- name: Commit and push binaries
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
git pull
git add .
git commit -m "build: update binaries for ${{ matrix.os }}"
git push

- name: Upload binaries
uses: actions/upload-artifact@v2
with:
name: XMLValidator-${{ matrix.os }}
path: support/dist/XMLValidator*
if-no-files-found: error
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": false
}
37 changes: 27 additions & 10 deletions lib/validator.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
const fs = require('fs');
const os = require('os');

// const os = require('os');
const path = require('path');

const which = require('which');

const spawn = require('child_process').spawn;

const CLASSPATH_SEPARATOR = os.platform() === 'win32' ? ';' : ':';
// const CLASSPATH_SEPARATOR = os.platform() === 'win32' ? ';' : ':';

const BASE_DIR = path.resolve(__dirname + '/../');

const VALIDATOR = path.resolve(__dirname + '/../support/XMLValidator');

const NATIVE_VALIDATOR_OS = process.platform === 'win32' ? 'windows' : process.platform === 'darwin' ? 'macos' : process.platform === 'linux' ? 'ubuntu' : undefined;

if (!NATIVE_VALIDATOR_OS) {
throw new Error(`unsupported platform ${process.platform}`);
}

const NATIVE_VALIDATOR = `XMLValidator-${NATIVE_VALIDATOR_OS}-latest${NATIVE_VALIDATOR_OS === 'windows' ? '.exe' : ''}`;


async function withJava(executable, debug) {

if (debug) {
Expand Down Expand Up @@ -133,14 +143,21 @@ Validator.prototype.validateXML = async function(xml, schema) {

return new Promise((resolve, reject) => {

const validator = spawn(java, [
'-Dfile.encoding=UTF-8',
'-classpath',
[ BASE_DIR, cwd ].join(CLASSPATH_SEPARATOR),
'support.XMLValidator',
input.file ? '-file=' + input.file : '-stdin',
'-schema=' + schema
], { cwd: cwd });
// const validator = spawn(java, [
// '-Dfile.encoding=UTF-8',
// '-classpath',
// [ BASE_DIR, cwd ].join(CLASSPATH_SEPARATOR),
// 'support.XMLValidator',
// input.file ? '-file=' + input.file : '-stdin',
// '-schema=' + schema
// ], { cwd: cwd });


const validator = spawn(
path.join(BASE_DIR, 'support', 'dist', NATIVE_VALIDATOR),
[ input.file ? '-file=' + input.file : '-stdin', '-schema=' + schema ],
{ cwd: cwd }
);

let result,
code,
Expand Down
2 changes: 1 addition & 1 deletion support/XMLValidator.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package support;
// package support;

import java.io.File;
import java.io.FileInputStream;
Expand Down
Empty file added support/dist/.gitkeep
Empty file.
Binary file added support/dist/XMLValidator-macos-latest
Binary file not shown.
Binary file added support/dist/XMLValidator-ubuntu-latest
Binary file not shown.
Binary file added support/dist/XMLValidator-windows-latest.exe
Binary file not shown.
Loading