-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathspm-service.ts
108 lines (96 loc) · 2.96 KB
/
spm-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { injector } from "../../common/yok";
import { IProjectConfigService, IProjectData } from "../../definitions/project";
import {
MobileProject,
IosSPMPackageDefinition,
} from "@rigor789/trapezedev-project";
import { IPlatformData } from "../../definitions/platform";
import path = require("path");
export class SPMService implements ISPMService {
constructor(
private $logger: ILogger,
private $projectConfigService: IProjectConfigService,
private $xcodebuildCommandService: IXcodebuildCommandService,
private $xcodebuildArgsService: IXcodebuildArgsService
) {}
public getSPMPackages(
projectData: IProjectData,
platform: string
): IosSPMPackageDefinition[] {
const spmPackages = this.$projectConfigService.getValue(
`${platform}.SPMPackages`,
[]
);
return spmPackages;
}
// note: this is not used anywhere at the moment.
// public hasSPMPackages(projectData: IProjectData): boolean {
// return this.getSPMPackages(projectData).length > 0;
// }
public async applySPMPackages(
platformData: IPlatformData,
projectData: IProjectData,
pluginSpmPackages?: IosSPMPackageDefinition[]
) {
try {
const spmPackages = this.getSPMPackages(
projectData,
platformData.platformNameLowerCase
);
if (pluginSpmPackages?.length) {
// include swift packages from plugin configs
spmPackages.push(...pluginSpmPackages);
}
if (!spmPackages.length) {
this.$logger.trace("SPM: no SPM packages to apply.");
return;
}
const project = new MobileProject(platformData.projectRoot, {
ios: {
path: ".",
},
enableAndroid: false,
});
await project.load();
// note: in trapeze both visionOS and iOS are handled by the ios project.
if (!project.ios) {
this.$logger.trace("SPM: no iOS project found via trapeze.");
return;
}
// todo: handle removing packages? Or just warn and require a clean?
for (const pkg of spmPackages) {
if ("path" in pkg) {
// resolve the path relative to the project root
this.$logger.trace("SPM: resolving path for package: ", pkg.path);
pkg.path = path.resolve(projectData.projectDir, pkg.path);
}
this.$logger.trace(`SPM: adding package ${pkg.name} to project.`, pkg);
await project.ios.addSPMPackage(projectData.projectName, pkg);
}
await project.commit();
// finally resolve the dependencies
await this.resolveSPMDependencies(platformData, projectData);
} catch (err) {
this.$logger.trace("SPM: error applying SPM packages: ", err);
}
}
public async resolveSPMDependencies(
platformData: IPlatformData,
projectData: IProjectData
) {
await this.$xcodebuildCommandService.executeCommand(
this.$xcodebuildArgsService
.getXcodeProjectArgs(platformData, projectData)
.concat([
"-destination",
"generic/platform=iOS",
"-resolvePackageDependencies",
]),
{
cwd: projectData.projectDir,
message: "Resolving SPM dependencies...",
}
);
}
}
injector.register("spmService", SPMService);