Skip to content

Commit d1cac1c

Browse files
authored
Merge pull request #184 from rage/node-bindings
Node.js bindings
2 parents e0dea25 + 17f9efd commit d1cac1c

Some content is hidden

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

59 files changed

+14038
-98
lines changed

.github/workflows/test.yml

Lines changed: 11 additions & 2 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
@@ -14,6 +14,9 @@ jobs:
1414
runs-on: ubuntu-latest
1515
steps:
1616
- uses: actions/checkout@v2
17+
- uses: actions/setup-node@v2
18+
with:
19+
node-version: '16'
1720
- name: Install depedencies
1821
run: |
1922
sudo apt update -y
@@ -38,6 +41,9 @@ jobs:
3841
runs-on: windows-latest
3942
steps: # TODO: install missing dependencies and run all tests on windows
4043
- uses: actions/checkout@v2
44+
- uses: actions/setup-node@v2
45+
with:
46+
node-version: '16'
4147
- uses: actions/setup-dotnet@v1
4248
with:
4349
dotnet-version: "5.0.x"
@@ -52,6 +58,9 @@ jobs:
5258
runs-on: macos-latest
5359
steps: # TODO: install missing dependencies and run all tests on macos
5460
- uses: actions/checkout@v2
61+
- uses: actions/setup-node@v2
62+
with:
63+
node-version: '16'
5564
- uses: actions/setup-dotnet@v1
5665
with:
5766
dotnet-version: "5.0.x"

.github/workflows/upload-binaries.yml

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ jobs:
1616
target: [i686-unknown-linux-gnu, x86_64-unknown-linux-gnu]
1717
steps:
1818
- uses: actions/checkout@v2
19+
- uses: actions/setup-node@v2
20+
with:
21+
node-version: '16'
1922
- uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
2023
with:
2124
project_id: ${{ secrets.GCP_PROJECT_ID }}
@@ -24,12 +27,15 @@ jobs:
2427
run: |
2528
sudo apt update
2629
sudo apt install -y gcc-multilib # required to build for 32-bit arch
27-
- name: Cargo build
30+
- name: Build
2831
run: |
29-
rustup target add i686-unknown-linux-gnu
32+
rustup target add ${{ matrix.target }}
3033
cargo build -p tmc-langs-cli --release --verbose --target ${{ matrix.target }}
34+
npm --prefix ./bindings/tmc-langs-node run build -- --release --verbose --target ${{ matrix.target }}
3135
- 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
36+
run: |
37+
gsutil cp target/${{ matrix.target }}/release/tmc-langs-cli gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-cli-${{ matrix.target }}-$TAG
38+
gsutil cp bindings/tmc-langs-node/ts/functions.node gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-${{ matrix.target }}-$TAG.node
3339
3440
windows:
3541
runs-on: windows-latest
@@ -38,25 +44,33 @@ jobs:
3844
target: [i686-pc-windows-msvc, x86_64-pc-windows-msvc]
3945
steps:
4046
- uses: actions/checkout@v2
47+
- uses: actions/setup-node@v2
48+
with:
49+
node-version: '16'
4150
- uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
4251
with:
4352
project_id: ${{ secrets.GCP_PROJECT_ID }}
4453
service_account_key: ${{ secrets.GCP_SA_KEY }}
45-
- name: Cargo build # crt-static is set with RUSTFLAGS to statically link MSVCRT (VCRUNTIME140.dll)
54+
- name: Build # crt-static is set with RUSTFLAGS to statically link MSVCRT (VCRUNTIME140.dll)
4655
run: |
47-
rustup target add i686-pc-windows-msvc
56+
rustup target add ${{ matrix.target }}
4857
$env:RUSTFLAGS="-C target-feature=+crt-static"
4958
cargo build -p tmc-langs-cli --release --verbose --target ${{ matrix.target }}
59+
npm run --prefix ./bindings/tmc-langs-node build -- --release --verbose --target ${{ matrix.target }}
5060
- name: Deploy
5161
run: |
5262
$env:python_version=$(python -c 'import sys; print(\".\".join(map(str, sys.version_info[:3])))')
5363
$env:CLOUDSDK_PYTHON="C:\hostedtoolcache\windows\Python\$env:python_version\x64\python"
5464
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
65+
gsutil cp bindings/tmc-langs-node/ts/functions.node gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-${{ matrix.target }}-$Env:TAG.node
5566
5667
macos:
5768
runs-on: macos-latest
5869
steps:
5970
- uses: actions/checkout@v2
71+
- uses: actions/setup-node@v2
72+
with:
73+
node-version: '16'
6074
- uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
6175
with:
6276
project_id: ${{ secrets.GCP_PROJECT_ID }}
@@ -66,7 +80,9 @@ jobs:
6680
- name: Sign
6781
run: codesign --force -s - target/release/tmc-langs-cli
6882
- 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
83+
run: |
84+
gsutil cp target/release/tmc-langs-cli gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-cli-x86_64-apple-darwin-$TAG
85+
gsutil cp bindings/tmc-langs-node/ts/functions.node gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-x86_64-apple-darwin-$TAG.node
7086
7187
arm:
7288
runs-on: ubuntu-18.04
@@ -75,6 +91,9 @@ jobs:
7591
target: [aarch64-unknown-linux-gnu, armv7-unknown-linux-gnueabihf]
7692
steps:
7793
- uses: actions/checkout@v2
94+
- uses: actions/setup-node@v2
95+
with:
96+
node-version: '16'
7897
- uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
7998
with:
8099
project_id: ${{ secrets.GCP_PROJECT_ID }}
@@ -84,5 +103,8 @@ jobs:
84103
rustup target add ${{ matrix.target }}
85104
cargo install cross
86105
cross build -p tmc-langs-cli --release --verbose --target ${{ matrix.target }}
106+
npm --prefix ./bindings/tmc-langs-node run cross --release --verbose --target ${{ matrix.target }}
87107
- 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
108+
run: |
109+
gsutil cp target/${{ matrix.target }}/release/tmc-langs-cli gs://${{ secrets.GCP_BUCKET }}/tmc-langs-rust/tmc-langs-cli-${{ matrix.target }}-$TAG
110+
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 [email protected]: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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
[workspace]
22
members =[
3+
# core libraries
34
"tmc-client",
45
"tmc-langs",
56
"tmc-langs-cli",
67
"tmc-langs-framework",
78
"tmc-langs-plugins",
89
"tmc-langs-util",
910

11+
# language plugins
1012
"plugins/csharp",
1113
"plugins/java",
1214
"plugins/make",
1315
"plugins/notests",
1416
"plugins/python3",
1517
"plugins/r",
18+
19+
# bindings to other languages
20+
"bindings/tmc-langs-node",
21+
22+
# test helpers
23+
"tmc-server-mock",
1624
]
25+
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[package]
2+
name = "tmc-langs-node"
3+
version = "0.1.0"
4+
authors = ["University of Helsinki <[email protected]>", "Daniel Martinez <[email protected]>"]
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", "default-panic-hook"]
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" }
26+
27+
[dev-dependencies]
28+
log = "0.4"
29+
once_cell = "1"
30+
simple_logger = "1"
31+
tmc-server-mock = { path = "../../tmc-server-mock" }

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+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
<attribute name="test" value="true"/>
14+
</attributes>
15+
</classpathentry>
16+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
17+
<attributes>
18+
<attribute name="maven.pomderived" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
</attributes>
25+
</classpathentry>
26+
<classpathentry kind="src" path="target/generated-sources/annotations">
27+
<attributes>
28+
<attribute name="optional" value="true"/>
29+
<attribute name="maven.pomderived" value="true"/>
30+
<attribute name="ignore_optional_problems" value="true"/>
31+
<attribute name="m2e-apt" value="true"/>
32+
</attributes>
33+
</classpathentry>
34+
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
35+
<attributes>
36+
<attribute name="optional" value="true"/>
37+
<attribute name="maven.pomderived" value="true"/>
38+
<attribute name="ignore_optional_problems" value="true"/>
39+
<attribute name="m2e-apt" value="true"/>
40+
<attribute name="test" value="true"/>
41+
</attributes>
42+
</classpathentry>
43+
<classpathentry kind="output" path="target/classes"/>
44+
</classpath>

0 commit comments

Comments
 (0)