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 +4 -4
- data/README.md +63 -11
- data/lib/codeshift/codeshift_options.rb +13 -0
- data/lib/codeshift/codeshift_transformer.rb +23 -0
- data/lib/codeshift/version.rb +1 -1
- data/lib/codeshift.rb +2 -5
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b5b1f7425f32b67e65b8ce7b0b2e22061495570b95600d8c6114b8a86ebbedd
|
4
|
+
data.tar.gz: 055d30acead1d71b2af0c0e9895adcf2cc64c13e600548b713bd108831fb3b23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77e1cdb4e9a99b634acbe0feeb5f6286ae439ed401f0df13a234f628def50a03900bd4eabb644fd879451b67fa1466cd3dd8520aedeb55aa57601ec12f9bb848
|
7
|
+
data.tar.gz: 847fd2a32bb79bcefdc3df64c5d0c1914f368ebe72ccf13f51f840d347bea43aa9decbe1e048330894d424e50c8fb0dbe37c7d57578696676ec08ba9f5f5b0ff
|
data/README.md
CHANGED
@@ -1,28 +1,80 @@
|
|
1
1
|
# Codeshift
|
2
2
|
|
3
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
50
|
+
If your source code looks like below in a folder called `~/Desktop/test/ruby`
|
16
51
|
|
17
|
-
|
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
|
-
|
60
|
+
Then use it against your source code
|
20
61
|
|
21
|
-
|
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,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
|
data/lib/codeshift/version.rb
CHANGED
data/lib/codeshift.rb
CHANGED
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.
|
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-
|
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:
|