kasoba 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in kasoba.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,23 @@
1
+ = Kasoba
2
+
3
+ Kasoba is a tool meant to assist programmers with large-scale code refactors.
4
+
5
+ == Install
6
+ [sudo] gem install kasoba
7
+
8
+ == History
9
+
10
+ === Version 0.01 beta - Current Version
11
+
12
+ Basic functions implemented.
13
+
14
+ == Usage
15
+ Suposse you are depecrating the use of <center> tag. You'll make progress by running:
16
+ kasoba -d app_folder/ --extensions php,html '<center>(.*?)</center>' '<div style="text-align:center">\1</div>'
17
+
18
+ Last two arguments are a regular expression and a substitution string. As you probably noticed already, kasoba supports match captures and you can include them in the substitution string.
19
+ At each match you'll be shown a colored diff, and asked if you want to accept the change.
20
+
21
+ == Copyright
22
+
23
+ Copyright (c) 2010-2011 Lacides Charris. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/bin/kasoba ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
+
4
+ require 'optparse'
5
+ require 'kasoba'
6
+ options = {}
7
+ optparse = OptionParser.new do |opt|
8
+ opt.on_tail('-h', '--help', 'Show help') do
9
+ puts opt
10
+ exit
11
+ end
12
+
13
+ options[:dir] = "/"
14
+ opt.on('-d PATH', 'Path whose ancestor files are to be explored') do |path|
15
+ options[:dir] = path
16
+ end
17
+
18
+ options[:extensions] = nil
19
+ opt.on('--extensions x,y,z', Array, 'List of extensions to process') do |list|
20
+ options[:extensions] = list
21
+ end
22
+
23
+ options[:editor] = ENV['EDITOR']
24
+ opt.on('--editor EDITOR', 'Specify an editor. If omitted default to $EDITOR enviroment variable') do |editor|
25
+ options[:editor] = editor
26
+ end
27
+
28
+ options[:count] = false
29
+ opt.on('--count', 'Print out number of times places in the codebase match the query') do
30
+ options[:count] = true
31
+ end
32
+
33
+
34
+ end.parse!
35
+ begin
36
+ regex = ARGV[0]
37
+ replacement = ARGV[1]
38
+ p options[:editor]
39
+ kasoba = Kasoba.new(options,regex,replacement)
40
+
41
+ p kasoba.inspect
42
+ kasoba.run
43
+ rescue SystemExit, Interrupt
44
+ exit
45
+ end
data/kasoba.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "kasoba/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "kasoba"
7
+ s.version = Kasoba::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["kasoba"]
10
+ s.email = ["lacidescharris@hotmail.es", "guilleiguaran@gmail.com"]
11
+ s.homepage = "https://github.com/guilleiguaran/kasoba"
12
+ s.summary = %q{Interactive tool for large scale code refactors}
13
+ s.description = %q{Kasoba is a tool meant to assist programmers with large-scale code refactors.}
14
+
15
+ s.rubyforge_project = "kasoba"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,3 @@
1
+ class Kasoba
2
+ VERSION = "0.0.1"
3
+ end
data/lib/kasoba.rb ADDED
@@ -0,0 +1,116 @@
1
+ require 'rubygems'
2
+ require 'highline/import'
3
+ require 'rainbow'
4
+
5
+ class Kasoba
6
+ attr_reader :files
7
+ def run
8
+ if @count == true
9
+ say("Counting ...")
10
+ p count
11
+ else
12
+ replace
13
+ end
14
+ end
15
+
16
+ def initialize(options, regex, replacement)
17
+ @files = FileNavigator.new(options[:dir],options[:extensions])
18
+ @count = options[:count]
19
+ @editor = options[:editor]
20
+ @regex = regex
21
+ @replacement = replacement
22
+ end
23
+
24
+ def run
25
+ if @count == true
26
+ say("Counting ...")
27
+ p count
28
+ else
29
+ replace
30
+ end
31
+ end
32
+
33
+ def count
34
+ num = 0
35
+ @files.each do |file|
36
+ file.each do |line|
37
+ cline = line.clone
38
+ while ((t = cline.partition(@regex)[2]) != "") or (cline.match(@regex)) do
39
+ cline = t
40
+ num = num +1
41
+ end
42
+ end
43
+ file.close
44
+ end
45
+ return num
46
+ end
47
+
48
+ def replace
49
+ @files.each do |file|
50
+ File.open(file.path + ".temp","w") do |tmpFile|
51
+ p file.path
52
+ fileContent = file.readlines.join
53
+ while (((t = fileContent.partition(Regexp.new(@regex)))[2] != "") or t.join.length > 0)
54
+ if(t[2] != "")
55
+ tmpFile << t[0]
56
+ fileContent = t[2]
57
+ oldline = t[0].split("\n").last.to_s + t[1]
58
+ puts "Oldline: " + oldline.color(:red)
59
+ localreplacement = @replacement.nil?? ask('Type substitution string') : @replacement
60
+ i = 1
61
+ t[1].match(@regex).captures.each do |capture|
62
+ localreplacement = localreplacement.gsub("\\#{i}" ,capture )
63
+ i = i + 1
64
+ end
65
+ newline = t[0].split("\n").last.to_s + localreplacement + t[2].split("\n").first.to_s
66
+ puts "Newline: " + newline.color(:green)
67
+ if agree("Agree? y/n".color(:blue))
68
+ tmpFile << localreplacement
69
+ else
70
+ tmpFile << t[1]
71
+ end
72
+ else
73
+ fileContent = t[2]
74
+ tmpFile << t.join
75
+ end
76
+ end
77
+ end
78
+ filePath = file.path
79
+ file.close
80
+ File.delete(filePath)
81
+ File.rename(filePath + ".temp", filePath)
82
+ end
83
+ end
84
+ end
85
+
86
+ class FileNavigator
87
+ def initialize(root, extensions)
88
+ extensionsPattern = extensions.nil?? "{.*,}" : ".{" + extensions.join(',') + "}"
89
+ filePattern = File.join(root.nil?? "**" : File.join(root, "**") , "*#{extensionsPattern}")
90
+ @fileNames = Dir.glob(filePattern).select {|fileName| File.file?(fileName) }
91
+ end
92
+ def method_missing(name, *args)
93
+ @fileNames.send(name,*args)
94
+ end
95
+ def [](i)
96
+ File.new(@fileNames[i], 'r')
97
+ end
98
+ def each
99
+ return to_enum if !block_given?
100
+ @fileNames.each do |fileName|
101
+ yield(File.new(fileName,'r'))
102
+ end
103
+ end
104
+ def collect
105
+ return to_enum if !block_given?
106
+ @fileNames.collect do |fileName|
107
+ yield(File.new(fileName, 'r'))
108
+ end
109
+ end
110
+ def map
111
+ return to_enum if !block_given?
112
+ @fileNames.map do |fileName|
113
+ yield(File.new(fileName, 'r'))
114
+ end
115
+ end
116
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kasoba
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - kasoba
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-24 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Kasoba is a tool meant to assist programmers with large-scale code refactors.
23
+ email:
24
+ - lacidescharris@hotmail.es
25
+ - guilleiguaran@gmail.com
26
+ executables:
27
+ - kasoba
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - README.rdoc
36
+ - Rakefile
37
+ - bin/kasoba
38
+ - kasoba.gemspec
39
+ - lib/kasoba.rb
40
+ - lib/kasoba/version.rb
41
+ has_rdoc: true
42
+ homepage: https://github.com/guilleiguaran/kasoba
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project: kasoba
71
+ rubygems_version: 1.5.2
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Interactive tool for large scale code refactors
75
+ test_files: []
76
+