Skip to content

Commit

Permalink
Merge pull request #15 from dherman/allow-presets-in-manifest
Browse files Browse the repository at this point in the history
Allow presets in manifest
  • Loading branch information
dherman authored Nov 26, 2023
2 parents aeac9fc + 109b5e9 commit e827817
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 71 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# `@neon-rs`

Experimental packaging and distribution tooling for [Neon](https://neon-bindings.com).

# Build Workflow of a Neon Project

```
( Start ) ---- npm run build ----> ( .node ) ---- npm run pack-target ----> ( .tgz ) ---- npm publish ----> [ npm ]
\ \ \ \ \
\ \ \ \ +--- npm run cross ----> ( .node ) ---- npm run pack-target ----> ( .tgz ) ---- npm publish ----> [ npm ]
\ \ \ \
\ \ \ +--- npm run cross ----> ( .node ) ---- npm run pack-target ----> ( .tgz ) ---- npm publish ----> [ npm ]
\ \ \
\ \ +--- npm run cross ----> ( .node ) ---- npm run pack-target ----> ( .tgz ) ---- npm publish ----> [ npm ]
\ \
\ +--- npm run cross ----> ( .node ) ---- npm run pack-target ----> ( .tgz ) ---- npm publish ----> [ npm ]
\
+--- npm run cross ----> ( .node ) ---- npm run pack-target ----> ( .tgz ) ---- npm publish ----> [ npm ]
```
87 changes: 62 additions & 25 deletions pkgs/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12052,12 +12052,12 @@ function assertIsNodeTarget(x) {
throw new RangeError(`invalid Node target: ${x}`);
}
}
function isTargetFamilyKey(x) {
function isTargetPreset(x) {
return (typeof x === 'string') && (x in family_namespaceObject);
}
function assertIsTargetFamilyKey(x) {
if (!isTargetFamilyKey(x)) {
throw new RangeError(`invalid target family name: ${x}`);
function assertIsTargetPreset(x) {
if (!isTargetPreset(x)) {
throw new RangeError(`invalid target family preset: ${x}`);
}
}
function lookupTargetFamily(key) {
Expand All @@ -12071,7 +12071,7 @@ function merge(maps) {
return merged;
}
function expandTargetFamily(family) {
return isTargetFamilyKey(family)
return isTargetPreset(family)
? expandTargetFamily(lookupTargetFamily(family))
: Array.isArray(family)
? merge(family.map(expandTargetFamily))
Expand Down Expand Up @@ -12175,6 +12175,19 @@ function assertIsTargetMap(json, path) {
}
}
}
function assertIsTargetFamily(json, path) {
if (typeof json === 'string') {
assertIsTargetPreset(json);
return;
}
if (Array.isArray(json)) {
for (const elt of json) {
assertIsTargetPreset(elt);
}
return;
}
assertIsTargetMap(json, path);
}
function assertIsBinaryV1(json) {
assertHasProps(['binary'], json, "neon");
const binary = json.binary;
Expand Down Expand Up @@ -12218,7 +12231,7 @@ function assertIsSourceCfg(json) {
if (typeof json.org !== 'string') {
throw new TypeError(`expected "neon.org" to be a string, found ${json.org}`);
}
assertIsTargetMap(json.targets, "neon.targets");
assertIsTargetFamily(json.targets, "neon.targets");
}
function assertIsPreamble(json) {
if (!json || typeof json !== 'object') {
Expand Down Expand Up @@ -12355,11 +12368,13 @@ function normalizeSourceCfg(json) {
}
class SourceManifest extends AbstractManifest {
_sourceJSON;
_expandedTargets;
constructor(json) {
super(json);
this._upgraded = normalizeSourceCfg(this._json);
assertHasSourceCfg(this._json);
this._sourceJSON = this._json;
this._expandedTargets = expandTargetFamily(this._sourceJSON.neon.targets);
}
static async load(dir) {
return new SourceManifest(await readManifest(dir));
Expand All @@ -12369,18 +12384,21 @@ class SourceManifest extends AbstractManifest {
}
packageNames() {
const cfg = this.cfg();
return Object.keys(cfg.targets).map(key => `${cfg.org}/${key}`);
return Object.keys(this._expandedTargets).map(key => `${cfg.org}/${key}`);
}
packageFor(target) {
const cfg = this.cfg();
for (const key in cfg.targets) {
const value = cfg.targets[key];
for (const key in this._expandedTargets) {
const value = this._expandedTargets[key];
if (value === target) {
return `${cfg.org}/${key}`;
}
}
return undefined;
}
rustTargetFor(node) {
return this._expandedTargets[node];
}
manifestFor(target) {
const targetInfo = getTargetDescriptor(target);
const name = this.packageFor(target);
Expand Down Expand Up @@ -12416,11 +12434,10 @@ class SourceManifest extends AbstractManifest {
}
async addTargetPair(pair) {
const { node, rust } = pair;
const targets = this.cfg().targets;
if (targets[node] === rust) {
if (this._expandedTargets[node] === rust) {
return null;
}
targets[node] = rust;
this._expandedTargets[node] = rust;
await this.save();
return pair;
}
Expand All @@ -12434,22 +12451,43 @@ class SourceManifest extends AbstractManifest {
async addRustTarget(target) {
return await this.addTargetPair({ node: rust2Node(target), rust: target });
}
async addTargets(family) {
const targets = this.cfg().targets;
let modified = [];
filterNewTargets(family) {
let newTargets = [];
for (const [key, value] of Object.entries(family)) {
const node = key;
const rust = value;
if (targets[node] === rust) {
if (this._expandedTargets[node] === rust) {
continue;
}
targets[node] = rust;
modified.push({ node, rust });
newTargets.push({ node, rust });
}
if (modified.length) {
await this.save();
return newTargets;
}
async addTargets(family, opts = {}) {
let newTargets = this.filterNewTargets(family);
if (!newTargets.length) {
return [];
}
for (const { node, rust } of newTargets) {
if (opts.targetsSrc) {
opts.targetsSrc[node] = rust;
}
this._expandedTargets[node] = rust;
}
await this.save();
return newTargets;
}
async addTargetPreset(preset) {
const targetsSrc = this.cfg().targets;
if (typeof targetsSrc === 'string') {
this.cfg().targets = [targetsSrc, preset];
return this.addTargets(expandTargetFamily(preset));
}
if (Array.isArray(targetsSrc)) {
targetsSrc.push(preset);
return this.addTargets(expandTargetFamily(preset));
}
return modified;
return this.addTargets(expandTargetFamily(preset), { targetsSrc });
}
async updateTargets(log, bundle) {
const packages = this.packageNames();
Expand Down Expand Up @@ -12752,8 +12790,8 @@ class AddTarget {
this.log(`adding Node target ${this._target}`);
return optionArray(await sourceManifest.addNodeTarget(this._target));
}
else if (isTargetFamilyKey(this._target)) {
return sourceManifest.addTargets(expandTargetFamily(this._target));
else if (isTargetPreset(this._target)) {
return sourceManifest.addTargetPreset(this._target);
}
else {
throw new Error(`unrecognized target ${this._target}`);
Expand Down Expand Up @@ -12909,8 +12947,7 @@ class RustTarget {
this.log(`reading package.json`);
const sourceManifest = await SourceManifest.load();
this.log(`manifest: ${sourceManifest.stringify()}`);
const targets = sourceManifest.cfg().targets;
const rust = targets[this._target];
const rust = sourceManifest.rustTargetFor(this._target);
if (!rust) {
throw new Error(`no Rust target found for ${this._target}`);
}
Expand Down
6 changes: 3 additions & 3 deletions src/cli/src/commands/add-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import commandLineArgs from 'command-line-args';
import { Command, CommandDetail, CommandSection } from '../command.js';
import { expandTargetFamily, getCurrentTarget, isNodeTarget, isRustTarget, isTargetFamilyKey, NodeTarget, RustTarget, TargetPair } from '../target.js';
import { getCurrentTarget, isNodeTarget, isRustTarget, isTargetPreset, TargetPair } from '../target.js';
import { SourceManifest } from '../manifest.js';

function optionArray<T>(option: T | undefined | null): T[] {
Expand Down Expand Up @@ -116,8 +116,8 @@ export default class AddTarget implements Command {
} else if (isNodeTarget(this._target)) {
this.log(`adding Node target ${this._target}`);
return optionArray(await sourceManifest.addNodeTarget(this._target));
} else if (isTargetFamilyKey(this._target)) {
return sourceManifest.addTargets(expandTargetFamily(this._target));
} else if (isTargetPreset(this._target)) {
return sourceManifest.addTargetPreset(this._target);
} else {
throw new Error(`unrecognized target ${this._target}`);
}
Expand Down
3 changes: 1 addition & 2 deletions src/cli/src/commands/rust-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ export default class RustTarget implements Command {
const sourceManifest = await SourceManifest.load();
this.log(`manifest: ${sourceManifest.stringify()}`);

const targets = sourceManifest.cfg().targets;
const rust = targets[this._target];
const rust = sourceManifest.rustTargetFor(this._target);
if (!rust) {
throw new Error(`no Rust target found for ${this._target}`);
}
Expand Down
Loading

0 comments on commit e827817

Please sign in to comment.