Home > Enterprise >  How to create package from existing project with Swift Package Manager?
How to create package from existing project with Swift Package Manager?

Time:06-03

Is it possible to create package from existing project ( UIKit application ) in Xcode13 ?

I have implemented application but it too big and take too much time on building phase. Because of that, I want to separate to stand-alone modular app ( mini app ) by pack them to package with Swift package manager.

Is it possible to do that ? Or has better way to implement modularization application.

Thank you.

CodePudding user response:

I think SPM is the way to go currently, cause you can easily handle versioning for the package.

You can create a new package locally in a different directory which you'll later add as a new repository.

Then move all the contents you want into that package. Don't forget to set the access to public so it will be accessible from the consumer applications. Also go to your project settings and in the tab Package Dependencies, click the plus button to add a new package, click on the add local and choose your newly created package. Also go to the target settings and add the package in Frameworks, Libraries, and Embedded Content section.

For your Package file you would get something like this where you'll need to setup the platforms you'll use it with, my example package is named SPM1.

// swift-tools-version: 5.6
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "SPM1",

    platforms: [
        .iOS(.v13) // set your minimal version here
    ],

    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "SPM1",
            targets: ["SPM1"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "SPM1",
            dependencies: []),
        .testTarget(
            name: "SPM1Tests",
            dependencies: ["SPM1"]),
    ]
)

In your application, do the import like so:

import SPM1

And start using your new package.

Once everything is OK, you can add your package in a new git repository. Don't forget to version your package in the format X.Y.Z (like 1.0.0) by tagging your commits. Go back to the project settings and add the URL the branch or version (tag).

You can use both locally and remotely. The advantage of having it locally is the agility to build and run quickly when developing.

I hope that helps! :)

  • Related