Skip to content
A Ruby CLI to transform source code using ASTs - A Ruby Codemod
Ruby Shell
Branch: master
Clone or download
rajasegar [CHORE] Removing glob options from paths
Now the glob pattern is automatically constructed
with paths like app/models => app/models/**/*.rb
Latest commit f183dd2 Sep 9, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin First Commit Aug 25, 2019
exe First Commit Aug 25, 2019
lib [CHORE] Removing glob options from paths Sep 9, 2019
spec [REFACTOR] Renaming lib modules Sep 3, 2019
.gitignore [CHORE] Adding Gemfile.lock to gitignore Sep 3, 2019
.rspec First Commit Aug 25, 2019
.travis.yml First Commit Aug 25, 2019
CODE_OF_CONDUCT.md First Commit Aug 25, 2019
Gemfile First Commit Aug 25, 2019
LICENSE.txt First Commit Aug 25, 2019
README.md [CHORE] Removing glob options from paths Sep 9, 2019
Rakefile First Commit Aug 25, 2019
codeshift.gemspec [REFACTOR] Renaming lib modules Sep 3, 2019

README.md

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

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

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

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

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.

You can’t perform that action at this time.