Skip to content

Commit fa48cdf

Browse files
committed
Node.js bindings
1 parent e0dea25 commit fa48cdf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+12980
-44
lines changed

.github/workflows/test.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: Run tests
22

33
on:
44
push:
5-
branches: [master, '[0-9]+.[0-9]+']
5+
branches: [master, "[0-9]+.[0-9]+"]
66
pull_request:
7-
branches: [master, '[0-9]+.[0-9]+']
7+
branches: [master, "[0-9]+.[0-9]+"]
88

99
env:
1010
RUST_LOG: debug,j4rs=warn
@@ -32,7 +32,9 @@ jobs:
3232
- name: Build test binary
3333
run: cargo test --no-run --verbose
3434
- name: Run tests
35-
run: cargo test --verbose --no-fail-fast
35+
run: |
36+
cargo test --verbose --no-fail-fast
37+
npm --prefix ./bindings/tmc-langs-node run jest
3638
3739
windows:
3840
runs-on: windows-latest
@@ -46,7 +48,9 @@ jobs:
4648
- name: Build test binary
4749
run: cargo test --no-run --verbose
4850
- name: Run tests
49-
run: cargo test --verbose --no-fail-fast
51+
run: |
52+
cargo test --verbose --no-fail-fast
53+
npm --prefix ./bindings/tmc-langs-node run jest
5054
5155
macos:
5256
runs-on: macos-latest
@@ -58,4 +62,6 @@ jobs:
5862
- name: Build test binary
5963
run: cargo test --no-run --verbose
6064
- name: Run tests
61-
run: cargo test --verbose --no-fail-fast
65+
run: |
66+
cargo test --verbose --no-fail-fast
67+
npm --prefix ./bindings/tmc-langs-node run jest

.github/workflows/upload-binaries.yml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ jobs:
2424
run: |
2525
sudo apt update
2626
sudo apt install -y gcc-multilib # required to build for 32-bit arch
27-
- name: Cargo build
27+
- name: Build
2828
run: |
29-
rustup target add i686-unknown-linux-gnu
29+
rustup target add ${{ matrix.target }}
3030
cargo build -p tmc-langs-cli --release --verbose --target ${{ matrix.target }}
31+
npm --prefix ./bindings/tmc-langs-node run build -- --release --verbose --target ${{ matrix.target }}
3132
- name: Deploy
32-
run: gsutil cp target/${{ matrix.target }}/release/tmc-langs-cli gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-cli-${{ matrix.target }}-$TAG
33+
run: |
34+
gsutil cp target/${{ matrix.target }}/release/tmc-langs-cli gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-cli-${{ matrix.target }}-$TAG
35+
gsutil cp bindings/tmc-langs-node/ts/functions.node gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-${{ matrix.target }}-$TAG.node
3336
3437
windows:
3538
runs-on: windows-latest
@@ -42,16 +45,18 @@ jobs:
4245
with:
4346
project_id: ${{ secrets.GCP_PROJECT_ID }}
4447
service_account_key: ${{ secrets.GCP_SA_KEY }}
45-
- name: Cargo build # crt-static is set with RUSTFLAGS to statically link MSVCRT (VCRUNTIME140.dll)
48+
- name: Build # crt-static is set with RUSTFLAGS to statically link MSVCRT (VCRUNTIME140.dll)
4649
run: |
47-
rustup target add i686-pc-windows-msvc
50+
rustup target add ${{ matrix.target }}
4851
$env:RUSTFLAGS="-C target-feature=+crt-static"
4952
cargo build -p tmc-langs-cli --release --verbose --target ${{ matrix.target }}
53+
npm run --prefix ./bindings/tmc-langs-node build -- --release --verbose --target ${{ matrix.target }}
5054
- name: Deploy
5155
run: |
5256
$env:python_version=$(python -c 'import sys; print(\".\".join(map(str, sys.version_info[:3])))')
5357
$env:CLOUDSDK_PYTHON="C:\hostedtoolcache\windows\Python\$env:python_version\x64\python"
5458
gsutil cp target/${{ matrix.target }}/release/tmc-langs-cli.exe gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-cli-${{ matrix.target }}-$Env:TAG.exe
59+
gsutil cp bindings/tmc-langs-node/ts/functions.node gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-${{ matrix.target }}-$Env:TAG.node
5560
5661
macos:
5762
runs-on: macos-latest
@@ -66,7 +71,9 @@ jobs:
6671
- name: Sign
6772
run: codesign --force -s - target/release/tmc-langs-cli
6873
- name: Deploy
69-
run: gsutil cp target/release/tmc-langs-cli gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-cli-x86_64-apple-darwin-$TAG
74+
run: |
75+
gsutil cp target/release/tmc-langs-cli gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-cli-x86_64-apple-darwin-$TAG
76+
gsutil cp bindings/tmc-langs-node/ts/functions.node gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-x86_64-apple-darwin-$TAG.node
7077
7178
arm:
7279
runs-on: ubuntu-18.04
@@ -84,5 +91,8 @@ jobs:
8491
rustup target add ${{ matrix.target }}
8592
cargo install cross
8693
cross build -p tmc-langs-cli --release --verbose --target ${{ matrix.target }}
94+
npm --prefix ./bindings/tmc-langs-node run cross --release --verbose --target ${{ matrix.target }}
8795
- name: Deploy
88-
run: gsutil cp target/${{ matrix.target }}/release/tmc-langs-cli gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-cli-${{ matrix.target }}-$TAG
96+
run: |
97+
gsutil cp target/${{ matrix.target }}/release/tmc-langs-cli gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-cli-${{ matrix.target }}-$TAG
98+
gsutil cp bindings/tmc-langs-node/ts/functions.node gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-${{ matrix.target }}-$TAG.node

CONTRIBUTING.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Install Rust according to https://www.rust-lang.org/tools/install
44

55
Install [zstd](https://github.com/facebook/zstd). For example, on Ubuntu you need the package `libzstd1`. For Windows, download the appropriate archive from the [releases](https://github.com/facebook/zstd/releases), extract it and add the extracted directory to your PATH.
66

7+
Install [Node.js and npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm).
8+
79
```bash
810
git clone git@github.com:rage/tmc-langs-rust.git
911
cd tmc-langs-rust
@@ -14,8 +16,13 @@ If you have any troubles building the project, please do make an issue!
1416

1517
## Testing
1618

19+
To run tests for all included projects, run the `run-tests.sh` script.
20+
```bash
21+
bash run-tests.sh
22+
```
23+
To run tests for the Node.js bindings run
1724
```bash
18-
cargo test
25+
npm --prefix bindings/tmc-langs-node/ run jest
1926
```
2027

2128
## Formatting and linting

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ members =[
1313
"plugins/notests",
1414
"plugins/python3",
1515
"plugins/r",
16+
17+
"bindings/tmc-langs-node",
1618
]
19+
resolver = "2"

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ TMC-langs provides an interface that encapsulates everything needed to support a
44

55
## Documentation
66

7-
Documentation for the latest release is available at https://rage.github.io/tmc-langs-rust
7+
Documentation for the latest release is available at https://rage.github.io/tmc-langs-rust. Documentation is automatically built and deployed when creating a GitHub release.
88

99
Additional documentation for other aspects of TMC such as configuration file formats is included in the [docs](./docs) directory.
1010

@@ -14,6 +14,10 @@ Additional documentation for other aspects of TMC such as configuration file for
1414

1515
A "frontend" for tmc-langs. A binary CLI client for TMC-langs for IDEs. Intended to be used programmatically, for a CLI meant for manual use see [tmc-cli-rust](https://github.com/rage/tmc-cli-rust).
1616

17+
### bindings/tmc-langs-node
18+
19+
Bindings for using tmc-langs from Node.js.
20+
1721
### tmc-langs
1822

1923
The "backend". A library that provides a convenient API for implementing different frontends. A frontend (such as a CLI) should only depend on this library. The other libraries are considered internal.
@@ -69,6 +73,7 @@ A TMC plugin for R.
6973
- Armv7 (armv7-unknown-linux-gnueabihf)
7074

7175
## Contributing
76+
7277
See [CONTRIBUTING](CONTRIBUTING.md).
7378

7479
## License

bindings/tmc-langs-node/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ts/functions.node
2+
**/target
3+
**/node_modules
4+
**/.DS_Store
5+
npm-debug.log*

bindings/tmc-langs-node/Cargo.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "tmc-langs-node"
3+
version = "0.1.0"
4+
authors = ["University of Helsinki <mooc@cs.helsinki.fi>", "Daniel Martinez <daniel.x.martinez@helsinki.fi>"]
5+
edition = "2018"
6+
license = "MIT OR Apache-2.0"
7+
exclude = ["index.node"]
8+
9+
[lib]
10+
crate-type = ["cdylib"]
11+
12+
[dependencies.neon]
13+
version = "0.8"
14+
default-features = false
15+
features = ["napi-6"]
16+
17+
[dependencies]
18+
tmc-langs = { path = "../../tmc-langs", features = ["ts"] }
19+
20+
base64 = "0.13"
21+
env_logger = "0.8"
22+
serde = "1"
23+
serde_json = "1"
24+
thiserror = "1"
25+
ts-rs = { git = "https://github.com/Heliozoa/ts-rs/", branch = "ext" }

bindings/tmc-langs-node/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# tmc-langs-node
2+
3+
Provides bindings to tmc-langs for Node.js. This project was bootstrapped by [create-neon](https://www.npmjs.com/package/create-neon).
4+
5+
## TypeScript definitions
6+
7+
You can find TypeScript files that can be used with the Node module in the `./ts` directory. `tmc.ts` is the main module which contains a `Tmc` class for using tmc-langs and re-exports the other modules. `generated.d.ts` contains type definitions for the relevant tmc-langs types like `ExercisePackagingConfiguration` and re-exported by `tmc.ts` as `langs`. `functions.d.ts` contains the types for the actual Node module and is wrapped by `tmc.ts`.
8+
9+
## Installing tmc-langs-node
10+
11+
Installing tmc-langs-node requires a [supported version of Node and Rust](https://github.com/neon-bindings/neon#platform-support).
12+
13+
You can install the project with npm. In the project directory, run:
14+
15+
```sh
16+
$ npm install
17+
```
18+
19+
This fully installs the project, including installing any dependencies and running the build.
20+
21+
## Building tmc-langs-node
22+
23+
If you have already installed the project and only want to run the build, run:
24+
25+
```sh
26+
$ npm run build
27+
```
28+
29+
This command uses the [cargo-cp-artifact](https://github.com/neon-bindings/cargo-cp-artifact) utility to run the Rust build and copy the built library into `./ts/functions.node`.
30+
31+
## Deployment
32+
33+
Node modules for the supported targets are built and deployed to Google Cloud when creating a GitHub release. The binaries are available at https://download.mooc.fi/tmc-langs-rust/, with each binary following the file name format `tmc-langs-{target}-{version}.node`. For a list of targets see the README at the repository root. For example, The 64-bit Linux binary for version 0.5.0 is available at https://download.mooc.fi/tmc-langs-rust/tmc-langs-x86_64-unknown-linux-gnu-0.5.0.node.
34+
35+
### Node module
36+
37+
The Node addon—i.e., a binary Node module—generated by building the project. This is the main module for this package, as dictated by the `"main"` key in `package.json`.
38+
39+
Under the hood, a [Node addon](https://nodejs.org/api/addons.html) is a [dynamically-linked shared object](https://en.wikipedia.org/wiki/Library_(computing)#Shared_libraries). The `"build"` script produces this file by copying it from within the `target/` directory, which is where the Rust build produces the shared object.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
["@babel/preset-env", { targets: { node: "current" } }],
4+
"@babel/preset-typescript",
5+
],
6+
};

0 commit comments

Comments
 (0)