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

Setting paths for nested groups #121

Closed
gfontenot opened this issue Feb 8, 2014 · 10 comments · Fixed by #124
Closed

Setting paths for nested groups #121

gfontenot opened this issue Feb 8, 2014 · 10 comments · Fixed by #124

Comments

@gfontenot
Copy link
Contributor

Having trouble groking what's going on here.

I'm trying to create this basic group structure:

main_group
└── foo
    └── bar
        └── baz

The paths for each group should be set relative to the containing group, so that the group structure mimics the structure on disk. For some reason, I can't seem to make this happen. I'm doing something similar to the following:

  def create_groups(parent_group, group_hash, path_to_group)
    group_hash.each_pair do |group_name, children|
      new_path = "#{path_to_group}/#{group_name}"
      created_group = parent_group.new_group(group_name, new_path)
      unless children.nil?
        create_groups(created_group, children, new_path)
      end
    end
  end

  create_groups(main_group, {"foo" => {"bar" => {"baz" => nil}}}, main_group.name)

This works great for the main group, as well as foo. But then bar gets set to main_group/bar and baz gets set to main_group/baz. What the hell am I missing?

@alloy
Copy link
Member

alloy commented Feb 10, 2014

Fabio is probably more familiar with this, but we're both out of time to look into this atm. At the very least I'll say that it should work as you expect.

@gfontenot
Copy link
Contributor Author

Looked into this a bit more, and I'm still not sure why it's not acting as expected, but it seems to be related to the relative path stuff. It's almost as if it's looking at main_group and basing the relative path on that (or maybe the project? it's hard to tell for sure), instead of basing the path on the actual parent group. However, setting the path directly on the group gives me the desired result. Here's a standalone version of this snippet that will display the bug:

require 'xcodeproj'

def create_new_project
  parent_group = @project.new_group('base-group', 'base-group')
  create_groups(parent_group, {'foo' => {'bar' => {'baz' => nil}}}, 'base-group')
end

def create_groups(parent_group, group_hash, path_to_group)
  group_hash.each_pair do |group_name, children|
    new_path = "#{path_to_group}/#{group_name}"
    created_group = parent_group.new_group(group_name, new_path)
    created_group.path = new_path # We shouldn't need this line. Remove to see the issue.
    unless children.nil?
      create_groups(created_group, children, new_path)
    end
  end
end

@project = Xcodeproj::Project.new('foo.xcodeproj')
create_new_project
@project.save

@fabiopelosin
Copy link
Member

- (PBXGroup) new_group(name, path = nil, source_tree = :group)
  • name: The name of the group
  • path: The path of the group in the file system relative to the source tree
  • source_tree: Used to interpretate the path (can be absolute, relative to the project file, etc.)

@fabiopelosin
Copy link
Member

Sorry I missed the point. If the source_tree is set to :group (the path is relative to the path of the parent group) the path should be:

    new_path = group_name

@gfontenot
Copy link
Contributor Author

Right, that's what I assumed initially. But if you modify the script above like that, you'll see that the path is then set to ../foo, ../bar and ../baz. The full path points outside the project directory.

@gfontenot
Copy link
Contributor Author

I should note that it seems like the issue is somewhere inside GroupableHelper.set_path_with_source_tree. So this means that I'm having the same wacky behavior when linking files.

@gfontenot
Copy link
Contributor Author

The simplest way to see that behavior is to add created_group.new_file('test.txt') to that script. By the time we get to the baz group, the path is ../../../../../../../../../../test.txt. That doesn't seem right to me.

@gfontenot
Copy link
Contributor Author

Kept digging. Everything is cool until this line in GroupableHelper. The relative path is coming back as ../foo instead of foo.

@fabiopelosin
Copy link
Member

Can you inspect the values of path and of source_tree_real_path?

This logic can create issues with relative file paths, and it is preferable to use absolute paths with Xcodeproj. I don’t have time to check at the moment and I don’t recall the exact details but this gotcha should be noted somewhere in the comments.

gfontenot added a commit that referenced this issue Feb 13, 2014
Fixes an issue where if you supplied a relative path when creating a
project, all of the relative references from that point on were
incorrect. By expanding the path on project creation, we ensure that
we're always setting the path the proper way.

Fixes #121
@gfontenot
Copy link
Contributor Author

The issue ended up being a bug that stemmed from using a relative path to create the project itself. Closing this issue in favor of #124

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants