Swiftpack.co - Package - xcodeswift/xcproj


Swift Package Manager Twitter: @xcodedotswift License

xcproj is a library written in Swift for parsing and working with Xcode projects. It's heavily inspired in CocoaPods XcodeProj and xcode.

Continuous Integration ✅

  • Master: Build Status
  • Integration: Build Status

Motivation 💅

Being able to write command line scripts in Swift to update your Xcode projects configuration. Here you have some examples:

  • Add new Build phases.
  • Update the project Build Settings.
  • Create new Schemes.

Projects that benefit from xcproj ❤️

| Project | Description | |---------|-------------| | XcodeGen | Generate Xcode projects dynamically from a YAML file | | xclint | Lint the format of your Xcode projects| | xctools | Handy command line tools |

Contribute 👨‍👩‍👧

  1. Git clone the repository git@github.com:xcodeswift/xcproj.git.
  2. Generate xcproj with swift package generate-xcodeproj.
  3. Open xcproj.xcodeproj.

Setup 🦋

Using Swift Package Manager

Add the dependency in your Package.swift file:

let package = Package(
    name: "myproject",
    dependencies: [
        .package(url: "https://github.com/xcodeswift/xcproj.git", .upToNextMajor(from: "1.8.0")),
        ],
    targets: [
        .target(
            name: "myproject",
            dependencies: ["xcproj"]),
        ]
)

Using Marathon

Edit your Marathonfile and specify the dependency in there:

https://github.com/xcodeswift/xcproj.git

Using CocoaPods

Edit your Podfile and specify the dependency:

pod "xcproj"

Using Carthage

Edit your Cartfile and specify the dependency:

github "xcodeswift/xcproj"

Note: xcproj is only available for macOS and iOS projects.

How to use xcproj 🐒

Xcode provides models that represent Xcode projects and are initialized by parsing the content from your project files. The generated models are classes that can be mutated at any time. These mutations in the models are kept in memory until they are persisted by writing them back to disk by writing either the XcodeProj or the XCWorkspace model. Modifications in your projects are usually executed in three steps:

  1. Read the project or workspace initializing a XcodeProj or a XCWorkspace object respectively.
  2. Modify those objects or any of its dependencies.
  3. Write it back to disk.
// Removing all frameworks build phases
let project = try! XcodeProj(path: "myproject.xcodeproj")
project.pbxproj.frameworksBuildPhases.removeAll()
try! project.write(path: "myproject.xcodeproj")

The diagram below shows the sructure of a Xcode project.

A XcodeProj has the following properties:

  • XCSharedData that contains the information about the schemes of the project.
  • XCWorkspace that defines the structure of the project workspace.
  • PBXProj that defines the strcuture of the project.

Among other properties, the most important one in the PBXProj object is Objects. Projects are defined by a list of those objects that can be classified in the following groups:

  • Build phases objects: Define the available build phases.
  • Target objects: Define your project targets and dependencies between them.
  • Configuration objects: Define the available configs and the link between them and the targets.
  • File objects: Define the project files, build files and groups.

All objects subclass PBXObject, and have an unique & deterministic reference. Moreover, they are hashable and conform the Equatable protocol.

diagram

You can read more about what each of these objects is for on the following link

Considerations

  • Objects references are used to define dependencies between objects. In the future we might rather use objects references instead of the unique identifier.
  • The write doesn't validate the structure of the project. It's up to the developer to validate the changes that have been done using xcproj.
  • New versions of Xcode might introduce new models or property that are not supported by xcproj. If you find any, don't hesitate to open an issue on the repository.

Examples

Reading MyApp.xcodeproj

let project = try XcodeProj(path: "MyApp.xcodeproj")

Writing MyApp.xcodeproj

try project.write(path: "MyApp.xcodeproj")

Adding Home group inside Sources group

guard var sourcesGroup = project.pbxproj.objects.groups.first(where: {$0.value.name == "Sources" || $0.value.path == "Sources"})?.value else { return }    
let homeGroup = PBXGroup(children: [], sourceTree: .group, path: "Home")
let groupRef = pbxproj.objects.generateReference(homeGroup, "Home")
sourcesGroup.children.append(homeGroup, reference: groupRef)
project.pbxproj.objects.addObject(groupRef)
Versions <2.0
guard var sourcesGroup = project.pbxproj.objects.groups.first(where: {$0.value.name == "Sources" || $0.value.path == "Sources"})?.value else { return }    
let homeGroup = PBXGroup(reference: project.pbxproj.generateUUID(for: PBXGroup.self), children: [], sourceTree: .group, path: "Home")
sourcesGroup.children.append(homeGroup.reference)
project.pbxproj.objects.addObject(homeGroup)

Add HomeViewController.swift file inside HomeGroup

let homeViewController = PBXFileReference(sourceTree: .group, name: "HomeViewController.swift", path: "HomeViewController.swift")
let fileRef = pbxproj.objects.generateReference(homeViewController, "HomeViewController.swift")
homeGroup.children.append(fileRef)
project.pbxproj.objects.addObject(homeViewController, reference: fileRef)
Versions <2.0
let homeViewController = PBXFileReference(reference: project.pbxproj.generateUUID(for: PBXFileReference.self), sourceTree: .group, name: "HomeViewController.swift", path: "HomeViewController.swift")
homeGroup.children.append(homeViewController.reference)
project.pbxproj.objects.addObject(homeViewController)

Add HomeViewController.swift file to MyApp target

guard let sourcesBuildPhase = project.pbxproj
    .objects.nativeTargets
    .values
    .first(where: {$0.name == "MyApp"})
    .flatMap({ target -> PBXSourcesBuildPhase? in
        return project.pbxproj.objects.sourcesBuildPhases.first(where: { target.buildPhases.contains($0.key) })?.value
    }) else { return }
// PBXBuildFile is a proxy model that allows specifying some build attributes to the files
let buildFile = PBXBuildFile(fileRef: fileRef)
let buildFileRef = project.pbxproj.objects.generateReference(buildFile, "HomeViewController.swift")
project.pbxproj.objects.addObject(buildFile, reference: buildFileRef)
sourcesBuildPhase.files.append(buildFileRef)
Versions <2.0
guard let sourcesBuildPhase = project.pbxproj
    .objects.nativeTargets
    .values
    .first(where: {$0.name == "MyApp"})
    .flatMap({  target -> PBXSourcesBuildPhase? in
        return project.pbxproj.objects.sourcesBuildPhases.values.first(where: { target.buildPhases.contains($0.reference) })
    }) else { return }
// PBXBuildFile is a proxy model that allows specifying some build attributes to the files
let buildFile = PBXBuildFile(reference: project.pbxproj.generateUUID(for: PBXBuildFile.self), fileRef: homeViewController.reference)
project.pbxproj.objects.addObject(buildFile)
sourcesBuildPhase.files.append(buildFile.reference)

Documentation 📄

You can check out the documentation on the following link. The documentation is automatically generated in every release by using Jazzy from Realm.

References 📚

Contributors

This project exists thanks to all the people who contribute. [Contribute].

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

License

MIT License

Copyright (c) 2017 xcode.swift

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Github

link
Stars: 535
Help us keep the lights on

Releases

3.0.0 - Jan 21, 2018

Fixed

  • Fix Xcode 9.2 warning https://github.com/xcodeswift/xcproj/pull/209 by @keith
  • macOS CLI targets now have a nil extension, instead of an empty string https://github.com/xcodeswift/xcproj/pull/208 by @keith
  • Fix unnecessary quotations in CommentedString https://github.com/xcodeswift/xcproj/pull/211 by @allu22
  • Fixed xml files format not matching Xcode format, added some missing actions attributes. https://github.com/xcodeswift/xcproj/pull/216 by @ilyapuchka

Changed

  • Breaking: XCWorkspace.Data renamed to XCWorkspaceData and removed references.
  • Improved README examples. https://github.com/xcodeswift/xcproj/pull/212 by @ilyapuchka
  • Added methods to get paths to workspace, project and breakpoints and shemes files, added public methods to write them separatery. https://github.com/xcodeswift/xcproj/pull/215 by @ilyapuchka
  • Added helper methods for adding source file to the project. https://github.com/xcodeswift/xcproj/pull/213 by @ilyapuchka

:musical_note: Release Soundtrack

2.0.0 - Dec 26, 2017

Added

  • Deterministic reference generation https://github.com/xcodeswift/xcproj/pull/185 by @pepibumur

Removed

  • Breaking Change Referenceable protocol https://github.com/xcodeswift/xcproj/pull/185 by @pepibumur.
  • Breaking Change Deprecated methods to access objects from the PBXProj. Developers should use the PBXProj.objects property instead. https://github.com/xcodeswift/xcproj/pull/185 by @pepibumur.

Fixed

  • Breaking: PBXSourceTree no longer has raw values and gained an associated value case to support custom locations https://github.com/xcodeswift/xcproj/pull/198 by @briantkelley

Changed

  • Breaking: The buildableProductRunnable property onXCScheme.LaunchAction and XCScheme.ProfileAction is now optional. Similarly, macroExpansion on XCScheme.TestAction is also optional. https://github.com/xcodeswift/xcproj/pull/194 by @briantkelley
  • The XCScheme initialization from an XML file has been relaxed, better matching Xcode's behavior. Default values will be used if the XML file is missing the relevant element or attribute. https://github.com/xcodeswift/xcproj/pull/194 by @briantkelley

Migrate from 1.x.x to 2.x.x

  • If you were using objects getters in PBXProj you should use the getters in PBXProj.objects instead.
  • Objects don't include a reference property anymore. Objects associated references are the keys in the dictionary that contains them.
  • When objects are added to the PBXProj.objects collection a reference needs to be passed. The reference can be calculated using the function PBXProj.objects.generateReference that generates a unique and deterministic reference based on the given object and identifier.
  • If you were using buildableProductRunnable and macroExpansion properties from XCScheme actions they are now optionals.

1.8.0 - Dec 26, 2017

Added

  • Add breakpoint condition parameter by @alexruperez.
  • Support Xcode Extension product type https://github.com/xcodeswift/xcproj/pull/190 by @briantkelley
  • Support for the legacy Build Carbon Resources build phase https://github.com/xcodeswift/xcproj/pull/196 by @briantkelley
  • Support for custom build rules by https://github.com/xcodeswift/xcproj/pull/197 @briantkelley

Fixed

  • Optimised escaping of CommentedString https://github.com/xcodeswift/xcproj/pull/195 by @kastiglione
  • Optimised performance of object lookups https://github.com/xcodeswift/xcproj/pull/191 by @kastiglione
  • fixed PBXLegacyTarget write order https://github.com/xcodeswift/xcproj/pull/199 by @kastiglione
  • fixed comment generation of PBXBuildFiles without a name https://github.com/xcodeswift/xcproj/pull/203 by @briantkelley
  • fixed PBXReferenceTarget encoding in pbxproj file https://github.com/xcodeswift/xcproj/pull/202 by @briantkelley

:musical_note: Release Soundtrack

1.7.0 - Dec 15, 2017

Added

  • Support more indentation options on PBXGroups https://github.com/xcodeswift/xcproj/pull/168 by @bkase.
  • Support PBXLegacyTarget https://github.com/xcodeswift/xcproj/pull/171 by @bkase.
  • Breakpoint support through XCBreakpointList. https://github.com/xcodeswift/xcproj/pull/172 by @alexruperez
  • Add convenience method to find targets with a given name https://github.com/xcodeswift/xcproj/pull/184 by @pepibumur.
  • Danger plugin that fails earlier if files have been added/deleted and the Carthage project hasn't been regenerated afterward https://github.com/xcodeswift/xcproj/pull/187 by @pepibumur.

:musical_note: Release soundtrack

1.6.1 - Dec 8, 2017

Fixed

  • Fix encoded line breaks in PBXFileReference https://github.com/xcodeswift/xcproj/pull/177 by @yonaskolb

:musical_note: Soundtrack