A Swift command-line tool that converts Cordova plugin.xml files to Swift Package Manager Package.swift format, facilitating the migration from CocoaPods to Swift Package Manager for iOS Cordova plugins.
- 📦 Package.swift Generation: Creates properly structured Swift Package Manager manifests
- 🤖 Auto-Resolve Dependencies: Automatically converts CocoaPods to Swift Package Manager equivalents
- ⚙️ Automatic iOS Platform Updates: Adds
package="swift"to iOS platform - 🙈 Gitignore Updates: Adds Swift Package Manager build artifacts to
.gitignore - 🎨 Colorized Output: Beautiful, colored terminal output with different log levels
- 🔧 Interactive Mode: Prompts for confirmation before making changes
- 🏃♂️ Dry Run Mode: Preview changes without modifying files
- 📁 Conditional Backup Creation: Creates backups when using
--backupflag - ✅ Comprehensive Testing: Full test coverage for all major components
# Clone the repository
git clone https://github.com/andredestro/cordova-plugin-converter.git
cd cordova-plugin-converter
# Build and install
make installThis will install the cdv2spm binary to /usr/local/bin.
# Build the project
swift build -c release
# Copy binary to your PATH
cp .build/release/cdv2spm /usr/local/bin/# Build and run directly
swift build
.build/debug/cdv2spm --help# Convert plugin.xml in current directory
cdv2spm
# Convert specific plugin.xml file
cdv2spm /path/to/plugin.xml
# Preview changes without modifying files
cdv2spm --dry-run --verbose
# Force conversion without prompts
cdv2spm --force
# Skip .gitignore updates
cdv2spm --no-gitignore--force: Skip all confirmation prompts--dry-run: Preview changes without writing files--verbose: Enable detailed logging output--no-gitignore: Skip .gitignore updates--backup: Create backups of modified files--auto-resolve: Automatically resolve CocoaPods to SPM dependencies--help,-h: Show help message
Convert a plugin with CocoaPods dependencies:
cdv2spm path/to/plugin.xml --verboseConvert with backup creation:
cdv2spm path/to/plugin.xml --backup --verbosePreview conversion for multiple plugins:
find . -name "plugin.xml" -exec cdv2spm --dry-run {} \;Automated conversion in CI/CD:
cdv2spm --force --no-gitignore plugin.xmlAutomatic dependency resolution:
cdv2spm --auto-resolve --verbose plugin.xml- Parses plugin.xml: Extracts plugin ID and CocoaPods dependencies (iOS platform only)
- Auto-resolves dependencies (with
--auto-resolve): Automatically converts CocoaPods to SPM equivalents when possible - Generates Package.swift: Creates a properly structured Swift Package Manager manifest
- Updates plugin.xml: Always adds
package="swift"to iOS platform, optionally removes<podspec>sections - Updates .gitignore: Adds
.build/,.swiftpm/, andPackage.resolvedentries (after plugin.xml update) - Creates backups: Conditionally backs up files when
--backupflag is used
When using the --auto-resolve flag, the tool attempts to automatically convert CocoaPods dependencies to their Swift Package Manager equivalents by:
- Fetching pod specifications: Uses
pod spec cat <pod_name> --version=<version>to get pod metadata - Extracting Git repository information: Finds the source Git URL and tag/branch from the podspec
- Checking for Package.swift: Verifies if the Git repository contains a
Package.swiftfile - Parsing Swift package information: Extracts library name and dependency details
- Converting version requirements: Translates CocoaPods version syntax to SPM equivalents
The tool follows this process for each CocoaPods dependency:
CocoaPods dependency → pod spec cat → Git repository → Package.swift → SPM dependency
Success criteria:
- Pod specification is accessible via CocoaPods
- Pod has a Git source repository
- Repository contains a valid
Package.swiftfile - Package.swift defines a library product (not just executable)
Fallback behavior:
- If automatic resolution fails, generates TODO comments for manual conversion
- Provides detailed status information for each dependency
- Continues processing other dependencies even if some fail
<plugin id="com.example.myplugin" version="1.0.0">
<platform name="ios">
<podspec>
<pods>
<pod name="AFNetworking" spec="~> 4.0"/>
<pod name="SDWebImage" spec="~> 5.0"/>
</pods>
</podspec>
</platform>
</plugin>// swift-tools-version:5.9
import PackageDescription
let package = Package(
name: "com.example.myplugin",
platforms: [.iOS(.v14)],
products: [
.library(
name: "com.example.myplugin",
targets: ["com.example.myplugin"])
],
dependencies: [
.package(url: "https://github.com/apache/cordova-ios.git", branch: "master"),
// TODO: Convert CocoaPods dependency: AFNetworking (~> 4.0)
// TODO: Convert CocoaPods dependency: SDWebImage (~> 5.0)
],
targets: [
.target(
name: "com.example.myplugin",
dependencies: [
.product(name: "Cordova", package: "cordova-ios"),
// TODO: Add Swift Package equivalent for: AFNetworking (~> 4.0)
// TODO: Add Swift Package equivalent for: SDWebImage (~> 5.0)
],
path: "src/ios")
]
)// swift-tools-version:5.9
import PackageDescription
let package = Package(
name: "com.example.myplugin",
platforms: [.iOS(.v14)],
products: [
.library(
name: "com.example.myplugin",
targets: ["com.example.myplugin"])
],
dependencies: [
.package(url: "https://github.com/apache/cordova-ios.git", branch: "master"),
.package(url: "https://github.com/AFNetworking/AFNetworking.git", .upToNextMajor(from: "4.0")),
.package(url: "https://github.com/SDWebImage/SDWebImage.git", .upToNextMajor(from: "5.0"))
],
targets: [
.target(
name: "com.example.myplugin",
dependencies: [
.product(name: "Cordova", package: "cordova-ios"),
.product(name: "AFNetworking", package: "AFNetworking"),
.product(name: "SDWebImage", package: "SDWebImage")
],
path: "src/ios")
]
)The tool is built with a modular, well-tested architecture:
- Models: Data structures for plugin metadata and configuration
- XMLParser: Robust XML parsing using SWXMLHash
- PackageGenerator: Swift Package Manager manifest generation
- CLI: Command-line interface with colorized logging
- FileSystemManager: Safe file operations with dry-run support
- Converter: Main orchestration class
- Error Handling: Comprehensive error types with descriptive messages
- Logging: Multi-level logging with ANSI colors
- Testing: Full unit test coverage for all components
- Safety: Backup creation and dry-run mode prevent data loss
- Swift 5.9 or later
- Xcode 15.0 or later (for iOS development)
# Build the project
make build
# Run tests
make test
# Run tests with verbose Swift output
swift test --verbose# Lint code (requires swiftlint)
make lint
# Format code (requires swiftformat)
make format
# Generate Xcode project
make xcodeOptional tools for development:
# Install SwiftLint for code linting
brew install swiftlint
# Install SwiftFormat for code formatting
brew install swiftformatThe project includes comprehensive unit tests (87 tests covering all major components):
# Run all tests
swift test
# Run specific test file
swift test --filter XMLParserBasicTests
# Run with verbose output
swift test --verboseRun make help to see all available targets:
build- Build in release modetest- Run all testsinstall- Install to system PATHuninstall- Remove installed binaryclean- Clean build artifactslint- Run SwiftLintformat- Format code with SwiftFormatxcode- Open project in Xcoderesolve- Resolve package dependenciesupdate- Update package dependencies
- ArgumentParser - Command-line argument parsing
- SWXMLHash - XML parsing library
This project is licensed under the MIT License - see the LICENSE file for details.