.pbxproj いぢり
ファイルの追加を適当にやってみたけどこれでいいのか謎すぎる。オブジェクトに id がついているけど、Xcode がどうやってあれを求めているのかわからないので SHA1 ハッシュとって最初の24文字を使うようにしてみたけど、なんだかなぁ。どっかにフォーマットのリファレンスがあるかなぁと思ってぐぐっていたけれど、ないっぽい。
require "digest/sha1"
require "osx/cocoa"
include OSX
class XcodeProject
class Group
def initialize(proj, id)
@proj, @dic = proj, proj[id]
end
def [](key)
@dic[key]
end
def inspect
"#<Group #{self["name"]}>"
end
def add_file(type, path, tree)
id = Digest::SHA1.hexdigest(path)[0, 24].upcase
@proj.plist["objects"][id] = NSDictionary.alloc.initWithDictionary({
"isa" => "PBXFileReference",
"lastKnownFileType" => type,
"path" => path,
"sourceTree" => tree,
})
@dic["children"] = NSArray.alloc.initWithArray(@dic["children"].to_a << id)
end
end
attr_accessor :objects
attr_reader :rootObject, :plist
def initialize(bundle_path)
@plist_path = "#{bundle_path}/project.pbxproj"
@plist = NSPropertyListSerialization.objc_send(
:propertyListFromData, NSData.alloc.initWithContentsOfFile(@plist_path),
:mutabilityOption, NSPropertyListMutableContainersAndLeaves,
:format, nil,
:errorDescription, nil
)
@objects = @plist["objects"]
@rootObject = self[@plist["rootObject"]]
end
def [](obj_id)
@objects[obj_id.to_s]
end
def groups
mainGroup = self[@rootObject["mainGroup"]]
Hash[*mainGroup["children"].map {|i|
g = Group.new(self, i)
[g["name"].to_s, g]
}.select {|i| i[1]["isa"].to_s == "PBXGroup"}.flatten]
end
def save
File.open(@plist_path, "w") do |f|
f.puts @plist
end
end
end
proj = XcodeProject.new("testcocoa.xcodeproj")
p proj.groups
proj.groups["Classes"].add_file("text.script.ruby", "path.rb", "<group>")
proj.save