recode 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 921e3c461e66aa6a0c098c01ee004dcd509ba0ff2d3e856af43bc81270f3ca63
4
+ data.tar.gz: f7dcbfec8d7ef26660a8d7f4fb83a91e4f56184f8884131a9bca577b12440799
5
+ SHA512:
6
+ metadata.gz: 2d62e6784ced7afb96d01d4947691e52fb286adf56e927821c5ff9e37a69f23756f54b86e9eba06ea1f26834651931751ec69c1176648d762f8267a66d9f2547
7
+ data.tar.gz: 37c79f46d9d376fd5036cc583fa6c4783d7385001d5390e4f7b6ba0182268c1ac332ec4fb1ae4eb7298ec15523ff2fed3e259ef56aacde1932c1860ccc4a15fe
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # Recode - Command line refactoring utility
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/recode.svg)](https://badge.fury.io/rb/recode)
4
+ [![Build Status](https://github.com/DannyBen/recode/workflows/Test/badge.svg)](https://github.com/DannyBen/recode/actions?query=workflow%3ATest)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/.../maintainability)](https://codeclimate.com/github/DannyBen/recode/maintainability)
6
+
7
+ ---
8
+
9
+ ## Installation
10
+
11
+ $ gem install recode
12
+
13
+
14
+
15
+ ## Usage
16
+
17
+ TODO
data/bin/recode ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ require 'recode/cli'
3
+ include Colsole
4
+
5
+ router = Recode::CLI.router
6
+
7
+ begin
8
+ exit router.run ARGV
9
+ rescue Interrupt, Recode::Abort
10
+ say "\nGoodbye"
11
+ exit 1
12
+ rescue => e
13
+ puts e.backtrace.reverse if ENV['DEBUG']
14
+ say! "!txtred!#{e.class}: #{e.message}"
15
+ exit 1
16
+ end
@@ -0,0 +1,14 @@
1
+ module Recode
2
+ module ChangeVariations
3
+ using Strings::Inflection::Extensions
4
+
5
+ def change_variations(old_string, new_string)
6
+ result = []
7
+ result << [old_string.pluralize.downcase, new_string.pluralize.downcase]
8
+ result << [old_string.pluralize, new_string.pluralize]
9
+ result << [old_string.downcase, new_string.downcase]
10
+ result << [old_string, new_string]
11
+ result.uniq
12
+ end
13
+ end
14
+ end
data/lib/recode/cli.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'mister_bin'
2
+ require 'recode'
3
+ require 'recode/command'
4
+
5
+ module Recode
6
+ class CLI
7
+ def self.router
8
+ router = MisterBin::Runner.new version: VERSION
9
+ router.route_all to: Command
10
+ router
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,59 @@
1
+ module Recode
2
+ class Command < MisterBin::Command
3
+ help "Code Refactoring Utility"
4
+
5
+ usage "recode OLD [to] NEW [in] EXTENSIONS... [options]"
6
+ usage "recode (-h|--help)"
7
+
8
+ option "-a --apply", "Apply changes"
9
+ option "-p --prompt", "Apply changes interactively "
10
+
11
+ param "OLD", "Old string, singular and capitalized"
12
+ param "NEW", "New string, singular and capitalized"
13
+ param "EXTENSIONS", "Space delimited file extensions"
14
+
15
+ example "recode refactor Member User rb"
16
+ example "recode refactor Member to User in rb js"
17
+ example "recode refactor Member to User in rb js --apply"
18
+ example "recode refactor Member to User in rb js -p"
19
+
20
+ def run
21
+ runner = Runner.new path: path,
22
+ extensions: extensions,
23
+ old_string: old_string,
24
+ new_string: new_string
25
+
26
+ runner.execute handler
27
+ end
28
+
29
+ private
30
+
31
+ def handler
32
+ if args['--prompt']
33
+ Handler::Prompt.new
34
+ elsif args['--apply']
35
+ Handler::Apply.new
36
+ else
37
+ Handler::Output.new
38
+ end
39
+ end
40
+
41
+ def path
42
+ "."
43
+ end
44
+
45
+ def extensions
46
+ args['EXTENSIONS']
47
+ end
48
+
49
+ def old_string
50
+ args['OLD']
51
+ end
52
+
53
+ def new_string
54
+ args['NEW']
55
+ end
56
+
57
+
58
+ end
59
+ end
@@ -0,0 +1,19 @@
1
+ module Recode
2
+ module Diffing
3
+ def diff(source, target)
4
+ diffy.new "#{source}\n", "#{target}\n", context: 1
5
+ end
6
+
7
+ private
8
+
9
+ def diffy
10
+ @diffy ||= diffy!
11
+ end
12
+
13
+ def diffy!
14
+ Diffy::Diff.default_format = :color
15
+ Diffy::Diff
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,3 @@
1
+ module Recode
2
+ Abort = Class.new Interrupt
3
+ end
@@ -0,0 +1,15 @@
1
+ module Recode
2
+ module Handler
3
+ class Apply < Output
4
+ def rename(source:, target:)
5
+ super
6
+ rename_file source, target
7
+ end
8
+
9
+ def edit(file:, before:, after:)
10
+ super
11
+ save_file file, after
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ module Recode
2
+ module Handler
3
+ class Base
4
+ include Diffing
5
+ include Colsole
6
+
7
+ def show_rename(source:, target:)
8
+ say "\n!txtpur!" + ("_" * terminal_width)
9
+ say "!txtpur!rename:"
10
+ puts diff source, target
11
+ end
12
+
13
+ def show_edit(file:, before:, after:)
14
+ say "\n!txtblu!" + ("_" * terminal_width)
15
+ say "!txtblu!edit: #{file}"
16
+ puts diff before, after
17
+ end
18
+
19
+ def rename_file(source, target)
20
+ FileUtils.mkdir_p File.dirname target
21
+ FileUtils.mv source, target
22
+ end
23
+
24
+ def save_file(file, content)
25
+ File.write file, content
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ module Recode
2
+ module Handler
3
+ class Output < Base
4
+ def rename(source:, target:)
5
+ show_rename source: source, target: target
6
+ end
7
+
8
+ def edit(file:, before:, after:)
9
+ show_edit file: file, before: before, after: after
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,36 @@
1
+ require 'io/console'
2
+
3
+ module Recode
4
+ module Handler
5
+ class Prompt < Output
6
+ def rename(source:, target:)
7
+ super
8
+ rename_file source, target if apply?
9
+ end
10
+
11
+ def edit(file:, before:, after:)
12
+ super
13
+ save_file file, after if apply?
14
+ end
15
+
16
+ private
17
+
18
+ def apply?
19
+ say "\nApply? !undgrn!Y!txtrst!es, !undgrn!n!txtrst!o, !undgrn!q!txtrst!uit : "
20
+ answer = $stdin.getch.downcase
21
+
22
+ if answer == 'q' or answer == "\u0003"
23
+ say "!txtblu!Quit"
24
+ raise Recode::Abort
25
+ elsif answer == 'n'
26
+ say "!txtred!No"
27
+ false
28
+ else
29
+ say "!txtgrn!Yes"
30
+ true
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,62 @@
1
+ module Recode
2
+ class Runner
3
+ include Colsole
4
+ include ChangeVariations
5
+
6
+ attr_accessor :path, :extensions, :old_string, :new_string
7
+ attr_accessor :handler
8
+
9
+ def initialize(path: nil, extensions: nil, old_string: nil, new_string: nil)
10
+ @path, @extensions = path, extensions
11
+ @old_string, @new_string = old_string, new_string
12
+ end
13
+
14
+ def execute(handler)
15
+ @handler = handler
16
+ refactor_filenames
17
+ refactor_contents
18
+ end
19
+
20
+ private
21
+
22
+ def refactor_filenames
23
+ files.each { |file| refactor_filename file }
24
+ refresh_files
25
+ end
26
+
27
+ def refactor_contents
28
+ files.each { |file| refactor_content file }
29
+ end
30
+
31
+ def refactor_filename(file)
32
+ target = apply_changes file
33
+ payload = { source: file, target: target }
34
+ handler.rename **payload unless target == file
35
+ end
36
+
37
+ def refactor_content(file)
38
+ before = File.read file
39
+ after = apply_changes before
40
+ payload = { file: file, before: before, after: after }
41
+ handler.edit **payload unless before == after
42
+ end
43
+
44
+ def apply_changes(string)
45
+ result = string.dup
46
+ changes.each { |from, to| result.gsub!(from, to) }
47
+ result
48
+ end
49
+
50
+ def changes
51
+ @changes ||= change_variations old_string, new_string
52
+ end
53
+
54
+ def files
55
+ @files ||= Dir["#{path}/**/*.{#{extensions.join ','}}"].sort
56
+ end
57
+
58
+ def refresh_files
59
+ @files = nil
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module Recode
2
+ VERSION = "0.0.1"
3
+ end
data/lib/recode.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'diffy'
2
+ require 'colsole'
3
+ require 'strings-inflection'
4
+ require 'strings/inflection/extensions'
5
+
6
+ require 'recode/exceptions'
7
+ require 'recode/diffing'
8
+ require 'recode/handler/base'
9
+ require 'recode/handler/output'
10
+ require 'recode/handler/apply'
11
+ require 'recode/handler/prompt'
12
+ require 'recode/change_variations'
13
+ require 'recode/runner'
14
+
15
+ require 'byebug' if ENV['BYEBUG']
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: recode
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Danny Ben Shitrit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mister_bin
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: colsole
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: diffy
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: strings-inflection
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.1'
69
+ description: Command line utility for refactoring code
70
+ email: db@dannyben.com
71
+ executables:
72
+ - recode
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - README.md
77
+ - bin/recode
78
+ - lib/recode.rb
79
+ - lib/recode/change_variations.rb
80
+ - lib/recode/cli.rb
81
+ - lib/recode/command.rb
82
+ - lib/recode/diffing.rb
83
+ - lib/recode/exceptions.rb
84
+ - lib/recode/handler/apply.rb
85
+ - lib/recode/handler/base.rb
86
+ - lib/recode/handler/output.rb
87
+ - lib/recode/handler/prompt.rb
88
+ - lib/recode/runner.rb
89
+ - lib/recode/version.rb
90
+ homepage: https://github.com/dannyben/recode
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 2.4.0
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubygems_version: 3.1.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Command line utility for refactoring code
113
+ test_files: []