Sunday, March 26, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Swift

Swift Package Manager tutorial – The.Swift.Dev.

learningcode_x1mckf by learningcode_x1mckf
October 10, 2022
in Swift
0
Swift Package Manager tutorial – The.Swift.Dev.
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


2017/11/09

Discover ways to use the Swift Bundle Supervisor to deal with exterior dependencies, create your library or app on macOS and Linux.

Swift

Swift Bundle Supervisor fundamentals

To start with, please test your Swift model in your gadget earlier than we leap on this tutorial will solely work with the most recent toolchain, so you will want Swift 5.2 or newer.


Apple Swift model 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)
Goal: x86_64-apple-darwin19.4.0



Creating apps

All of the arduous work is finished by the swift package deal command. You may enter that right into a terminal window and see the accessible subcommands. To generate a brand new package deal you need to go along with the init command, in the event you do not present a sort flag, by default it’s going to create a library, however this time we might prefer to make an executable software.

swift package deal init --type executable
swift construct
swift run my-app


The compiler can construct your supply recordsdata with the assistance of the swift construct command. The executable file goes to be positioned somewehere below the .construct/ listing, in the event you run the newly created software with the swift run my-app command, you need to see the essential ‘Hey, world!’ message.

Congratulations to your first command line Swift software!

Now you need to do some precise coding. Often your swift supply recordsdata needs to be below the Sources listing, nonetheless you may wish to create some reusable elements to your app. So let’s put together for that situation by beginning a model new library.


Making a library

We begin with the init command, however this time we do not specify the kind. We truly may enter swift package deal init --type library however that is means too might phrases to kind. 😜 Additionally as a result of we’re making a library, the SPM instrument is gona present us some fundamental checks, let’s run them too with the swift take a look at command.


swift package deal init
swift take a look at


When you test the file construction now you will not discover a primary.swift file contained in the supply folder, however as a substitute of this you will get an instance unit take a look at below the Assessments listing.

Now know the fundamentals. You’ve an instance software and a library, so let’s join them along with the assistance of the Swift Bundle Supervisor Manifest API!



The Manifest API – Bundle.swift

Each SPM bundle has a Bundle.swift manifest file within it. On this manifest file you’ll be able to outline all of your dependencies, targets and even the precise supply recordsdata to your mission. On this part I am going to educate you the fundamentals of the manifest file.


Device model

To start with if you wish to help the brand new manifest file format (aka. Swift 4 model), you need to set the swift-tools-version as remark in your manifest file.



Now you are able to work with the model new manifest API.


Dependencies

Let's simply add our library as a dependency for the primary software first by creating a brand new package deal dependency contained in the Bundle.swift file. The primary argument is a package deal url string, which could be a native file path or a distant url (normally a github repo hyperlink). Notice that you need to add your dependency to the targets as nicely. Often the precise title of a package deal is outlined contained in the library manifest file.



import PackageDescription

let package deal = Bundle(
    title: "my-app",
    dependencies: [
        .package(url: "../my-lib", .branch("master")),
    ],
    targets: [
        .target(name: "my-app", dependencies: [
            .product(name: "my-lib", package: "my-lib"),
        ]),
    ]
)


Now in the event you run swift construct you will fail to construct your sources. That is as a result of the SPM solely works with git repositories. This implies you need to create a repository to your library. Let's transfer to the listing of the library and run the next instructions.


git init
git add .
git commit -m 'preliminary'


You also needs to observe that we specified the department within the package deal dependencies. You should use model numbers, and even commit hashes too. All of the accessible choices are nicely written contained in the manifest api redesign proposal doc.

Now let's return to the appliance listing and replace the dependencies with the swift package deal replace command. This time it is going to have the ability to fetch, clone and eventually resolve our dependency.

You may construct and run, nonetheless we have forgot to set the entry degree of our struct inside our library to public, so nothing goes to be seen from that API.


public struct my_lib 
    public var textual content = "Hey, World!"

    public init() 


Let's do some adjustments and commit them into the library's primary department.


git add .
git commit -m 'entry degree repair'


You are prepared to make use of the lib within the app, change the primary.swift file like this.


import my_lib

print(my_lib().textual content)


Replace the dependencies once more, and let's do a launch construct this time.


swift package deal replace
swift construct -c launch
swift run -c launch


With the -c or --configuration flag you may make a launch construct.


Merchandise and targets

By default the SPM works with the next goal directories:

Common targets: package deal root, Sources, Supply, src, srcs. Take a look at targets: Assessments, package deal root, Sources, Supply, src, srcs.

This implies, that in the event you create .swift recordsdata inside these folders, these sources can be compiled or examined, relying on the file location. Additionally the generated manifest file comprises just one construct goal (like Xcode targets), however typically you wish to create a number of apps or libraries from the identical bundle. Let's change our Bundle.swift file just a little bit, and see how can we make a model new goal.



import PackageDescription

let package deal = Bundle(
    title: "my-app",
    dependencies: [
        .package(url: "../my-lib", .branch("master")),
        .package(url: "https://github.com/kylef/Commander", from: "0.8.0"),
    ],
    targets: [
        .target(name: "my-app", dependencies: [
            .product(name: "my-lib", package: "my-lib"),
        ]),
        .goal(title: "my-cmd", dependencies: [
            .product(name: "Commander", package: "Commander"),
        ], path: "./Sources/my-cmd", sources: ["main.swift"]),
    ]
)


We simply created a brand new dependency from github, and a model new goal which is able to comprise solely the primary.swift file from the Sources/my-cmd listing. Now let's create this listing and add the supply code for the brand new app.


import Basis
import Commander

let primary = command  (title:String) in
    print("Hey, (title.capitalized)!")


primary.run()


Construct the mission with swift construct and run the newly created app with one additional title parameter. Hopefully you will see one thing like this.


swift run my-cmd visitor


So we simply made a model new executable goal, nonetheless if you would like to show your targets for different packages, you need to outline them as merchandise as nicely. When you open the manifest file for the library, you will see that there's a product outlined from the library goal. This fashion the package deal supervisor can hyperlink the product dependencies based mostly on the given product title.

You may outline static or dynamic libraries, nonetheless it is strongly recommended to make use of automated so the SPM can determine acceptable linkage.



import PackageDescription

let package deal = Bundle(
    title: "my-lib-package",
    merchandise: [
        .library(name: "my-lib", targets: ["my-lib"]),
        
    ],
    dependencies: [
        
    ],
    targets: [
        .target(name: "my-lib", dependencies: []),
        .testTarget(title: "my-libTests", dependencies: ["my-lib"]),
    ]
)


Deployment goal, different construct flags

Generally you will have to specify a deployment goal to your package deal. Now that is attainable with the Swift Bundle Supervisor (it was buggy a log time ago), you simply have to supply some additional arguments for the compiler, in the course of the construct section.


swift construct -Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.12"


Additionally if you want to outline construct flags, that is attainable too.


swift construct -Xswiftc "-D" -Xswiftc "DEBUG"


Now in your supply code you'll be able to test for the existence of the DEBUG flag.


#if DEBUG
    print("debug mode")
#endif


If you wish to know extra concerning the construct course of, simply kind swift construct --help and you may see your accessible choices for the construct command.




Yet one more factor

You may generate Xcode initiatives with the Swift Bundle Supervisor.


swift package deal generate-xcodeproj

This was SPM in a nutshell. Really we now have coreverd extra than simply the fundamentals, we deep-dived just a little into the Swift Bundle Supervisor, now you have to be accustomed to targets, merchandise and a lot of the accessible instructions, however there's all the time extra to study. So if you wish to know much more about this superb instrument, you need to test the Swift evolution dashboard for more information. Get pleasure from. 😉




Source link

You might also like

Introducing – AI Utils for macOS

Encoding and decoding data using the Hummingbird framework

Hummingbird routing and requests – The.Swift.Dev.

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Introducing – AI Utils for macOS

by learningcode_x1mckf
March 24, 2023
0
Introducing – AI Utils for macOS

⚠️ REQUIRES a FREE OpenAI API key. You will get one utilizing this hyperlink. ⚠️ Improve your productiveness with our AI powered utility application. - accessible...

Read more

Encoding and decoding data using the Hummingbird framework

by learningcode_x1mckf
March 22, 2023
0
Encoding and decoding data using the Hummingbird framework

HTTP is all about sending and receiving information over the community. Initially it was solely utilized to switch HTML paperwork, however these days we use HTTP to switch...

Read more

Hummingbird routing and requests – The.Swift.Dev.

by learningcode_x1mckf
March 17, 2023
0
Hummingbird routing and requests – The.Swift.Dev.

Routing on the server facet means the server goes to ship a response primarily based on the URL path that the consumer referred to as when firing up...

Read more

Building a Scrollable Custom Tab Bar in SwiftUI

by learningcode_x1mckf
March 10, 2023
0
Building a Scrollable Custom Tab Bar in SwiftUI

Whether or not you’re making a social media app or a productiveness device, the tab bar interface can improve the consumer expertise by making it extra intuitive and...

Read more

Beginner’s guide to server-side Swift using the Hummingbird framework

by learningcode_x1mckf
March 8, 2023
0
Beginner’s guide to server-side Swift using the Hummingbird framework

Swift on the Server in 2023 Three years in the past I began to focus on Vapor, the preferred web-framework written in Swift, which served me very...

Read more
Next Post
JavaScript Developers on What Matters and What’s Next

JavaScript Developers on What Matters and What's Next

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Write Robust Assignments – Real Python

Write Robust Assignments – Real Python

January 16, 2023
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Google AI Introduces ‘TensorStore,’ An Open-Source C++ And Python Library Designed For Reading And Writing Large Multi-Dimensional Arrays – MarkTechPost

February 23, 2023
Minecraft Exit Code 1 Error Fix: Unexpected issue invalid Java Runtime configuration game crash

Minecraft Exit Code 1 Error Fix: Unexpected issue invalid Java Runtime configuration game crash

January 19, 2023

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • 2023 Java roadmap for developers – TheServerSide.com
  • YS Jagan launches Ragi Java in Jagananna Gorumudda, says focused on intellectual development of students – The Hans India
  • Disadvantages of Java – TheServerSide.com

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?