codeshift 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6af6f02701bdb6430dd72cfc5dbd43b0e0e16c7382905d0c951c13366d60acde
4
- data.tar.gz: 500607afbfeea607ca65d5c95eb3030c6a37adbf8bb22092a6e06ce6a68b1d63
3
+ metadata.gz: 3b5b1f7425f32b67e65b8ce7b0b2e22061495570b95600d8c6114b8a86ebbedd
4
+ data.tar.gz: 055d30acead1d71b2af0c0e9895adcf2cc64c13e600548b713bd108831fb3b23
5
5
  SHA512:
6
- metadata.gz: 8e0613d86f951f5c664f9fa092b6c1f7c5a49e05fbe5b8c0aa58f1f67a1a10fd6df80359b94db2ecb293c601d467586b8c862d998d3f08749cf33e0bc171d4af
7
- data.tar.gz: 43593a3b6f35c47ba04c810ce86124723d18500c0b4b60644996bc6800361415ad7feecf785989e4e395e88a48db62b046fe1fb0d4c8db5d2c59abd6eb9a3bcf
6
+ metadata.gz: 77e1cdb4e9a99b634acbe0feeb5f6286ae439ed401f0df13a234f628def50a03900bd4eabb644fd879451b67fa1466cd3dd8520aedeb55aa57601ec12f9bb848
7
+ data.tar.gz: 847fd2a32bb79bcefdc3df64c5d0c1914f368ebe72ccf13f51f840d347bea43aa9decbe1e048330894d424e50c8fb0dbe37c7d57578696676ec08ba9f5f5b0ff
data/README.md CHANGED
@@ -1,28 +1,80 @@
1
1
  # Codeshift
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/codeshift`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ A Ruby codemod CLI to transform your source code using AST(Abstract Syntax Trees) and the [parser](https://github.com/whitequark/parser) gem. It is typically used on ruby codebase like RAILS applications and other stuff.
6
4
 
7
5
  ## Installation
8
6
 
9
- Add this line to your application's Gemfile:
7
+ ```sh
8
+ $ gem install codeshift
9
+ ```
10
+
11
+
12
+ ## Usage
13
+ This tool requires a `transform.rb` file which contains the transform logic to be
14
+ applied on the source files in a folder.
15
+
16
+ ```sh
17
+ $ codeshift -t [TRANSFORM FILE] [PATHS]
18
+ ```
19
+
20
+ To apply the transform logic on your `app/models` files
21
+
22
+ ```sh
23
+ $ codeshift -t transform.rb app/models/**/*.rb
24
+ ```
25
+
26
+ For example if you want to reverse the local variable names and method names in your code
27
+ you will be doing something like this:
10
28
 
29
+ Create a new ruby file with the tranformation logic to be applied on the
30
+ AST of the source code. For writing transforms you can make use of the
31
+ [Ruby AST Explorer](https://ruby-ast-explorer.herokuapp.com/)
32
+
33
+ ### transform.rb
11
34
  ```ruby
12
- gem 'codeshift'
35
+ # Your Transform Class should always extend from
36
+ # Parser:: TreeRewriter
37
+ class Transform < Parser::TreeRewriter
38
+ def on_lvasgn(node)
39
+ # Reverse the variable names
40
+ replace(node.loc.to_hash[:name], node.children[0].to_s.reverse)
41
+ end
42
+
43
+ def on_def(node)
44
+ replace(node.loc.to_hash[:name], node.children[0].to_s.reverse)
45
+ end
46
+ end
47
+
13
48
  ```
14
49
 
15
- And then execute:
50
+ If your source code looks like below in a folder called `~/Desktop/test/ruby`
16
51
 
17
- $ bundle
52
+ ### sample.rb
53
+ ```ruby
54
+ tips = ["hello", "world"]
55
+ def print_tips
56
+ tips.each { |key, value| print "Tip #{key}: #{value}" }
57
+ end
58
+ ```
18
59
 
19
- Or install it yourself as:
60
+ Then use it against your source code
20
61
 
21
- $ gem install codeshift
62
+ ```sh
63
+ $ codeshift -t transform.rb ~/Desktop/test/ruby/**/*.rb
64
+ ```
65
+
66
+ Then your source will be transformed something like:
67
+
68
+ ### sample.rb
69
+ ```ruby
70
+ spit = ["hello", "world"]
71
+ def spit_tnirp
72
+ tips.each { |key, value| print "Tip #{key}: #{value}" }
73
+ end
74
+
75
+ ```
22
76
 
23
- ## Usage
24
77
 
25
- TODO: Write usage instructions here
26
78
 
27
79
  ## Development
28
80
 
@@ -0,0 +1,13 @@
1
+ module Codeshift
2
+ class CodeshiftOptions
3
+ attr_accessor :file_path
4
+ attr_accessor :transform
5
+
6
+ def initialize
7
+ @file_path = nil
8
+ @transform = "./transform.rb"
9
+ end
10
+
11
+
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ require 'parser/current'
2
+
3
+ module Codeshift
4
+ class CodeshiftTransformer
5
+ def initialize(code, transform)
6
+ @code = code
7
+ @transform = transform
8
+ end
9
+
10
+ def transform
11
+ eval(@transform)
12
+
13
+ buffer = Parser::Source::Buffer.new('(example)')
14
+ buffer.source = @code
15
+ temp = Parser::CurrentRuby.parse(@code)
16
+ rewriter = Transform.new
17
+
18
+ # Rewrite the AST, returns a String with the new form.
19
+ output = rewriter.rewrite(buffer, temp)
20
+
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Codeshift
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/codeshift.rb CHANGED
@@ -1,6 +1,3 @@
1
1
  require "codeshift/version"
2
-
3
- module Codeshift
4
- class Error < StandardError; end
5
- # Your code goes here...
6
- end
2
+ require "codeshift/codeshift_options"
3
+ require "codeshift/codeshift_transformer"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codeshift
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rajasegar Chandran
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-25 00:00:00.000000000 Z
11
+ date: 2019-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,6 +76,8 @@ files:
76
76
  - exe/codeshift
77
77
  - lib/codeshift.rb
78
78
  - lib/codeshift/cli.rb
79
+ - lib/codeshift/codeshift_options.rb
80
+ - lib/codeshift/codeshift_transformer.rb
79
81
  - lib/codeshift/version.rb
80
82
  homepage: https://github.com/rajasegar/codeshift
81
83
  licenses: