-
Notifications
You must be signed in to change notification settings - Fork 823
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
Add ability to set an order of groups #613
Changes from 4 commits
58f4260
2225c9e
3ead44b
9eb9638
63bb6ad
de5e0e9
b9da574
2448168
9e174f8
cb1b079
9ffa271
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Foundation | ||
import JSONUtilities | ||
|
||
/// Describes an order of groups. | ||
public struct GroupOrder: Equatable { | ||
|
||
public enum FileSortPosition: String { | ||
/// groups are at the top | ||
case top | ||
/// groups are at the bottom | ||
case bottom | ||
} | ||
|
||
/// A group name pattern. | ||
public var pattern: String | ||
|
||
/// Subgroups orders. | ||
public var order: [String] | ||
|
||
/// File sort position in a group. | ||
public var fileSortPosition: FileSortPosition = .top | ||
|
||
} | ||
|
||
extension GroupOrder: JSONObjectConvertible { | ||
|
||
public init(jsonDictionary: JSONDictionary) throws { | ||
pattern = jsonDictionary.json(atKeyPath: "pattern") ?? "" | ||
order = jsonDictionary.json(atKeyPath: "order") ?? [] | ||
fileSortPosition = jsonDictionary.json(atKeyPath: "fileSortPosition") ?? .top | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Foundation | ||
|
||
public extension String { | ||
|
||
func isMatch(to regex: String) -> Bool { | ||
guard let regex = try? NSRegularExpression(pattern: regex) else { return false } | ||
Beniamiiin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let range = NSRange(location: 0, length: self.utf16.count) | ||
return regex.firstMatch(in: self, options: [], range: range) != nil | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -376,23 +376,33 @@ public class PBXProjGenerator { | |
|
||
func sortGroups(group: PBXGroup) { | ||
// sort children | ||
let children = group.children | ||
.sorted { child1, child2 in | ||
let sortOrder1 = child1.getSortOrder(groupSortPosition: project.options.groupSortPosition) | ||
let sortOrder2 = child2.getSortOrder(groupSortPosition: project.options.groupSortPosition) | ||
|
||
if sortOrder1 != sortOrder2 { | ||
return sortOrder1 < sortOrder2 | ||
} else { | ||
if child1.nameOrPath != child2.nameOrPath { | ||
return child1.nameOrPath.localizedStandardCompare(child2.nameOrPath) == .orderedAscending | ||
if let groupOrder = project.options.groupsOrder.first(where: { group.nameOrPath == $0.pattern || group.nameOrPath.isMatch(to: $0.pattern) }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, so should I to use just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yonaskolb I need your help. I don't understand what is the correct way to do it. |
||
let files = group.children.filter { $0 is PBXFileReference } | ||
Beniamiiin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let groups = groupOrder.order | ||
brentleyjones marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.compactMap { groupName in | ||
group.children.first(where: { $0 is PBXGroup && $0.nameOrPath == groupName }) | ||
} | ||
|
||
group.children = groupOrder.fileSortPosition == .top ? files + groups : groups + files | ||
} else { | ||
let children = group.children | ||
.sorted { child1, child2 in | ||
let sortOrder1 = child1.getSortOrder(groupSortPosition: project.options.groupSortPosition) | ||
let sortOrder2 = child2.getSortOrder(groupSortPosition: project.options.groupSortPosition) | ||
|
||
if sortOrder1 != sortOrder2 { | ||
return sortOrder1 < sortOrder2 | ||
} else { | ||
return child1.context ?? "" < child2.context ?? "" | ||
if child1.nameOrPath != child2.nameOrPath { | ||
return child1.nameOrPath.localizedStandardCompare(child2.nameOrPath) == .orderedAscending | ||
} else { | ||
return child1.context ?? "" < child2.context ?? "" | ||
} | ||
} | ||
} | ||
} | ||
group.children = children.filter { $0 != group } | ||
|
||
group.children = children.filter { $0 != group } | ||
} | ||
|
||
// sort sub groups | ||
let childGroups = group.children.compactMap { $0 as? PBXGroup } | ||
childGroups.forEach(sortGroups) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not in love with the name
groupsOrder
, but I also can't think of something better right now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not only you, I don't like it too, but I couldn't find more lovely name :( I'll try to think about it again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
groupOrdering
?