gulp-structify
Version:
Generates WebGL-compatible Structs and StructBuffers from a template file.
104 lines (86 loc) • 3.25 kB
Markdown
Generates WebGL-compatible structs and struct buffers from a template file.
[](https://nodei.co/npm/gulp-structify/)
`npm install gulp-structify --save`
Suppose we want to create a `Vec2` struct backed by a `Float32Array`. We start with a minimal template file called `Vec2.template.ts`:
```TypeScript
import {Template} from "gulp-structify/template";
/**
* A two-dimensional vector with (x,y) components.
*/
class Vec2 extends Template<Float32Array> {
/**
* The X component of this Vec2.
*/
x: number;
/**
* The Y component of this Vec2.
*/
y: number;
}
```
Note: the template should not include a constructor or any static methods.
Now suppose we want to add a `dot` method to our `Vec2` struct. We do this by adding the method to our template:
```TypeScript
// ...
class Vec2 extends Template<Float32Array> {
// ...
/**
* Computes the dot product of this Vec2 with the other Vec2.
*/
dot(other: Vec2): number {
return this.x * other.x + this.y * other.y;
}
}
```
Note: `gulp-structify` automatically generates the following methods:
- `set(other: this)`
- `setScalar(k: number)`
- `add(other: this)`
- `subtract(other: this)`
- `mulScalar(k: number)`
- `divScalar(k: number)`
- `equals(other: this)`
- `equalsScalar(k: number)`
- `epsilonEquals(other: this, e: number)`
- `epsilonEqualsScalar(k: number, e: number)`
- `toString()`
```javascript
var gulp = require("gulp");
var rename = require("gulp-rename");
var structify = require("gulp-structify");
// Directory where template file is located
var directory = "./";
gulp.task("structify", function () {
// Search for files ending in .template.ts
return gulp.src(directory + "*.template.ts")
// Generate struct file
.pipe(structify())
// Remove ".template" from filename
.pipe(rename(function(p) {
let base = p.basename;
let length = base.length - ".template".length;
p.basename = base.substr(0, length);
}))
// Output to same directory to preserve imports
.pipe(gulp.dest(directory));
});
```
`gulp structify`
Template | Output | Usage
-------- | ------ | -----
[][1] | [point.ts][2] |
[][4] | [vec2.ts][5] |
[][7] | [color.ts][8] |
[]: https://github.com/wjheesen/gulp-structify/blob/master/examples/point.template.ts "Point Template"
[]: https://github.com/wjheesen/gulp-structify/blob/master/examples/point.ts "Point Output"
[]: https://github.com/wjheesen/gulp-structify/blob/master/examples/vec2.template.ts "Vec2 Template"
[]: https://github.com/wjheesen/gulp-structify/blob/master/examples/vec2.ts "Vec2 Output"
[]: https://github.com/wjheesen/gulp-structify/blob/master/examples/color.template.ts "Color Template"
[]: https://github.com/wjheesen/gulp-structify/blob/master/examples/color.ts "Color Output"