@voxgig/sdkgen 4.43.0 → 4.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -195,6 +195,14 @@ const FeatureRoot = cmp(function FeatureRoot(props: any) {
195
195
  content: ctx$.meta.content.feature_index,
196
196
  }))
197
197
 
198
+ if (source.owned) {
199
+ log.info({
200
+ point: 'feature-owned', feature: fname,
201
+ note: fname + ': defined in this project, so its files stay as they are'
202
+ })
203
+ return
204
+ }
205
+
198
206
  each(target, (t) => {
199
207
  // The target's OWN tree, under the name it has in ITS source — an
200
208
  // aliased target's templates live at `tm/<origname>`, so searching
@@ -227,6 +235,18 @@ const FeatureRoot = cmp(function FeatureRoot(props: any) {
227
235
 
228
236
  const tmfolder = 0 < overlay.length ? featuretm : owntm
229
237
 
238
+ // A target the project defines itself carries its own feature sources.
239
+ const inplace = 0 === sources.length && 0 < findFeatureSources(
240
+ fs, Path.join(ctx$.folder ?? '.', 'tm', t.name), [fname]).length
241
+
242
+ if (inplace) {
243
+ log.info({
244
+ point: 'feature-source-in-place', feature: fname, target: t.name,
245
+ note: fname + ': target ' + t.name + ' has its own source'
246
+ })
247
+ return
248
+ }
249
+
230
250
  if (0 === sources.length) {
231
251
  log.warn({
232
252
  point: 'feature-source-missing', feature: fname, target: t.name,
@@ -164,6 +164,10 @@ function kindModel(props: {
164
164
  const fs = ctx$.fs()
165
165
  const log = ctx$.log
166
166
 
167
+ if (source.owned) {
168
+ return
169
+ }
170
+
167
171
  const aliased = source.name !== source.origname
168
172
  const owned = aliased && true === def.ownedWhenAliased
169
173
 
@@ -200,6 +200,10 @@ async function planRemove(
200
200
  plan.refused.push('(source not found: the copy cannot be compared with what' +
201
201
  ' `' + kind + ' add` wrote; every file below is deleted only under --force)')
202
202
  }
203
+ else if (source.owned) {
204
+ plan.refused.push('(defined in this project: these files are its only' +
205
+ ' source, and are deleted only under --force)')
206
+ }
203
207
 
204
208
  // The feature being removed counts as selected, so its own source is
205
209
  // compared rather than written off as stale — see checkTarget.
@@ -6,6 +6,8 @@ import { getelem } from '@voxgig/struct'
6
6
 
7
7
  import { KIT } from '../types'
8
8
 
9
+ import { SdkGenError } from '../utility'
10
+
9
11
  import { definitionPathAny } from '../helpers/definition'
10
12
  import { readManifest, checkShape, ITEM_NAME_RE } from '../helpers/manifest'
11
13
 
@@ -33,6 +35,10 @@ type Source = {
33
35
 
34
36
  // That package's version, read from the same manifest.
35
37
  version?: string
38
+
39
+ // Defined in the project's own `.sdk` and provided by nothing else, so the
40
+ // project's files ARE the source: an add registers it and copies nothing.
41
+ owned?: boolean
36
42
  }
37
43
 
38
44
 
@@ -109,6 +115,27 @@ function resolveSource(ref: string, kind: string, ctx$: any): Source {
109
115
  capitalise(kind) + ' folder not found in:\n' + search.join('\n '))
110
116
  }
111
117
 
118
+ if (insideProject(root, folder)) {
119
+ throw new SdkGenError(
120
+ capitalise(kind) + ' source ' + folder + ' is inside this project.' +
121
+ '\n A project\'s own ' + kind + ' lives in its .sdk: define it in ' +
122
+ 'model/' + kind + '/' + origname + '.aontu, put its sources under tm/ ' +
123
+ '(and src/cmp/ for a target), and add it by name.' +
124
+ '\n A package is for sharing ' + kind + 's between projects: install ' +
125
+ 'it from npm, or add it from a checkout outside the project.')
126
+ }
127
+
128
+ const ownmodel = definitionPathAny(fs, root, kind, origname)
129
+ if (name === origname &&
130
+ Path.normalize(folder) === Path.normalize(Path.join(root, BUNDLED)) &&
131
+ !fs.existsSync(definitionPathAny(fs, folder, kind, origname)) &&
132
+ fs.existsSync(ownmodel)) {
133
+ return {
134
+ name, origname, folder: Path.normalize(root), base: '.', model: ownmodel,
135
+ owned: true,
136
+ }
137
+ }
138
+
112
139
  // `base` is the folder relative to the project root. Compare with the
113
140
  // PLATFORM separator: on Windows `root + '/'` never prefixes a normalised
114
141
  // absolute path, so the root would not be stripped and `base` would stay
@@ -138,6 +165,20 @@ function resolveSource(ref: string, kind: string, ctx$: any): Source {
138
165
  }
139
166
 
140
167
 
168
+ // A package folder beside `.sdk` in the same project, such as an `ext/`:
169
+ // the project's own items belong in `.sdk` itself.
170
+ function insideProject(root: string, folder: string): boolean {
171
+ const sdk = Path.resolve(root)
172
+ if ('.sdk' !== Path.basename(sdk)) {
173
+ return false
174
+ }
175
+
176
+ const abs = Path.resolve(folder)
177
+ return abs.startsWith(Path.dirname(sdk) + Path.sep) &&
178
+ !abs.startsWith(Path.join(sdk, 'node_modules') + Path.sep)
179
+ }
180
+
181
+
141
182
  function sourcePackage(
142
183
  fs: any, folder: string, kind: string, origname: string, ctx$: any,
143
184
  ): { name: string, version?: string } | undefined {
@@ -175,8 +175,8 @@ const TargetRoot = cmp(function TargetRoot(props: any) {
175
175
  const dryrun = !!actx?.opts?.dryrun
176
176
 
177
177
 
178
- // `feature add` skips a feature it cannot resolve and leaves its copies
179
- // alone, so the prune must not delete them first.
178
+ // `feature add` copies nothing for a feature it cannot resolve or that the
179
+ // project defines itself, so the prune must not delete those files first.
180
180
  const unresolved = unresolvedFeatures(features ?? [], ctx$)
181
181
 
182
182
  Project({}, () => {
@@ -221,6 +221,14 @@ const TargetRoot = cmp(function TargetRoot(props: any) {
221
221
  content: ctx$.meta.content.target_index,
222
222
  }))
223
223
 
224
+ if (source.owned) {
225
+ log.info({
226
+ point: 'target-owned', target: tname,
227
+ note: tname + ': defined in this project, so its files stay as they are'
228
+ })
229
+ return
230
+ }
231
+
224
232
  if (aliased) {
225
233
  aliasCmpTree(ctx$, tfolder + '/src/cmp/' + torigname,
226
234
  'src/cmp/' + tname, torigname, tname)
@@ -354,8 +362,7 @@ function aliasCmpTree(
354
362
  function unresolvedFeatures(features: string[], ctx$: any): string[] {
355
363
  return features.filter((fname: string) => {
356
364
  try {
357
- resolveKind(fname, 'feature', ctx$)
358
- return false
365
+ return true === resolveKind(fname, 'feature', ctx$).owned
359
366
  }
360
367
  catch (err: any) {
361
368
  return true
@@ -194,18 +194,24 @@ npm run add-feature <name> # e.g. log (comma-separated for several)
194
194
  npm run build && npm run generate
195
195
  \`\`\`
196
196
 
197
- To author a **new** feature:
197
+ To author a **new** feature, when editing a template cannot make the change:
198
198
 
199
199
  1. Define its model at \`.sdk/model/feature/<name>.aontu\` — \`name: key()\`,
200
200
  \`title\`, \`version\`, \`active\`, \`config.options.active\`, a \`hook\`
201
201
  map (\`<Stage>: active: true\`), and per-language \`deps\`.
202
- 2. Register it in \`.sdk/model/feature/feature-index.aontu\` with
203
- \`@"<name>.aontu"\`.
204
- 3. Provide the per-language runtime under that target's feature template dir
202
+ 2. Provide the per-language runtime under that target's feature template dir
205
203
  (\`.sdk/tm/<lang>/src/feature/<name>/\` for ts/js, \`.sdk/tm/<lang>/feature/\`
206
204
  otherwise) — the \`FEATURE_Name\` / \`FEATURE_VERSION\` placeholders are
207
- substituted on \`add-feature\`.
208
- 4. \`npm run add-feature <name> && npm run build && npm run generate\`.
205
+ substituted when \`generate\` copies it.
206
+ 3. \`npm run add-feature <name> && npm run build && npm run generate\`. No
207
+ package provides the name, so the add registers the feature in
208
+ \`feature-index.aontu\` and copies nothing: the files in \`.sdk\` are its
209
+ source.
210
+
211
+ A target of the project's own is written the same way:
212
+ \`.sdk/model/target/<name>.aontu\`, \`.sdk/src/cmp/<name>/\` and
213
+ \`.sdk/tm/<name>/\`, then \`npm run add-target <name>\`. Never put a package
214
+ folder such as \`ext/\` inside the project: sdkgen refuses it.
209
215
  `
210
216
  }
211
217
 
@@ -605,11 +605,24 @@ Pass custom features via the \`extend\` option at construction time.
605
605
  `)
606
606
 
607
607
  // 11c. Customization: model-driven declarations, in-repo templates and
608
- // components, regeneration that overwrites, packages, and a map of which
609
- // paths are the project's to edit. Every SDK repo carries its own
610
- // generator, so this belongs in every README.
608
+ // components, regeneration that overwrites, customising in `.sdk`, and a
609
+ // map of which paths are the project's to edit. Every SDK repo carries
610
+ // its own generator, so this belongs in every README. A repository that
611
+ // regenerates `.sdk` in full says so, since its copies do not merge.
611
612
  const mapTarget = sdkTargets[0] || docTargets[0]
612
613
  const targetDir = mapTarget ? `\`<target>/\`, such as \`${mapTarget.name}/\`` : `\`<target>/\``
614
+ const merging = 'overwrite' !== model.main?.kit?.copies
615
+ const mergeNote = merging ?
616
+ `An add merges toolchain updates into the
617
+ edited copies, and \`voxgig-sdkgen doctor\` reports any drift from what a
618
+ resync would write.` :
619
+ `This repository regenerates \`.sdk\` in full
620
+ (\`main: kit: copies: 'overwrite'\`): set it to \`'merge'\` before
621
+ customising, so that an add merges toolchain updates into the edited
622
+ copies.`
623
+ const copiesRow = merging ?
624
+ `Yes. They are this project's copies, and \`target add\` merges toolchain updates into your edits.` :
625
+ `Only after setting \`copies\` to \`'merge'\`. This repository regenerates them in full.`
613
626
  Content(`## Customizing this SDK
614
627
 
615
628
  This repository contains its own generator (\`.sdk/\`), so the SDK is
@@ -630,10 +643,11 @@ customizable without forking any upstream tool:
630
643
  (\`.sdk/model/project.aontu\`), or extend a target with a component of its
631
644
  own in \`.sdk/src/cmp/<target>/\`, registered with \`registerComponent\`,
632
645
  which \`voxgig-sdkgen doctor\` reports as an addition rather than drift.
633
- - **Custom features and entire custom targets** arrive through sdkgen
634
- packages (\`voxgig-sdkgen package add\`), on the same rails as the
635
- bundled languages, and \`voxgig-sdkgen doctor\` reports any drift from
636
- what a resync would write.
646
+ - **Customise in \`.sdk\`.** Edit the templates and components first. When
647
+ they cannot express a change, write a feature of this project's own, or
648
+ an entire custom target, in \`.sdk/model/\`, \`.sdk/tm/\` and
649
+ \`.sdk/src/cmp/\`, and add it by name (\`voxgig-sdkgen feature add\`,
650
+ \`voxgig-sdkgen target add\`). ${mergeNote}
637
651
 
638
652
  ### Project map
639
653
 
@@ -642,9 +656,9 @@ customizable without forking any upstream tool:
642
656
  | \`.sdk/model/project.aontu\` | This project's own decisions: package names, versions, features and per-target settings | Yes. The scaffold writes it once and never again. |
643
657
  | \`.sdk/model/text.aontu\` | The README wording, in \`main.kit.text\`: the tagline, the summary and the entity descriptions | Yes. The scaffold writes it once and never again. |
644
658
  | \`.sdk/model/guide/guide.aontu\` | How the spec's paths map onto entities and operations | Yes. The scaffold writes it once and never again. |
645
- | \`.sdk/model/\`, the rest | The API model built from the spec, and the target and feature definitions | Not by hand. Generation, \`target add\` and a new scaffold run rewrite it. |
659
+ | \`.sdk/model/\`, the rest | The API model built from the spec, and the target and feature definitions | Not by hand. Generation, \`target add\` and a new scaffold run rewrite it, apart from a feature or target this project defines itself. |
646
660
  | \`.sdk/def/\` | The OpenAPI spec | Replace it when the API changes. Generation rebuilds the model from it. |
647
- | \`.sdk/src/cmp/<target>/\`, \`.sdk/tm/<target>/\` | The components and templates that generate each target | Only to add a component of your own. \`target add\` overwrites the copies. |
661
+ | \`.sdk/src/cmp/<target>/\`, \`.sdk/tm/<target>/\` | The components and templates that generate each target | ${copiesRow} |
648
662
  | \`.sdk/test/\` | The shared test cases the generated tests run | No. |
649
663
  | ${targetDir} | One generated SDK per target, with its own tests | No. Generation overwrites it. |
650
664
  | \`README.md\`, \`LICENSE\`, \`Makefile\` and the other root files | The repository's generated files | No. Generation overwrites them. |