findr 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,16 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ findr (0.0.4)
5
+ rake
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ rake (0.9.2.2)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ findr!
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ *findr* is a ruby find and replace tool for batch editing text files.
2
+
3
+ [![Build Status](https://travis-ci.org/mstrauss/findr.svg)](https://travis-ci.org/mstrauss/findr)
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ task :default => [:test]
2
+
3
+ task :test do
4
+ exit system "bin/findr -g '*.rb' VERSION"
5
+ end
data/findr.gemspec CHANGED
@@ -6,33 +6,34 @@ require "findr/version"
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "findr"
8
8
  s.version = Findr::VERSION.dup
9
- s.date = "2012-06-01"
9
+ s.date = "2014-11-20"
10
10
  s.summary = "A ruby find and replace tool for batch editing text files."
11
11
  s.email = "Markus@ITstrauss.eu"
12
12
  s.homepage = "https://github.com/mstrauss/findr"
13
13
  s.authors = ['Markus Strauss']
14
-
14
+
15
15
  s.description = <<-EOF
16
16
  A ruby find and replace tool for batch editing text files.
17
17
  EOF
18
-
18
+
19
19
  dependencies = [
20
+ [:test, 'rake']
20
21
  # Examples:
21
22
  # [:runtime, "rack", "~> 1.1"],
22
23
  # [:development, "rspec", "~> 2.1"],
23
24
  ]
24
-
25
+
25
26
  s.files = Dir['**/*']
26
27
  s.test_files = Dir['test/**/*'] + Dir['spec/**/*']
27
28
  s.executables = Dir['bin/*'].map { |f| File.basename(f) }
28
29
  s.require_paths = ["lib"]
29
-
30
-
30
+
31
+
31
32
  ## Make sure you can build the gem on older versions of RubyGems too:
32
33
  s.rubygems_version = "1.8.11"
33
34
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
34
35
  s.specification_version = 3 if s.respond_to? :specification_version
35
-
36
+
36
37
  dependencies.each do |type, name, version|
37
38
  if s.respond_to?("add_#{type}_dependency")
38
39
  s.send("add_#{type}_dependency", name, version)
data/lib/findr/cli.rb CHANGED
@@ -3,47 +3,45 @@ require 'pathname'
3
3
  require 'tempfile'
4
4
  require 'optparse'
5
5
  require 'yaml'
6
-
7
- # this is deprecated on 1.9, but still works fine
8
- require 'iconv'
6
+ require 'pp'
9
7
 
10
8
  require 'findr/version'
11
-
9
+ require 'findr/encoder'
12
10
 
13
11
  module Findr
14
-
12
+
15
13
  class CLI
16
-
14
+
17
15
  CONFIGFILE = '.findr-config'
18
-
16
+
19
17
  def colorize(text, color_code)
20
18
  "\e[#{color_code}m#{text}\e[0m"
21
19
  end
22
-
20
+
23
21
  def red(text); colorize(text, 31); end
24
22
  def green(text); colorize(text, 32); end
25
23
  def yellow(text); colorize(text, 33); end
26
24
  def blue(text); colorize(text, 34); end
27
-
25
+
28
26
  def banner
29
27
  red( "FINDR VERSION #{Findr::VERSION}. THIS PROGRAM COMES WITH NO WARRANTY WHATSOEVER. MAKE BACKUPS!") + $/ +
30
28
  "Usage: #{Pathname($0).basename} [options] <search regex> [<replacement string>]"
31
29
  end
32
-
30
+
33
31
  def show_usage
34
32
  puts(@option_parser.help)
35
33
  exit
36
34
  end
37
-
35
+
38
36
  def execute(stdout, arguments=[])
39
37
  # unless arguments[0]
40
38
  # end
41
-
39
+
42
40
  # default options
43
41
  options = {}
44
42
  options[:glob] = '*'
45
43
  options[:coding] = 'utf-8'
46
-
44
+
47
45
  # options from file, if present
48
46
  if File.exists?( CONFIGFILE )
49
47
  file_options = YAML.load_file(CONFIGFILE)
@@ -51,7 +49,7 @@ module Findr
51
49
  options.merge!( file_options )
52
50
  stdout.puts green "Using #{CONFIGFILE}."
53
51
  end
54
-
52
+
55
53
  # parse command line
56
54
  @option_parser = OptionParser.new do |opts|
57
55
  opts.on('-g', '--glob FILE SEARCH GLOB', 'e.g. "*.{rb,erb}"') do |glob|
@@ -66,10 +64,14 @@ module Findr
66
64
  opts.on('-s', '--save', "saves your options to #{CONFIGFILE} for future use") do
67
65
  options[:save] = true
68
66
  end
67
+ opts.on('-C', '--codings-list', "list available encodings") do
68
+ pp Encoder.list
69
+ exit
70
+ end
69
71
  end
70
72
  @option_parser.banner = self.banner
71
73
  @option_parser.parse!( arguments )
72
-
74
+
73
75
  # optionally save the configuration to file
74
76
  if options[:save]
75
77
  options.delete(:save)
@@ -78,7 +80,7 @@ module Findr
78
80
  stdout.puts green "Saved options to file #{CONFIGFILE}."
79
81
  exit
80
82
  end
81
-
83
+
82
84
  show_usage if arguments.size == 0
83
85
  arguments.clone.each do |arg|
84
86
  if !options[:find]
@@ -91,21 +93,20 @@ module Findr
91
93
  show_usage
92
94
  end
93
95
  end
94
-
96
+
95
97
  show_usage if arguments.size != 0
96
98
  stdout.puts green "File inclusion glob: " + options[:glob]
97
99
  stdout.puts green "Searching for regex " + options[:find].to_s
98
-
100
+
99
101
  # some statistics
100
102
  stats = {}
101
103
  stats[:total_hits] = stats[:local_hits] = stats[:hit_files] = stats[:total_files] = 0
102
-
104
+
103
105
  replacement_done = false
104
106
  tempfile = nil
105
-
106
- coding_to_utf8 = Iconv.new('utf-8', options[:coding])
107
- utf8_to_coding = Iconv.new(options[:coding], 'utf-8')
108
-
107
+
108
+ coder = Encoder.new( options[:coding] )
109
+
109
110
  Pathname.glob("**/#{options[:glob]}").each do |current_file|
110
111
  next unless current_file.file?
111
112
  stats[:total_files] += 1
@@ -115,9 +116,9 @@ module Findr
115
116
  tempfile = Tempfile.new( 'current_file.basename' ) if options[:replace] && options[:force]
116
117
  current_file.each_line do |l|
117
118
  begin
118
- l = coding_to_utf8.iconv(l)
119
- rescue Iconv::IllegalSequence
120
- stdout.puts "Skipping file #{current_file} because of error on line #{linenumber}: #{$!.class} #{$!.message}"
119
+ l = coder.decode(l)
120
+ rescue Encoder::Error
121
+ stdout.puts "Skipping file #{current_file} because of error on line #{linenumber}: #{$!.original.class} #{$!.original.message}"
121
122
  tempfile.unlink if tempfile
122
123
  break
123
124
  end
@@ -125,7 +126,7 @@ module Findr
125
126
  if l=~ options[:find]
126
127
  stats[:local_hits] += 1
127
128
  if firstmatch
128
- stdout.puts red("#{current_file.cleanpath}:")
129
+ stdout.puts red("#{current_file.cleanpath}:")
129
130
  end
130
131
  stdout.write( yellow( "%6d:" % [linenumber, l] ) )
131
132
  stdout.puts l
@@ -133,12 +134,12 @@ module Findr
133
134
  if options[:replace]
134
135
  stdout.write( blue( "%6d:" % [linenumber, l] ) )
135
136
  l_repl = l.gsub( options[:find], options[:replace] )
136
- tempfile.puts utf8_to_coding.iconv(l_repl) if tempfile
137
+ tempfile.puts coder.encode(l_repl) if tempfile
137
138
  stdout.puts blue l_repl
138
139
  replacement_done = true
139
140
  end
140
141
  elsif tempfile
141
- tempfile.puts utf8_to_coding.iconv(l)
142
+ tempfile.puts coder.encode(l)
142
143
  end
143
144
  end
144
145
  if tempfile
@@ -146,18 +147,18 @@ module Findr
146
147
  FileUtils.cp( tempfile.path, current_file ) if stats[:local_hits] > 0
147
148
  tempfile.unlink
148
149
  end
149
-
150
+
150
151
  if stats[:local_hits] > 0
151
152
  stats[:total_hits] += stats[:local_hits]
152
153
  stats[:hit_files] += 1
153
154
  end
154
-
155
+
155
156
  end
156
-
157
+
157
158
  # some statistics
158
159
  stdout.puts green( "#{stats[:total_hits]} occurences (lines) in #{stats[:hit_files]} of #{stats[:total_files]} files found." )
159
160
  end
160
-
161
+
161
162
  end
162
163
 
163
164
  end
@@ -0,0 +1,57 @@
1
+ FIRST_RUBY_WITHOUT_ICONV = '1.9'
2
+ require 'iconv' if RUBY_VERSION < FIRST_RUBY_WITHOUT_ICONV
3
+
4
+ module Findr
5
+
6
+ # Class for wrapping original exceptions, which could be from Iconv (Ruby 1.8)
7
+ # or String (Ruby >=1.9).
8
+ # ()
9
+ class Error < ::StandardError
10
+ attr_reader :original
11
+ def initialize(msg, original=$!)
12
+ super(msg)
13
+ @original = original;
14
+ end
15
+ end
16
+
17
+ # Wrapper class for String#encode (Ruby >=1.9) and Iconv#iconv (Ruby 1.8).
18
+ class Encoder
19
+
20
+ class Error < Findr::Error; end
21
+
22
+ class <<self
23
+ def list
24
+ return Iconv.list if RUBY_VERSION < FIRST_RUBY_WITHOUT_ICONV
25
+ return Encoding.list.map(&:to_s)
26
+ end
27
+ end
28
+
29
+ def initialize( other_coding )
30
+ if RUBY_VERSION < FIRST_RUBY_WITHOUT_ICONV
31
+ @coding_to_utf8 = Iconv.new('UTF-8', other_coding)
32
+ @utf8_to_coding = Iconv.new(other_coding, 'UTF-8')
33
+ else
34
+ @other_coding = Encoding.find(other_coding)
35
+ end
36
+ end
37
+
38
+ # Encodes given +string+ from +@other_coding+ to utf8.
39
+ def decode( string )
40
+ return @coding_to_utf8.iconv(string) if RUBY_VERSION < FIRST_RUBY_WITHOUT_ICONV
41
+ string.force_encoding(@other_coding)
42
+ fail Error.new("Encoding '#{@other_coding}' is invalid.") unless string.valid_encoding?
43
+ return string.encode('UTF-8')
44
+ rescue
45
+ raise Error, "Error when decoding from '#{@other_coding}' into 'UTF-8'."
46
+ end
47
+
48
+ # Encodes given utf8 +string+ into +@other_coding+.
49
+ def encode( string )
50
+ return @utf8_to_coding.iconv(string) if RUBY_VERSION < FIRST_RUBY_WITHOUT_ICONV
51
+ return string.encode(@other_coding)
52
+ rescue
53
+ raise Error, "Error when encoding from 'UTF-8' into '#{@other_coding}'."
54
+ end
55
+ end
56
+
57
+ end
data/lib/findr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Findr
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
metadata CHANGED
@@ -1,74 +1,73 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: findr
3
- version: !ruby/object:Gem::Version
4
- hash: 25
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 3
10
- version: 0.0.3
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Markus Strauss
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
12
+ date: 2014-11-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: ! 'A ruby find and replace tool for batch editing text files.
17
31
 
18
- date: 2012-06-01 00:00:00 Z
19
- dependencies: []
20
-
21
- description: |
22
- A ruby find and replace tool for batch editing text files.
23
-
32
+ '
24
33
  email: Markus@ITstrauss.eu
25
- executables:
34
+ executables:
26
35
  - findr
27
36
  extensions: []
28
-
29
37
  extra_rdoc_files: []
30
-
31
- files:
38
+ files:
32
39
  - bin/findr
33
- - findr-0.0.1.gem
34
- - findr-0.0.2.gem
35
40
  - findr.gemspec
41
+ - Gemfile
42
+ - Gemfile.lock
36
43
  - lib/findr/cli.rb
44
+ - lib/findr/encoder.rb
37
45
  - lib/findr/version.rb
38
46
  - lib/findr.rb
47
+ - Rakefile
48
+ - README.md
39
49
  homepage: https://github.com/mstrauss/findr
40
50
  licenses: []
41
-
42
51
  post_install_message:
43
52
  rdoc_options: []
44
-
45
- require_paths:
53
+ require_paths:
46
54
  - lib
47
- required_ruby_version: !ruby/object:Gem::Requirement
55
+ required_ruby_version: !ruby/object:Gem::Requirement
48
56
  none: false
49
- requirements:
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- hash: 3
53
- segments:
54
- - 0
55
- version: "0"
56
- required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
62
  none: false
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- hash: 3
62
- segments:
63
- - 0
64
- version: "0"
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
65
67
  requirements: []
66
-
67
68
  rubyforge_project:
68
- rubygems_version: 1.8.10
69
+ rubygems_version: 1.8.23.2
69
70
  signing_key:
70
71
  specification_version: 3
71
72
  summary: A ruby find and replace tool for batch editing text files.
72
73
  test_files: []
73
-
74
- has_rdoc:
data/findr-0.0.1.gem DELETED
Binary file
data/findr-0.0.2.gem DELETED
Binary file