@pgpmjs/core 5.0.0 → 5.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.
- package/esm/export/export-meta.js +0 -18
- package/esm/export/export-migrations.js +0 -2
- package/esm/index.js +1 -0
- package/esm/slice/index.js +3 -0
- package/esm/slice/output.js +117 -0
- package/esm/slice/slice.js +520 -0
- package/esm/slice/types.js +1 -0
- package/export/export-meta.js +0 -18
- package/export/export-migrations.js +0 -2
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +3 -2
- package/slice/index.d.ts +3 -0
- package/slice/index.js +19 -0
- package/slice/output.d.ts +22 -0
- package/slice/output.js +124 -0
- package/slice/slice.d.ts +59 -0
- package/slice/slice.js +536 -0
- package/slice/types.d.ts +133 -0
- package/slice/types.js +2 -0
|
@@ -91,14 +91,6 @@ const config = {
|
|
|
91
91
|
hash: 'uuid'
|
|
92
92
|
}
|
|
93
93
|
},
|
|
94
|
-
database_extension: {
|
|
95
|
-
schema: 'metaschema_public',
|
|
96
|
-
table: 'database_extension',
|
|
97
|
-
fields: {
|
|
98
|
-
name: 'text',
|
|
99
|
-
database_id: 'uuid'
|
|
100
|
-
}
|
|
101
|
-
},
|
|
102
94
|
schema: {
|
|
103
95
|
schema: 'metaschema_public',
|
|
104
96
|
table: 'schema',
|
|
@@ -429,16 +421,6 @@ const config = {
|
|
|
429
421
|
data: 'jsonb'
|
|
430
422
|
}
|
|
431
423
|
},
|
|
432
|
-
api_extensions: {
|
|
433
|
-
schema: 'services_public',
|
|
434
|
-
table: 'api_extensions',
|
|
435
|
-
fields: {
|
|
436
|
-
id: 'uuid',
|
|
437
|
-
database_id: 'uuid',
|
|
438
|
-
api_id: 'uuid',
|
|
439
|
-
schema_name: 'text'
|
|
440
|
-
}
|
|
441
|
-
},
|
|
442
424
|
api_schemas: {
|
|
443
425
|
schema: 'services_public',
|
|
444
426
|
table: 'api_schemas',
|
|
@@ -212,7 +212,6 @@ SET session_replication_role TO DEFAULT;`;
|
|
|
212
212
|
// Tables that depend on 'database' being inserted first
|
|
213
213
|
const tableOrder = [
|
|
214
214
|
'database',
|
|
215
|
-
'database_extension',
|
|
216
215
|
'schema',
|
|
217
216
|
'table',
|
|
218
217
|
'field',
|
|
@@ -238,7 +237,6 @@ SET session_replication_role TO DEFAULT;`;
|
|
|
238
237
|
'site_themes',
|
|
239
238
|
'site_metadata',
|
|
240
239
|
'api_modules',
|
|
241
|
-
'api_extensions',
|
|
242
240
|
'api_schemas',
|
|
243
241
|
'rls_module',
|
|
244
242
|
'user_auth_module',
|
package/esm/index.js
CHANGED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
/**
|
|
4
|
+
* Write sliced packages to disk
|
|
5
|
+
*/
|
|
6
|
+
export function writeSliceResult(result, options) {
|
|
7
|
+
const { outputDir, overwrite = false, copySourceFiles = false, sourceDir } = options;
|
|
8
|
+
// Create output directory
|
|
9
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
10
|
+
// Write each package
|
|
11
|
+
for (const pkg of result.packages) {
|
|
12
|
+
writePackage(pkg, outputDir, overwrite, copySourceFiles, sourceDir);
|
|
13
|
+
}
|
|
14
|
+
// Write workspace manifest
|
|
15
|
+
const manifestPath = path.join(outputDir, 'pgpm-workspace.json');
|
|
16
|
+
const manifestContent = JSON.stringify({
|
|
17
|
+
packages: result.workspace.packages.map(p => `packages/${p}`),
|
|
18
|
+
slicing: {
|
|
19
|
+
deployOrder: result.workspace.deployOrder,
|
|
20
|
+
dependencies: result.workspace.dependencies
|
|
21
|
+
},
|
|
22
|
+
stats: result.stats
|
|
23
|
+
}, null, 2);
|
|
24
|
+
fs.writeFileSync(manifestPath, manifestContent);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Write a single package to disk
|
|
28
|
+
*/
|
|
29
|
+
function writePackage(pkg, outputDir, overwrite, copySourceFiles, sourceDir) {
|
|
30
|
+
const pkgDir = path.join(outputDir, 'packages', pkg.name);
|
|
31
|
+
// Check if package already exists
|
|
32
|
+
if (fs.existsSync(pkgDir) && !overwrite) {
|
|
33
|
+
throw new Error(`Package directory already exists: ${pkgDir}. Use --overwrite to replace.`);
|
|
34
|
+
}
|
|
35
|
+
// Create package directory structure
|
|
36
|
+
fs.mkdirSync(pkgDir, { recursive: true });
|
|
37
|
+
fs.mkdirSync(path.join(pkgDir, 'deploy'), { recursive: true });
|
|
38
|
+
fs.mkdirSync(path.join(pkgDir, 'revert'), { recursive: true });
|
|
39
|
+
fs.mkdirSync(path.join(pkgDir, 'verify'), { recursive: true });
|
|
40
|
+
// Write plan file
|
|
41
|
+
fs.writeFileSync(path.join(pkgDir, 'pgpm.plan'), pkg.planContent);
|
|
42
|
+
// Write control file
|
|
43
|
+
fs.writeFileSync(path.join(pkgDir, `${pkg.name}.control`), pkg.controlContent);
|
|
44
|
+
// Copy SQL files if requested
|
|
45
|
+
if (copySourceFiles && sourceDir) {
|
|
46
|
+
for (const change of pkg.changes) {
|
|
47
|
+
copyChangeFiles(change.name, sourceDir, pkgDir);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Copy deploy/revert/verify SQL files for a change
|
|
53
|
+
*/
|
|
54
|
+
function copyChangeFiles(changeName, sourceDir, targetDir) {
|
|
55
|
+
const types = ['deploy', 'revert', 'verify'];
|
|
56
|
+
for (const type of types) {
|
|
57
|
+
const sourceFile = path.join(sourceDir, type, `${changeName}.sql`);
|
|
58
|
+
const targetFile = path.join(targetDir, type, `${changeName}.sql`);
|
|
59
|
+
if (fs.existsSync(sourceFile)) {
|
|
60
|
+
// Create target directory if needed
|
|
61
|
+
const targetSubDir = path.dirname(targetFile);
|
|
62
|
+
fs.mkdirSync(targetSubDir, { recursive: true });
|
|
63
|
+
// Copy file
|
|
64
|
+
fs.copyFileSync(sourceFile, targetFile);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Generate a dry-run report of what would be created
|
|
70
|
+
*/
|
|
71
|
+
export function generateDryRunReport(result) {
|
|
72
|
+
const lines = [];
|
|
73
|
+
lines.push('=== PGPM Slice Dry Run Report ===\n');
|
|
74
|
+
// Statistics
|
|
75
|
+
lines.push('Statistics:');
|
|
76
|
+
lines.push(` Total changes: ${result.stats.totalChanges}`);
|
|
77
|
+
lines.push(` Packages to create: ${result.stats.packagesCreated}`);
|
|
78
|
+
lines.push(` Internal edges: ${result.stats.internalEdges}`);
|
|
79
|
+
lines.push(` Cross-package edges: ${result.stats.crossPackageEdges}`);
|
|
80
|
+
lines.push(` Cross-package ratio: ${(result.stats.crossPackageRatio * 100).toFixed(1)}%`);
|
|
81
|
+
lines.push('');
|
|
82
|
+
// Warnings
|
|
83
|
+
if (result.warnings.length > 0) {
|
|
84
|
+
lines.push('Warnings:');
|
|
85
|
+
for (const warning of result.warnings) {
|
|
86
|
+
lines.push(` [${warning.type}] ${warning.message}`);
|
|
87
|
+
if (warning.suggestedAction) {
|
|
88
|
+
lines.push(` Suggestion: ${warning.suggestedAction}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
lines.push('');
|
|
92
|
+
}
|
|
93
|
+
// Deploy order
|
|
94
|
+
lines.push('Deploy Order:');
|
|
95
|
+
for (let i = 0; i < result.workspace.deployOrder.length; i++) {
|
|
96
|
+
const pkg = result.workspace.deployOrder[i];
|
|
97
|
+
const deps = result.workspace.dependencies[pkg] || [];
|
|
98
|
+
const depStr = deps.length > 0 ? ` (depends on: ${deps.join(', ')})` : '';
|
|
99
|
+
lines.push(` ${i + 1}. ${pkg}${depStr}`);
|
|
100
|
+
}
|
|
101
|
+
lines.push('');
|
|
102
|
+
// Package details
|
|
103
|
+
lines.push('Packages:');
|
|
104
|
+
for (const pkg of result.packages) {
|
|
105
|
+
lines.push(`\n ${pkg.name}/`);
|
|
106
|
+
lines.push(` Changes: ${pkg.changes.length}`);
|
|
107
|
+
lines.push(` Dependencies: ${pkg.packageDependencies.join(', ') || 'none'}`);
|
|
108
|
+
lines.push(' Contents:');
|
|
109
|
+
for (const change of pkg.changes.slice(0, 5)) {
|
|
110
|
+
lines.push(` - ${change.name}`);
|
|
111
|
+
}
|
|
112
|
+
if (pkg.changes.length > 5) {
|
|
113
|
+
lines.push(` ... and ${pkg.changes.length - 5} more`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return lines.join('\n');
|
|
117
|
+
}
|
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
import { parsePlanFile } from '../files/plan/parser';
|
|
2
|
+
import { minimatch } from 'minimatch';
|
|
3
|
+
/**
|
|
4
|
+
* Build a dependency graph from a parsed plan file
|
|
5
|
+
*/
|
|
6
|
+
export function buildDependencyGraph(plan) {
|
|
7
|
+
const graph = {
|
|
8
|
+
nodes: new Map(),
|
|
9
|
+
edges: new Map(),
|
|
10
|
+
reverseEdges: new Map(),
|
|
11
|
+
tags: new Map(),
|
|
12
|
+
plan
|
|
13
|
+
};
|
|
14
|
+
// Add all changes as nodes
|
|
15
|
+
for (const change of plan.changes) {
|
|
16
|
+
graph.nodes.set(change.name, change);
|
|
17
|
+
graph.edges.set(change.name, new Set(change.dependencies || []));
|
|
18
|
+
// Build reverse edges for dependency analysis
|
|
19
|
+
for (const dep of change.dependencies || []) {
|
|
20
|
+
if (!graph.reverseEdges.has(dep)) {
|
|
21
|
+
graph.reverseEdges.set(dep, new Set());
|
|
22
|
+
}
|
|
23
|
+
graph.reverseEdges.get(dep).add(change.name);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
// Index tags by change
|
|
27
|
+
for (const tag of plan.tags || []) {
|
|
28
|
+
if (!graph.tags.has(tag.change)) {
|
|
29
|
+
graph.tags.set(tag.change, []);
|
|
30
|
+
}
|
|
31
|
+
graph.tags.get(tag.change).push(tag);
|
|
32
|
+
}
|
|
33
|
+
return graph;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Validate that the dependency graph is a DAG (no cycles)
|
|
37
|
+
*/
|
|
38
|
+
export function validateDAG(graph) {
|
|
39
|
+
const visited = new Set();
|
|
40
|
+
const visiting = new Set();
|
|
41
|
+
function dfs(node, path) {
|
|
42
|
+
if (visiting.has(node)) {
|
|
43
|
+
throw new Error(`Cycle detected: ${[...path, node].join(' -> ')}`);
|
|
44
|
+
}
|
|
45
|
+
if (visited.has(node))
|
|
46
|
+
return;
|
|
47
|
+
visiting.add(node);
|
|
48
|
+
const deps = graph.edges.get(node) || new Set();
|
|
49
|
+
for (const dep of deps) {
|
|
50
|
+
// Only follow internal dependencies
|
|
51
|
+
if (graph.nodes.has(dep)) {
|
|
52
|
+
dfs(dep, [...path, node]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
visiting.delete(node);
|
|
56
|
+
visited.add(node);
|
|
57
|
+
}
|
|
58
|
+
for (const node of graph.nodes.keys()) {
|
|
59
|
+
dfs(node, []);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Extract package name from a change path using folder-based strategy
|
|
64
|
+
*/
|
|
65
|
+
export function extractPackageFromPath(changeName, depth = 1, prefixToStrip = 'schemas') {
|
|
66
|
+
const parts = changeName.split('/');
|
|
67
|
+
// Handle special folders that should go to core
|
|
68
|
+
if (parts[0] === 'extensions' || parts[0] === 'migrate') {
|
|
69
|
+
return 'core';
|
|
70
|
+
}
|
|
71
|
+
// Strip prefix if present
|
|
72
|
+
let startIdx = 0;
|
|
73
|
+
if (parts[0] === prefixToStrip) {
|
|
74
|
+
startIdx = 1;
|
|
75
|
+
}
|
|
76
|
+
// Get package name at specified depth
|
|
77
|
+
if (parts.length > startIdx) {
|
|
78
|
+
// For depth > 1, join multiple segments
|
|
79
|
+
const endIdx = Math.min(startIdx + depth, parts.length - 1);
|
|
80
|
+
if (endIdx > startIdx) {
|
|
81
|
+
return parts.slice(startIdx, endIdx).join('/');
|
|
82
|
+
}
|
|
83
|
+
return parts[startIdx];
|
|
84
|
+
}
|
|
85
|
+
return 'core';
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Find the matching package for a change using pattern-based strategy.
|
|
89
|
+
* Returns the first matching slice's package name, or undefined if no match.
|
|
90
|
+
*/
|
|
91
|
+
export function findMatchingPattern(changeName, strategy) {
|
|
92
|
+
for (const slice of strategy.slices) {
|
|
93
|
+
for (const pattern of slice.patterns) {
|
|
94
|
+
if (minimatch(changeName, pattern, { dot: true })) {
|
|
95
|
+
return slice.packageName;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Assign changes to packages based on grouping strategy
|
|
103
|
+
*/
|
|
104
|
+
export function assignChangesToPackages(graph, strategy, defaultPackage = 'core') {
|
|
105
|
+
const assignments = new Map();
|
|
106
|
+
for (const [changeName] of graph.nodes) {
|
|
107
|
+
let packageName;
|
|
108
|
+
switch (strategy.type) {
|
|
109
|
+
case 'folder': {
|
|
110
|
+
const depth = strategy.depth ?? 1;
|
|
111
|
+
const prefix = strategy.prefixToStrip ?? 'schemas';
|
|
112
|
+
packageName = extractPackageFromPath(changeName, depth, prefix);
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
case 'pattern': {
|
|
116
|
+
const matched = findMatchingPattern(changeName, strategy);
|
|
117
|
+
packageName = matched || defaultPackage;
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case 'explicit': {
|
|
121
|
+
packageName = strategy.mapping[changeName] || defaultPackage;
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
default:
|
|
125
|
+
packageName = defaultPackage;
|
|
126
|
+
}
|
|
127
|
+
// Fallback to default package
|
|
128
|
+
if (!packageName) {
|
|
129
|
+
packageName = defaultPackage;
|
|
130
|
+
}
|
|
131
|
+
if (!assignments.has(packageName)) {
|
|
132
|
+
assignments.set(packageName, new Set());
|
|
133
|
+
}
|
|
134
|
+
assignments.get(packageName).add(changeName);
|
|
135
|
+
}
|
|
136
|
+
return assignments;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Merge small packages into larger ones
|
|
140
|
+
*/
|
|
141
|
+
export function mergeSmallPackages(assignments, minChanges, defaultPackage = 'core') {
|
|
142
|
+
const result = new Map();
|
|
143
|
+
// Ensure default package exists
|
|
144
|
+
if (!result.has(defaultPackage)) {
|
|
145
|
+
result.set(defaultPackage, new Set());
|
|
146
|
+
}
|
|
147
|
+
for (const [pkg, changes] of assignments) {
|
|
148
|
+
if (changes.size < minChanges && pkg !== defaultPackage) {
|
|
149
|
+
// Merge into default package
|
|
150
|
+
const defaultChanges = result.get(defaultPackage);
|
|
151
|
+
for (const change of changes) {
|
|
152
|
+
defaultChanges.add(change);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
result.set(pkg, new Set(changes));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// Remove empty default package if it exists
|
|
160
|
+
if (result.get(defaultPackage)?.size === 0) {
|
|
161
|
+
result.delete(defaultPackage);
|
|
162
|
+
}
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Build package-level dependency graph
|
|
167
|
+
*/
|
|
168
|
+
export function buildPackageDependencies(graph, assignments) {
|
|
169
|
+
const packageDeps = new Map();
|
|
170
|
+
// Build reverse lookup: change -> package
|
|
171
|
+
const changeToPackage = new Map();
|
|
172
|
+
for (const [pkg, changes] of assignments) {
|
|
173
|
+
packageDeps.set(pkg, new Set());
|
|
174
|
+
for (const change of changes) {
|
|
175
|
+
changeToPackage.set(change, pkg);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// Check each change's dependencies
|
|
179
|
+
for (const [changeName, deps] of graph.edges) {
|
|
180
|
+
const myPackage = changeToPackage.get(changeName);
|
|
181
|
+
if (!myPackage)
|
|
182
|
+
continue;
|
|
183
|
+
for (const dep of deps) {
|
|
184
|
+
const depPackage = changeToPackage.get(dep);
|
|
185
|
+
if (depPackage && depPackage !== myPackage) {
|
|
186
|
+
// Cross-package dependency
|
|
187
|
+
packageDeps.get(myPackage).add(depPackage);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return packageDeps;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Detect cycles in package dependency graph
|
|
195
|
+
*/
|
|
196
|
+
export function detectPackageCycle(deps) {
|
|
197
|
+
const visited = new Set();
|
|
198
|
+
const visiting = new Set();
|
|
199
|
+
function dfs(pkg, path) {
|
|
200
|
+
if (visiting.has(pkg)) {
|
|
201
|
+
return [...path, pkg];
|
|
202
|
+
}
|
|
203
|
+
if (visited.has(pkg))
|
|
204
|
+
return null;
|
|
205
|
+
visiting.add(pkg);
|
|
206
|
+
const pkgDeps = deps.get(pkg) || new Set();
|
|
207
|
+
for (const dep of pkgDeps) {
|
|
208
|
+
const cycle = dfs(dep, [...path, pkg]);
|
|
209
|
+
if (cycle)
|
|
210
|
+
return cycle;
|
|
211
|
+
}
|
|
212
|
+
visiting.delete(pkg);
|
|
213
|
+
visited.add(pkg);
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
for (const pkg of deps.keys()) {
|
|
217
|
+
const cycle = dfs(pkg, []);
|
|
218
|
+
if (cycle)
|
|
219
|
+
return cycle;
|
|
220
|
+
}
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Compute deployment order for packages (topological sort)
|
|
225
|
+
*/
|
|
226
|
+
export function computeDeployOrder(packageDeps) {
|
|
227
|
+
const order = [];
|
|
228
|
+
const visited = new Set();
|
|
229
|
+
const allPackages = new Set();
|
|
230
|
+
// Collect all packages
|
|
231
|
+
for (const pkg of packageDeps.keys()) {
|
|
232
|
+
allPackages.add(pkg);
|
|
233
|
+
}
|
|
234
|
+
for (const deps of packageDeps.values()) {
|
|
235
|
+
for (const dep of deps) {
|
|
236
|
+
allPackages.add(dep);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function visit(pkg) {
|
|
240
|
+
if (visited.has(pkg))
|
|
241
|
+
return;
|
|
242
|
+
visited.add(pkg);
|
|
243
|
+
const deps = packageDeps.get(pkg) || new Set();
|
|
244
|
+
for (const dep of deps) {
|
|
245
|
+
visit(dep);
|
|
246
|
+
}
|
|
247
|
+
order.push(pkg);
|
|
248
|
+
}
|
|
249
|
+
// Visit all packages (sorted for determinism)
|
|
250
|
+
for (const pkg of [...allPackages].sort()) {
|
|
251
|
+
visit(pkg);
|
|
252
|
+
}
|
|
253
|
+
return order;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Topological sort of changes within a package
|
|
257
|
+
*/
|
|
258
|
+
export function topologicalSortWithinPackage(changes, graph) {
|
|
259
|
+
const result = [];
|
|
260
|
+
const visited = new Set();
|
|
261
|
+
function visit(change) {
|
|
262
|
+
if (visited.has(change))
|
|
263
|
+
return;
|
|
264
|
+
if (!changes.has(change))
|
|
265
|
+
return;
|
|
266
|
+
visited.add(change);
|
|
267
|
+
const deps = graph.edges.get(change) || new Set();
|
|
268
|
+
for (const dep of deps) {
|
|
269
|
+
if (changes.has(dep)) {
|
|
270
|
+
visit(dep);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
result.push(change);
|
|
274
|
+
}
|
|
275
|
+
// Visit in alphabetical order for determinism
|
|
276
|
+
for (const change of [...changes].sort()) {
|
|
277
|
+
visit(change);
|
|
278
|
+
}
|
|
279
|
+
return result;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Generate plan file content for a package
|
|
283
|
+
*/
|
|
284
|
+
export function generatePlanContent(pkgName, entries, tags = []) {
|
|
285
|
+
let content = `%syntax-version=1.0.0\n`;
|
|
286
|
+
content += `%project=${pkgName}\n`;
|
|
287
|
+
content += `%uri=${pkgName}\n\n`;
|
|
288
|
+
for (const entry of entries) {
|
|
289
|
+
let line = entry.name;
|
|
290
|
+
if (entry.dependencies && entry.dependencies.length > 0) {
|
|
291
|
+
line += ` [${entry.dependencies.join(' ')}]`;
|
|
292
|
+
}
|
|
293
|
+
if (entry.timestamp) {
|
|
294
|
+
line += ` ${entry.timestamp}`;
|
|
295
|
+
if (entry.planner) {
|
|
296
|
+
line += ` ${entry.planner}`;
|
|
297
|
+
if (entry.email) {
|
|
298
|
+
line += ` <${entry.email}>`;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (entry.comment) {
|
|
303
|
+
line += ` # ${entry.comment}`;
|
|
304
|
+
}
|
|
305
|
+
content += line + '\n';
|
|
306
|
+
// Add tags associated with this change
|
|
307
|
+
const changeTags = tags.filter(t => t.change === entry.name);
|
|
308
|
+
for (const tag of changeTags) {
|
|
309
|
+
let tagLine = `@${tag.name}`;
|
|
310
|
+
if (tag.timestamp) {
|
|
311
|
+
tagLine += ` ${tag.timestamp}`;
|
|
312
|
+
if (tag.planner) {
|
|
313
|
+
tagLine += ` ${tag.planner}`;
|
|
314
|
+
if (tag.email) {
|
|
315
|
+
tagLine += ` <${tag.email}>`;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (tag.comment) {
|
|
320
|
+
tagLine += ` # ${tag.comment}`;
|
|
321
|
+
}
|
|
322
|
+
content += tagLine + '\n';
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return content;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Generate control file content for a package
|
|
329
|
+
*/
|
|
330
|
+
export function generateControlContent(pkgName, deps) {
|
|
331
|
+
let content = `# ${pkgName} extension\n`;
|
|
332
|
+
content += `comment = '${pkgName} module'\n`;
|
|
333
|
+
content += `default_version = '0.0.1'\n`;
|
|
334
|
+
content += `relocatable = false\n`;
|
|
335
|
+
if (deps.size > 0) {
|
|
336
|
+
content += `requires = '${[...deps].sort().join(', ')}'\n`;
|
|
337
|
+
}
|
|
338
|
+
return content;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Generate a single package output
|
|
342
|
+
*/
|
|
343
|
+
export function generateSinglePackage(pkgName, changes, graph, changeToPackage, pkgDeps, useTagsForCrossPackageDeps = false) {
|
|
344
|
+
// Sort changes in topological order within package
|
|
345
|
+
const sortedChanges = topologicalSortWithinPackage(changes, graph);
|
|
346
|
+
// Build plan entries with updated dependencies
|
|
347
|
+
const planEntries = [];
|
|
348
|
+
for (const changeName of sortedChanges) {
|
|
349
|
+
const originalChange = graph.nodes.get(changeName);
|
|
350
|
+
const deps = graph.edges.get(changeName) || new Set();
|
|
351
|
+
const newDeps = [];
|
|
352
|
+
for (const dep of deps) {
|
|
353
|
+
const depPkg = changeToPackage.get(dep);
|
|
354
|
+
if (!depPkg) {
|
|
355
|
+
// External dependency (from installed module) - keep as-is
|
|
356
|
+
newDeps.push(dep);
|
|
357
|
+
}
|
|
358
|
+
else if (depPkg === pkgName) {
|
|
359
|
+
// Internal dependency - keep as-is
|
|
360
|
+
newDeps.push(dep);
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
// Cross-package dependency
|
|
364
|
+
if (useTagsForCrossPackageDeps) {
|
|
365
|
+
// Find latest tag in the dependency package
|
|
366
|
+
const depPkgChanges = [...graph.nodes.keys()]
|
|
367
|
+
.filter(c => changeToPackage.get(c) === depPkg);
|
|
368
|
+
const lastChange = depPkgChanges[depPkgChanges.length - 1];
|
|
369
|
+
const tags = graph.tags.get(lastChange);
|
|
370
|
+
if (tags && tags.length > 0) {
|
|
371
|
+
const latestTag = tags[tags.length - 1];
|
|
372
|
+
newDeps.push(`${depPkg}:@${latestTag.name}`);
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
newDeps.push(`${depPkg}:${dep}`);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
newDeps.push(`${depPkg}:${dep}`);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
planEntries.push({
|
|
384
|
+
name: changeName,
|
|
385
|
+
dependencies: newDeps,
|
|
386
|
+
timestamp: originalChange.timestamp,
|
|
387
|
+
planner: originalChange.planner,
|
|
388
|
+
email: originalChange.email,
|
|
389
|
+
comment: originalChange.comment
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
// Collect tags for this package
|
|
393
|
+
const packageTags = [];
|
|
394
|
+
for (const changeName of sortedChanges) {
|
|
395
|
+
const changeTags = graph.tags.get(changeName) || [];
|
|
396
|
+
packageTags.push(...changeTags);
|
|
397
|
+
}
|
|
398
|
+
// Generate plan content
|
|
399
|
+
const planContent = generatePlanContent(pkgName, planEntries, packageTags);
|
|
400
|
+
// Generate control file content
|
|
401
|
+
const controlContent = generateControlContent(pkgName, pkgDeps);
|
|
402
|
+
return {
|
|
403
|
+
name: pkgName,
|
|
404
|
+
planContent,
|
|
405
|
+
controlContent,
|
|
406
|
+
changes: planEntries,
|
|
407
|
+
packageDependencies: [...pkgDeps]
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Main slicing function
|
|
412
|
+
*/
|
|
413
|
+
export function slicePlan(config) {
|
|
414
|
+
// Parse the source plan file
|
|
415
|
+
const planResult = parsePlanFile(config.sourcePlan);
|
|
416
|
+
if (!planResult.data) {
|
|
417
|
+
const errorMessages = planResult.errors?.map(e => `Line ${e.line}: ${e.message}`).join('\n') || 'Unknown error';
|
|
418
|
+
throw new Error(`Failed to parse plan file: ${errorMessages}`);
|
|
419
|
+
}
|
|
420
|
+
const plan = planResult.data;
|
|
421
|
+
const warnings = [];
|
|
422
|
+
// Build dependency graph
|
|
423
|
+
const graph = buildDependencyGraph(plan);
|
|
424
|
+
// Validate DAG
|
|
425
|
+
try {
|
|
426
|
+
validateDAG(graph);
|
|
427
|
+
}
|
|
428
|
+
catch (error) {
|
|
429
|
+
throw new Error(`Invalid plan file: ${error.message}`);
|
|
430
|
+
}
|
|
431
|
+
// Assign changes to packages
|
|
432
|
+
let assignments = assignChangesToPackages(graph, config.strategy, config.defaultPackage || 'core');
|
|
433
|
+
// Merge small packages if configured
|
|
434
|
+
if (config.minChangesPerPackage && config.minChangesPerPackage > 1) {
|
|
435
|
+
assignments = mergeSmallPackages(assignments, config.minChangesPerPackage, config.defaultPackage || 'core');
|
|
436
|
+
}
|
|
437
|
+
// Build package dependencies
|
|
438
|
+
const packageDeps = buildPackageDependencies(graph, assignments);
|
|
439
|
+
// Check for package cycles
|
|
440
|
+
const cycle = detectPackageCycle(packageDeps);
|
|
441
|
+
if (cycle) {
|
|
442
|
+
warnings.push({
|
|
443
|
+
type: 'cycle_detected',
|
|
444
|
+
message: `Package cycle detected: ${cycle.join(' -> ')}`,
|
|
445
|
+
suggestedAction: 'Consider merging these packages or reorganizing dependencies'
|
|
446
|
+
});
|
|
447
|
+
// For now, we'll proceed but warn - in production we might want to auto-merge
|
|
448
|
+
}
|
|
449
|
+
// Build reverse lookup
|
|
450
|
+
const changeToPackage = new Map();
|
|
451
|
+
for (const [pkg, changes] of assignments) {
|
|
452
|
+
for (const change of changes) {
|
|
453
|
+
changeToPackage.set(change, pkg);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
// Compute deploy order
|
|
457
|
+
const deployOrder = computeDeployOrder(packageDeps);
|
|
458
|
+
// Generate packages
|
|
459
|
+
const packages = [];
|
|
460
|
+
for (const pkgName of deployOrder) {
|
|
461
|
+
const changes = assignments.get(pkgName);
|
|
462
|
+
if (!changes || changes.size === 0)
|
|
463
|
+
continue;
|
|
464
|
+
const pkgOutput = generateSinglePackage(pkgName, changes, graph, changeToPackage, packageDeps.get(pkgName) || new Set(), config.useTagsForCrossPackageDeps || false);
|
|
465
|
+
packages.push(pkgOutput);
|
|
466
|
+
// Check for heavy cross-package dependencies
|
|
467
|
+
let crossDeps = 0;
|
|
468
|
+
let totalDeps = 0;
|
|
469
|
+
for (const change of changes) {
|
|
470
|
+
const deps = graph.edges.get(change) || new Set();
|
|
471
|
+
for (const dep of deps) {
|
|
472
|
+
totalDeps++;
|
|
473
|
+
const depPkg = changeToPackage.get(dep);
|
|
474
|
+
if (depPkg && depPkg !== pkgName) {
|
|
475
|
+
crossDeps++;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
const ratio = totalDeps > 0 ? crossDeps / totalDeps : 0;
|
|
480
|
+
if (ratio > 0.5) {
|
|
481
|
+
warnings.push({
|
|
482
|
+
type: 'heavy_cross_deps',
|
|
483
|
+
message: `Package "${pkgName}" has ${Math.round(ratio * 100)}% cross-package dependencies`,
|
|
484
|
+
suggestedAction: 'Consider merging with dependent packages or reorganizing'
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
// Calculate stats
|
|
489
|
+
let internalEdges = 0;
|
|
490
|
+
let crossPackageEdges = 0;
|
|
491
|
+
for (const [change, deps] of graph.edges) {
|
|
492
|
+
const myPkg = changeToPackage.get(change);
|
|
493
|
+
for (const dep of deps) {
|
|
494
|
+
const depPkg = changeToPackage.get(dep);
|
|
495
|
+
if (depPkg === myPkg) {
|
|
496
|
+
internalEdges++;
|
|
497
|
+
}
|
|
498
|
+
else if (depPkg) {
|
|
499
|
+
crossPackageEdges++;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
const totalEdges = internalEdges + crossPackageEdges;
|
|
504
|
+
return {
|
|
505
|
+
packages,
|
|
506
|
+
workspace: {
|
|
507
|
+
packages: deployOrder,
|
|
508
|
+
deployOrder,
|
|
509
|
+
dependencies: Object.fromEntries([...packageDeps.entries()].map(([k, v]) => [k, [...v].sort()]))
|
|
510
|
+
},
|
|
511
|
+
warnings,
|
|
512
|
+
stats: {
|
|
513
|
+
totalChanges: graph.nodes.size,
|
|
514
|
+
packagesCreated: packages.length,
|
|
515
|
+
internalEdges,
|
|
516
|
+
crossPackageEdges,
|
|
517
|
+
crossPackageRatio: totalEdges > 0 ? crossPackageEdges / totalEdges : 0
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|