|
| 1 | +import { injector } from "../../common/yok"; |
| 2 | +import { IProjectConfigService, IProjectData } from "../../definitions/project"; |
| 3 | +import { |
| 4 | + MobileProject, |
| 5 | + IosSPMPackageDefinition, |
| 6 | +} from "@rigor789/trapezedev-project"; |
| 7 | +import { IPlatformData } from "../../definitions/platform"; |
| 8 | + |
| 9 | +export class SPMService implements ISPMService { |
| 10 | + constructor( |
| 11 | + private $logger: ILogger, |
| 12 | + private $projectConfigService: IProjectConfigService, |
| 13 | + private $xcodebuildCommandService: IXcodebuildCommandService, |
| 14 | + private $xcodebuildArgsService: IXcodebuildArgsService |
| 15 | + ) {} |
| 16 | + |
| 17 | + public getSPMPackages(projectData: IProjectData): IosSPMPackageDefinition[] { |
| 18 | + const spmPackages = this.$projectConfigService.getValue( |
| 19 | + "ios.SPMPackages", |
| 20 | + [] |
| 21 | + ); |
| 22 | + |
| 23 | + return spmPackages; |
| 24 | + } |
| 25 | + |
| 26 | + public hasSPMPackages(projectData: IProjectData): boolean { |
| 27 | + return this.getSPMPackages(projectData).length > 0; |
| 28 | + } |
| 29 | + |
| 30 | + public async applySPMPackages( |
| 31 | + platformData: IPlatformData, |
| 32 | + projectData: IProjectData |
| 33 | + ) { |
| 34 | + try { |
| 35 | + const spmPackages = this.getSPMPackages(projectData); |
| 36 | + |
| 37 | + if (!spmPackages.length) { |
| 38 | + this.$logger.trace("SPM: no SPM packages to apply."); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + const project = new MobileProject(platformData.projectRoot, { |
| 43 | + ios: { |
| 44 | + path: ".", |
| 45 | + }, |
| 46 | + enableAndroid: false, |
| 47 | + }); |
| 48 | + await project.load(); |
| 49 | + |
| 50 | + if (!project.ios) { |
| 51 | + this.$logger.trace("SPM: no iOS project found via trapeze."); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + // todo: handle removing packages? Or just warn and require a clean? |
| 56 | + for (const pkg of spmPackages) { |
| 57 | + this.$logger.trace(`SPM: adding package ${pkg.name} to project.`, pkg); |
| 58 | + await project.ios.addSPMPackage(projectData.projectName, pkg); |
| 59 | + } |
| 60 | + await project.commit(); |
| 61 | + |
| 62 | + // finally resolve the dependencies |
| 63 | + await this.resolveSPMDependencies(platformData, projectData); |
| 64 | + } catch (err) { |
| 65 | + this.$logger.trace("SPM: error applying SPM packages: ", err); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public async resolveSPMDependencies( |
| 70 | + platformData: IPlatformData, |
| 71 | + projectData: IProjectData |
| 72 | + ) { |
| 73 | + await this.$xcodebuildCommandService.executeCommand( |
| 74 | + this.$xcodebuildArgsService |
| 75 | + .getXcodeProjectArgs(platformData.projectRoot, projectData) |
| 76 | + .concat([ |
| 77 | + "-destination", |
| 78 | + "generic/platform=iOS", |
| 79 | + "-resolvePackageDependencies", |
| 80 | + ]), |
| 81 | + { |
| 82 | + cwd: projectData.projectDir, |
| 83 | + message: "Resolving SPM dependencies...", |
| 84 | + } |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
| 88 | +injector.register("spmService", SPMService); |
0 commit comments