Create Python version; fix Rust code sample on README

This commit is contained in:
Wilson Lin 2020-01-19 16:50:21 +11:00
parent 6b2b8cf552
commit 7940fae01b
8 changed files with 135 additions and 7 deletions

58
.github/workflows/python.yml vendored Normal file
View File

@ -0,0 +1,58 @@
name: Build and upload Python native module
on:
create:
tags:
- 'v*'
jobs:
nodejs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
include:
- os: ubuntu-latest
ARCH: linux-x86_64
LIBFILE: 'libhyperbuild_python_lib.so'
PYEXT: 'so'
- os: windows-latest
ARCH: windows-x86_64
LIBFILE: 'hyperbuild_python_lib.dll'
PYEXT: 'pyd'
- os: macos-latest
ARCH: macos-x86_64
LIBFILE: 'libhyperbuild_python_lib.dylib'
PYEXT: 'so'
steps:
- uses: actions/checkout@v1
- name: Get version
id: version
shell: bash
run: echo ::set-output name=VERSION::${GITHUB_REF#refs/tags/v}
- name: Get file name
id: file
shell: bash
run: echo ::set-output name=FILE::${{ steps.version.outputs.VERSION }}-${{ matrix.ARCH }}-python.${{ matrix.PYEXT }}
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: '3.5'
architecture: 'x64'
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2019-07-19
profile: minimal
default: true
- name: Build native module
working-directory: ./python
run: |
cargo build --release
- uses: chrislennon/action-aws-cli@v1.1
- name: Upload to S3
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: us-west-2
run: aws s3 cp ./python/target/release/${{ matrix.LIBFILE }} s3://${{ secrets.AWS_S3_BUCKET }}/hyperbuild/bin/${{ steps.file.outputs.FILE }}

View File

@ -53,10 +53,10 @@ hyperbuild = "0.0.19"
use hyperbuild::hyperbuild;
fn main() {
let mut code = b"<p> Hello, world! </p>";
let mut code = b"<p> Hello, world! </p>".to_vec();
match hyperbuild(&mut code) {
Ok(minified_len) => {}
Err(error_type, error_at_char_no) => {}
Err((error_type, error_at_char_no)) => {}
};
}
```
@ -66,7 +66,7 @@ fn main() {
<details>
<summary><strong>Node.js</strong></summary>
hyperbuild is available as a [Node.js native module](https://www.npmjs.com/package/hyperbuild), and supports Node.js versions 8 and higher.
hyperbuild is [on npm](https://www.npmjs.com/package/hyperbuild), available as a [Node.js native module](https://neon-bindings.com/), and supports Node.js versions 8 and higher.
##### Get
@ -95,7 +95,7 @@ const minified = hyperbuild.minify("<p> Hello, world! </p>");
<details>
<summary><strong>Java</strong></summary>
hyperbuild is available via JNI, and supports Java versions 7 and higher.
hyperbuild is available via [JNI](https://github.com/jni-rs/jni-rs), and supports Java versions 7 and higher.
##### Get
@ -117,6 +117,29 @@ class Main {
</details>
<details>
<summary><strong>Java</strong></summary>
hyperbuild is available as a [native module](https://github.com/PyO3/pyo3), and supports CPython (default Python interpreter) versions 3.5 and higher.
##### Get
Download the native module for [Windows](https://wilsonl.in/hyperbuild/bin/0.0.19-windows-x86_64-python.pyd), [macOS](https://wilsonl.in/hyperbuild/bin/0.0.19-macos-x86_64-python.so), or [Linux](https://wilsonl.in/hyperbuild/bin/0.0.19-linux-x86_64-python.so).
Rename the file to `hyperbuild.pyd` on Windows or `hyperbuild.so` on macOS/Linux.
##### Use
Make sure the native module can be [found by Python](https://docs.python.org/3/tutorial/modules.html#the-module-search-path). This is usually done by placing it into a folder declared in the `PYTHONPATH` environment variable or `sys.path` value, or the same folder as the script that will import it.
```java
import hyperbuild;
minified = hyperbuild.minify("<p> Hello, world! </p>")
```
</details>
## Minification
### Whitespace

View File

@ -1,5 +1,6 @@
[package]
name = "hyperbuild-java"
publish = false
version = "0.0.19"
authors = ["Wilson Lin <code@wilsonl.in>"]
edition = "2018"

View File

@ -39,7 +39,7 @@ pub extern "system" fn Java_in_wilsonl_hyperbuild_Hyperbuild_minify(
)
-> jstring {
let source: String = env.get_string(input).unwrap().into();
let mut code = source.as_bytes().to_vec();
let mut code = source.into_bytes();
match hyperbuild(&mut code) {
Ok(out_len) => env.new_string(unsafe { from_utf8_unchecked(&code[0..out_len]) }).unwrap().into_inner(),

2
python/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/Cargo.lock
/target

23
python/Cargo.toml Normal file
View File

@ -0,0 +1,23 @@
[package]
name = "hyperbuild-python"
publish = false
version = "0.0.19"
authors = ["Wilson Lin <code@wilsonl.in>"]
edition = "2018"
[lib]
name = "hyperbuild_python_lib"
crate-type = ["cdylib"]
[dependencies]
hyperbuild = "0.0.19"
[dependencies.pyo3]
version = "0.9.0-alpha.1"
features = ["extension-module"]
[target.x86_64-apple-darwin]
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]

21
python/src/lib.rs Normal file
View File

@ -0,0 +1,21 @@
use hyperbuild::hyperbuild as hyperbuild_native;
use pyo3::prelude::*;
use pyo3::exceptions::SyntaxError;
use pyo3::wrap_pyfunction;
use std::str::from_utf8_unchecked;
#[pyfunction]
fn minify(code: String) -> PyResult<String> {
let mut code = code.into_bytes();
match hyperbuild_native(&mut code) {
Ok(out_len) => Ok(unsafe { from_utf8_unchecked(&code[0..out_len]).to_string() }),
Err((err, pos)) => Err(SyntaxError::py_err(format!("{} [Character {}]", err.message(), pos))),
}
}
#[pymodule]
fn hyperbuild(py: Python, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(minify))?;
Ok(())
}

View File

@ -48,11 +48,11 @@ if (cmd('git', 'status', '--porcelain', {throwOnStderr: true, captureStdio: true
throw new Error(`Working directory not clean`);
}
for (const f of ["Cargo.toml", "nodejs/native/Cargo.toml", "java/Cargo.toml"]) {
for (const f of ["Cargo.toml", "nodejs/native/Cargo.toml", "java/Cargo.toml", "python/Cargo.toml"]) {
replaceInFile(f, /^version = "\d+\.\d+\.\d+"\s*$/m, `version = "${NEW_VERSION}"`);
}
for (const f of ["README.md", "nodejs/native/Cargo.toml", "java/Cargo.toml"]) {
for (const f of ["README.md", "nodejs/native/Cargo.toml", "java/Cargo.toml", "python/Cargo.toml"]) {
replaceInFile(f, /^hyperbuild = "\d+\.\d+\.\d+"\s*$/m, `hyperbuild = "${NEW_VERSION}"`);
}