mopup 0.0.2

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.
Files changed (3) hide show
  1. data/bin/mopup +59 -0
  2. data/lib/mopup.rb +100 -0
  3. metadata +79 -0
data/bin/mopup ADDED
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'mopup'
5
+ require 'optparse'
6
+
7
+ options = {}
8
+ path = verbose_proc = nil
9
+
10
+ OptionParser.new do |opts|
11
+
12
+ opts.on('-t', '--translate [TAB_STOP]', 'Translate tabs to spaces') do |ts|
13
+ options[:translate_tabs_to_spaces] = true
14
+ options[:tab_stop] = ts.to_i if ts
15
+ end
16
+
17
+ opts.on('-v', '--verbose', 'Log progress to terminal') do
18
+ options[:verbose] = true
19
+ end
20
+
21
+ opts.on('-f', '--file FILE', 'File to parse') do |file|
22
+ path = file
23
+ end
24
+
25
+ opts.on('-d', '--directory DIR', 'Directory to parse') do |dir|
26
+ path = dir
27
+ end
28
+
29
+ opts.on('-r', '--recursive', 'Recurse through sub directories') do
30
+ options[:recurse] = true
31
+ end
32
+
33
+ opts.on('--version', 'Show the current version of mopup') do
34
+ puts Mopup::VERSION
35
+ exit(false)
36
+ end
37
+
38
+ opts.on('-h', '--help', 'Show this message') do
39
+ puts opts
40
+ exit(false)
41
+ end
42
+
43
+ end.parse!
44
+
45
+ mopup = Mopup.new(options)
46
+ path = Dir.pwd unless path
47
+
48
+ if options[:verbose]
49
+ verbose_proc = Proc.new do |file, result|
50
+ puts "#{result}: #{file}"
51
+ end
52
+ end
53
+
54
+ if File.directory? path
55
+ mopup.clean_dir(path, options[:recurse], &verbose_proc)
56
+ elsif File.file? path
57
+ mopup.clean_file(path)
58
+ end
59
+
data/lib/mopup.rb ADDED
@@ -0,0 +1,100 @@
1
+ ##
2
+ # Mops up trailing white space and optionally converts
3
+ # leading tabs to spaces
4
+ class Mopup
5
+
6
+ require 'tempfile'
7
+ require 'ptools'
8
+ require 'fileutils'
9
+
10
+ VERSION = '0.0.2'
11
+
12
+ ##
13
+ # The clean-up options
14
+ #
15
+ # * +:translate_tabs_to_spaces+ Translate leading tabs to spaces
16
+ # * +:tab_stop+ Number of spaces to which tabs should be translated
17
+ #
18
+ attr_reader :options
19
+
20
+ ##
21
+ # == Examples
22
+ #
23
+ # # To translate tabs to 2 spaces
24
+ # Mopup.new :translate_tabs_to_spaces => true, :tab_stop => 2
25
+ #
26
+ def initialize(options = nil)
27
+ @options = {
28
+ :translate_tabs_to_spaces => false,
29
+ :tab_stop => 2
30
+ }
31
+ @options.merge!(options) if options
32
+ end
33
+
34
+ ##
35
+ # Cleans the whitespace in a line of text
36
+ #
37
+ # == Examples
38
+ #
39
+ # mopup = Mopup.new
40
+ # mopup.clean 'A dirty line of text to clean ' #=> 'A dirty line of text to clean'
41
+ #
42
+ def clean(line)
43
+ if @options[:translate_tabs_to_spaces]
44
+ line.gsub!(/\t/, ' ' * @options[:tab_stop])
45
+ end
46
+ line.rstrip!
47
+ line
48
+ end
49
+
50
+ # Returns a copy of text with the whitespace in each line cleaned up
51
+ def clean_text(text)
52
+ buffer = ''
53
+ text.lines do |line|
54
+ clean(line)
55
+ yield(line) if block_given?
56
+ buffer << line
57
+ end
58
+ buffer
59
+ end
60
+
61
+ # Cleans each line in file located at +path+
62
+ def clean_file(path)
63
+ tmp = Tempfile.new('mopup')
64
+ stat = File.stat(path)
65
+ begin
66
+ File.open(path) do |f|
67
+ while line = f.gets
68
+ clean(line)
69
+ tmp.puts line
70
+ end
71
+ end
72
+ tmp.close
73
+ FileUtils.mv(tmp.path, path)
74
+ File.chmod(stat.mode, path)
75
+ File.chown(stat.uid, stat.gid, path)
76
+ ensure
77
+ tmp.close!
78
+ end
79
+ end
80
+
81
+ # Cleans each plain text file within the directory located at +path+
82
+ def clean_dir(path, recurse = false, &block)
83
+ Dir.foreach(path) do |entry|
84
+ unless entry =~ /^\./
85
+ full_path = path.chomp('/') + '/' + entry
86
+ if File.file? full_path
87
+ result = 'skipped'
88
+ unless File.binary? full_path
89
+ clean_file(full_path)
90
+ result = 'cleaned'
91
+ end
92
+ block.call(full_path, result) if block
93
+ elsif File.directory? full_path and recurse
94
+ clean_dir(full_path, recurse, &block)
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mopup
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Phil Parsons
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-08-03 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: ptools
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: White space and tab clean up and removal tool
35
+ email: phil@profilepicture.co.uk
36
+ executables:
37
+ - mopup
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - bin/mopup
44
+ - lib/mopup.rb
45
+ homepage: https://github.com/p-m-p/mopup
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.15
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Code cleanup utils
78
+ test_files: []
79
+