Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved README examples #212

Merged
merged 3 commits into from
Jan 12, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,24 +155,66 @@ try project.write(path: "MyApp.xcodeproj")
#### Adding `Home` group inside `Sources` group

```swift
guard var sourcesGroup = project.pbxproj.objects.groups.first(where: {$0.value.name == "Sources"})?.value else { return }
let homeGroup = PBXGroup(reference: "xxx", children: [], sourceTree: .group, path: "Home")
guard var sourcesGroup = project.pbxproj.objects.groups.first(where: {$0.value.name == "Sources" || $0.value.path == "Sources"})?.value else { return }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Xcode 9 groups are created with paths by default, unless they are added via "New Group without Folder". So name property is not stored in project xml and then is not decoded.

But before Xcode 9 when creating folder with File-New-Group menu (instead of dragging folder into the project) only name property will be set and path will be nil.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification. Makes sense to me! 👍

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)
```

<details>
<summary>Versions <2.0</summary>

```swift
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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've spent some time searching for a way to generate references until found this method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we introduced it in the version 2.0 but we forgot to update the readme. The function is generateReference instead of generateUUID and it should be used like this:

project.pbxproj.generateReference(PBXGroup.self, "home")

The second argument must be a string that uniquely identifies the object. In this case we can just use "home".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, looks like I was using 1.7.0 version

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately it means that readme examples are completely out of date. I would suggest to keep section with examples for 1.x version and with 2.x

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much @ilyapuchka

sourcesGroup.children.append(homeGroup.reference)
project.pbxproj.objects.addObject(homeGroup)
```

</details>

#### Add `HomeViewController.swift` file inside `HomeGroup`

```swift
let homeGroup = PBXGroup(reference: "xxx", children: [], sourceTree: .group, path: "Home")
let homeViewController = PBXFileReference(reference: "xxx", sourceTree: .group, path: "HomeViewController.swift")
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)
```

<details>
<summary>Versions <2.0</summary>

```swift
let homeViewController = PBXFileReference(reference: project.pbxproj.generateUUID(for: PBXFileReference.self), sourceTree: .group, name: "HomeViewController.swift", path: "HomeViewController.swift")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment regarding the generateUUID

homeGroup.children.append(homeViewController.reference)
project.pbxproj.objects.addObject(homeViewController)
```

</details>

#### Add `HomeViewController.swift` file to `MyApp` target

```swift
let homeViewController = PBXFileReference(reference: "xxx", sourceTree: .group, path: "HomeViewController.swift")
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)
```

<details>
<summary>Versions <2.0</summary>

```swift
guard let sourcesBuildPhase = project.pbxproj
.objects.nativeTargets
.values
Expand All @@ -181,11 +223,13 @@ guard let sourcesBuildPhase = project.pbxproj
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: "yyy", fileRef: homeViewController.reference)
let buildFile = PBXBuildFile(reference: project.pbxproj.generateUUID(for: PBXBuildFile.self), fileRef: homeViewController.reference)
project.pbxproj.objects.addObject(buildFile)
sourcesBuildPhase.files.append(buildFile.reference)
```

</details>

## Documentation 📄
You can check out the documentation on the following [link](https://xcodeswift.github.io/xcproj/index.html). The documentation is automatically generated in every release by using [Jazzy](https://github.com/realm/jazzy) from [Realm](https://realm.io).

Expand Down