codeshift

A Ruby CLI to transform source code by converting them to ASTs using the parser gem.


Keywords
codemod, codemods, gem, ruby-gem
License
MIT
Install
gem install codeshift -v 0.1.7

Documentation

Codeshift

Gem Version

A Ruby codemod CLI to transform your source code using AST(Abstract Syntax Trees) and the parser gem. It is typically used on ruby codebase like RAILS applications and other stuff.

Installation

$ gem install codeshift

Usage

This tool requires a transform.rb file which contains the transform logic to be applied on the source files in a folder.

$ codeshift -t [TRANSFORM FILE] [PATHS]

To apply the transform logic on your app/models files

$ codeshift -t transform.rb app/models/**/*.rb

For example if you want to reverse the local variable names and method names in your code you will be doing something like this:

Create a new ruby file with the tranformation logic to be applied on the AST of the source code. For writing transforms you can make use of the Ruby AST Explorer

transform.rb

# Your Transform Class should always extend from 
# Parser:: TreeRewriter
class Transform < Parser::TreeRewriter
  def on_lvasgn(node)
    # Reverse the variable names
    replace(node.loc.to_hash[:name], node.children[0].to_s.reverse)
  end

  def on_def(node)
    replace(node.loc.to_hash[:name], node.children[0].to_s.reverse)
  end
end

If your source code looks like below in a folder called ~/Desktop/test/ruby

sample.rb

tips = ["hello", "world"]
def print_tips
  tips.each { |key, value| print "Tip #{key}: #{value}" }
end

Then use it against your source code

$ codeshift -t transform.rb ~/Desktop/test/ruby/**/*.rb

Then your source will be transformed something like:

sample.rb

spit = ["hello", "world"]
def spit_tnirp
  tips.each { |key, value| print "Tip #{key}: #{value}" }
end

Options

Usage: codeshift -t [path]

  • --version Print version number
  • -t, --transform=TRANSFORM path to the transform file. Can be either a local path or url (default: ./transform.rb)
  • -h, --help Prints this help

tranfrom-file

The transform file could be a local file or a remote url. For example you can use like

$ codeshift -t https://gist.githubusercontent.com/[user]/.../transform.rb ~/Desktop/test/ruby/**/*.rb

path

The path could be a list of directories or files separated by space.

$ codeshift -t transform.rb ~/app/legacy/ruby/**/*.rb ~/app/old/**/*.rb
$ codeshift -t transform.rb ~/code/legacy/app/models/account.rb ~/old/app/models/customer.rb

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also 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, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rajasegar/codeshift. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Codeshift project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.