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
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Changed
- **Breaking:** `XCWorkspace.Data` renamed to `XCWorkspaceData` and removed `references`.
- Improved README examples

## 2.0.0

Expand Down
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")
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")
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