@domainlang/language 0.1.20

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 (212) hide show
  1. package/README.md +163 -0
  2. package/out/ast-augmentation.d.ts +6 -0
  3. package/out/ast-augmentation.js +2 -0
  4. package/out/ast-augmentation.js.map +1 -0
  5. package/out/domain-lang-module.d.ts +57 -0
  6. package/out/domain-lang-module.js +67 -0
  7. package/out/domain-lang-module.js.map +1 -0
  8. package/out/generated/ast.d.ts +759 -0
  9. package/out/generated/ast.js +556 -0
  10. package/out/generated/ast.js.map +1 -0
  11. package/out/generated/grammar.d.ts +6 -0
  12. package/out/generated/grammar.js +2407 -0
  13. package/out/generated/grammar.js.map +1 -0
  14. package/out/generated/module.d.ts +13 -0
  15. package/out/generated/module.js +21 -0
  16. package/out/generated/module.js.map +1 -0
  17. package/out/index.d.ts +16 -0
  18. package/out/index.js +22 -0
  19. package/out/index.js.map +1 -0
  20. package/out/lsp/domain-lang-code-actions.d.ts +55 -0
  21. package/out/lsp/domain-lang-code-actions.js +143 -0
  22. package/out/lsp/domain-lang-code-actions.js.map +1 -0
  23. package/out/lsp/domain-lang-completion.d.ts +37 -0
  24. package/out/lsp/domain-lang-completion.js +452 -0
  25. package/out/lsp/domain-lang-completion.js.map +1 -0
  26. package/out/lsp/domain-lang-formatter.d.ts +15 -0
  27. package/out/lsp/domain-lang-formatter.js +43 -0
  28. package/out/lsp/domain-lang-formatter.js.map +1 -0
  29. package/out/lsp/domain-lang-naming.d.ts +34 -0
  30. package/out/lsp/domain-lang-naming.js +49 -0
  31. package/out/lsp/domain-lang-naming.js.map +1 -0
  32. package/out/lsp/domain-lang-scope.d.ts +59 -0
  33. package/out/lsp/domain-lang-scope.js +102 -0
  34. package/out/lsp/domain-lang-scope.js.map +1 -0
  35. package/out/lsp/domain-lang-workspace-manager.d.ts +21 -0
  36. package/out/lsp/domain-lang-workspace-manager.js +93 -0
  37. package/out/lsp/domain-lang-workspace-manager.js.map +1 -0
  38. package/out/lsp/hover/ddd-pattern-explanations.d.ts +50 -0
  39. package/out/lsp/hover/ddd-pattern-explanations.js +196 -0
  40. package/out/lsp/hover/ddd-pattern-explanations.js.map +1 -0
  41. package/out/lsp/hover/domain-lang-hover.d.ts +19 -0
  42. package/out/lsp/hover/domain-lang-hover.js +302 -0
  43. package/out/lsp/hover/domain-lang-hover.js.map +1 -0
  44. package/out/lsp/hover/domain-lang-keywords.d.ts +13 -0
  45. package/out/lsp/hover/domain-lang-keywords.js +47 -0
  46. package/out/lsp/hover/domain-lang-keywords.js.map +1 -0
  47. package/out/lsp/manifest-diagnostics.d.ts +82 -0
  48. package/out/lsp/manifest-diagnostics.js +230 -0
  49. package/out/lsp/manifest-diagnostics.js.map +1 -0
  50. package/out/main-browser.d.ts +1 -0
  51. package/out/main-browser.js +11 -0
  52. package/out/main-browser.js.map +1 -0
  53. package/out/main.d.ts +1 -0
  54. package/out/main.js +74 -0
  55. package/out/main.js.map +1 -0
  56. package/out/sdk/ast-augmentation.d.ts +136 -0
  57. package/out/sdk/ast-augmentation.js +62 -0
  58. package/out/sdk/ast-augmentation.js.map +1 -0
  59. package/out/sdk/index.d.ts +94 -0
  60. package/out/sdk/index.js +97 -0
  61. package/out/sdk/index.js.map +1 -0
  62. package/out/sdk/indexes.d.ts +16 -0
  63. package/out/sdk/indexes.js +97 -0
  64. package/out/sdk/indexes.js.map +1 -0
  65. package/out/sdk/loader-node.d.ts +51 -0
  66. package/out/sdk/loader-node.js +119 -0
  67. package/out/sdk/loader-node.js.map +1 -0
  68. package/out/sdk/loader.d.ts +49 -0
  69. package/out/sdk/loader.js +85 -0
  70. package/out/sdk/loader.js.map +1 -0
  71. package/out/sdk/patterns.d.ts +93 -0
  72. package/out/sdk/patterns.js +123 -0
  73. package/out/sdk/patterns.js.map +1 -0
  74. package/out/sdk/query.d.ts +90 -0
  75. package/out/sdk/query.js +679 -0
  76. package/out/sdk/query.js.map +1 -0
  77. package/out/sdk/resolution.d.ts +52 -0
  78. package/out/sdk/resolution.js +68 -0
  79. package/out/sdk/resolution.js.map +1 -0
  80. package/out/sdk/types.d.ts +280 -0
  81. package/out/sdk/types.js +8 -0
  82. package/out/sdk/types.js.map +1 -0
  83. package/out/services/dependency-analyzer.d.ts +58 -0
  84. package/out/services/dependency-analyzer.js +254 -0
  85. package/out/services/dependency-analyzer.js.map +1 -0
  86. package/out/services/dependency-resolver.d.ts +146 -0
  87. package/out/services/dependency-resolver.js +452 -0
  88. package/out/services/dependency-resolver.js.map +1 -0
  89. package/out/services/git-url-resolver.browser.d.ts +10 -0
  90. package/out/services/git-url-resolver.browser.js +19 -0
  91. package/out/services/git-url-resolver.browser.js.map +1 -0
  92. package/out/services/git-url-resolver.d.ts +158 -0
  93. package/out/services/git-url-resolver.js +416 -0
  94. package/out/services/git-url-resolver.js.map +1 -0
  95. package/out/services/governance-validator.d.ts +44 -0
  96. package/out/services/governance-validator.js +153 -0
  97. package/out/services/governance-validator.js.map +1 -0
  98. package/out/services/import-resolver.d.ts +77 -0
  99. package/out/services/import-resolver.js +240 -0
  100. package/out/services/import-resolver.js.map +1 -0
  101. package/out/services/performance-optimizer.d.ts +60 -0
  102. package/out/services/performance-optimizer.js +140 -0
  103. package/out/services/performance-optimizer.js.map +1 -0
  104. package/out/services/relationship-inference.d.ts +11 -0
  105. package/out/services/relationship-inference.js +98 -0
  106. package/out/services/relationship-inference.js.map +1 -0
  107. package/out/services/semver.d.ts +98 -0
  108. package/out/services/semver.js +195 -0
  109. package/out/services/semver.js.map +1 -0
  110. package/out/services/types.d.ts +340 -0
  111. package/out/services/types.js +46 -0
  112. package/out/services/types.js.map +1 -0
  113. package/out/services/workspace-manager.d.ts +123 -0
  114. package/out/services/workspace-manager.js +489 -0
  115. package/out/services/workspace-manager.js.map +1 -0
  116. package/out/syntaxes/domain-lang.monarch.d.ts +76 -0
  117. package/out/syntaxes/domain-lang.monarch.js +29 -0
  118. package/out/syntaxes/domain-lang.monarch.js.map +1 -0
  119. package/out/utils/import-utils.d.ts +49 -0
  120. package/out/utils/import-utils.js +128 -0
  121. package/out/utils/import-utils.js.map +1 -0
  122. package/out/validation/bounded-context.d.ts +11 -0
  123. package/out/validation/bounded-context.js +79 -0
  124. package/out/validation/bounded-context.js.map +1 -0
  125. package/out/validation/classification.d.ts +3 -0
  126. package/out/validation/classification.js +3 -0
  127. package/out/validation/classification.js.map +1 -0
  128. package/out/validation/constants.d.ts +180 -0
  129. package/out/validation/constants.js +235 -0
  130. package/out/validation/constants.js.map +1 -0
  131. package/out/validation/domain-lang-validator.d.ts +2 -0
  132. package/out/validation/domain-lang-validator.js +27 -0
  133. package/out/validation/domain-lang-validator.js.map +1 -0
  134. package/out/validation/domain.d.ts +11 -0
  135. package/out/validation/domain.js +63 -0
  136. package/out/validation/domain.js.map +1 -0
  137. package/out/validation/import.d.ts +68 -0
  138. package/out/validation/import.js +237 -0
  139. package/out/validation/import.js.map +1 -0
  140. package/out/validation/manifest.d.ts +144 -0
  141. package/out/validation/manifest.js +327 -0
  142. package/out/validation/manifest.js.map +1 -0
  143. package/out/validation/maps.d.ts +21 -0
  144. package/out/validation/maps.js +60 -0
  145. package/out/validation/maps.js.map +1 -0
  146. package/out/validation/metadata.d.ts +7 -0
  147. package/out/validation/metadata.js +16 -0
  148. package/out/validation/metadata.js.map +1 -0
  149. package/out/validation/model.d.ts +12 -0
  150. package/out/validation/model.js +29 -0
  151. package/out/validation/model.js.map +1 -0
  152. package/out/validation/relationships.d.ts +12 -0
  153. package/out/validation/relationships.js +94 -0
  154. package/out/validation/relationships.js.map +1 -0
  155. package/out/validation/shared.d.ts +6 -0
  156. package/out/validation/shared.js +12 -0
  157. package/out/validation/shared.js.map +1 -0
  158. package/package.json +110 -0
  159. package/src/ast-augmentation.ts +5 -0
  160. package/src/domain-lang-module.ts +112 -0
  161. package/src/domain-lang.langium +351 -0
  162. package/src/generated/ast.ts +986 -0
  163. package/src/generated/grammar.ts +2409 -0
  164. package/src/generated/module.ts +25 -0
  165. package/src/index.ts +24 -0
  166. package/src/lsp/domain-lang-code-actions.ts +189 -0
  167. package/src/lsp/domain-lang-completion.ts +514 -0
  168. package/src/lsp/domain-lang-formatter.ts +51 -0
  169. package/src/lsp/domain-lang-naming.ts +56 -0
  170. package/src/lsp/domain-lang-scope.ts +137 -0
  171. package/src/lsp/domain-lang-workspace-manager.ts +104 -0
  172. package/src/lsp/hover/ddd-pattern-explanations.ts +237 -0
  173. package/src/lsp/hover/domain-lang-hover.ts +338 -0
  174. package/src/lsp/hover/domain-lang-keywords.ts +50 -0
  175. package/src/lsp/manifest-diagnostics.ts +290 -0
  176. package/src/main-browser.ts +15 -0
  177. package/src/main.ts +85 -0
  178. package/src/sdk/README.md +297 -0
  179. package/src/sdk/ast-augmentation.ts +157 -0
  180. package/src/sdk/index.ts +126 -0
  181. package/src/sdk/indexes.ts +155 -0
  182. package/src/sdk/loader-node.ts +146 -0
  183. package/src/sdk/loader.ts +99 -0
  184. package/src/sdk/patterns.ts +147 -0
  185. package/src/sdk/query.ts +802 -0
  186. package/src/sdk/resolution.ts +78 -0
  187. package/src/sdk/types.ts +323 -0
  188. package/src/services/dependency-analyzer.ts +321 -0
  189. package/src/services/dependency-resolver.ts +551 -0
  190. package/src/services/git-url-resolver.browser.ts +26 -0
  191. package/src/services/git-url-resolver.ts +517 -0
  192. package/src/services/governance-validator.ts +177 -0
  193. package/src/services/import-resolver.ts +292 -0
  194. package/src/services/performance-optimizer.ts +170 -0
  195. package/src/services/relationship-inference.ts +121 -0
  196. package/src/services/semver.ts +213 -0
  197. package/src/services/types.ts +415 -0
  198. package/src/services/workspace-manager.ts +607 -0
  199. package/src/syntaxes/domain-lang.monarch.ts +29 -0
  200. package/src/utils/import-utils.ts +152 -0
  201. package/src/validation/bounded-context.ts +99 -0
  202. package/src/validation/classification.ts +5 -0
  203. package/src/validation/constants.ts +304 -0
  204. package/src/validation/domain-lang-validator.ts +33 -0
  205. package/src/validation/domain.ts +77 -0
  206. package/src/validation/import.ts +295 -0
  207. package/src/validation/manifest.ts +439 -0
  208. package/src/validation/maps.ts +76 -0
  209. package/src/validation/metadata.ts +18 -0
  210. package/src/validation/model.ts +37 -0
  211. package/src/validation/relationships.ts +154 -0
  212. package/src/validation/shared.ts +14 -0
@@ -0,0 +1,517 @@
1
+ /**
2
+ * Git Repository Resolver Service
3
+ *
4
+ * Resolves git-based package imports to local cached repositories.
5
+ * Supports simplified GitHub syntax (owner/repo@version) and full URLs.
6
+ *
7
+ * Design: Repository-level imports (not individual files)
8
+ * - Imports load entire package
9
+ * - Package entry point defined in model.yaml
10
+ * - Version pinning at repository level
11
+ */
12
+
13
+ import { URI } from 'langium';
14
+ import path from 'node:path';
15
+ import fs from 'node:fs/promises';
16
+ import { exec } from 'node:child_process';
17
+ import { promisify } from 'node:util';
18
+ import YAML from 'yaml';
19
+ import type { GitImportInfo, ResolvingPackage, LockFile } from './types.js';
20
+
21
+ const execAsync = promisify(exec);
22
+
23
+ /**
24
+ * Parses import URLs into structured git import information.
25
+ *
26
+ * Supported formats:
27
+ * - owner/repo@version (GitHub assumed)
28
+ * - owner/repo (GitHub, defaults to main)
29
+ * - https://github.com/owner/repo@version
30
+ * - https://gitlab.com/owner/repo@version
31
+ * - https://git.example.com/owner/repo@version
32
+ */
33
+ export class GitUrlParser {
34
+ /**
35
+ * Determines if an import string is a git repository import.
36
+ */
37
+ static isGitUrl(importStr: string): boolean {
38
+ // GitHub shorthand: owner/repo or owner/repo@version
39
+ if (/^[a-zA-Z0-9-]+\/[a-zA-Z0-9-_.]+(@[^/]+)?$/.test(importStr)) {
40
+ return true;
41
+ }
42
+
43
+ // Full URLs
44
+ return (
45
+ importStr.startsWith('https://github.com/') ||
46
+ importStr.startsWith('https://gitlab.com/') ||
47
+ importStr.startsWith('https://bitbucket.org/') ||
48
+ importStr.startsWith('https://git.') ||
49
+ importStr.startsWith('git://')
50
+ );
51
+ }
52
+
53
+ /**
54
+ * Parses a git import URL into structured components.
55
+ *
56
+ * @param importStr - The import URL string
57
+ * @returns Parsed git import information
58
+ * @throws Error if URL format is invalid
59
+ */
60
+ static parse(importStr: string): GitImportInfo {
61
+ // Handle GitHub shorthand (owner/repo or owner/repo@version)
62
+ if (this.isGitHubShorthand(importStr)) {
63
+ return this.parseGitHubShorthand(importStr);
64
+ }
65
+
66
+ // Handle full URLs
67
+ if (importStr.startsWith('https://') || importStr.startsWith('git://')) {
68
+ return this.parseFullUrl(importStr);
69
+ }
70
+
71
+ throw new Error(
72
+ `Invalid git import URL: '${importStr}'.\n` +
73
+ `Hint: Use 'owner/repo' or 'owner/repo@version' format (e.g., 'domainlang/core@v1.0.0').`
74
+ );
75
+ }
76
+
77
+ /**
78
+ * Checks if string is GitHub shorthand format.
79
+ */
80
+ private static isGitHubShorthand(importStr: string): boolean {
81
+ return /^[a-zA-Z0-9-]+\/[a-zA-Z0-9-_.]+(@[^/]+)?$/.test(importStr);
82
+ }
83
+
84
+ /**
85
+ * Parses GitHub shorthand (owner/repo or owner/repo@version).
86
+ */
87
+ private static parseGitHubShorthand(importStr: string): GitImportInfo {
88
+ const match = importStr.match(/^([a-zA-Z0-9-]+)\/([a-zA-Z0-9-_.]+)(?:@([^/]+))?$/);
89
+ if (!match) {
90
+ throw new Error(
91
+ `Invalid GitHub shorthand format: '${importStr}'.\n` +
92
+ `Hint: Use 'owner/repo' or 'owner/repo@version' format.`
93
+ );
94
+ }
95
+
96
+ const [, owner, repo, version] = match;
97
+ const resolvedVersion = version || 'main';
98
+
99
+ return {
100
+ original: importStr,
101
+ platform: 'github',
102
+ owner,
103
+ repo,
104
+ version: resolvedVersion,
105
+ repoUrl: `https://github.com/${owner}/${repo}`,
106
+ entryPoint: 'index.dlang', // Default, will be resolved from model.yaml
107
+ };
108
+ }
109
+
110
+ /**
111
+ * Parses full git URLs (https://...).
112
+ *
113
+ * Supported:
114
+ * - https://github.com/owner/repo@version
115
+ * - https://gitlab.com/owner/repo@version
116
+ * - https://git.example.com/owner/repo@version
117
+ */
118
+ private static parseFullUrl(importStr: string): GitImportInfo {
119
+ // GitHub
120
+ const ghMatch = importStr.match(
121
+ /^https:\/\/github\.com\/([^/]+)\/([^/@]+)(?:@([^/]+))?$/
122
+ );
123
+ if (ghMatch) {
124
+ const [, owner, repo, version] = ghMatch;
125
+ return {
126
+ original: importStr,
127
+ platform: 'github',
128
+ owner,
129
+ repo,
130
+ version: version || 'main',
131
+ repoUrl: `https://github.com/${owner}/${repo}`,
132
+ entryPoint: 'index.dlang',
133
+ };
134
+ }
135
+
136
+ // GitLab
137
+ const glMatch = importStr.match(
138
+ /^https:\/\/gitlab\.com\/([^/]+)\/([^/@]+)(?:@([^/]+))?$/
139
+ );
140
+ if (glMatch) {
141
+ const [, owner, repo, version] = glMatch;
142
+ return {
143
+ original: importStr,
144
+ platform: 'gitlab',
145
+ owner,
146
+ repo,
147
+ version: version || 'main',
148
+ repoUrl: `https://gitlab.com/${owner}/${repo}`,
149
+ entryPoint: 'index.dlang',
150
+ };
151
+ }
152
+
153
+ // Bitbucket
154
+ const bbMatch = importStr.match(
155
+ /^https:\/\/bitbucket\.org\/([^/]+)\/([^/@]+)(?:@([^/]+))?$/
156
+ );
157
+ if (bbMatch) {
158
+ const [, owner, repo, version] = bbMatch;
159
+ return {
160
+ original: importStr,
161
+ platform: 'bitbucket',
162
+ owner,
163
+ repo,
164
+ version: version || 'main',
165
+ repoUrl: `https://bitbucket.org/${owner}/${repo}`,
166
+ entryPoint: 'index.dlang',
167
+ };
168
+ }
169
+
170
+ // Generic git URL
171
+ const genericMatch = importStr.match(
172
+ /^(?:https|git):\/\/([^/]+)\/([^/]+)\/([^/@]+)(?:@([^/]+))?$/
173
+ );
174
+ if (genericMatch) {
175
+ const [, host, owner, repo, version] = genericMatch;
176
+ return {
177
+ original: importStr,
178
+ platform: 'generic',
179
+ owner,
180
+ repo,
181
+ version: version || 'main',
182
+ repoUrl: `https://${host}/${owner}/${repo}`,
183
+ entryPoint: 'index.dlang',
184
+ };
185
+ }
186
+
187
+ throw new Error(
188
+ `Unsupported git URL format: '${importStr}'.\n` +
189
+ `Supported formats:\n` +
190
+ ` • owner/repo (GitHub shorthand)\n` +
191
+ ` • owner/repo@version\n` +
192
+ ` • https://github.com/owner/repo\n` +
193
+ ` • https://gitlab.com/owner/repo`
194
+ );
195
+ }
196
+ }
197
+
198
+ /**
199
+ * Resolves git repository imports to local entry point files.
200
+ *
201
+ * Implements a content-addressable cache:
202
+ * - Cache location: .dlang/packages/ (project-local, per PRS-010)
203
+ * - Cache key: {owner}/{repo}/{commit-hash}
204
+ * - Downloads entire repository on first use
205
+ * - Reads model.yaml to find entry point
206
+ * - Returns URI to entry point file
207
+ */
208
+ export class GitUrlResolver {
209
+ private cacheDir: string;
210
+ private lockFile?: LockFile;
211
+
212
+ /**
213
+ * Creates a GitUrlResolver with a project-local cache directory.
214
+ *
215
+ * @param cacheDir - The cache directory path. Per PRS-010, this should be
216
+ * the project's `.dlang/packages/` directory for isolation
217
+ * and reproducibility (like node_modules).
218
+ */
219
+ constructor(cacheDir: string) {
220
+ this.cacheDir = cacheDir;
221
+ }
222
+
223
+ /**
224
+ * Sets the lock file for dependency resolution.
225
+ *
226
+ * When a lock file is set, all package imports will use
227
+ * the locked commit hashes instead of resolving versions.
228
+ * This ensures reproducible builds and handles transitive dependencies.
229
+ *
230
+ * @param lockFile - The parsed lock file from the workspace root
231
+ */
232
+ setLockFile(lockFile: LockFile | undefined): void {
233
+ this.lockFile = lockFile;
234
+ }
235
+
236
+ /**
237
+ * Resolves a git import URL to the package's entry point file.
238
+ *
239
+ * Process:
240
+ * 1. Parse git URL
241
+ * 2. Check lock file for pinned version (transitive dependency support)
242
+ * 3. Resolve version to commit hash (if not locked)
243
+ * 4. Check cache
244
+ * 5. Download repository if not cached
245
+ * 6. Read model.yaml to find entry point
246
+ * 7. Return URI to entry point file
247
+ *
248
+ * @param importUrl - The git import URL
249
+ * @returns URI to the package's entry point file
250
+ */
251
+ async resolve(importUrl: string, options: { allowNetwork?: boolean } = {}): Promise<URI> {
252
+ const gitInfo = GitUrlParser.parse(importUrl);
253
+
254
+ // Check lock file for pinned version (handles transitive dependencies)
255
+ let commitHash: string;
256
+ const packageKey = `${gitInfo.owner}/${gitInfo.repo}`;
257
+
258
+ if (this.lockFile?.dependencies[packageKey]) {
259
+ // Use locked commit hash (reproducible build)
260
+ commitHash = this.lockFile.dependencies[packageKey].commit;
261
+ } else {
262
+ // No lock file entry - need to resolve dynamically
263
+ if (options.allowNetwork === false) {
264
+ // LSP mode: cannot perform network operations
265
+ throw new Error(
266
+ `Dependency '${packageKey}' not installed.\n` +
267
+ `Hint: Run 'dlang install' to fetch dependencies and generate model.lock.`
268
+ );
269
+ }
270
+ // CLI/dev mode: resolve version via network
271
+ commitHash = await this.resolveCommit(gitInfo);
272
+ }
273
+
274
+ // Check cache
275
+ const cachedPath = this.getCachePath(gitInfo, commitHash);
276
+
277
+ if (!(await this.existsInCache(cachedPath))) {
278
+ if (options.allowNetwork === false) {
279
+ throw new Error(
280
+ `Dependency '${packageKey}' not installed.\n` +
281
+ `Hint: Run 'dlang install' to fetch dependencies.`
282
+ );
283
+ }
284
+
285
+ // Download repository
286
+ await this.downloadRepo(gitInfo, commitHash, cachedPath);
287
+ }
288
+
289
+ // Read package metadata to get entry point
290
+ const entryPoint = await this.getEntryPoint(cachedPath);
291
+ const entryFile = path.join(cachedPath, entryPoint);
292
+
293
+ // Verify entry point exists
294
+ if (!(await this.existsInCache(entryFile))) {
295
+ throw new Error(
296
+ `Entry point '${entryPoint}' not found in package '${gitInfo.owner}/${gitInfo.repo}@${gitInfo.version}'.\n` +
297
+ `Hint: Ensure the package has an entry point file (default: index.dlang).`
298
+ );
299
+ }
300
+
301
+ return URI.file(entryFile);
302
+ }
303
+
304
+ /**
305
+ * Reads model.yaml to get the package entry point.
306
+ * Falls back to index.dlang if no model.yaml found.
307
+ */
308
+ private async getEntryPoint(repoPath: string): Promise<string> {
309
+ const yamlPath = path.join(repoPath, 'model.yaml');
310
+
311
+ try {
312
+ const yamlContent = await fs.readFile(yamlPath, 'utf-8');
313
+ const metadata = this.parseYaml(yamlContent);
314
+ return metadata.entry ?? 'index.dlang';
315
+ } catch {
316
+ // No model.yaml or parse error, use default
317
+ return 'index.dlang';
318
+ }
319
+ }
320
+
321
+ /**
322
+ * Parses model.yaml content to extract entry point.
323
+ *
324
+ * Expected structure:
325
+ * model:
326
+ * entry: index.dlang
327
+ */
328
+ private parseYaml(content: string): ResolvingPackage {
329
+ const parsed = YAML.parse(content) as {
330
+ model?: {
331
+ name?: string;
332
+ version?: string;
333
+ entry?: string;
334
+ };
335
+ };
336
+
337
+ return {
338
+ entry: parsed.model?.entry,
339
+ name: parsed.model?.name,
340
+ version: parsed.model?.version,
341
+ };
342
+ }
343
+
344
+ /**
345
+ * Resolves a version (tag/branch) to a commit hash using git ls-remote.
346
+ */
347
+ private async resolveCommit(gitInfo: GitImportInfo): Promise<string> {
348
+ try {
349
+ // Try to resolve as tag or branch
350
+ const { stdout } = await execAsync(
351
+ `git ls-remote ${gitInfo.repoUrl} ${gitInfo.version}`
352
+ );
353
+
354
+ if (stdout.trim()) {
355
+ const commitHash = stdout.split('\t')[0];
356
+ return commitHash;
357
+ }
358
+
359
+ // If not found, assume it's already a commit hash
360
+ if (/^[0-9a-f]{7,40}$/i.test(gitInfo.version)) {
361
+ return gitInfo.version;
362
+ }
363
+
364
+ throw new Error(
365
+ `Could not resolve version '${gitInfo.version}' for ${gitInfo.repoUrl}.\n` +
366
+ `Hint: Check that the version (tag, branch, or commit) exists in the repository.`
367
+ );
368
+ } catch (error) {
369
+ throw new Error(
370
+ `Failed to resolve git version '${gitInfo.version}' for ${gitInfo.repoUrl}.\n` +
371
+ `Error: ${error}\n` +
372
+ `Hint: Verify the repository URL is correct and accessible.`
373
+ );
374
+ }
375
+ }
376
+
377
+ /**
378
+ * Gets the local cache path for a git repository.
379
+ *
380
+ * Format: .dlang/packages/{owner}/{repo}/{version}/
381
+ *
382
+ * Per PRS-010: Project-local cache structure mirrors the Design Considerations
383
+ * section showing `.dlang/packages/{owner}/{repo}/{version}/` layout.
384
+ */
385
+ private getCachePath(gitInfo: GitImportInfo, commitHash: string): string {
386
+ return path.join(
387
+ this.cacheDir,
388
+ gitInfo.owner,
389
+ gitInfo.repo,
390
+ commitHash
391
+ );
392
+ }
393
+
394
+ /**
395
+ * Checks if a file or directory exists in the cache.
396
+ */
397
+ private async existsInCache(filePath: string): Promise<boolean> {
398
+ try {
399
+ await fs.access(filePath);
400
+ return true;
401
+ } catch {
402
+ return false;
403
+ }
404
+ }
405
+
406
+ /**
407
+ * Downloads a git repository to the cache.
408
+ *
409
+ * Uses shallow clone for efficiency (only downloads the specific commit).
410
+ */
411
+ private async downloadRepo(
412
+ gitInfo: GitImportInfo,
413
+ commitHash: string,
414
+ cachePath: string
415
+ ): Promise<void> {
416
+ const targetDir = path.resolve(cachePath);
417
+ const parentDir = path.dirname(targetDir);
418
+ await fs.mkdir(parentDir, { recursive: true });
419
+
420
+ try {
421
+ await execAsync(
422
+ `git clone ${gitInfo.repoUrl}.git "${targetDir}" --no-checkout`
423
+ );
424
+
425
+ await execAsync(
426
+ `git -C "${targetDir}" fetch --depth 1 origin ${commitHash}`
427
+ );
428
+
429
+ await execAsync(
430
+ `git -C "${targetDir}" checkout --force --detach ${commitHash}`
431
+ );
432
+
433
+ await fs.rm(path.join(targetDir, '.git'), { recursive: true, force: true });
434
+ } catch (error) {
435
+ await fs.rm(targetDir, { recursive: true, force: true });
436
+ const message = error instanceof Error ? error.message : String(error);
437
+ throw new Error(
438
+ `Failed to download package '${gitInfo.owner}/${gitInfo.repo}@${gitInfo.version}'.\n` +
439
+ `Error: ${message}\n` +
440
+ `Hint: Check your network connection and verify the repository URL is correct.`
441
+ );
442
+ }
443
+ }
444
+
445
+ /**
446
+ * Clears the entire cache.
447
+ */
448
+ async clearCache(): Promise<void> {
449
+ await fs.rm(this.cacheDir, { recursive: true, force: true });
450
+ }
451
+
452
+ /**
453
+ * Gets cache statistics (size, number of cached repos, etc.).
454
+ *
455
+ * Cache structure: .dlang/packages/{owner}/{repo}/{version}/
456
+ */
457
+ async getCacheStats(): Promise<{
458
+ totalSize: number;
459
+ repoCount: number;
460
+ cacheDir: string;
461
+ }> {
462
+ let totalSize = 0;
463
+ let repoCount = 0;
464
+
465
+ try {
466
+ const owners = await fs.readdir(this.cacheDir);
467
+ for (const owner of owners) {
468
+ const ownerPath = path.join(this.cacheDir, owner);
469
+ const ownerStat = await fs.stat(ownerPath);
470
+ if (!ownerStat.isDirectory()) continue;
471
+
472
+ const repos = await fs.readdir(ownerPath);
473
+ for (const repo of repos) {
474
+ const repoPath = path.join(ownerPath, repo);
475
+ const repoStat = await fs.stat(repoPath);
476
+ if (!repoStat.isDirectory()) continue;
477
+
478
+ const versions = await fs.readdir(repoPath);
479
+ repoCount += versions.length;
480
+
481
+ for (const version of versions) {
482
+ const versionPath = path.join(repoPath, version);
483
+ totalSize += await this.getDirectorySize(versionPath);
484
+ }
485
+ }
486
+ }
487
+ } catch {
488
+ // Cache directory doesn't exist yet
489
+ }
490
+
491
+ return {
492
+ totalSize,
493
+ repoCount,
494
+ cacheDir: this.cacheDir,
495
+ };
496
+ }
497
+
498
+ /**
499
+ * Gets the total size of a directory in bytes.
500
+ */
501
+ private async getDirectorySize(dirPath: string): Promise<number> {
502
+ let size = 0;
503
+ const entries = await fs.readdir(dirPath, { withFileTypes: true });
504
+
505
+ for (const entry of entries) {
506
+ const entryPath = path.join(dirPath, entry.name);
507
+ if (entry.isDirectory()) {
508
+ size += await this.getDirectorySize(entryPath);
509
+ } else {
510
+ const stats = await fs.stat(entryPath);
511
+ size += stats.size;
512
+ }
513
+ }
514
+
515
+ return size;
516
+ }
517
+ }
@@ -0,0 +1,177 @@
1
+ /**
2
+ * Governance and Compliance Validation Service
3
+ *
4
+ * Enforces organizational policies and best practices:
5
+ * - Allowed/blocked dependency sources
6
+ * - Version policy enforcement (no pre-release in production)
7
+ * - Team ownership validation
8
+ * - License compliance
9
+ * - Audit trail generation
10
+ *
11
+ * Governance policies are defined in the `governance` section of model.yaml:
12
+ *
13
+ * ```yaml
14
+ * governance:
15
+ * allowedSources:
16
+ * - github.com/acme
17
+ * requireStableVersions: true
18
+ * requireTeamOwnership: true
19
+ * ```
20
+ */
21
+
22
+ import type { LockFile, GovernancePolicy, GovernanceMetadata, GovernanceViolation } from './types.js';
23
+ import path from 'node:path';
24
+ import fs from 'node:fs/promises';
25
+ import YAML from 'yaml';
26
+ import { isPreRelease } from './semver.js';
27
+
28
+ /**
29
+ * Validates dependencies against organizational governance policies.
30
+ */
31
+ export class GovernanceValidator {
32
+ constructor(private readonly policy: GovernancePolicy) {}
33
+
34
+ /**
35
+ * Validates a lock file against governance policies.
36
+ */
37
+ async validate(lockFile: LockFile, workspaceRoot: string): Promise<GovernanceViolation[]> {
38
+ const violations: GovernanceViolation[] = [];
39
+
40
+ // Validate each dependency
41
+ for (const [packageKey, locked] of Object.entries(lockFile.dependencies)) {
42
+ // Check allowed sources
43
+ if (this.policy.allowedSources && this.policy.allowedSources.length > 0) {
44
+ const isAllowed = this.policy.allowedSources.some(pattern =>
45
+ locked.resolved.includes(pattern) || packageKey.startsWith(pattern)
46
+ );
47
+
48
+ if (!isAllowed) {
49
+ violations.push({
50
+ type: 'blocked-source',
51
+ packageKey,
52
+ message: `Package from unauthorized source: ${locked.resolved}`,
53
+ severity: 'error',
54
+ });
55
+ }
56
+ }
57
+
58
+ // Check blocked packages
59
+ if (this.policy.blockedPackages) {
60
+ const isBlocked = this.policy.blockedPackages.some(pattern =>
61
+ packageKey.includes(pattern)
62
+ );
63
+
64
+ if (isBlocked) {
65
+ violations.push({
66
+ type: 'blocked-source',
67
+ packageKey,
68
+ message: `Package is blocked by governance policy`,
69
+ severity: 'error',
70
+ });
71
+ }
72
+ }
73
+
74
+ // Check version stability
75
+ if (this.policy.requireStableVersions) {
76
+ if (isPreRelease(locked.ref)) {
77
+ violations.push({
78
+ type: 'unstable-version',
79
+ packageKey,
80
+ message: `Pre-release ref not allowed: ${locked.ref}`,
81
+ severity: 'error',
82
+ });
83
+ }
84
+ }
85
+ }
86
+
87
+ // Validate workspace metadata
88
+ if (this.policy.requireTeamOwnership) {
89
+ const metadata = await this.loadGovernanceMetadata(workspaceRoot);
90
+ if (!metadata.team || !metadata.contact) {
91
+ violations.push({
92
+ type: 'missing-metadata',
93
+ packageKey: 'workspace',
94
+ message: 'Missing required team ownership metadata in model.yaml',
95
+ severity: 'warning',
96
+ });
97
+ }
98
+ }
99
+
100
+ return violations;
101
+ }
102
+
103
+ /**
104
+ * Loads governance metadata from model.yaml.
105
+ */
106
+ async loadGovernanceMetadata(workspaceRoot: string): Promise<GovernanceMetadata> {
107
+ const manifestPath = path.join(workspaceRoot, 'model.yaml');
108
+
109
+ try {
110
+ const content = await fs.readFile(manifestPath, 'utf-8');
111
+ const manifest = YAML.parse(content) as {
112
+ metadata?: GovernanceMetadata;
113
+ };
114
+
115
+ return manifest.metadata || {};
116
+ } catch {
117
+ return {};
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Generates an audit report for compliance tracking.
123
+ */
124
+ async generateAuditReport(lockFile: LockFile, workspaceRoot: string): Promise<string> {
125
+ const metadata = await this.loadGovernanceMetadata(workspaceRoot);
126
+ const violations = await this.validate(lockFile, workspaceRoot);
127
+
128
+ const lines: string[] = [];
129
+ lines.push('=== Dependency Audit Report ===');
130
+ lines.push('');
131
+ lines.push(`Workspace: ${workspaceRoot}`);
132
+ lines.push(`Team: ${metadata.team || 'N/A'}`);
133
+ lines.push(`Contact: ${metadata.contact || 'N/A'}`);
134
+ lines.push(`Domain: ${metadata.domain || 'N/A'}`);
135
+ lines.push('');
136
+ lines.push('Dependencies:');
137
+
138
+ for (const [packageKey, locked] of Object.entries(lockFile.dependencies)) {
139
+ lines.push(` - ${packageKey}@${locked.ref}`);
140
+ lines.push(` Source: ${locked.resolved}`);
141
+ lines.push(` Commit: ${locked.commit}`);
142
+ }
143
+
144
+ if (violations.length > 0) {
145
+ lines.push('');
146
+ lines.push('Violations:');
147
+ for (const violation of violations) {
148
+ lines.push(` [${violation.severity.toUpperCase()}] ${violation.packageKey}: ${violation.message}`);
149
+ }
150
+ } else {
151
+ lines.push('');
152
+ lines.push('\u2713 No policy violations detected');
153
+ }
154
+
155
+ return lines.join('\n');
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Loads governance policy from model.yaml governance section.
161
+ */
162
+ export async function loadGovernancePolicy(workspaceRoot: string): Promise<GovernancePolicy> {
163
+ const manifestPath = path.join(workspaceRoot, 'model.yaml');
164
+
165
+ try {
166
+ const content = await fs.readFile(manifestPath, 'utf-8');
167
+ const manifest = YAML.parse(content) as {
168
+ governance?: GovernancePolicy;
169
+ };
170
+
171
+ // Return governance section or empty policy if not defined
172
+ return manifest.governance || {};
173
+ } catch {
174
+ // No manifest or parse error = permissive defaults
175
+ return {};
176
+ }
177
+ }