Skip to content

Commit 450fd6e

Browse files
authored
Merge pull request #52 from codewars/packaging
2 parents 65034d0 + 23b744f commit 450fd6e

16 files changed

+1970
-42
lines changed

.dockerignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*
2+
!workspace/
3+
workspace/node_modules/

.github/workflows/publish-image.yml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Manually trigger image build and push.
2+
# This must be done after publishing a new version of the package
3+
# and updating `workspace/package.json` to use it.
4+
name: Push Image
5+
on:
6+
workflow_dispatch:
7+
inputs:
8+
tag:
9+
description: Tag for the new image
10+
required: true
11+
jobs:
12+
build-and-push-image:
13+
if: ${{ github.repository == 'codewars/lambda-calculus' }}
14+
name: Build Images
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: read
18+
packages: write
19+
steps:
20+
- uses: actions/checkout@v2
21+
22+
- name: Set up Docker Buildx
23+
uses: docker/setup-buildx-action@v1
24+
25+
- name: Login to GitHub Container Registry
26+
uses: docker/login-action@v1
27+
with:
28+
registry: ghcr.io
29+
username: ${{ github.repository_owner }}
30+
password: ${{ secrets.GITHUB_TOKEN }}
31+
32+
- name: Build and push image
33+
uses: docker/build-push-action@v2
34+
with:
35+
context: .
36+
push: true
37+
tags: |
38+
ghcr.io/${{ github.repository_owner }}/lambda-calculus:latest
39+
ghcr.io/${{ github.repository_owner }}/lambda-calculus:${{ github.event.inputs.tag }}
40+
cache-from: type=gha
41+
cache-to: type=gha,mode=max

.github/workflows/publish-package.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Publish Package
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
publish:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions/setup-node@v2
13+
with:
14+
node-version: '16'
15+
cache: 'npm'
16+
registry-url: https://registry.npmjs.org/
17+
- run: npm ci
18+
- run: npm publish --access public
19+
env:
20+
NODE_AUTH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}}
21+
22+
- uses: actions/setup-node@v2
23+
with:
24+
node-version: '16'
25+
cache: 'npm'
26+
registry-url: https://npm.pkg.github.com/
27+
- run: npm publish --access public
28+
env:
29+
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

Dockerfile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM node:16.13-alpine3.15
2+
3+
RUN set -ex; \
4+
adduser -D codewarrior; \
5+
mkdir -p /workspace; \
6+
chown codewarrior:codewarrior /workspace;
7+
8+
COPY --chown=codewarrior:codewarrior workspace/package.json /workspace/package.json
9+
COPY --chown=codewarrior:codewarrior workspace/package-lock.json /workspace/package-lock.json
10+
COPY --chown=codewarrior:codewarrior workspace/files.js /workspace/files.js
11+
12+
USER codewarrior
13+
WORKDIR /workspace
14+
RUN npm install

README.md

+33-17
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,31 @@
55

66
Written by [Kacarott](https://github.com/Kacarott) and [JohanWiltink](https://github.com/JohanWiltink)
77

8+
### Install
89

9-
### Running example tests
10-
11-
**Requirements:**
12-
- `node`
13-
- `npm`
14-
15-
From within this directory:
16-
17-
1. Install dependancies with `npm install`
18-
2. Run tests with `npm test`
10+
```bash
11+
$ npm i --save @codewars/lambda-calculus
12+
```
1913

2014
### Usage
2115

16+
> NOTE: When writing tests on Codewars, you can use the predefined wrapper module "./files.js" to get
17+
> the solution file instead of using `fs` like below.
18+
2219
```javascript
20+
import { readFileSync } from "fs";
2321
// Import module
24-
const LC = require("./src/lambda-calculus.js");
22+
import * as LC from "@codewars/lambda-calculus";
2523

2624
// Set config options
2725
LC.config.purity = "Let";
2826
LC.config.numEncoding = "Church";
2927

28+
const code = readFileSync("solution.lc", {encoding: "utf8"});
3029
// Compile
31-
const solution = compile().TRUE;
30+
const solution = compile(code).TRUE;
3231
// or
33-
const {TRUE,FALSE} = compile();
32+
const {TRUE,FALSE} = compile(code);
3433

3534
// Use
3635
console.log(solution(true)(false)); // true
@@ -43,13 +42,10 @@ console.log(TRUE(true)(false)) // true
4342

4443
---
4544

46-
`compile :: String? -> {String: (Term -> Term)}`
45+
`compile :: String -> {String: (Term -> Term)}`
4746

4847
`compile` is the main entry point of the module. Compiles the specified text according the current `config` options, and returns an object binding all defined terms to their corresponding functions.
4948

50-
If called without an argument, will try open a file called `solution.txt` in the same directory, and parse its contents.
51-
52-
5349
---
5450

5551
`config :: {String: String}`
@@ -64,3 +60,23 @@ If called without an argument, will try open a file called `solution.txt` in the
6460
| `numEncoding` | `None` | No number encoding. Use of number literals will cause an error. |
6561
| | `Church`<br>`Scott`<br>`BinaryScott` | Number literals will be automatically converted to corresponding LC terms, according to the encoding chosen. |
6662
| `verbosity` | `Calm`<br>`Concise`<br>`Loquacious`<br>`Verbose` | Controls the amount of information that will be reported during compilation. |
63+
64+
### Container Image
65+
66+
The container image used by the Code Runner is available on [GHCR](https://github.com/codewars/lambda-calculus/pkgs/container/lambda-calculus).
67+
68+
```bash
69+
docker pull ghcr.io/codewars/lambda-calculus:latest
70+
```
71+
72+
#### Building
73+
74+
The image can be built from this repository:
75+
76+
```bash
77+
docker build -t ghcr.io/codewars/lambda-calculus:latest .
78+
```
79+
80+
#### Usage
81+
82+
See [example/](./example/) to learn how to use the image to test locally.

example/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Minimal example with a solution and tests.
2+
3+
Use `./test.sh` to run this in a container.

example/solution.lc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Kacarott
2+
3+
true = \ a b . a
4+
false = \ a b . b
5+
6+
zero = false
7+
succ = \ n f x . f (n f x)
8+
9+
y = \ f . (\ x . f (x x)) (\ x . f (x x))
10+
11+
counter = y (\ count n b . b (count (succ n)) (n) ) zero

example/test.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { assert, config as chaiConfig } from "chai";
2+
chaiConfig.truncateThreshold = 0;
3+
4+
import * as LC from "@codewars/lambda-calculus";
5+
import { solution } from "./files.js"; // /workspace/files.js
6+
7+
LC.config.purity = "Let";
8+
LC.config.numEncoding = "Church";
9+
const toInt = LC.toIntWith(LC.config);
10+
const { counter } = LC.compile(solution());
11+
12+
const T = t => _ => t;
13+
const F = _ => f => f;
14+
15+
describe("counter", () => {
16+
it("fixed tests", () => {
17+
assert.strictEqual(toInt(counter(T)(T)(T)(F)), 3);
18+
assert.strictEqual(toInt(counter(T)(F)), 1);
19+
assert.strictEqual(toInt(counter(T)(T)(T)(T)(T)(T)(T)(F)), 7);
20+
});
21+
});

example/test.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
# Test example in a container similar to the runner
3+
W=/workspace/
4+
C=$(docker container create --rm -w $W ghcr.io/codewars/lambda-calculus:latest npx mocha)
5+
docker container cp ./solution.lc $C:$W
6+
docker container cp ./test.js $C:$W
7+
docker container start --attach $C

kata/submit-tests.js

-18
This file was deleted.

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
22
"name": "@codewars/lambda-calculus",
33
"type": "module",
4-
"version": "1.0.0",
4+
"version": "1.0.0-rc.1",
55
"description": "Lambda Calculus evaluator for Codewars",
66
"main": "src/lambda-calculus.js",
7-
"directories": {
8-
"example": "example",
9-
"test": "tests"
10-
},
7+
"files": [
8+
"src/"
9+
],
1110
"scripts": {
1211
"test": "mocha --reporter spec ./tests/*/test.js",
1312
"repl": "node ./src/LC-REPL.js"

workspace/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
The `/workspace` directory where the submitted solution is tested in.
2+
3+
```text
4+
├─ files.js a wrapper module to load submitted solution and preloaded
5+
├─ preloaded.lc optional preloaded file
6+
├─ solution.lc submitted solution file
7+
└─ test.js tests
8+
```
9+
10+
See `Dockerfile` and `examples/`.

workspace/files.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { readFileSync } from "fs";
2+
3+
const read = (path) => readFileSync(new URL(path, import.meta.url), {encoding: "utf8"});
4+
5+
/** Return the contents of the solution file */
6+
export const solution = () => read("./solution.lc");
7+
8+
/** Return the contents of the optional preloaded file */
9+
export const preloaded = () => read("./preloaded.lc");

0 commit comments

Comments
 (0)