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

WIP - feat: Add support for SPM plugins #1430

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ class Api {
return this.addPodSpecs(plugin, podSpecs, installOptions);
}
})
.then(() => {
if (plugin != null && fs.existsSync(path.join(plugin.dir, 'Package.swift'))) {
return this.updatePackageSwift(plugin, 'add');
}
})
// CB-11022 Return truthy value to prevent running prepare after
.then(() => true);
}
Expand Down Expand Up @@ -325,6 +330,11 @@ class Api {
return this.removePodSpecs(plugin, podSpecs, uninstallOptions);
}
})
.then(() => {
if (plugin != null && fs.existsSync(path.join(plugin.dir, 'Package.swift'))) {
return this.updatePackageSwift(plugin, 'remove');
}
})
// CB-11022 Return truthy value to prevent running prepare after
.then(() => true);
}
Expand Down Expand Up @@ -512,6 +522,28 @@ class Api {
return Promise.resolve();
}

/**
* Updates the Package.swift file to add or remove plugin dependencies
* @param {PluginInfo} plugin plugin to edit the Package.swift with
* @param {String} mode Add or remove plugin
*/
updatePackageSwift (plugin, mode) {
const project_dir = this.locations.root;
const pluginDep = `
package.dependencies.append(.package(name: "${plugin.id}", path: "../../../plugins/${plugin.id}"))
package.targets.first?.dependencies.append(.product(name: "${plugin.id}", package: "${plugin.id}"))
`;
const packagePath = path.join(project_dir, 'CordovaPluginsSPM', 'Package.swift');
let packageContent = fs.readFileSync(packagePath, 'utf8');
if (mode === 'add') {
packageContent = packageContent + pluginDep;
} else {
packageContent = packageContent.replace(pluginDep, '');
}
fs.writeFileSync(packagePath, packageContent);
return Promise.resolve();
}

/**
* set Swift Version for all CocoaPods libraries
*
Expand Down
43 changes: 0 additions & 43 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class ProjectCreator {
create () {
this.provideProjectTemplate();
this.provideCordovaJs();
this.provideCordovaLib();
this.provideBuildScripts();
this.expandTokens();
}
Expand All @@ -92,11 +91,6 @@ class ProjectCreator {
);
}

provideCordovaLib () {
this.copyOrLinkCordovaLib();
this.configureCordovaLibPath();
}

provideBuildScripts () {
const srcScriptsDir = path.join(ROOT, 'templates', 'cordova');
const destScriptsDir = this.projectPath('cordova');
Expand All @@ -122,43 +116,6 @@ class ProjectCreator {
}
}

configureCordovaLibPath () {
// CordovaLib could be a symlink, so we resolve it
const cdvLibRealPath = fs.realpathSync(this.projectPath('CordovaLib'));

const cdvLibXcodeAbsPath = path.join(cdvLibRealPath, 'CordovaLib.xcodeproj');
let cdvLibXcodePath = path.relative(this.project.path, cdvLibXcodeAbsPath);

if (path.sep !== path.posix.sep) {
// If the Cordova project is being created on Windows, we need to
// make sure the Xcode project file uses POSIX-style paths or else
// Xcode considers it invalid
cdvLibXcodePath = cdvLibXcodePath.replace(path.sep, path.posix.sep);
}

// Replace magic line in project.pbxproj
const pbxprojPath = this.projectPath('__PROJECT_NAME__.xcodeproj', 'project.pbxproj');
transformFileContents(pbxprojPath, contents => {
const regex = /(.+CordovaLib.xcodeproj.+PBXFileReference.+wrapper.pb-project.+)(path = .+?;)(.*)(sourceTree.+;)(.+)/;
const line = contents.split(/\r?\n/)
.find(l => regex.test(l));

if (!line) {
throw new Error(`Entry not found in project file for sub-project: ${cdvLibXcodePath}`);
}

let newLine = line
.replace(/path = .+?;/, `path = ${cdvLibXcodePath};`)
.replace(/sourceTree.+?;/, 'sourceTree = "<group>";');

if (!newLine.match('name')) {
newLine = newLine.replace('path = ', 'name = CordovaLib.xcodeproj; path = ');
}

return contents.replace(line, newLine);
});
}

expandTokensInFileContents () {
// Expand __PROJECT_ID__ token in file contents
transformFileContents(
Expand Down
43 changes: 43 additions & 0 deletions templates/project/CordovaPluginsSPM/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// swift-tools-version:5.5

/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

import PackageDescription

let package = Package(
name: "CordovaPluginsSPM",
platforms: [.iOS(.v11)],
products: [
.library(
name: "CordovaPluginsSPM",
targets: ["CordovaPluginsSPM"])
],
dependencies: [
.package(url: "https://github.com/apache/cordova-ios.git", branch: "master")
],
targets: [
.target(
name: "CordovaPluginsSPM",
dependencies: [
.product(name: "CordovaLib", package: "cordova-ios")
]
)
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public let isCordovaApp = true
Loading
Loading