Skip to content

A Ruby CLI to transform source code using ASTs - A Ruby Codemod

License

Notifications You must be signed in to change notification settings

rajasegar/codeshift

Repository files navigation

Codeshift

Gem Version Build Status Coverage Status

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

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 Ruby AST Explorer and cybertron

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.name, node.children[0].to_s.reverse)
  end

  def on_def(node)
    replace(node.loc.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 the transform against your source code

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

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

transform-file

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

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

path

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

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

Related tools

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.