sync_project.rb 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. #!/usr/bin/ruby
  2. # Copyright 2018 Google
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Syncs Xcode project folder and target structure with the filesystem. This
  16. # script finds all files on the filesystem that match the patterns supplied
  17. # below and changes the project to match what it found.
  18. #
  19. # Run this script after adding/removing tests to keep the project in sync.
  20. require 'cocoapods'
  21. require 'optparse'
  22. require 'pathname'
  23. # Note that xcodeproj 1.5.8 appears to be broken
  24. # https://github.com/CocoaPods/Xcodeproj/issues/572
  25. gem 'xcodeproj', '!= 1.5.8'
  26. require 'xcodeproj'
  27. ROOT_DIR = Pathname.new(__FILE__).dirname().join('..').expand_path()
  28. PODFILE_DIR = ROOT_DIR.join('Firestore', 'Example')
  29. def main()
  30. test_only = false
  31. OptionParser.new do |opts|
  32. opts.on('--test-only', 'Check diffs without writing') do |v|
  33. test_only = v
  34. end
  35. end.parse!
  36. # Make all filenames relative to the project root.
  37. Dir.chdir(ROOT_DIR.to_s)
  38. changes = sync_firestore(test_only)
  39. status = test_only && changes > 0 ? 2 : 0
  40. exit(status)
  41. end
  42. # Make it so that you can "add" hash literals together by merging their
  43. # contents.
  44. class Hash
  45. def +(other)
  46. return merge(other)
  47. end
  48. end
  49. def sync_firestore(test_only)
  50. project = Xcodeproj::Project.open('Firestore/Example/Firestore.xcodeproj')
  51. spec = Pod::Spec.from_file('FirebaseFirestore.podspec')
  52. swift_spec = Pod::Spec.from_file('FirebaseFirestoreSwift.podspec')
  53. # Enable warnings after opening the project to avoid the warnings in
  54. # xcodeproj itself
  55. $VERBOSE = true
  56. s = Syncer.new(project, ROOT_DIR)
  57. # Files on the filesystem that should be ignored.
  58. s.ignore_files = [
  59. 'CMakeLists.txt',
  60. 'README.md',
  61. 'InfoPlist.strings',
  62. '*.orig',
  63. '*.plist',
  64. '.*',
  65. ]
  66. # Folder groups in the Xcode project that contain tests.
  67. s.groups = [
  68. 'Tests',
  69. 'CoreTests',
  70. 'CoreTestsProtos',
  71. 'SwiftTests',
  72. ]
  73. # Copy key settings from the podspec
  74. podspec_settings = [
  75. 'CLANG_CXX_LANGUAGE_STANDARD',
  76. 'GCC_C_LANGUAGE_STANDARD',
  77. ]
  78. xcconfig_spec = spec.attributes_hash['pod_target_xcconfig'].dup
  79. xcconfig_spec.select! { |k, v| podspec_settings.include?(k) }
  80. # Settings for all Objective-C/C++ targets
  81. xcconfig_objc = xcconfig_spec + {
  82. 'INFOPLIST_FILE' => '"${SRCROOT}/Tests/Tests-Info.plist"',
  83. # Duplicate the header search paths from the main podspec because they're
  84. # phrased in terms of PODS_TARGET_SRCROOT, which isn't defined for other
  85. # targets.
  86. 'HEADER_SEARCH_PATHS' => [
  87. # Include fully qualified from the root of the repo
  88. '"${PODS_ROOT}/../../.."',
  89. # Make public headers available as "FIRQuery.h"
  90. '"${PODS_ROOT}/../../../Firestore/Source/Public"',
  91. # Generated protobuf and nanopb output expects to search relative to the
  92. # output path.
  93. '"${PODS_ROOT}/../../../Firestore/Protos/cpp"',
  94. '"${PODS_ROOT}/../../../Firestore/Protos/nanopb"',
  95. # Other dependencies that assume #includes are relative to their roots.
  96. '"${PODS_ROOT}/../../../Firestore/third_party/abseil-cpp"',
  97. '"${PODS_ROOT}/GoogleBenchmark/include"',
  98. '"${PODS_ROOT}/GoogleTest/googlemock/include"',
  99. '"${PODS_ROOT}/GoogleTest/googletest/include"',
  100. '"${PODS_ROOT}/leveldb-library/include"',
  101. ],
  102. 'SYSTEM_HEADER_SEARCH_PATHS' => [
  103. # Nanopb wants to #include <pb.h>
  104. '"${PODS_ROOT}/nanopb"',
  105. # Protobuf wants to #include <google/protobuf/stubs/common.h>
  106. '"${PODS_ROOT}/ProtobufCpp/src"',
  107. ]
  108. }
  109. xcconfig_swift = {
  110. 'SWIFT_OBJC_BRIDGING_HEADER' =>
  111. '${PODS_ROOT}/../../../Firestore/Swift/Tests/BridgingHeader.h',
  112. 'SWIFT_VERSION' => pick_swift_version(swift_spec),
  113. }
  114. ['iOS', 'macOS', 'tvOS'].each do |platform|
  115. s.target "Firestore_Example_#{platform}" do |t|
  116. t.xcconfig = xcconfig_objc + xcconfig_swift + {
  117. # Passing -all_load is required to get all our C++ code into the test
  118. # host.
  119. #
  120. # Normally when running tests, the test target contains only the tests
  121. # proper, and links against the test host for the code under test. The
  122. # test host doesn't do anything though, so the linker strips C++-only
  123. # object code away.
  124. #
  125. # This is particular to C++ because by default CocoaPods configures the
  126. # test host to link with the -ObjC flag. This causes the linker to pull
  127. # in all Objective-C object code. -all_load fixes this by forcing the
  128. # linker to pull in everything.
  129. 'OTHER_LDFLAGS' => '-all_load',
  130. }
  131. end
  132. s.target "Firestore_Tests_#{platform}" do |t|
  133. t.source_files = [
  134. 'Firestore/Example/Tests/**',
  135. 'Firestore/core/test/**',
  136. 'Firestore/Protos/cpp/**',
  137. ]
  138. t.exclude_files = [
  139. # needs to be in project but not in target
  140. 'Firestore/Example/Tests/Tests-Info.plist',
  141. # These files are integration tests, handled below
  142. 'Firestore/Example/Tests/Integration/**',
  143. ]
  144. t.xcconfig = xcconfig_objc + xcconfig_swift
  145. end
  146. end
  147. ['iOS', 'macOS', 'tvOS'].each do |platform|
  148. s.target "Firestore_IntegrationTests_#{platform}" do |t|
  149. t.source_files = [
  150. 'Firestore/Example/Tests/**',
  151. 'Firestore/Protos/cpp/**',
  152. 'Firestore/Swift/Tests/**',
  153. 'Firestore/core/test/**',
  154. ]
  155. t.exclude_files = [
  156. # needs to be in project but not in target
  157. 'Firestore/Example/Tests/Tests-Info.plist',
  158. ]
  159. t.xcconfig = xcconfig_objc + xcconfig_swift
  160. end
  161. s.target 'Firestore_Benchmarks_iOS' do |t|
  162. t.xcconfig = xcconfig_objc + {
  163. 'INFOPLIST_FILE' => '${SRCROOT}/Benchmarks/Info.plist',
  164. }
  165. end
  166. s.target 'Firestore_FuzzTests_iOS' do |t|
  167. t.xcconfig = xcconfig_objc + {
  168. 'INFOPLIST_FILE' =>
  169. '${SRCROOT}/FuzzTests/Firestore_FuzzTests_iOS-Info.plist',
  170. 'OTHER_CFLAGS' => [
  171. '-fsanitize=fuzzer',
  172. ]
  173. }
  174. end
  175. s.target 'Firestore_SwiftTests_iOS' do |t|
  176. t.xcconfig = xcconfig_objc + xcconfig_swift
  177. end
  178. end
  179. changes = s.sync(test_only)
  180. if not test_only
  181. sort_project(project)
  182. if project.dirty?
  183. project.save()
  184. end
  185. end
  186. return changes
  187. end
  188. # Picks a swift version to use from a podspec's swift_versions
  189. def pick_swift_version(spec)
  190. versions = spec.attributes_hash['swift_versions']
  191. if versions.is_a?(Array)
  192. return versions[-1]
  193. end
  194. return versions
  195. end
  196. # A list of filesystem patterns
  197. class PatternList
  198. def initialize()
  199. @patterns = []
  200. end
  201. attr_accessor :patterns
  202. # Evaluates the rel_path against the given list of fnmatch patterns.
  203. def matches?(rel_path)
  204. @patterns.each do |pattern|
  205. if rel_path.fnmatch?(pattern)
  206. return true
  207. end
  208. end
  209. return false
  210. end
  211. end
  212. # The definition of a test target including the target name, its source_files
  213. # and exclude_files. A file is considered part of a target if it matches a
  214. # pattern in source_files but does not match a pattern in exclude_files.
  215. class TargetDef
  216. def initialize(name)
  217. @name = name
  218. @sync_sources = false
  219. @source_files = PatternList.new()
  220. @exclude_files = PatternList.new()
  221. @xcconfig = {}
  222. end
  223. attr_reader :name, :sync_sources, :source_files, :exclude_files
  224. attr_accessor :xcconfig
  225. def source_files=(value)
  226. @sync_sources = true
  227. @source_files.patterns.replace(value)
  228. end
  229. def exclude_files=(value)
  230. @exclude_files.patterns.replace(value)
  231. end
  232. # Returns true if the given rel_path matches this target's source_files
  233. # but not its exclude_files.
  234. #
  235. # Args:
  236. # - rel_path: a Pathname instance with a path relative to the project root.
  237. def matches?(rel_path)
  238. return @source_files.matches?(rel_path) && !@exclude_files.matches?(rel_path)
  239. end
  240. def diff(project_files, target)
  241. diff = Diff.new
  242. project_files.each do |file_ref|
  243. if matches?(relative_path(file_ref))
  244. entry = diff.track(file_ref.real_path)
  245. entry.in_source = true
  246. entry.ref = file_ref
  247. end
  248. end
  249. each_target_file(target) do |file_ref|
  250. entry = diff.track(file_ref.real_path)
  251. entry.in_target = true
  252. entry.ref = file_ref
  253. end
  254. return diff
  255. end
  256. # We're only managing synchronization of files in these phases.
  257. INTERESTING_PHASES = [
  258. Xcodeproj::Project::Object::PBXHeadersBuildPhase,
  259. Xcodeproj::Project::Object::PBXSourcesBuildPhase,
  260. Xcodeproj::Project::Object::PBXResourcesBuildPhase,
  261. ]
  262. # Finds all the files referred to by any phase in a target
  263. def each_target_file(target)
  264. target.build_phases.each do |phase|
  265. next if not INTERESTING_PHASES.include?(phase.class)
  266. phase.files.each do |build_file|
  267. yield build_file.file_ref
  268. end
  269. end
  270. end
  271. end
  272. class Syncer
  273. HEADERS = %w{.h}
  274. SOURCES = %w{.c .cc .m .mm .swift}
  275. def initialize(project, root_dir)
  276. @project = project
  277. @finder = DirectoryLister.new(root_dir)
  278. @groups = []
  279. @targets = []
  280. @seen_groups = {}
  281. end
  282. # Considers the given fnmatch glob patterns to be ignored by the syncer.
  283. # Patterns are matched both against the basename and project-relative
  284. # qualified pathname.
  285. def ignore_files=(patterns)
  286. @finder.add_patterns(patterns)
  287. end
  288. # Names the groups within the project that serve as roots for tests within
  289. # the project.
  290. def groups=(groups)
  291. @groups = []
  292. groups.each do |group|
  293. project_group = @project[group]
  294. if project_group.nil?
  295. raise "Project does not contain group #{group}"
  296. end
  297. @groups.push(@project[group])
  298. end
  299. end
  300. # Starts a new target block. Creates a new TargetDef and yields it.
  301. def target(name, &block)
  302. t = TargetDef.new(name)
  303. @targets.push(t)
  304. block.call(t)
  305. end
  306. # Finds the target definition with the given name.
  307. def find_target(name)
  308. @targets.each do |target|
  309. if target.name == name
  310. return target
  311. end
  312. end
  313. return nil
  314. end
  315. # Synchronizes the filesystem with the project.
  316. #
  317. # Generally there are three separate ways a file is referenced within a project:
  318. #
  319. # 1. The file must be in the global list of files, assigning it a UUID.
  320. # 2. The file must be added to folder groups, describing where it is in the
  321. # folder view of the Project Navigator.
  322. # 3. The file must be added to a target phase describing how it's built.
  323. #
  324. # The Xcodeproj library handles (1) for us automatically if we do (2).
  325. #
  326. # Returns the number of changes made during synchronization.
  327. def sync(test_only = false)
  328. # Figure the diff between the filesystem and the group structure
  329. group_differ = GroupDiffer.new(@finder)
  330. group_diff = group_differ.diff(@groups)
  331. changes = group_diff.changes
  332. to_remove = group_diff.to_remove
  333. # Add all files first, to ensure they exist for later steps
  334. add_to_project(group_diff.to_add)
  335. project_files = find_project_files_after_removal(@project.files, to_remove)
  336. @project.native_targets.each do |target|
  337. target_def = find_target(target.name)
  338. next if target_def.nil?
  339. if target_def.sync_sources
  340. target_diff = target_def.diff(project_files, target)
  341. target_diff.sorted_entries.each do |entry|
  342. changes += sync_target_entry(target, entry)
  343. end
  344. end
  345. if not test_only
  346. # Don't sync xcconfig changes in test-only mode.
  347. sync_xcconfig(target_def, target)
  348. end
  349. end
  350. remove_from_project(to_remove)
  351. return changes
  352. end
  353. private
  354. def find_project_files_after_removal(files, to_remove)
  355. remove_paths = Set.new()
  356. to_remove.each do |entry|
  357. remove_paths.add(entry.path)
  358. end
  359. result = []
  360. files.each do |file_ref|
  361. next if file_ref.source_tree != '<group>'
  362. next if remove_paths.include?(file_ref.real_path)
  363. path = file_ref.real_path
  364. next if @finder.ignore_basename?(path.basename)
  365. next if @finder.ignore_pathname?(path)
  366. result.push(file_ref)
  367. end
  368. return result
  369. end
  370. # Adds the given file to the project, in a path starting from the test root
  371. # that fully prefixes the file.
  372. def add_to_project(to_add)
  373. to_add.each do |entry|
  374. path = entry.path
  375. root_group = find_group_containing(path)
  376. # Find or create the group to contain the path.
  377. dir_rel_path = path.relative_path_from(root_group.real_path).dirname
  378. group = root_group.find_subpath(dir_rel_path.to_s, true)
  379. file_ref = group.new_file(path.to_s)
  380. ext = path.extname
  381. entry.ref = file_ref
  382. end
  383. end
  384. # Finds a group whose path prefixes the given entry. Starting from the
  385. # project root may not work since not all directories exist within the
  386. # example app.
  387. def find_group_containing(path)
  388. @groups.each do |group|
  389. rel = path.relative_path_from(group.real_path)
  390. next if rel.to_s.start_with?('..')
  391. return group
  392. end
  393. raise "Could not find an existing group that's a parent of #{entry.path}"
  394. end
  395. # Removes the given file references from the project after the file is found
  396. # to not exist on the filesystem but references to it still exist in the
  397. # project.
  398. def remove_from_project(to_remove)
  399. to_remove.each do |entry|
  400. file_ref = entry.ref
  401. file_ref.remove_from_project
  402. end
  403. end
  404. # Syncs a single build file for a given phase. Returns the number of changes
  405. # made.
  406. def sync_target_entry(target, entry)
  407. return 0 if entry.unchanged?
  408. phase = find_phase(target, entry.path)
  409. return 0 if phase.nil?
  410. mark_change_in_group(target.display_name)
  411. if entry.to_add?
  412. printf(" %s - added\n", basename(entry.ref))
  413. phase.add_file_reference(entry.ref)
  414. else
  415. printf(" %s - removed\n", basename(entry.ref))
  416. phase.remove_file_reference(entry.ref)
  417. end
  418. return 1
  419. end
  420. # Finds the phase to which the given pathname belongs based on its file
  421. # extension.
  422. #
  423. # Returns nil if the path does not belong in any phase.
  424. def find_phase(target, path)
  425. path = normalize_to_pathname(path)
  426. ext = path.extname
  427. if SOURCES.include?(ext)
  428. return target.source_build_phase
  429. elsif HEADERS.include?(ext)
  430. # TODO(wilhuff): sync headers
  431. #return target.headers_build_phase
  432. return nil
  433. else
  434. return target.resources_build_phase
  435. end
  436. end
  437. # Syncs build settings to the .xcconfig file for the build configuration,
  438. # avoiding any changes to the Xcode project file.
  439. def sync_xcconfig(target_def, target)
  440. dirty = false
  441. target.build_configurations.each do |config|
  442. requested = flatten(target_def.xcconfig)
  443. path = PODFILE_DIR.join(config.base_configuration_reference.path)
  444. contents = Xcodeproj::Config.new(path)
  445. contents.merge!(requested)
  446. contents.save_as(path)
  447. end
  448. end
  449. # Converts a hash of lists to a flat hash of strings.
  450. def flatten(xcconfig)
  451. result = {}
  452. xcconfig.each do |key, value|
  453. if value.is_a?(Array)
  454. value = value.join(' ')
  455. end
  456. result[key] = value
  457. end
  458. return result
  459. end
  460. end
  461. def normalize_to_pathname(file_ref)
  462. if !file_ref.is_a? Pathname
  463. if file_ref.is_a? String
  464. file_ref = Pathname.new(file_ref)
  465. else
  466. file_ref = file_ref.real_path
  467. end
  468. end
  469. return file_ref
  470. end
  471. def basename(file_ref)
  472. return normalize_to_pathname(file_ref).basename
  473. end
  474. def relative_path(file_ref)
  475. path = normalize_to_pathname(file_ref)
  476. return path.relative_path_from(ROOT_DIR)
  477. end
  478. def mark_change_in_group(group)
  479. path = group.to_s
  480. if !@seen_groups.has_key?(path)
  481. puts "#{path} ..."
  482. @seen_groups[path] = true
  483. end
  484. end
  485. def sort_project(project)
  486. project.groups.each do |group|
  487. sort_group(group)
  488. end
  489. project.targets.each do |target|
  490. target.build_phases.each do |phase|
  491. phase.files.sort! { |a, b|
  492. a.file_ref.real_path.basename <=> b.file_ref.real_path.basename
  493. }
  494. end
  495. end
  496. end
  497. def sort_group(group)
  498. group.groups.each do |child|
  499. sort_group(child)
  500. end
  501. group.children.sort! do |a, b|
  502. # Sort groups first
  503. if a.isa == 'PBXGroup' && b.isa != 'PBXGroup'
  504. -1
  505. elsif a.isa != 'PBXGroup' && b.isa == 'PBXGroup'
  506. 1
  507. elsif a.display_name && b.display_name
  508. File.basename(a.display_name) <=> File.basename(b.display_name)
  509. else
  510. 0
  511. end
  512. end
  513. end
  514. # Tracks how a file is referenced: in the project file, on the filesystem,
  515. # neither, or both.
  516. class DiffEntry
  517. def initialize(path)
  518. @path = path
  519. @in_source = false
  520. @in_target = false
  521. @ref = nil
  522. end
  523. attr_reader :path
  524. attr_accessor :in_source, :in_target, :ref
  525. def unchanged?()
  526. return @in_source && @in_target
  527. end
  528. def to_add?()
  529. return @in_source && !@in_target
  530. end
  531. def to_remove?()
  532. return !@in_source && @in_target
  533. end
  534. end
  535. # A set of differences between some source and a target.
  536. class Diff
  537. def initialize()
  538. @entries = {}
  539. end
  540. attr_accessor :entries
  541. def track(path)
  542. if @entries.has_key?(path)
  543. return @entries[path]
  544. end
  545. entry = DiffEntry.new(path)
  546. @entries[path] = entry
  547. return entry
  548. end
  549. # Returns a list of entries that are to be added to the target
  550. def to_add()
  551. return @entries.values.select { |entry| entry.to_add? }
  552. end
  553. # Returns a list of entries that are to be removed to the target
  554. def to_remove()
  555. return @entries.values.select { |entry| entry.to_remove? }
  556. end
  557. # Returns a list of entries in sorted order.
  558. def sorted_entries()
  559. return @entries.values.sort { |a, b| a.path.basename <=> b.path.basename }
  560. end
  561. def changes()
  562. return @entries.values.count { |entry| entry.to_add? || entry.to_remove? }
  563. end
  564. end
  565. # Diffs folder groups against the filesystem directories referenced by those
  566. # folder groups.
  567. #
  568. # Folder groups in the project may each refer to an arbitrary path, so
  569. # traversing from a parent group to a subgroup may jump to a radically
  570. # different filesystem location or alias a previously processed directory.
  571. #
  572. # This class performs a diff by essentially tracking only whether or not a
  573. # given absolute path has been seen in either the filesystem or the group
  574. # structure, without paying attention to where in the group structure the file
  575. # reference actually occurs.
  576. #
  577. # This helps ensure that the default arbitrary splits in group structure are
  578. # preserved. For example, "Supporting Files" is an alias for the same directory
  579. # as the parent group, and Apple's default project setup hides some files in
  580. # "Supporting Files". The approach this diff takes preserves this arrangement
  581. # without understanding specifically which files should be hidden and which
  582. # should exist in the parent.
  583. #
  584. # However, this approach has limitations: removing a file from "Supporting
  585. # Files" will be handled, but re-adding the file is likely to add it to the
  586. # group that mirrors the filesystem hierarchy rather than back into its
  587. # original position. So far this approach has been acceptable because there's
  588. # nothing of value in these aliasing folders. Should this change we'll have to
  589. # revisit.
  590. class GroupDiffer
  591. def initialize(dir_lister)
  592. @dir_lister = dir_lister
  593. @dirs = {}
  594. @diff = Diff.new()
  595. end
  596. # Finds all files on the filesystem contained within the paths of the given
  597. # groups and computes a list of DiffEntries describing the state of the
  598. # files.
  599. #
  600. # Args:
  601. # - groups: A list of PBXGroup objects representing folder groups within the
  602. # project that contain files of interest.
  603. #
  604. # Returns:
  605. # A hash of Pathname to DiffEntry objects, one for each file found. If the
  606. # file exists on the filesystem, :in_source will be true. If the file exists
  607. # in the project :in_target will be true and :ref will be set to the
  608. # PBXFileReference naming the file.
  609. def diff(groups)
  610. groups.each do |group|
  611. diff_project_files(group)
  612. end
  613. return @diff
  614. end
  615. private
  616. # Recursively traverses all the folder groups in the Xcode project and finds
  617. # files both on the filesystem and the group file listing.
  618. def diff_project_files(group)
  619. find_fs_files(group.real_path)
  620. group.groups.each do |child|
  621. diff_project_files(child)
  622. end
  623. group.files.each do |file_ref|
  624. path = file_ref.real_path
  625. entry = @diff.track(path)
  626. entry.in_target = true
  627. entry.ref = file_ref
  628. if path.file?
  629. entry.in_source = true
  630. end
  631. end
  632. end
  633. def find_fs_files(parent_path)
  634. # Avoid re-traversing the filesystem
  635. if @dirs.has_key?(parent_path)
  636. return
  637. end
  638. @dirs[parent_path] = true
  639. @dir_lister.entries(parent_path).each do |path|
  640. if path.directory?
  641. find_fs_files(path)
  642. next
  643. end
  644. entry = @diff.track(path)
  645. entry.in_source = true
  646. end
  647. end
  648. end
  649. # Finds files on the filesystem while ignoring files that have been declared to
  650. # be ignored.
  651. class DirectoryLister
  652. def initialize(root_dir)
  653. @root_dir = root_dir
  654. @ignore_basenames = ['.', '..']
  655. @ignore_pathnames = []
  656. end
  657. def add_patterns(patterns)
  658. patterns.each do |pattern|
  659. if File.basename(pattern) != pattern
  660. @ignore_pathnames.push(File.join(@root_dir, pattern))
  661. else
  662. @ignore_basenames.push(pattern)
  663. end
  664. end
  665. end
  666. # Finds filesystem entries that are immediate children of the given Pathname,
  667. # ignoring files that match the global ignore_files patterns.
  668. def entries(path)
  669. result = []
  670. return result if not path.exist?
  671. path.entries.each do |entry|
  672. next if ignore_basename?(entry)
  673. file = path.join(entry)
  674. next if ignore_pathname?(file)
  675. result.push(file)
  676. end
  677. return result
  678. end
  679. def ignore_basename?(basename)
  680. @ignore_basenames.each do |ignore|
  681. if basename.fnmatch(ignore)
  682. return true
  683. end
  684. end
  685. return false
  686. end
  687. def ignore_pathname?(file)
  688. @ignore_pathnames.each do |ignore|
  689. if file.fnmatch(ignore)
  690. return true
  691. end
  692. end
  693. return false
  694. end
  695. end
  696. if __FILE__ == $0
  697. main()
  698. end