├── .gitignore ├── .pre-commit-hooks.yaml ├── Makefile ├── README.md ├── bin └── xcodeproj-sort ├── lib └── xcodeproj-sort.rb └── xcodeproj-sort.gemspec /.gitignore: -------------------------------------------------------------------------------- 1 | xcodeproj-sort*.gem 2 | -------------------------------------------------------------------------------- /.pre-commit-hooks.yaml: -------------------------------------------------------------------------------- 1 | - id: xcodeproj-sort 2 | name: Sort the xcode project file 3 | description: This hook sorts your .xcodeproj file 4 | entry: xcodeproj-sort 5 | language: ruby 6 | files: '.*\.pbxproj' -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | install: 2 | gem build xcodeproj-sort.gemspec 3 | gem install xcodeproj-sort-1.0.1.gem -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # xcodeproj-sort-pre-commit-hook 2 | A pre-commit hook that sorts your xcodeproj file. 3 | 4 | 5 | 6 | ## What is it? 7 | This repo provides a ready to use [pre-commit](https://pre-commit.com/) hook for automatically sorting your Xcode project. The hook looks for files ending in .pbxproj that have been modified and sorts their project group hierarchy automatically using the [Xcodeproj](https://github.com/CocoaPods/Xcodeproj/) gem. The effect is that the sort leaves your project file modified if it's not sorted, so that pre-commit won't allow the unsorted file to go through. 8 | 9 | ## Usage 10 | If you haven't set up pre-commit, check out [pre-commit's installation docs](https://pre-commit.com/#install) first. 11 | 12 | Add the following to your `.pre-commit-config.yaml`: 13 | 14 | ``` 15 | - repo: git://github.com/noahsark769/xcodeproj-sort-pre-commit-hook 16 | sha: v1.1.2 17 | hooks: 18 | - id: xcodeproj-sort 19 | args: [--groups-position=above] 20 | ``` 21 | 22 | Then, run: 23 | 24 | ``` 25 | pre-commit install 26 | ``` 27 | 28 | ### Options 29 | Use the `--groups-position` option to specify the position of groups in the sort: 30 | - `above`: Positions groups above objects in the sort 31 | - `below`: Positions groups below objects in the sort 32 | 33 | The default is to interleave groups and objects in the sort. 34 | 35 | ### Running manually 36 | The code runs in a rubygem which is built by `pre-commit`. To run a sort manually outside of `pre-commit`, install the gem locally: 37 | 38 | ``` 39 | gem install xcodeproj-sort 40 | ``` 41 | 42 | Then, run the gem with the project file as an argument: 43 | 44 | ``` 45 | xcodeproj-sort MyProject.xcodeproj/project.pbxproj 46 | ``` 47 | 48 | ## Contributing 49 | I use this in the development of [Trestle](https://itunes.apple.com/us/app/trestle-the-new-sudoku/id1300230302?mt=8) and [CIFilter.io](https://itunes.apple.com/us/app/cifilter-io/id1457458557?mt=8), but your mileage might vary. If you notice a bug or have a feature request, please open a github issue or submit a pull request. It's best to open issues first so that work isn't duplicated. 50 | 51 | Also, feel free to reach out [on Twitter](https://twitter.com/noahsark769) if you have any questions. 52 | 53 | ## Development 54 | After closing the repo, you can run `make install` to build the gem and install locally, after which `xcodeproj-sort` should be in your path. 55 | -------------------------------------------------------------------------------- /bin/xcodeproj-sort: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'optparse' 4 | require 'xcodeproj-sort' 5 | 6 | if $PROGRAM_NAME == __FILE__ 7 | ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__) 8 | require 'bundler/setup' 9 | end 10 | 11 | xcodeproj_sort_options = nil 12 | OptionParser.new do |opts| 13 | opts.banner = "Usage: xcodeproj-sort [path/to/Project.xcodeproj] ..." 14 | 15 | opts.on( 16 | "-p", 17 | "--groups-position [above|below]", 18 | "Whether to position groups before files (above) or after files (below). Default is to interleave." 19 | ) do |p| 20 | if p 21 | xcodeproj_sort_options = { :groups_position => p.to_sym } 22 | end 23 | end 24 | end.parse! 25 | 26 | abort "An xcodeproj filename is required." if ARGV.empty? 27 | 28 | while filename = ARGV.pop 29 | XcodeprojSort.sort(filename.gsub('project.pbxproj', ''), xcodeproj_sort_options) 30 | end 31 | -------------------------------------------------------------------------------- /lib/xcodeproj-sort.rb: -------------------------------------------------------------------------------- 1 | require 'xcodeproj' 2 | 3 | module XcodeprojSort 4 | def sort(project_name, xcodeproj_sort_options) 5 | project = Xcodeproj::Project.open(project_name) 6 | project.sort xcodeproj_sort_options 7 | project.save 8 | end 9 | module_function :sort 10 | end -------------------------------------------------------------------------------- /xcodeproj-sort.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | s.name = 'xcodeproj-sort' 3 | s.version = '1.1.2' 4 | s.licenses = ['MIT'] 5 | s.summary = "Sort your xcodeproj file in a pre-commit hook." 6 | s.description = %( 7 | Xcodeproj-sort provides a simple script to sort the objects in your .xcodeproj file. 8 | The script modifies the file and prints a message only if the file is modified, which 9 | makes it good for use in a pre-commit hook. 10 | ).strip.gsub(/\s+/, ' ') 11 | s.authors = ["Noah Gilmore"] 12 | s.email = 'noah.w.gilmore@gmail.com' 13 | s.files = %w(README.md) + Dir['lib/**/*.rb'] 14 | s.homepage = 'https://github.com/noahsark769/xcodeproj-sort-pre-commit-hook' 15 | s.required_ruby_version = '>= 2.0.0' 16 | s.executables = %w(xcodeproj-sort) 17 | s.add_runtime_dependency 'xcodeproj', '~> 1.27.0' 18 | s.add_runtime_dependency 'claide', '~> 1.0' 19 | s.metadata = { "source_code_uri" => "https://github.com/noahsark769/xcodeproj-sort" } 20 | end 21 | --------------------------------------------------------------------------------