@cratis/scene.engine 0.1.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.
Files changed (221) hide show
  1. package/BindingResolver.ts +11 -0
  2. package/ComponentResolution.ts +22 -0
  3. package/PackageSelection.ts +87 -0
  4. package/README.md +40 -0
  5. package/Renderer.ts +33 -0
  6. package/aggregateContributions.ts +40 -0
  7. package/buildStarterProfile.ts +21 -0
  8. package/computeSizeClass.ts +30 -0
  9. package/dist/cjs/PackageSelection.js +15 -0
  10. package/dist/cjs/PackageSelection.js.map +1 -0
  11. package/dist/cjs/aggregateContributions.js +37 -0
  12. package/dist/cjs/aggregateContributions.js.map +1 -0
  13. package/dist/cjs/buildStarterProfile.js +27 -0
  14. package/dist/cjs/buildStarterProfile.js.map +1 -0
  15. package/dist/cjs/computeSizeClass.js +31 -0
  16. package/dist/cjs/computeSizeClass.js.map +1 -0
  17. package/dist/cjs/elementKind.js +26 -0
  18. package/dist/cjs/elementKind.js.map +1 -0
  19. package/dist/cjs/evaluateFlowArrangement.js +31 -0
  20. package/dist/cjs/evaluateFlowArrangement.js.map +1 -0
  21. package/dist/cjs/evaluateFreeformArrangement.js +16 -0
  22. package/dist/cjs/evaluateFreeformArrangement.js.map +1 -0
  23. package/dist/cjs/evaluateFreeformSlotArrangement.js +18 -0
  24. package/dist/cjs/evaluateFreeformSlotArrangement.js.map +1 -0
  25. package/dist/cjs/flowNodeKind.js +40 -0
  26. package/dist/cjs/flowNodeKind.js.map +1 -0
  27. package/dist/cjs/incompatibleStarterThemes.js +29 -0
  28. package/dist/cjs/incompatibleStarterThemes.js.map +1 -0
  29. package/dist/cjs/index.js +59 -0
  30. package/dist/cjs/index.js.map +1 -0
  31. package/dist/cjs/package.json +3 -0
  32. package/dist/cjs/packageCatalog.js +63 -0
  33. package/dist/cjs/packageCatalog.js.map +1 -0
  34. package/dist/cjs/packageVersionRange.js +113 -0
  35. package/dist/cjs/packageVersionRange.js.map +1 -0
  36. package/dist/cjs/renderElement.js +39 -0
  37. package/dist/cjs/renderElement.js.map +1 -0
  38. package/dist/cjs/resolveComponentName.js +67 -0
  39. package/dist/cjs/resolveComponentName.js.map +1 -0
  40. package/dist/cjs/resolvePackageDependencies.js +149 -0
  41. package/dist/cjs/resolvePackageDependencies.js.map +1 -0
  42. package/dist/cjs/resolveScreenTemplates.js +136 -0
  43. package/dist/cjs/resolveScreenTemplates.js.map +1 -0
  44. package/dist/cjs/themeCompatibility.js +45 -0
  45. package/dist/cjs/themeCompatibility.js.map +1 -0
  46. package/dist/esm/BindingResolver.d.ts +3 -0
  47. package/dist/esm/BindingResolver.d.ts.map +1 -0
  48. package/dist/esm/BindingResolver.js +2 -0
  49. package/dist/esm/BindingResolver.js.map +1 -0
  50. package/dist/esm/ComponentResolution.d.ts +6 -0
  51. package/dist/esm/ComponentResolution.d.ts.map +1 -0
  52. package/dist/esm/ComponentResolution.js +2 -0
  53. package/dist/esm/ComponentResolution.js.map +1 -0
  54. package/dist/esm/PackageSelection.d.ts +19 -0
  55. package/dist/esm/PackageSelection.d.ts.map +1 -0
  56. package/dist/esm/PackageSelection.js +13 -0
  57. package/dist/esm/PackageSelection.js.map +1 -0
  58. package/dist/esm/Renderer.d.ts +8 -0
  59. package/dist/esm/Renderer.d.ts.map +1 -0
  60. package/dist/esm/Renderer.js +2 -0
  61. package/dist/esm/Renderer.js.map +1 -0
  62. package/dist/esm/aggregateContributions.d.ts +3 -0
  63. package/dist/esm/aggregateContributions.d.ts.map +1 -0
  64. package/dist/esm/aggregateContributions.js +35 -0
  65. package/dist/esm/aggregateContributions.js.map +1 -0
  66. package/dist/esm/buildStarterProfile.d.ts +3 -0
  67. package/dist/esm/buildStarterProfile.d.ts.map +1 -0
  68. package/dist/esm/buildStarterProfile.js +25 -0
  69. package/dist/esm/buildStarterProfile.js.map +1 -0
  70. package/dist/esm/computeSizeClass.d.ts +5 -0
  71. package/dist/esm/computeSizeClass.d.ts.map +1 -0
  72. package/dist/esm/computeSizeClass.js +27 -0
  73. package/dist/esm/computeSizeClass.js.map +1 -0
  74. package/dist/esm/elementKind.d.ts +6 -0
  75. package/dist/esm/elementKind.d.ts.map +1 -0
  76. package/dist/esm/elementKind.js +21 -0
  77. package/dist/esm/elementKind.js.map +1 -0
  78. package/dist/esm/evaluateFlowArrangement.d.ts +3 -0
  79. package/dist/esm/evaluateFlowArrangement.d.ts.map +1 -0
  80. package/dist/esm/evaluateFlowArrangement.js +29 -0
  81. package/dist/esm/evaluateFlowArrangement.js.map +1 -0
  82. package/dist/esm/evaluateFreeformArrangement.d.ts +3 -0
  83. package/dist/esm/evaluateFreeformArrangement.d.ts.map +1 -0
  84. package/dist/esm/evaluateFreeformArrangement.js +14 -0
  85. package/dist/esm/evaluateFreeformArrangement.js.map +1 -0
  86. package/dist/esm/evaluateFreeformSlotArrangement.d.ts +3 -0
  87. package/dist/esm/evaluateFreeformSlotArrangement.d.ts.map +1 -0
  88. package/dist/esm/evaluateFreeformSlotArrangement.js +16 -0
  89. package/dist/esm/evaluateFreeformSlotArrangement.js.map +1 -0
  90. package/dist/esm/flowNodeKind.d.ts +8 -0
  91. package/dist/esm/flowNodeKind.d.ts.map +1 -0
  92. package/dist/esm/flowNodeKind.js +33 -0
  93. package/dist/esm/flowNodeKind.js.map +1 -0
  94. package/dist/esm/for_aggregateContributions/when_aggregating_by_contribution_point.d.ts +2 -0
  95. package/dist/esm/for_aggregateContributions/when_aggregating_by_contribution_point.d.ts.map +1 -0
  96. package/dist/esm/for_aggregateContributions/when_aggregating_by_contribution_point.js +23 -0
  97. package/dist/esm/for_aggregateContributions/when_aggregating_by_contribution_point.js.map +1 -0
  98. package/dist/esm/for_buildStarterProfile/when_checking_against_the_shared_fixture_corpus.d.ts +2 -0
  99. package/dist/esm/for_buildStarterProfile/when_checking_against_the_shared_fixture_corpus.d.ts.map +1 -0
  100. package/dist/esm/for_buildStarterProfile/when_checking_against_the_shared_fixture_corpus.js +16 -0
  101. package/dist/esm/for_buildStarterProfile/when_checking_against_the_shared_fixture_corpus.js.map +1 -0
  102. package/dist/esm/for_computeSizeClass/when_computing_the_size_class.d.ts +2 -0
  103. package/dist/esm/for_computeSizeClass/when_computing_the_size_class.d.ts.map +1 -0
  104. package/dist/esm/for_computeSizeClass/when_computing_the_size_class.js +17 -0
  105. package/dist/esm/for_computeSizeClass/when_computing_the_size_class.js.map +1 -0
  106. package/dist/esm/for_evaluateFlowArrangement/when_evaluating_against_the_shared_fixture_corpus.d.ts +2 -0
  107. package/dist/esm/for_evaluateFlowArrangement/when_evaluating_against_the_shared_fixture_corpus.d.ts.map +1 -0
  108. package/dist/esm/for_evaluateFlowArrangement/when_evaluating_against_the_shared_fixture_corpus.js +43 -0
  109. package/dist/esm/for_evaluateFlowArrangement/when_evaluating_against_the_shared_fixture_corpus.js.map +1 -0
  110. package/dist/esm/for_evaluateFreeformArrangement/when_evaluating_against_the_shared_fixture_corpus.d.ts +2 -0
  111. package/dist/esm/for_evaluateFreeformArrangement/when_evaluating_against_the_shared_fixture_corpus.d.ts.map +1 -0
  112. package/dist/esm/for_evaluateFreeformArrangement/when_evaluating_against_the_shared_fixture_corpus.js +46 -0
  113. package/dist/esm/for_evaluateFreeformArrangement/when_evaluating_against_the_shared_fixture_corpus.js.map +1 -0
  114. package/dist/esm/for_evaluateFreeformSlotArrangement/when_evaluating_against_the_shared_fixture_corpus.d.ts +2 -0
  115. package/dist/esm/for_evaluateFreeformSlotArrangement/when_evaluating_against_the_shared_fixture_corpus.d.ts.map +1 -0
  116. package/dist/esm/for_evaluateFreeformSlotArrangement/when_evaluating_against_the_shared_fixture_corpus.js +25 -0
  117. package/dist/esm/for_evaluateFreeformSlotArrangement/when_evaluating_against_the_shared_fixture_corpus.js.map +1 -0
  118. package/dist/esm/for_incompatibleStarterThemes/when_checking_against_the_shared_fixture_corpus.d.ts +2 -0
  119. package/dist/esm/for_incompatibleStarterThemes/when_checking_against_the_shared_fixture_corpus.d.ts.map +1 -0
  120. package/dist/esm/for_incompatibleStarterThemes/when_checking_against_the_shared_fixture_corpus.js +17 -0
  121. package/dist/esm/for_incompatibleStarterThemes/when_checking_against_the_shared_fixture_corpus.js.map +1 -0
  122. package/dist/esm/for_packageCatalog/when_querying_against_the_shared_fixture_corpus.d.ts +2 -0
  123. package/dist/esm/for_packageCatalog/when_querying_against_the_shared_fixture_corpus.d.ts.map +1 -0
  124. package/dist/esm/for_packageCatalog/when_querying_against_the_shared_fixture_corpus.js +22 -0
  125. package/dist/esm/for_packageCatalog/when_querying_against_the_shared_fixture_corpus.js.map +1 -0
  126. package/dist/esm/for_packageDependencies/fixtures.d.ts +38 -0
  127. package/dist/esm/for_packageDependencies/fixtures.d.ts.map +1 -0
  128. package/dist/esm/for_packageDependencies/fixtures.js +5 -0
  129. package/dist/esm/for_packageDependencies/fixtures.js.map +1 -0
  130. package/dist/esm/for_packageDependencies/when_resolving_against_the_shared_fixture_corpus.d.ts +2 -0
  131. package/dist/esm/for_packageDependencies/when_resolving_against_the_shared_fixture_corpus.d.ts.map +1 -0
  132. package/dist/esm/for_packageDependencies/when_resolving_against_the_shared_fixture_corpus.js +29 -0
  133. package/dist/esm/for_packageDependencies/when_resolving_against_the_shared_fixture_corpus.js.map +1 -0
  134. package/dist/esm/for_packageVersionRange/when_checking_against_the_shared_fixture_corpus.d.ts +2 -0
  135. package/dist/esm/for_packageVersionRange/when_checking_against_the_shared_fixture_corpus.d.ts.map +1 -0
  136. package/dist/esm/for_packageVersionRange/when_checking_against_the_shared_fixture_corpus.js +10 -0
  137. package/dist/esm/for_packageVersionRange/when_checking_against_the_shared_fixture_corpus.js.map +1 -0
  138. package/dist/esm/for_renderElement/when_rendering_a_mixed_tree.d.ts +2 -0
  139. package/dist/esm/for_renderElement/when_rendering_a_mixed_tree.d.ts.map +1 -0
  140. package/dist/esm/for_renderElement/when_rendering_a_mixed_tree.js +46 -0
  141. package/dist/esm/for_renderElement/when_rendering_a_mixed_tree.js.map +1 -0
  142. package/dist/esm/for_resolveComponentName/when_resolving_against_the_shared_fixture_corpus.d.ts +2 -0
  143. package/dist/esm/for_resolveComponentName/when_resolving_against_the_shared_fixture_corpus.d.ts.map +1 -0
  144. package/dist/esm/for_resolveComponentName/when_resolving_against_the_shared_fixture_corpus.js +20 -0
  145. package/dist/esm/for_resolveComponentName/when_resolving_against_the_shared_fixture_corpus.js.map +1 -0
  146. package/dist/esm/for_resolveScreenTemplates/when_resolving_against_the_shared_fixture_corpus.d.ts +2 -0
  147. package/dist/esm/for_resolveScreenTemplates/when_resolving_against_the_shared_fixture_corpus.d.ts.map +1 -0
  148. package/dist/esm/for_resolveScreenTemplates/when_resolving_against_the_shared_fixture_corpus.js +30 -0
  149. package/dist/esm/for_resolveScreenTemplates/when_resolving_against_the_shared_fixture_corpus.js.map +1 -0
  150. package/dist/esm/for_themeCompatibility/when_checking_against_the_shared_fixture_corpus.d.ts +2 -0
  151. package/dist/esm/for_themeCompatibility/when_checking_against_the_shared_fixture_corpus.d.ts.map +1 -0
  152. package/dist/esm/for_themeCompatibility/when_checking_against_the_shared_fixture_corpus.js +18 -0
  153. package/dist/esm/for_themeCompatibility/when_checking_against_the_shared_fixture_corpus.js.map +1 -0
  154. package/dist/esm/incompatibleStarterThemes.d.ts +3 -0
  155. package/dist/esm/incompatibleStarterThemes.d.ts.map +1 -0
  156. package/dist/esm/incompatibleStarterThemes.js +27 -0
  157. package/dist/esm/incompatibleStarterThemes.js.map +1 -0
  158. package/dist/esm/index.d.ts +21 -0
  159. package/dist/esm/index.d.ts.map +1 -0
  160. package/dist/esm/index.js +18 -0
  161. package/dist/esm/index.js.map +1 -0
  162. package/dist/esm/package.json +3 -0
  163. package/dist/esm/packageCatalog.d.ts +7 -0
  164. package/dist/esm/packageCatalog.d.ts.map +1 -0
  165. package/dist/esm/packageCatalog.js +57 -0
  166. package/dist/esm/packageCatalog.js.map +1 -0
  167. package/dist/esm/packageVersionRange.d.ts +2 -0
  168. package/dist/esm/packageVersionRange.d.ts.map +1 -0
  169. package/dist/esm/packageVersionRange.js +111 -0
  170. package/dist/esm/packageVersionRange.js.map +1 -0
  171. package/dist/esm/renderElement.d.ts +5 -0
  172. package/dist/esm/renderElement.d.ts.map +1 -0
  173. package/dist/esm/renderElement.js +37 -0
  174. package/dist/esm/renderElement.js.map +1 -0
  175. package/dist/esm/resolveComponentName.d.ts +7 -0
  176. package/dist/esm/resolveComponentName.d.ts.map +1 -0
  177. package/dist/esm/resolveComponentName.js +63 -0
  178. package/dist/esm/resolveComponentName.js.map +1 -0
  179. package/dist/esm/resolvePackageDependencies.d.ts +4 -0
  180. package/dist/esm/resolvePackageDependencies.d.ts.map +1 -0
  181. package/dist/esm/resolvePackageDependencies.js +147 -0
  182. package/dist/esm/resolvePackageDependencies.js.map +1 -0
  183. package/dist/esm/resolveScreenTemplates.d.ts +20 -0
  184. package/dist/esm/resolveScreenTemplates.d.ts.map +1 -0
  185. package/dist/esm/resolveScreenTemplates.js +133 -0
  186. package/dist/esm/resolveScreenTemplates.js.map +1 -0
  187. package/dist/esm/themeCompatibility.d.ts +5 -0
  188. package/dist/esm/themeCompatibility.d.ts.map +1 -0
  189. package/dist/esm/themeCompatibility.js +41 -0
  190. package/dist/esm/themeCompatibility.js.map +1 -0
  191. package/dist/esm/tsconfig.tsbuildinfo +1 -0
  192. package/elementKind.ts +26 -0
  193. package/evaluateFlowArrangement.ts +35 -0
  194. package/evaluateFreeformArrangement.ts +15 -0
  195. package/evaluateFreeformSlotArrangement.ts +17 -0
  196. package/flowNodeKind.ts +38 -0
  197. package/for_aggregateContributions/when_aggregating_by_contribution_point.ts +31 -0
  198. package/for_buildStarterProfile/when_checking_against_the_shared_fixture_corpus.ts +32 -0
  199. package/for_computeSizeClass/when_computing_the_size_class.ts +23 -0
  200. package/for_evaluateFlowArrangement/when_evaluating_against_the_shared_fixture_corpus.ts +67 -0
  201. package/for_evaluateFreeformArrangement/when_evaluating_against_the_shared_fixture_corpus.ts +68 -0
  202. package/for_evaluateFreeformSlotArrangement/when_evaluating_against_the_shared_fixture_corpus.ts +47 -0
  203. package/for_incompatibleStarterThemes/when_checking_against_the_shared_fixture_corpus.ts +34 -0
  204. package/for_packageCatalog/when_querying_against_the_shared_fixture_corpus.ts +26 -0
  205. package/for_packageDependencies/fixtures.ts +44 -0
  206. package/for_packageDependencies/when_resolving_against_the_shared_fixture_corpus.ts +39 -0
  207. package/for_packageVersionRange/when_checking_against_the_shared_fixture_corpus.ts +13 -0
  208. package/for_renderElement/when_rendering_a_mixed_tree.ts +58 -0
  209. package/for_resolveComponentName/when_resolving_against_the_shared_fixture_corpus.ts +36 -0
  210. package/for_resolveScreenTemplates/when_resolving_against_the_shared_fixture_corpus.ts +49 -0
  211. package/for_themeCompatibility/when_checking_against_the_shared_fixture_corpus.ts +37 -0
  212. package/incompatibleStarterThemes.ts +24 -0
  213. package/index.ts +23 -0
  214. package/package.json +45 -0
  215. package/packageCatalog.ts +65 -0
  216. package/packageVersionRange.ts +90 -0
  217. package/renderElement.ts +40 -0
  218. package/resolveComponentName.ts +66 -0
  219. package/resolvePackageDependencies.ts +156 -0
  220. package/resolveScreenTemplates.ts +191 -0
  221. package/themeCompatibility.ts +46 -0
@@ -0,0 +1,65 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { PackageKind, ScenePackage } from '@cratis/scene.model';
5
+
6
+ /**
7
+ * The packages of a given kind.
8
+ */
9
+ export function packagesOfKind(catalog: ScenePackage[], kind: PackageKind): ScenePackage[] {
10
+ return catalog.filter((scenePackage) => scenePackage.kind === kind);
11
+ }
12
+
13
+ /**
14
+ * The component libraries a profile can be founded on — the ones that do not themselves layer on another
15
+ * component library.
16
+ *
17
+ * "Base" is not a declared property; it falls out of the dependency graph. PrimeReact depends on a
18
+ * styling package but on no other component library, so it is a base. `@cratis/components` depends on
19
+ * PrimeReact, so it is not — it is something you add on top of a base you already picked. Deriving it
20
+ * this way means a third party shipping their own library gets classified correctly without having to
21
+ * declare anything extra.
22
+ */
23
+ export function baseComponentLibraries(catalog: ScenePackage[]): ScenePackage[] {
24
+ const libraries = packagesOfKind(catalog, PackageKind.ComponentLibrary);
25
+ const names = new Set(libraries.map((scenePackage) => scenePackage.name));
26
+ return libraries.filter((scenePackage) => !scenePackage.dependencies.some((dependency) => names.has(dependency.name)));
27
+ }
28
+
29
+ /**
30
+ * The packages that can be added to a selection without pulling anything else in — every dependency they
31
+ * declare is already selected.
32
+ *
33
+ * This is the "what else works with what I have picked" list: choose PrimeReact and Tailwind, and
34
+ * `@cratis/components` becomes available because both of its dependencies are now met. It is
35
+ * deliberately stricter than {@link resolvePackageDependencies}, which will happily add the missing
36
+ * dependencies for you — a picker wants to show what fits, not what would drag more in.
37
+ */
38
+ export function availablePackagesFor(catalog: ScenePackage[], selected: string[]): ScenePackage[] {
39
+ const chosen = new Set(selected);
40
+ return catalog.filter(
41
+ (scenePackage) => !chosen.has(scenePackage.name) && scenePackage.dependencies.every((dependency) => chosen.has(dependency.name))
42
+ );
43
+ }
44
+
45
+ /**
46
+ * Every component name the selected packages declare between them, without duplicates, sorted so a
47
+ * component picker has a stable list to show.
48
+ */
49
+ export function componentsForPackages(catalog: ScenePackage[], selected: string[]): string[] {
50
+ const chosen = new Set(selected);
51
+ const components = catalog.filter((scenePackage) => chosen.has(scenePackage.name)).flatMap((scenePackage) => scenePackage.components);
52
+ return [...new Set(components)].sort();
53
+ }
54
+
55
+ /**
56
+ * Builds the component-name catalog `resolveComponentName` resolves against.
57
+ */
58
+ export function toComponentCatalog(catalog: ScenePackage[]): Record<string, string[]> {
59
+ const components: Record<string, string[]> = {};
60
+ for (const scenePackage of catalog) {
61
+ components[scenePackage.name] = scenePackage.components;
62
+ }
63
+
64
+ return components;
65
+ }
@@ -0,0 +1,90 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ /**
5
+ * Checks a {@link PackageDependency.versionRange} against a package's actual version. A deliberately
6
+ * small subset of semver — enough to express what a package declaration realistically needs, and small
7
+ * enough that the C# twin in `Cratis.Scene.Engine` can match it exactly rather than approximately. Both
8
+ * sides are asserted against the same shared fixture corpus.
9
+ *
10
+ * Supported: an empty range or `*` (anything), a bare or `=`-prefixed exact version, `^` (npm caret —
11
+ * compatible within the leftmost non-zero component), `~` (patch-level changes), and the comparisons
12
+ * `>=`, `>`, `<=`, `<`. Versions are read as `major.minor.patch` with any pre-release or build metadata
13
+ * suffix ignored. Anything the parser does not recognize is treated as unsatisfiable rather than as
14
+ * "anything", so a typo surfaces as a conflict instead of silently passing.
15
+ */
16
+ export function isVersionSatisfiedBy(version: string, range: string | undefined): boolean {
17
+ const trimmed = range?.trim();
18
+ if (!trimmed || trimmed === '*') return true;
19
+
20
+ const actual = parseVersion(version);
21
+ if (!actual) return false;
22
+
23
+ const [operator, literal] = splitRange(trimmed);
24
+ const required = parseVersion(literal);
25
+ if (!required) return false;
26
+
27
+ switch (operator) {
28
+ case '^':
29
+ return compare(actual, required) >= 0 && compare(actual, caretUpperBound(required)) < 0;
30
+ case '~':
31
+ return compare(actual, required) >= 0 && compare(actual, [required[0], required[1] + 1, 0]) < 0;
32
+ case '>=':
33
+ return compare(actual, required) >= 0;
34
+ case '>':
35
+ return compare(actual, required) > 0;
36
+ case '<=':
37
+ return compare(actual, required) <= 0;
38
+ case '<':
39
+ return compare(actual, required) < 0;
40
+ default:
41
+ return compare(actual, required) === 0;
42
+ }
43
+ }
44
+
45
+ type Version = [number, number, number];
46
+
47
+ function splitRange(range: string): [string, string] {
48
+ for (const operator of ['>=', '<=', '^', '~', '>', '<', '=']) {
49
+ if (range.startsWith(operator)) {
50
+ return [operator === '=' ? '' : operator, range.substring(operator.length).trim()];
51
+ }
52
+ }
53
+
54
+ return ['', range];
55
+ }
56
+
57
+ /**
58
+ * npm's caret rule: compatibility is bounded by the leftmost non-zero component, so `^1.2.3` allows
59
+ * anything below `2.0.0`, `^0.2.3` anything below `0.3.0`, and `^0.0.3` only `0.0.3` itself.
60
+ */
61
+ function caretUpperBound(version: Version): Version {
62
+ if (version[0] > 0) return [version[0] + 1, 0, 0];
63
+ if (version[1] > 0) return [0, version[1] + 1, 0];
64
+ return [0, 0, version[2] + 1];
65
+ }
66
+
67
+ function compare(left: Version, right: Version): number {
68
+ if (left[0] !== right[0]) return left[0] - right[0];
69
+ if (left[1] !== right[1]) return left[1] - right[1];
70
+ return left[2] - right[2];
71
+ }
72
+
73
+ function parseVersion(value: string): Version | undefined {
74
+ let numeric = value.trim();
75
+ const suffix = numeric.search(/[-+]/);
76
+ if (suffix >= 0) {
77
+ numeric = numeric.substring(0, suffix);
78
+ }
79
+
80
+ const parts = numeric.split('.');
81
+ if (parts.length === 0 || parts.length > 3) return undefined;
82
+
83
+ const components: Version = [0, 0, 0];
84
+ for (let index = 0; index < parts.length; index++) {
85
+ if (!/^\d+$/.test(parts[index])) return undefined;
86
+ components[index] = parseInt(parts[index], 10);
87
+ }
88
+
89
+ return components;
90
+ }
@@ -0,0 +1,40 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { SceneElement } from '@cratis/scene.model';
5
+ import { Renderer } from './Renderer';
6
+ import { BindingResolver } from './BindingResolver';
7
+ import { isContentControl, isItemsControl, isExternalComponent, isPanel } from './elementKind';
8
+
9
+ /**
10
+ * Walks a Scene element tree, rendering each node's children before the node itself, and hands the
11
+ * result to the supplied {@link Renderer}. This is a structural tree walk only - resolving a `ui
12
+ * profile`'s package list, aggregating contribution points, and computing layout arrangement are the
13
+ * jobs of the profile resolution, contribution runtime and layout engines (Scene#3, Scene#2, Scene#4),
14
+ * layered on top of this walk rather than folded into it.
15
+ */
16
+ export function renderElement<TOutput>(element: SceneElement, renderer: Renderer<TOutput>, resolveBinding: BindingResolver): TOutput {
17
+ if (isContentControl(element)) {
18
+ return renderer.renderContentControl(element, renderElement(element.content, renderer, resolveBinding));
19
+ }
20
+
21
+ if (isItemsControl(element)) {
22
+ const items = resolveBinding(element.itemsSource);
23
+ const itemCount = Array.isArray(items) ? items.length : 0;
24
+ const rendered = Array.from({ length: itemCount }, () => renderElement(element.itemTemplate, renderer, resolveBinding));
25
+ return renderer.renderItemsControl(element, rendered);
26
+ }
27
+
28
+ if (isExternalComponent(element)) {
29
+ const slots = Object.fromEntries(
30
+ Object.entries(element.slots).map(([name, children]) => [name, children.map(child => renderElement(child, renderer, resolveBinding))]),
31
+ );
32
+ return renderer.renderExternalComponent(element, slots);
33
+ }
34
+
35
+ if (isPanel(element)) {
36
+ return renderer.renderPanel(element, element.children.map(child => renderElement(child, renderer, resolveBinding)));
37
+ }
38
+
39
+ throw new Error(`Scene element '${element.id}' is not a recognized concrete kind (ContentControl, ItemsControl, ExternalComponent or Panel).`);
40
+ }
@@ -0,0 +1,66 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { UiProfile } from '@cratis/scene.model';
5
+ import { ComponentResolution } from './ComponentResolution';
6
+
7
+ /** The name of the package that is always present as the final fallback, regardless of what a {@link UiProfile} lists in its own `packages`. */
8
+ export const corePackage = 'core';
9
+
10
+ /** Every active package's declared component names, keyed by package name. */
11
+ export type PackageCatalog = Record<string, string[]>;
12
+
13
+ /**
14
+ * Computes a {@link UiProfile}'s effective package priority order - its own declared packages, with
15
+ * {@link corePackage} prepended as the final fallback when not already present. {@link corePackage} is
16
+ * the lowest-priority fallback, so it belongs at the front of the ascending-priority order, not the
17
+ * back - prepending it (rather than appending) is what makes every explicitly listed package outrank it.
18
+ *
19
+ * @returns The packages in ascending priority order - the last entry wins when more than one declares the same name.
20
+ */
21
+ export function effectivePackages(profile: UiProfile): string[] {
22
+ return profile.packages.includes(corePackage) ? profile.packages : [corePackage, ...profile.packages];
23
+ }
24
+
25
+ /**
26
+ * Resolves a component name against a {@link UiProfile} - the runtime half of Cratis/Scene#3, run
27
+ * whenever a profile is applied. Studio's design-time tooling and Stage's build-time resolution use the
28
+ * C# twin of this algorithm in `Cratis.Scene.Engine`; both sides are asserted against the same shared
29
+ * fixture corpus so they cannot drift apart.
30
+ *
31
+ * A name containing a `.` is package-qualified - everything before the last `.` is the package,
32
+ * everything after is the bare name - and resolves directly against that one package, bypassing shadow
33
+ * tracking and the profile's priority order entirely (an author naming the package explicitly has
34
+ * already disambiguated). A name with no `.` is bare and resolves by walking {@link effectivePackages}
35
+ * from highest to lowest priority; every other active package that also declares the name is recorded in
36
+ * the result's `shadows`, not discarded, so a caller can explain the pick rather than only report it.
37
+ *
38
+ * @param requestedName The name as written on a screen - bare (`button`) or package-qualified (`Internal.Widgets.TrendChart`).
39
+ * @param profile The {@link UiProfile} whose package list to resolve against.
40
+ * @param catalog Every active package's declared component names, keyed by package name.
41
+ * @returns The {@link ComponentResolution}, or `undefined` when nothing in scope declares the name.
42
+ */
43
+ export function resolveComponentName(requestedName: string, profile: UiProfile, catalog: PackageCatalog): ComponentResolution | undefined {
44
+ const lastDot = requestedName.lastIndexOf('.');
45
+ if (lastDot >= 0) {
46
+ const qualifiedPackage = requestedName.slice(0, lastDot);
47
+ const qualifiedName = requestedName.slice(lastDot + 1);
48
+ return declares(catalog, qualifiedPackage, qualifiedName)
49
+ ? { name: qualifiedName, package: qualifiedPackage, shadows: [] }
50
+ : undefined;
51
+ }
52
+
53
+ const priority = effectivePackages(profile);
54
+ const matches: string[] = [];
55
+ for (let index = priority.length - 1; index >= 0; index--) {
56
+ if (declares(catalog, priority[index], requestedName)) {
57
+ matches.push(priority[index]);
58
+ }
59
+ }
60
+
61
+ return matches.length === 0 ? undefined : { name: requestedName, package: matches[0], shadows: matches.slice(1) };
62
+ }
63
+
64
+ function declares(catalog: PackageCatalog, packageName: string, name: string): boolean {
65
+ return catalog[packageName]?.includes(name) ?? false;
66
+ }
@@ -0,0 +1,156 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { ScenePackage } from '@cratis/scene.model';
5
+ import { MissingPackageDependency, PackageSelection, PackageVersionConflict } from './PackageSelection';
6
+ import { isVersionSatisfiedBy } from './packageVersionRange';
7
+
8
+ /**
9
+ * Expands a chosen set of package names into the complete, correctly ordered list a {@link UiProfile}
10
+ * needs — the runtime half. The C# twin in `Cratis.Scene.Engine` implements the same algorithm for
11
+ * Studio's package picker and Stage's build; both are asserted against the same shared fixture corpus so
12
+ * they cannot drift apart.
13
+ *
14
+ * This is a different concern from `resolveComponentName`, which resolves one component *name* against
15
+ * an already-decided package list. This decides what that list is.
16
+ *
17
+ * Ordering is a stable topological sort: among packages whose dependencies are equally satisfied, the one
18
+ * the caller named first comes first, and packages pulled in transitively follow in the order they were
19
+ * discovered. When a cycle makes ordering impossible, the packages involved are reported in `cycles` and
20
+ * appended in discovery order rather than dropped.
21
+ */
22
+ export function resolvePackageDependencies(selected: string[], catalog: ScenePackage[]): PackageSelection {
23
+ const index = new Map(catalog.map((scenePackage) => [scenePackage.name, scenePackage]));
24
+ const discovered: string[] = [];
25
+ const seen = new Set<string>(selected);
26
+ const missing: MissingPackageDependency[] = [];
27
+ const versionConflicts: PackageVersionConflict[] = [];
28
+
29
+ const pending = [...selected];
30
+ while (pending.length > 0) {
31
+ const name = pending.shift()!;
32
+ discovered.push(name);
33
+
34
+ const scenePackage = index.get(name);
35
+ if (!scenePackage) continue;
36
+
37
+ for (const dependency of scenePackage.dependencies) {
38
+ const dependedOn = index.get(dependency.name);
39
+ if (!dependedOn) {
40
+ missing.push({ package: scenePackage.name, dependsOn: dependency.name });
41
+ continue;
42
+ }
43
+
44
+ if (!isVersionSatisfiedBy(dependedOn.version, dependency.versionRange)) {
45
+ versionConflicts.push({
46
+ package: scenePackage.name,
47
+ dependsOn: dependency.name,
48
+ requiredRange: dependency.versionRange!,
49
+ actualVersion: dependedOn.version,
50
+ });
51
+ }
52
+
53
+ if (!seen.has(dependency.name)) {
54
+ seen.add(dependency.name);
55
+ pending.push(dependency.name);
56
+ }
57
+ }
58
+ }
59
+
60
+ const cycles = findCycles(discovered, index);
61
+ const packages = topologicallyOrder(discovered, index);
62
+ const chosen = new Set(selected);
63
+
64
+ return {
65
+ packages,
66
+ added: packages.filter((name) => !chosen.has(name)),
67
+ missing,
68
+ versionConflicts,
69
+ cycles,
70
+ };
71
+ }
72
+
73
+ function dependenciesWithin(name: string, index: Map<string, ScenePackage>, within: Set<string>): string[] {
74
+ const scenePackage = index.get(name);
75
+ if (!scenePackage) return [];
76
+ const names = scenePackage.dependencies.map((dependency) => dependency.name).filter((dependency) => within.has(dependency));
77
+ return [...new Set(names)];
78
+ }
79
+
80
+ /**
81
+ * Orders an already-closed set of package names so each follows the packages it depends on.
82
+ */
83
+ function topologicallyOrder(names: string[], index: Map<string, ScenePackage>): string[] {
84
+ const within = new Set(names);
85
+ const remaining = new Map(names.map((name) => [name, dependenciesWithin(name, index, within).length]));
86
+
87
+ const ordered: string[] = [];
88
+ const placed = new Set<string>();
89
+ let progressed = true;
90
+ while (progressed) {
91
+ progressed = false;
92
+ for (const name of names) {
93
+ if (placed.has(name) || remaining.get(name) !== 0) continue;
94
+
95
+ ordered.push(name);
96
+ placed.add(name);
97
+ progressed = true;
98
+
99
+ for (const other of names) {
100
+ if (placed.has(other)) continue;
101
+ if (dependenciesWithin(other, index, within).includes(name)) {
102
+ remaining.set(other, remaining.get(other)! - 1);
103
+ }
104
+ }
105
+ }
106
+ }
107
+
108
+ // Anything left is inside a cycle, or downstream of one. It is reported through `cycles`; keeping it
109
+ // in the list means a package picker never silently loses a package it was told about.
110
+ ordered.push(...names.filter((name) => !placed.has(name)));
111
+ return ordered;
112
+ }
113
+
114
+ /**
115
+ * Finds every dependency cycle, walking depth-first from each package in discovery order so the result
116
+ * is deterministic. A cycle is recorded once, keyed by the set of packages in it.
117
+ */
118
+ function findCycles(names: string[], index: Map<string, ScenePackage>): string[][] {
119
+ const within = new Set(names);
120
+ const cycles: string[][] = [];
121
+ const recorded = new Set<string>();
122
+ const path: string[] = [];
123
+ const onPath = new Set<string>();
124
+ const explored = new Set<string>();
125
+
126
+ const walk = (name: string) => {
127
+ if (onPath.has(name)) {
128
+ const cycle = path.slice(path.indexOf(name));
129
+ const key = [...cycle].sort().join(' ');
130
+ if (!recorded.has(key)) {
131
+ recorded.add(key);
132
+ cycles.push(cycle);
133
+ }
134
+
135
+ return;
136
+ }
137
+
138
+ if (explored.has(name)) return;
139
+ explored.add(name);
140
+
141
+ path.push(name);
142
+ onPath.add(name);
143
+ for (const dependency of dependenciesWithin(name, index, within)) {
144
+ walk(dependency);
145
+ }
146
+
147
+ onPath.delete(name);
148
+ path.pop();
149
+ };
150
+
151
+ for (const name of names) {
152
+ walk(name);
153
+ }
154
+
155
+ return cycles;
156
+ }
@@ -0,0 +1,191 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { Layout, ScreenTemplate } from '@cratis/scene.model';
5
+
6
+ /**
7
+ * Where one {@link ScreenTemplate} ended up: the slot it declared, and the layout or template that turned
8
+ * out to declare that slot.
9
+ */
10
+ export interface ScreenTemplatePlacement {
11
+ /** The template being placed. */
12
+ template: string;
13
+ /** The slot name the template declared it fits. */
14
+ slot: string;
15
+ /** The name of the {@link Layout} or {@link ScreenTemplate} declaring that slot. */
16
+ container: string;
17
+ /** How far below the layout the template sits — 1 directly inside the layout, 2 inside a template that is. */
18
+ depth: number;
19
+ }
20
+
21
+ /**
22
+ * A template that names a slot, where the set of containers in scope does not agree on exactly one home for
23
+ * it.
24
+ */
25
+ export interface UnplacedScreenTemplate {
26
+ /** The template that could not be placed. */
27
+ template: string;
28
+ /** The slot name it declared it fits. */
29
+ slot: string;
30
+ /**
31
+ * The containers declaring a slot of that name. Empty when nothing declares it; more than one when the
32
+ * name is ambiguous. Never exactly one — that case is a {@link ScreenTemplatePlacement} instead.
33
+ */
34
+ candidates: string[];
35
+ }
36
+
37
+ /**
38
+ * The outcome of working out how a blueprint's templates nest inside its layout.
39
+ */
40
+ export interface ScreenTemplateResolution {
41
+ /** Every template that found exactly one home, ordered shallowest first so a caller can build the tree top-down. */
42
+ placements: ScreenTemplatePlacement[];
43
+ /**
44
+ * Templates that found none or several. Reported rather than guessed: placing a template in the wrong
45
+ * parent renders content in the wrong region, which is far harder to diagnose than being told the slot
46
+ * name is ambiguous.
47
+ */
48
+ unplaced: UnplacedScreenTemplate[];
49
+ /** Template nesting cycles — a template that transitively contains itself. */
50
+ cycles: string[][];
51
+ }
52
+
53
+ /**
54
+ * Whether every template found exactly one home and nothing nests inside itself.
55
+ */
56
+ export function isScreenTemplateResolutionValid(resolution: ScreenTemplateResolution): boolean {
57
+ return resolution.unplaced.length === 0 && resolution.cycles.length === 0;
58
+ }
59
+
60
+ /**
61
+ * Works out how a blueprint's {@link ScreenTemplate}s nest inside its {@link Layout}.
62
+ *
63
+ * A template declares only the *name* of the slot it fits, never which container owns that slot. That is
64
+ * what makes templates reusable — a feature's template says "I go in the module content area", not "I go
65
+ * inside this specific module". Resolution is the step that turns those names into a tree, by finding which
66
+ * layout or template declares each name.
67
+ *
68
+ * The same rule applies at every level, so nesting has no depth limit and no separate mechanism per level.
69
+ * The C# twin in `Cratis.Scene.Engine` implements the same algorithm; both are asserted against the same
70
+ * shared fixture corpus so they cannot drift apart.
71
+ */
72
+ export function resolveScreenTemplates(layout: Layout, templates: ScreenTemplate[]): ScreenTemplateResolution {
73
+ const containersBySlot = buildSlotIndex(layout, templates);
74
+ const parents = new Map<string, string>();
75
+ const slots = new Map<string, string>();
76
+ const unplaced: UnplacedScreenTemplate[] = [];
77
+
78
+ for (const template of templates) {
79
+ if (template.fitsSlot === undefined) continue;
80
+
81
+ const [qualifier, slot] = splitQualifiedSlot(template.fitsSlot);
82
+
83
+ const candidates = (containersBySlot.get(slot) ?? []).filter(
84
+ (container) => container !== template.name && (qualifier === undefined || container === qualifier)
85
+ );
86
+
87
+ if (candidates.length === 1) {
88
+ parents.set(template.name, candidates[0]);
89
+ slots.set(template.name, slot);
90
+ } else {
91
+ unplaced.push({ template: template.name, slot: template.fitsSlot, candidates });
92
+ }
93
+ }
94
+
95
+ const cycles = findCycles(parents);
96
+ const inCycle = new Set(cycles.flat());
97
+
98
+ const placements: ScreenTemplatePlacement[] = [];
99
+ for (const template of templates) {
100
+ const container = parents.get(template.name);
101
+ if (container === undefined || inCycle.has(template.name)) continue;
102
+
103
+ placements.push({
104
+ template: template.name,
105
+ slot: slots.get(template.name)!,
106
+ container,
107
+ depth: depthOf(template.name, parents, layout.name),
108
+ });
109
+ }
110
+
111
+ placements.sort((left, right) => left.depth - right.depth || left.template.localeCompare(right.template));
112
+
113
+ return { placements, unplaced, cycles };
114
+ }
115
+
116
+ /**
117
+ * Splits a `fitsSlot` into the container it names and the slot within it.
118
+ *
119
+ * The same rule component names use: a bare name searches, a qualified one goes straight to what it names.
120
+ * A slot called `body` is a good name at every level of a nesting chain, so several templates legitimately
121
+ * declare one — and a bare `body` then has no single answer. Qualifying it says which, without forcing
122
+ * every slot in an application to carry a unique name.
123
+ */
124
+ function splitQualifiedSlot(fitsSlot: string): [string | undefined, string] {
125
+ const lastDot = fitsSlot.lastIndexOf('.');
126
+ return lastDot < 0 ? [undefined, fitsSlot] : [fitsSlot.substring(0, lastDot), fitsSlot.substring(lastDot + 1)];
127
+ }
128
+
129
+ function buildSlotIndex(layout: Layout, templates: ScreenTemplate[]): Map<string, string[]> {
130
+ const index = new Map<string, string[]>();
131
+
132
+ const declare = (slot: string, container: string) => {
133
+ const containers = index.get(slot) ?? [];
134
+ if (!containers.includes(container)) containers.push(container);
135
+ index.set(slot, containers);
136
+ };
137
+
138
+ for (const slot of layout.slots) declare(slot.name, layout.name);
139
+ for (const template of templates) {
140
+ for (const slot of template.slots) declare(slot.name, template.name);
141
+ }
142
+
143
+ return index;
144
+ }
145
+
146
+ function findCycles(parents: Map<string, string>): string[][] {
147
+ const cycles: string[][] = [];
148
+ const recorded = new Set<string>();
149
+
150
+ for (const start of [...parents.keys()].sort()) {
151
+ const path: string[] = [];
152
+ const onPath = new Set<string>();
153
+ let current = start;
154
+
155
+ for (;;) {
156
+ const parent = parents.get(current);
157
+ if (parent === undefined) break;
158
+
159
+ path.push(current);
160
+ onPath.add(current);
161
+ if (onPath.has(parent)) {
162
+ const cycle = path.slice(path.indexOf(parent));
163
+ const key = [...cycle].sort().join(' ');
164
+ if (!recorded.has(key)) {
165
+ recorded.add(key);
166
+ cycles.push(cycle);
167
+ }
168
+
169
+ break;
170
+ }
171
+
172
+ onPath.add(parent);
173
+ current = parent;
174
+ }
175
+ }
176
+
177
+ return cycles;
178
+ }
179
+
180
+ function depthOf(template: string, parents: Map<string, string>, layoutName: string): number {
181
+ let depth = 0;
182
+ let current = template;
183
+ while (parents.has(current) && depth <= parents.size) {
184
+ depth++;
185
+ const parent = parents.get(current)!;
186
+ if (parent === layoutName) break;
187
+ current = parent;
188
+ }
189
+
190
+ return depth;
191
+ }
@@ -0,0 +1,46 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { Theme, UiProfile } from '@cratis/scene.model';
5
+
6
+ /**
7
+ * Finds the packages a {@link UiProfile} declares that a {@link Theme} is not declared compatible with -
8
+ * part of Cratis/Scene#5, the same rule Screenplay's compiler already applies to warn on an incompatible
9
+ * `ui profile`/`theme` pairing, reused here so `Scene.React` knows which packages to actually apply a
10
+ * theme's tokens to, and so Studio's design-time tooling and Stage's build-time checks don't each
11
+ * reimplement it. There is deliberately no implicit exemption for `core`: a theme wanting broad
12
+ * applicability declares `compatible with core` itself, exactly like the profile/package resolver has no
13
+ * implicit special case for it either.
14
+ *
15
+ * @param theme The {@link Theme} to check.
16
+ * @param profile The {@link UiProfile} whose packages to check against.
17
+ * @returns The packages in `profile.packages` that `theme` does not declare compatibility with.
18
+ */
19
+ export function incompatiblePackages(theme: Theme, profile: UiProfile): string[] {
20
+ return profile.packages.filter(package_ => !theme.compatibleWith.includes(package_));
21
+ }
22
+
23
+ /**
24
+ * Whether a {@link Theme} is compatible with every package a {@link UiProfile} declares.
25
+ *
26
+ * @param theme The {@link Theme} to check.
27
+ * @param profile The {@link UiProfile} whose packages to check against.
28
+ * @returns `true` when `theme` declares compatibility with every package `profile` lists.
29
+ */
30
+ export function isThemeCompatible(theme: Theme, profile: UiProfile): boolean {
31
+ return incompatiblePackages(theme, profile).length === 0;
32
+ }
33
+
34
+ /**
35
+ * Finds the packages a {@link Theme}'s tokens actually apply to for a given {@link UiProfile} - the
36
+ * packages the profile activates that the theme also declares compatibility with. This is what a
37
+ * renderer scopes token application to, rather than applying a theme's tokens globally - and is
38
+ * recomputed on every theme switch for the live re-resolution Cratis/Scene#5 requires (no reload).
39
+ *
40
+ * @param theme The {@link Theme} to apply.
41
+ * @param profile The active {@link UiProfile}.
42
+ * @returns The packages both active in `profile` and declared compatible by `theme`.
43
+ */
44
+ export function applicableThemePackages(theme: Theme, profile: UiProfile): string[] {
45
+ return profile.packages.filter(package_ => theme.compatibleWith.includes(package_));
46
+ }