Skip to content

Commit 2d12903

Browse files
Copilotfarfromrefug
andcommitted
Refactor: Extract SPM merge logic to improve code quality
Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>
1 parent c826956 commit 2d12903

2 files changed

Lines changed: 45 additions & 47 deletions

File tree

lib/services/ios/spm-service.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,29 @@ export class SPMService implements ISPMService {
2424
return spmPackages;
2525
}
2626

27+
/**
28+
* Merges plugin SPM packages with app SPM packages.
29+
* App packages take precedence over plugin packages with the same name.
30+
* @param appPackages - Array of app SPM packages (modified in place)
31+
* @param pluginPackages - Array of plugin SPM packages to merge
32+
*/
33+
private mergePluginSPMPackages(
34+
appPackages: IosSPMPackage[],
35+
pluginPackages: IosSPMPackage[],
36+
): void {
37+
// include swift packages from plugin configs
38+
// but allow app packages to override plugin packages with the same name
39+
const appPackageNames = new Set(appPackages.map(pkg => pkg.name));
40+
41+
for (const pluginPkg of pluginPackages) {
42+
if (appPackageNames.has(pluginPkg.name)) {
43+
this.$logger.trace(`SPM: app package overrides plugin package: ${pluginPkg.name}`);
44+
} else {
45+
appPackages.push(pluginPkg);
46+
}
47+
}
48+
}
49+
2750
// note: this is not used anywhere at the moment.
2851
// public hasSPMPackages(projectData: IProjectData): boolean {
2952
// return this.getSPMPackages(projectData).length > 0;
@@ -41,17 +64,7 @@ export class SPMService implements ISPMService {
4164
);
4265

4366
if (pluginSpmPackages?.length) {
44-
// include swift packages from plugin configs
45-
// but allow app packages to override plugin packages with the same name
46-
const appPackageNames = new Set(spmPackages.map(pkg => pkg.name));
47-
48-
for (const pluginPkg of pluginSpmPackages) {
49-
if (appPackageNames.has(pluginPkg.name)) {
50-
this.$logger.trace(`SPM: app package overrides plugin package: ${pluginPkg.name}`);
51-
} else {
52-
spmPackages.push(pluginPkg);
53-
}
54-
}
67+
this.mergePluginSPMPackages(spmPackages, pluginSpmPackages);
5568
}
5669

5770
if (!spmPackages.length) {

test/spm-service.ts

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
import { assert } from "chai";
22

3+
/**
4+
* Helper function to merge app and plugin SPM packages.
5+
* App packages take precedence over plugin packages with the same name.
6+
*/
7+
function mergeSPMPackages(appPackages: any[], pluginPackages: any[]): any[] {
8+
const spmPackages = [...appPackages];
9+
const appPackageNames = new Set(spmPackages.map(pkg => pkg.name));
10+
11+
for (const pluginPkg of pluginPackages) {
12+
if (!appPackageNames.has(pluginPkg.name)) {
13+
spmPackages.push(pluginPkg);
14+
}
15+
}
16+
17+
return spmPackages;
18+
}
19+
320
describe("SPM Service - Package Override Logic", () => {
421
describe("merging app and plugin SPM packages", () => {
522
it("should allow app packages to override plugin packages with the same name", () => {
@@ -28,15 +45,7 @@ describe("SPM Service - Package Override Logic", () => {
2845
},
2946
];
3047

31-
// Simulate the merge logic from spm-service.ts
32-
const spmPackages = [...appPackages];
33-
const appPackageNames = new Set(spmPackages.map(pkg => pkg.name));
34-
35-
for (const pluginPkg of pluginPackages) {
36-
if (!appPackageNames.has(pluginPkg.name)) {
37-
spmPackages.push(pluginPkg);
38-
}
39-
}
48+
const spmPackages = mergeSPMPackages(appPackages, pluginPackages);
4049

4150
// Verify the result
4251
assert.equal(spmPackages.length, 2, "Should have 2 packages total");
@@ -72,15 +81,7 @@ describe("SPM Service - Package Override Logic", () => {
7281
},
7382
];
7483

75-
// Simulate the merge logic
76-
const spmPackages = [...appPackages];
77-
const appPackageNames = new Set(spmPackages.map(pkg => pkg.name));
78-
79-
for (const pluginPkg of pluginPackages) {
80-
if (!appPackageNames.has(pluginPkg.name)) {
81-
spmPackages.push(pluginPkg);
82-
}
83-
}
84+
const spmPackages = mergeSPMPackages(appPackages, pluginPackages);
8485

8586
// Verify the result
8687
assert.equal(spmPackages.length, 2, "Should include both plugin packages");
@@ -107,15 +108,7 @@ describe("SPM Service - Package Override Logic", () => {
107108
},
108109
];
109110

110-
// Simulate the merge logic
111-
const spmPackages = [...appPackages];
112-
const appPackageNames = new Set(spmPackages.map(pkg => pkg.name));
113-
114-
for (const pluginPkg of pluginPackages) {
115-
if (!appPackageNames.has(pluginPkg.name)) {
116-
spmPackages.push(pluginPkg);
117-
}
118-
}
111+
const spmPackages = mergeSPMPackages(appPackages, pluginPackages);
119112

120113
// Verify the result
121114
assert.equal(spmPackages.length, 1, "Should have exactly 1 package");
@@ -154,15 +147,7 @@ describe("SPM Service - Package Override Logic", () => {
154147
},
155148
];
156149

157-
// Simulate the merge logic
158-
const spmPackages = [...appPackages];
159-
const appPackageNames = new Set(spmPackages.map(pkg => pkg.name));
160-
161-
for (const pluginPkg of pluginPackages) {
162-
if (!appPackageNames.has(pluginPkg.name)) {
163-
spmPackages.push(pluginPkg);
164-
}
165-
}
150+
const spmPackages = mergeSPMPackages(appPackages, pluginPackages);
166151

167152
// Verify the result
168153
assert.equal(spmPackages.length, 3, "Should have all 3 packages");

0 commit comments

Comments
 (0)