Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(spm): add product lines to Package.swift #7278

Merged
merged 16 commits into from
Mar 7, 2024
Next Next commit
fix(spm): add product lines to Package.swift
  • Loading branch information
markemer committed Feb 22, 2024
commit f5818ddb1c730c6f0103bcbb14374edd6ee473aa
72 changes: 61 additions & 11 deletions cli/src/util/spm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ export interface SwiftPlugin {
path: string;
}

export async function checkPackageManager(config: Config): Promise<string> {
markemer marked this conversation as resolved.
Show resolved Hide resolved
const iosDirectory = config.ios.nativeProjectDirAbs;
if (existsSync(resolve(iosDirectory, 'CapApp-SPM'))) {
return 'SPM';
}

return 'Cocoapods';
}

export async function findPackageSwiftFile(config: Config): Promise<string> {
const packageDirectory = resolve(
config.ios.nativeProjectDirAbs,
Expand All @@ -28,16 +37,29 @@ function readSwiftPackage(packageLine: string): string | null {
return lineMatch[1];
}

function readSwiftProduct(productLine: string): string | null {
const packageRegex = RegExp(/.product\(\s*name:\s*"([A-Za-z0-9_-]+)"/);
const lineMatch = productLine.match(packageRegex);
if (lineMatch === null) {
return null;
}

return lineMatch[1];
}

export async function generatePackageFile(
config: Config,
plugins: Plugin[],
): Promise<void> {
const swiftPluginList: string[] = [];
const swiftPluginProductList: string[] = [];

for (const plugin of plugins) {
const relPath = relative(config.ios.nativeXcodeProjDirAbs, plugin.rootPath);
const pluginStatement = `.package(name: "${plugin.ios?.name}", path: "${relPath}"),`;
const pluginProduct = `.product(name: "${plugin.ios?.name}", package: "${plugin.ios?.name}"),`
swiftPluginList.push(pluginStatement);
swiftPluginProductList.push(pluginProduct);
}

const packageSwiftFile = await findPackageSwiftFile(config);
Expand All @@ -53,14 +75,15 @@ export async function generatePackageFile(

let textToWrite = '';
const packages: string[] = [];
const products: string[] = [];
for (const lineIndex in packageSwiftTextLines) {
const line = packageSwiftTextLines;
const index = parseInt(lineIndex);

if (
line[index].includes('dependencies: [') &&
line[index + 1].includes(
'.package(url: "https://github.com/ionic-team/capacitor6-spm-test.git", branch: "main")',
'.package(url: "https://github.com/ionic-team/capacitor-spm.git", branch: "main")',
)
) {
let tempIndex = index + 1;
Expand All @@ -73,9 +96,29 @@ export async function generatePackageFile(
}
}

if(
line[index].includes('dependencies: [') &&
line[index + 1].includes(
'.product(name: "Capacitor", package: "capacitor-spm")',
) &&
line[index + 2].includes(
'.product(name: "Cordova", package: "capacitor-spm")',
)
) {
let tempIndex = index + 1;
while (!line[tempIndex].includes(']')) {
const swiftPack = readSwiftProduct(line[tempIndex]);
if (swiftPack !== null) {
console.log(swiftPack)
products.push(swiftPack);
}
tempIndex++;
}
}

if (
line[index].includes(
'.package(url: "https://github.com/ionic-team/capacitor6-spm-test.git", branch: "main")',
'.package(url: "https://github.com/ionic-team/capacitor-spm.git", branch: "main")',
)
) {
if (line[index].endsWith(',')) {
Expand All @@ -90,6 +133,22 @@ export async function generatePackageFile(
textToWrite += ' ' + swiftPlugin + '\n';
}
}
} else if (
line[index].includes(
'.product(name: "Cordova", package: "capacitor-spm")',
)
) {
if (line[index].endsWith(',')) {
textToWrite += line[index] + '\n';
} else {
textToWrite += line[index] + ',\n';
}
for (const swiftProduct of swiftPluginProductList) {
const name = readSwiftProduct(swiftProduct) ?? ''
if (!products.includes(name)) {
textToWrite += ' ' + swiftProduct + '\n';
}
}
} else {
textToWrite += line[index] + '\n';
}
Expand All @@ -102,12 +161,3 @@ export async function generatePackageFile(
);
}
}

export async function checkPackageManager(config: Config): Promise<string> {
const iosDirectory = config.ios.nativeProjectDirAbs;
if (existsSync(resolve(iosDirectory, 'CapApp-SPM'))) {
return 'SPM';
}

return 'Cocoapods';
}
Loading