$30 off During Our Annual Pro Sale. View Details »

Dependency Management With Carthage

Dependency Management With Carthage

Slides from my talk about Carthage at Cocoaheads Stockholm

The video is available at https://vimeo.com/165920026

Abizer Nasir

May 02, 2016
Tweet

More Decks by Abizer Nasir

Other Decks in Programming

Transcript

  1. Dependency
    Management With
    Carthage
    Abizer Nasir | @abizern | abizern.org

    View Slide

  2. !
    How to use Carthage to manage dependencies.
    How to write a dependency that can be used with Carthage.
    How to make working with Carthage a little easier.

    View Slide

  3. !
    Whether or not dependencies are a good thing.
    Cocoapods.

    View Slide

  4. Yet Another
    Dependency Manager

    View Slide

  5. Git
    Git is a lot of things, but it’s not a dependency manager.

    View Slide

  6. Cocoapods
    Code is stored in a central location 1.
    The code is built into a framework that is added to the project
    workspace.
    1 They don’t have to be.

    View Slide

  7. Swift Package Manage
    Library locations are specified in Package file.
    Builds dependencies as libraries (the irony!).
    Only for Desktop.

    View Slide

  8. Carthage
    Developed after Swift and framework support in iOS.
    Dynamic libraries built according to their own project settings.
    Dependencies are dynamic frameworks and supported by OS X
    and iOS 2.
    2 All versions of OS X, iOS 8+

    View Slide

  9. Decentralised and designed for
    simplicity.
    You need to find third party projects yourself.
    You need to integrate the code into your project manually.
    Doesn’t (and probably won’t) do a lot of cool stuff that
    Cocoapods can do.

    View Slide

  10. Getting Carthage

    View Slide

  11. Install Binary release.
    https://github.com/Carthage/Carthage/releases

    View Slide

  12. Install using Homebrew (recommended)
    brew update && brew install carthage
    You may need to use xcode-select to choose the correct version
    of Xcode/Swift to build the tool.

    View Slide

  13. What Does Carthage do?
    » Takes a list of dependencies, that may have their own
    dependencies.
    » Resolves versions semantically.
    » Fetches the dependencies at the correct versions.
    » Builds them with their own project settings - (optional).

    View Slide

  14. That’s it!
    The rest is up to you!

    View Slide

  15. Adding Dependencies
    (Recommended3)
    » Specify dependencies in the Cartfile.
    » Fetch and build the dependencies.
    » Link the frameworks to the correct target.
    » Copy the dSYM files to the target.
    » Add a workaround script for iOS targets.
    3 As recommended by the documentation, but not the only way.

    View Slide

  16. Cartfile - Specify public
    dependencies.
    github “ReactiveCocoa/ReactiveCocoa” >= 2.3.1
    github “Mantle/Mantle” ~> 1.0
    github “jspahrsummers/libextobjc” == 0.4.1
    #arbitrary server
    git “https://enterprise.local/desktop/git-error-translations.git” “development”
    #local file
    git “file:///directory/to/project” “branch”

    View Slide

  17. Normal Semantic versioning
    rules.
    » >= 1.0 for “at least version 1.0”
    » ~> 1.0 for “compatible with version 1.0”
    » == 1.0 for “exactly version 1.0”
    » some-branch-or-tag-or-commit for a specific Git object
    (anything allowed by git rev-parse)

    View Slide

  18. Cartfile.private
    Same format as the Cartfile.
    Dependencies that your project needs that aren’t necessary for it
    to be used.
    These are not shared upstream.
    For example; things you need for testing, things you need for
    building.

    View Slide

  19. Cartfile.resolved
    This is where the actual versions to be used are written to.
    If you’re familiar with gemfile.lock, podfile.lock, you know what
    these are.

    View Slide

  20. Demo
    Using Carthage

    View Slide

  21. carthage update
    Resolves dependencies and builds them.
    If you only want to update one or some dependencies.
    Carthage update xxx

    View Slide

  22. carthage help update
    Update and rebuild the project's dependencies
    [--configuration (string)]
    the Xcode configuration to build (ignored if --no-build option is present)
    [--platform (platform)]
    the platforms to build for (one of ‘all’, ‘Mac’, ‘iOS’, ‘watchOS’, 'tvOS', or comma-separated values of the formers except for ‘all’)
    (ignored if --no-build option is present)
    [--derived-data (string)]
    path to the custom derived data folder
    [--verbose]
    print xcodebuild output inline (ignored if --no-build option is present)
    [--no-checkout]
    skip the checking out of dependencies after updating
    [--no-build]
    skip the building of dependencies after updating (ignored if --no-checkout option is present)
    [--use-ssh]
    use SSH for downloading GitHub repositories
    [--use-submodules]
    add dependencies as Git submodules
    [--no-use-binaries]
    check out dependency repositories even when prebuilt frameworks exist, disabled if --use-submodules option is present (ignored if --no-build option is present)
    [--color (color)]
    whether to apply color and terminal formatting (one of ‘auto’, ‘always’, or ‘never’)
    [--project-directory (string)]
    the directory containing the Carthage project
    [[]]
    the dependency names to update, checkout and build

    View Slide

  23. carthage bootstrap
    Uses Cartfile.resolved to build dependencies.
    This is what is usually used when checking out a project.
    Note: If you are using submodules update the submodules first
    and then run build:
    git submodule update --init
    carthage build

    View Slide

  24. def run(command)
    system(command) or raise "RAKE TASK FAILED: #{command}"
    end
    namespace "carthage" do
    desc "Bootstrap carthage dependencies"
    task :bootstrap do |t|
    run "git submodule update --init"
    run "carthage bootstrap --use-submodules --platform ios"
    end
    desc "Update carthage dependencies"
    task :update do |t|
    run "carthage update --use-submodules --platform ios"
    end
    end
    task :default do
    system "rake -T"
    end

    View Slide

  25. carthage build
    Just builds what you have already.
    This is useful if you are using submodules.

    View Slide

  26. Writing modules
    » Share your build schemes
    » Link any dependencies, don’t add them to the bundle.
    » When writing a Foundation module, create a separate target
    for iOS and Mac, tvOS, watchOS (takes some tweaking, but it
    can be done).

    View Slide

  27. Demo
    Writing a Framework for Carthage

    View Slide

  28. Artefacts
    Cartfile, Cartfile.resolved should be checked in to version
    control
    Carthage/Checkouts, Carthage/Build - It depends
    Use submodules and Carthage/Checkouts problem goes away.

    View Slide

  29. When things go wrong !
    Because they will!
    - Check what compiler you are using with xcode-select -p
    - fix your build with carthage build --no-skip-current, build the
    scheme to see more detailed errors.
    - Blitz Carthage folder and Cartfile.resolved
    - Blitz the cache at ~/Library/Caches/carthage
    - Bitcode!

    View Slide

  30. When it all goes completely
    wrong! !!
    Because it will!
    » Change the Carthage update method to use submodules and
    not build.
    » Remove the linked frameworks.
    » Drag the dependency project files into your project and build it
    “old school”.

    View Slide

  31. So...

    View Slide

  32. View Slide

  33. View Slide

  34. Simple

    View Slide

  35. Easy

    View Slide

  36. Carthage is simple, not easy.
    CocoaPods is easy, not simple.

    View Slide

  37. View Slide

  38. Git

    View Slide

  39. Thank You
    Abizer Nasir | @abizern | abizern.org

    View Slide