objc2swift_assistant

This ruby gem is a command line tool to generate Swift files from Objective-C source files. The file structure of the original source is mirrored by the generated code by default.

Installation

Add this line to your application's Gemfile:

gem 'objc2swift_assistant'

And then execute:

$ bundle

Or install it yourself as:

$ gem install objc2swift_assistant

Usage

obj2swift generate --source SOURCE_PATH --dest GENERATED_CODE_PATH --config CONFIG_FILE

Config Files

Config files can specify:

  • Directories to ignore
  • Source files to ignore
  • Coming Soon: Mappings from Objective-C types to Swift types

An example configuration file:

{
  "company_name": "MyGreatCompany.com",
  "ignore": [
    "**/CoreData",
    "**/Test",
    "SourceToOmit.h",
    "SourceToOmit.m"
  ],
  "subdirs": [
    {
      "path": "Customers/Customer1",
      "company_name": "Customer1 Inc."
    }
  ]
}

Config File Structure

Config File Keys

The keys supported in config files at this time:

  • "path" : The path of the current node, relative to the parent node. "**" wildcards allowed in leftmost position. Ignored in root node.
  • "company_name" : The company name to insert into the copyright and company fields in file comment blocks.
  • "subdirs" : The list of sub-nodes under a node. Each must have a "path" specified. Wildcards are supported.
  • "ignore" : An array of source-file paths to ignore at this place in the directory structure. Note that only files with the specific extension will be ignored. Note in the example that both the ".h" and the ".m" files must be specified to prevent the generation of code for classes defined in those files. "**" wildcard patterns are allowed.
  • "emit_original_code" : Include original Objective-C method bodies and class variable declarations in the generated file as commented out bodies of the swift entities. Default: true.
  • "emit_original_signatures" : Include original Objective-C method signatures (commented out) in the generated file. Ignored if emit_converted_code is false. Default: true.

Note that other options (including JSON serialization, generation of unit tests) are available through the Ruby API, but are not supported or documented at this time.

Wildcard Support

Only the **/... wildcard pattern is currently supported. Full "file globbing" may be supported in the future.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  1. Fork it ( https://github.com/[my-github-username]/objc2swift_assistant/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request