GithubHelp home page GithubHelp logo

nick96 / codeshift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rajasegar/codeshift

0.0 1.0 0.0 42 KB

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

License: MIT License

Shell 2.30% Ruby 97.70%

codeshift's Introduction

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.

codeshift's People

Contributors

amanda-mitchell avatar dependabot[bot] avatar rajasegar avatar spone avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.