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.

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: "0.2.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.

Amongt 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.
  • Targets objects: Define your project targets and dependencies between them.
  • Configuration objects: Define the available configs and the link between them and the targets.
  • Files 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.

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 swift-code

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: 404

Advertisement

Releases

1.4.0 - Nov 17, 2017

Added

  • Danger integration https://github.com/xcodeswift/xcproj/pull/158 by @pepibumur

Changed

  • Improve efficiency of looking up PBXObject's from PBXProj https://github.com/xcodeswift/xcproj/pull/136 by @rahul-malik

Deprecated

  • PBXObject objects accessors https://github.com/xcodeswift/xcproj/pull/136/files#diff-f4369d9af58a6914f0e5cdf81ed18530R6 by @rahul-malik.

Fixed

  • Fix PBXBuildFile wrongly defaulting the settings attribute when it was nil https://github.com/xcodeswift/xcproj/pull/149 by @allu22
  • Fix PBXTarget generating the wrong comment for the productReference property https://github.com/xcodeswift/xcproj/pull/151 by @allu22.
  • Add missing usesTabs property to PBXGroup https://github.com/xcodeswift/xcproj/pull/147 by @allu22.
  • Fix generated comment for PBXHeadersBuildPhase by @allu22.
  • Fix wrong BuidlSettings.swift file name https://github.com/xcodeswift/xcproj/pull/146 by @allu22.
  • Fix projectReferences type https://github.com/xcodeswift/xcproj/pull/135 by @solgar.

:musical_note: Release Soundtrack

1.3.0 - Nov 1, 2017

Added

  • Add PBXSourceTree.developerDir type https://github.com/xcodeswift/xcproj/commit/5504fcde00bc56cf6c240ecd7cc36c05296861f8 by @pepibumur.

Fixed

  • Fix PBXShellScriptBuildPhase bug decoding showEnvVarsInLog https://github.com/xcodeswift/xcproj/commit/521b4e62b70f5fc43a06d00c43916d4899138553 by @pepibumur.
  • Fix PBXFileReference bug decoding useTabs https://github.com/xcodeswift/xcproj/commit/c533987496959a3e32c0ddfe45a0f2db8d5daae0 by @pepibumur.
  • Fix PBXFileReference bug decoding lineEnding https://github.com/xcodeswift/xcproj/commit/8a2c94effbe94859a68d58e0c49d66156ba1eaea by @pepibumur.

:musical_note: Release Soundtrack

1.2.0 - Oct 28, 2017

Added

  • Carthage support https://github.com/xcodeswift/xcproj/pull/125 by @pepibumur.
  • buildPhases property to PBXProj https://github.com/xcodeswift/xcproj/pull/132 by @pepibumur.

Fixed

  • Build phase buildActionMask wrong default value https://github.com/xcodeswift/xcproj/pull/131 by @pepibumur.

:musical_note: Release Soundtrack

1.1.0 - Oct 21, 2017

Added

  • It supports now SPM-generated projects https://github.com/xcodeswift/xcproj/pull/124 by @pepibumur. Thanks, @josefdolezal for the report.
  • Project and workspace initializer that takes the path as a string https://github.com/xcodeswift/xcproj/pull/123 by @pepibumur.

Fixed

  • Fix the decoding of the PBXFileReference.fileEncoding property https://github.com/xcodeswift/xcproj/pull/127 by @gubikmic.
  • Fix some wrong comments and typos https://github.com/xcodeswift/xcproj/pull/126 by @gubikmic

:musical_note: Release Soundtrack

1.0.0 - Oct 14, 2017

Changed

  • Breaking: Review optionality of attributes to align it with Xcode one https://github.com/xcodeswift/xcproj/pull/107 by @pepibumur.
  • Contributing, and code of conduct point to the organization ones by @pepibumur.
  • New changelog format introduced by @pepibumur.

Fixed

  • Use the super init to decode reference in some objects https://github.com/xcodeswift/xcproj/pull/110 by @yonaskolb
  • Schemes being shared with an extension https://github.com/xcodeswift/xcproj/pull/113 by @esttorhe.
  • Contributors link in the README.md https://github.com/xcodeswift/xcproj/pull/117 by @tapanprakasht.

This version 1.0.0 introduces breaking changes in the API. We revisited the optionality of some properties, validating with Xcode. xcproj 1.0.0 is closer to the Xcode behavior if a project can be opened with Xcode, xcproj should also be able to open it.

If you were using a previous version of xcproj, you will have to update the access to objects properties, since some might be optional now.

:musical_note: Release Soundtrack