line_ender 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +5 -5
  2. data/lib/le +47 -61
  3. data/lib/line_ender.rb +114 -106
  4. data/lib/test_line_ender.rb +45 -24
  5. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA512:
3
- data.tar.gz: 9186cd233ab899bfb47b425f4039fda71d7d840692e20bdc355ee0bc878c0ab3b945f5905a10bfbf0b1d74974b0b8d917e1c5ee7e37955f978e6a2ac89aff24e
4
- metadata.gz: 3394b3cdaa70f8a5d7542251a514d45ff1f04338c5f58a99dffe920c9cf97d4e0856ca7dd35dc81be9b8eb541ef07f128e3d8330a1133a967bb921b7cd69d724
5
2
  SHA1:
6
- data.tar.gz: ea6ae3546c2020c688b55afcc63dcd37a043f294
7
- metadata.gz: 0ae0ea43faecf40c84ce5f6d8610733bfdd2030f
3
+ data.tar.gz: 0a8805fe53c0e3b98af8ffb911b9486b5d21033d
4
+ metadata.gz: ff506642f441d4fd8a53aff88a65bbdc806b1d4c
5
+ SHA512:
6
+ data.tar.gz: a6a6147ba4e9f07754e98f80200980c862165335a2989e10354271c2c29e3c951aac85620ad1e374ed6a5278271373c79d7e9ce130d2e0455824337969189a08
7
+ metadata.gz: 222a545605d345cb95c07e6a92417996374f7a1972c5b2e3389f27675c8d138a1bce7fcbf5a210f935aebdac0d33bd1cd149ee826a90293bc054c027bac7d987
data/lib/le CHANGED
@@ -1,78 +1,64 @@
1
- #!/usr/bin/env ruby
2
- # == Synopsis
3
- # le: This is program that replaces line endings of a file. It either does an inplace update or will write to another file with (-o) switch.
4
- # The default line ending is Unix (-u), but other line endings are Windows (-w) or Mac (-m).
5
- #
6
- # == Note
7
- # The -u, -m, and -w flags are mutually exclusive. (Can only choose one).
8
- #
9
- # == Usage
10
- # le [OPTION] ... INPUTFILE
11
- #
12
- # -h, --help: show help
13
- #
14
- # -w, --windows: gives the file line endings of carriage return + line feed (\r\n)
15
- #
16
- # -u, --unix: gives the file line endings of line feed (\n) (default)
17
- #
18
- # -m, --mac: gives the file line endings of carriage return (\r)
19
- #
20
- # -o, --out: will write the output to a file with this name
21
- #
22
- # INPUTFILE: The file you want to modify the line endings for.
23
-
1
+ #! /usr/bin/env ruby
24
2
  require 'rubygems'
25
- require 'getoptlong'
26
- require 'rdoc/usage'
3
+ require 'trollop'
27
4
  require 'line_ender'
28
5
 
29
6
  include LineEnder
30
7
 
31
- opts = GetoptLong.new(
32
- [ '--windows', '-w', GetoptLong::OPTIONAL_ARGUMENT ],
33
- [ '--mac', '-m', GetoptLong::OPTIONAL_ARGUMENT ],
34
- [ '--unix', '-u', GetoptLong::OPTIONAL_ARGUMENT ],
35
- [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
36
- [ '--out', '-o', GetoptLong::OPTIONAL_ARGUMENT ]
37
- )
38
-
39
- outfile = nil
40
- infile = nil
41
- ending = nil
42
- opts.each do |opt, arg|
43
- case opt
44
- when '--windows'
45
- RDoc::usage if ending
46
- ending = LineEnder::Ending::Windows
47
- when '--unix'
48
- RDoc::usage if ending
49
- ending = LineEnder::Ending::Unix
50
- when '--mac'
51
- RDoc::usage if ending
52
- ending = LineEnder::Ending::Mac
53
- when '--help'
54
- RDoc::usage
55
- when '--out'
56
- outfile = arg
57
- end
8
+ opts = Trollop::options do
9
+ banner <<-EOS
10
+ Synopsis: le is a line ender script to be used from the command prompt if you put it in your path
11
+ Usage: le [flags] [-o outfile] inputfile
12
+ Note: don't have to set --no-unix flag if looking for different endings ... will override.
13
+ Flags:
14
+ EOS
15
+ opt :windows, "Windows ending", :default => false
16
+ opt :unix, "Unix ending", :default => true
17
+ opt :mac, "Mac ending", :default => false
18
+ opt :debug, "Debug mode", :default => false
19
+ opt :output, "Output to another file", :type => :string
58
20
  end
59
21
 
60
- ending = LineEnder::Ending::Unix unless ending
22
+ p opts if opts[:debug]
23
+
24
+ outfile = opts[:output]
25
+ ending = nil
26
+ ending = opts[:windows] ? LineEnder::Ending::Windows : ending
27
+ ending = opts[:mac] ? LineEnder::Ending::Mac : ending
28
+ ending = opts[:unix] ? LineEnder::Ending::Unix : ending if ending.nil?
61
29
 
62
30
  if ARGV.length != 1
63
- puts "***---Missing INPUTFILE argument---***"
64
- puts
65
- RDoc::usage
31
+ puts "Missing or too many INPUTFILE argument(s)."
32
+ puts "example: le -m -o fixed.txt somefile.txt"
33
+ puts "Try the -h flag for help on flags"
66
34
  exit 0
67
35
  end
68
36
 
69
- infile = ARGV.shift
37
+ infile = ARGV[0]
70
38
 
71
- if outfile
72
- output_to_file( infile, ending, outfile )
73
- else
74
- output_to_file( infile, ending )
39
+ if opts[:debug]
40
+ puts "outfile: #{outfile}, #{outfile.inspect}"
41
+ puts "infile: #{infile}, #{infile.inspect}"
42
+ puts "ending: #{ending.inspect}"
75
43
  end
76
44
 
45
+ ending_string = ""
46
+ case ending
47
+ when LineEnder::Ending::Windows
48
+ ending_string = "windows"
49
+ when LineEnder::Ending::Unix
50
+ ending_string = "unix"
51
+ when LineEnder::Ending::Mac
52
+ ending_string = "mac"
53
+ else
54
+ ending_string = "unix"
55
+ end
77
56
 
57
+ if outfile
58
+ output_to_file( infile, ending, outfile )
59
+ puts "Succesfully created #{outfile}, with #{ending_string}-style line endings!"
60
+ else
61
+ output_to_file( infile, ending )
62
+ puts "Successfully updated #{infile}, with #{ending_string}-style line endings!"
63
+ end
78
64
 
data/lib/line_ender.rb CHANGED
@@ -3,112 +3,120 @@ require 'rubygems'
3
3
 
4
4
  module LineEnder
5
5
 
6
- module Ending
7
- Mac = 1
8
- Windows = 2
9
- Unix = 3
10
-
11
- def self.valid?(value)
12
- return true if value == Ending::Mac || value == Ending::Windows || value == Ending::Unix
13
- return false
14
- end
15
- end
16
-
17
- def initialize
18
- @debug = false
19
- end
20
-
21
- def self.included(cls)
22
- include Ending
23
- end
24
-
25
- def output_to_file(input_filepath, ending, output_filename='')
26
- raise RuntimeError, "Something is wrong with the input input filepath!" unless valid_filepath?(input_filepath)
27
- raise RuntimeError, "Something is wrong with the 'Ending' parameter passed in!" unless valid_ending?(ending)
28
- output_filepath = get_output_filepath(input_filepath, output_filename)
29
- puts "output filepath = #{output_filepath}" if @debug
30
-
31
- #this will only happen for an inplace file update
32
- if (output_filepath == input_filepath)
33
- raise RuntimeError, "File is not writeable!" unless File.writable?(output_filepath)
34
- end
35
-
36
- file_as_string = file_to_string(input_filepath)
37
- fixed_file_string = change_line_endings(file_as_string, ending)
38
- string_to_file(fixed_file_string, output_filepath)
39
- return true
40
- end
41
-
42
- def output_to_string(input_filepath, ending)
43
- raise RuntimeError, "Something is wrong with the input input filepath!" unless valid_filepath?(input_filepath)
44
- raise RuntimeError, "Something is wrong with the 'Ending' parameter passed in!" unless valid_ending?(ending)
45
-
46
- file_as_string = file_to_string(input_filepath)
47
- change_line_endings(file_as_string, ending)
48
- end
49
-
50
- protected
51
-
52
- def get_output_filepath(input_filepath, output_filename)
53
- input_path = File.expand_path(input_filepath).split(::File::Separator)
54
- input_filename = input_path.last
55
- puts "in get_output_filepath ... input_path = #{input_path}" if @debug
56
- puts "in get_output_filepath ... input_filename = #{input_filename}" if @debug
57
- output_filepath = ''
58
- if output_filename.empty?
59
- output_filepath = input_filepath
60
- else
61
- output_path = output_filename.split(::File::Separator)
62
- output_filename = output_path.last
63
- if output_path.first == output_filename
64
- if input_filename == output_filename
65
- output_filepath = input_filepath
66
- else
67
- input_path.pop
68
- output_filepath = File.join(input_path,output_filename)
69
- puts "in get_output_filepath ... output_filepath = #{output_filepath}" if @debug
70
- end
71
- else
72
- #the output_filename is a path ... lets expand it
73
- output_path = File.expand_path(output_filename).split(::File::Separator)
74
- output_filepath = File.join(output_path)
75
- end
76
- end
77
- output_filepath
78
- end
79
-
80
- def string_to_file(fixed_file_string, output_filepath)
81
- File.delete(output_filepath) if File.exists?(output_filepath)
82
- File.open(output_filepath, "wb"){|out_file| out_file.write(fixed_file_string)}
83
- end
84
-
85
- def change_line_endings(file_as_string, ending)
86
- fixed_file_string = ''
87
- fixed_file_string = file_as_string.gsub(/(\n)|(\r\n?)/, "\n") if ending == Ending::Unix
88
- fixed_file_string = file_as_string.gsub(/(\n)|(\r\n?)/, "\r") if ending == Ending::Mac
89
- fixed_file_string = file_as_string.gsub(/(\n)|(\r\n?)/, "\r\n") if ending == Ending::Windows
90
- fixed_file_string
91
- end
92
-
93
- def file_to_string(filepath)
94
- file = File.open(filepath, "rb")
95
- file_string = file.read
96
- file.close
97
- file_string
98
- end
99
-
100
- def valid_ending?(ending)
101
- raise RuntimeError, "Ending parameter is required!" unless ending
102
- raise RuntimeError, "Ending parameter must be of type Ending" unless Ending.valid?(ending)
103
- return true
104
- end
105
-
106
- def valid_filepath?(filepath)
107
- raise RuntimeError, "Filepath parameter is required!" unless filepath
108
- raise RuntimeError, "File with that filepath is not found!" unless File.exists?(filepath)
109
- raise RuntimeError, "File is not readable!" unless File.readable?(filepath)
110
- return true
111
- end
6
+ module Ending
7
+ Mac = "\r"
8
+ Windows = "\r\n"
9
+ Unix = "\n"
10
+
11
+ def self.valid?(value)
12
+ value == Ending::Mac || value == Ending::Windows || value == Ending::Unix
13
+ end
14
+ end
15
+
16
+ def initialize
17
+ end
18
+
19
+ def self.included(cls)
20
+ include Ending
21
+ end
22
+
23
+ def output_to_file(input_filepath, ending, output_filename='')
24
+ raise RuntimeError, "Something is wrong with the input input filepath!" unless valid_filepath?(input_filepath)
25
+ raise RuntimeError, "Something is wrong with the 'Ending' parameter passed in!" unless valid_ending?(ending)
26
+ output_filepath = get_output_filepath(input_filepath, output_filename)
27
+ debug_print "output filepath = #{output_filepath}"
28
+
29
+ #this will only happen for an inplace file update
30
+ if (output_filepath == input_filepath)
31
+ raise RuntimeError, "File is not writeable!" unless File.writable?(output_filepath)
32
+ end
33
+
34
+ file_as_string = file_to_string(input_filepath)
35
+ fixed_file_string = change_line_endings(file_as_string, ending)
36
+ string_to_file(fixed_file_string, output_filepath)
37
+ return true
38
+ end
39
+
40
+ def output_to_string(input_filepath, ending)
41
+ raise RuntimeError, "Something is wrong with the input filepath!" unless valid_filepath?(input_filepath)
42
+ raise RuntimeError, "Something is wrong with the 'Ending' parameter passed in!" unless valid_ending?(ending)
43
+
44
+ file_as_string = file_to_string(input_filepath)
45
+ change_line_endings(file_as_string, ending)
46
+ end
47
+
48
+ protected
49
+
50
+ def debug_print(message)
51
+ puts message if $DEBUG
52
+ end
53
+
54
+ def get_output_filepath(input_filepath, output_filename)
55
+ input_path = File.expand_path(input_filepath).split(::File::Separator)
56
+ input_filename = input_path.last
57
+ debug_print "in get_output_filepath ... input_path = #{input_path}"
58
+ debug_print "in get_output_filepath ... input_filename = #{input_filename}"
59
+ output_filepath = ''
60
+ #do we have a value in output_file?
61
+ #if we don't ... assume writing to same file
62
+ #if we do ...
63
+ #is output_filename a path?
64
+ #if it is ... return that
65
+ #if it is not ... take input_path and join this filename
66
+ if output_filename.empty?
67
+ output_filepath = input_filepath
68
+ else
69
+ output_path = output_filename.split(::File::Separator)
70
+ output_filename = output_path.last
71
+ if output_path.first == output_filename
72
+ if input_filename == output_filename
73
+ output_filepath = input_filepath
74
+ else
75
+ input_path.pop
76
+ output_filepath = File.join(input_path,output_filename)
77
+ debug_print "in get_output_filepath ... output_filepath = #{output_filepath}"
78
+ end
79
+ else
80
+ #the output_filename is a path ... lets expand it
81
+ output_path = File.expand_path(output_filename).split(::File::Separator)
82
+ output_filepath = File.join(output_path)
83
+ end
84
+ end
85
+ output_filepath
86
+ end
87
+
88
+ def string_to_file(fixed_file_string, output_filepath)
89
+ File.delete(output_filepath) if File.exists?(output_filepath)
90
+ File.open(output_filepath, "wb"){|out_file| out_file.write(fixed_file_string)}
91
+ end
92
+
93
+ def change_line_endings(file_as_string, ending)
94
+ fixed_file_string = ''
95
+ fixed_file_string = file_as_string.gsub(/(\n)|(\r\n?)/, "\n") if ending == Ending::Unix
96
+ fixed_file_string = file_as_string.gsub(/(\n)|(\r\n?)/, "\r") if ending == Ending::Mac
97
+ fixed_file_string = file_as_string.gsub(/(\n)|(\r\n?)/, "\r\n") if ending == Ending::Windows
98
+ fixed_file_string
99
+ end
100
+
101
+ def file_to_string(filepath)
102
+ file = File.open(filepath, "rb")
103
+ file_string = file.read
104
+ file.close
105
+ file_string
106
+ end
107
+
108
+ def valid_ending?(ending)
109
+ raise RuntimeError, "Ending parameter is required!" unless ending
110
+ raise RuntimeError, "Ending parameter must be of type Ending" unless Ending.valid?(ending)
111
+ return true
112
+ end
113
+
114
+ def valid_filepath?(filepath)
115
+ raise RuntimeError, "Filepath parameter is required!" unless filepath
116
+ raise RuntimeError, "File with that filepath is not found!" unless File.exists?(filepath)
117
+ raise RuntimeError, "File is not readable!" unless File.readable?(filepath)
118
+ return true
119
+ end
112
120
 
113
121
 
114
122
  end
@@ -11,7 +11,6 @@ class TestLineEnder < Test:: Unit::TestCase
11
11
 
12
12
  def setup
13
13
  puts 'setup'
14
- @debug = true
15
14
  @tle = TLE.new
16
15
  @file1 = "file1.txt"
17
16
  @file2 = "file2.txt"
@@ -25,44 +24,66 @@ class TestLineEnder < Test:: Unit::TestCase
25
24
  end
26
25
 
27
26
  def test_mac_to_unix_written_to_new_file
28
- puts "in test: #{__method__} ..."
29
- start_value = "a\rb\rc\r"
30
- fixed_value = "a\nb\nc\n"
31
- fs = helper_for_file_test(start_value, fixed_value , LineEnder::Ending::Unix, true)
27
+ puts "---> in test: #{__method__} ..."
28
+ start_value = ["a", "b", "c"].join("\r")
29
+ expected_value = ["a", "b", "c"].join("\n")
30
+
31
+ helper_print_start_values(start_value, expected_value)
32
+ output_string = helper_for_file_test(start_value, expected_value , LineEnder::Ending::Unix, true)
33
+ helper_print_output_value(output_string)
34
+
32
35
  assert(File.exists?(@file2), "#{__method__} ... has not written to new file")
33
- assert(fs == fixed_value, "#{__method__} ... fixed file is not a match to expected value") if File.exists?(@file2)
36
+ assert(output_string == expected_value, "#{__method__} ... output string is not a match to expected value") if File.exists?(@file2)
34
37
  end
35
38
 
36
39
  def test_mac_to_windows_write_to_same_file
37
- puts "in test: #{__method__} ..."
38
- start_value = "a\rb\rc\r\r\r"
39
- fixed_value = "a\r\nb\r\nc\r\n\r\n\r\n"
40
- fs = helper_for_file_test(start_value, fixed_value , LineEnder::Ending::Windows)
41
- assert(fs == fixed_value, "#{__method__} ... fixed file is not a match to expected value")
40
+ puts "---> in test: #{__method__} ..."
41
+ start_value = ["a", "b", "c"].join("\r")
42
+ expected_value = ["a", "b", "c"].join("\r\n")
43
+
44
+ helper_print_start_values(start_value, expected_value)
45
+ output_string = helper_for_file_test(start_value, expected_value , LineEnder::Ending::Windows)
46
+ helper_print_output_value(output_string)
47
+
48
+ assert(output_string == expected_value, "#{__method__} ... output string is not a match to expected value")
42
49
  end
43
50
 
44
51
  def test_mac_to_windows_string_dump
45
- puts "in test: #{__method__} ..."
46
- start_value = "a\rb\rc\r\r"
47
- fixed_value = "a\r\nb\r\nc\r\n\r\n"
52
+ puts "---> in test: #{__method__} ..."
53
+ start_value = ["a", "b", "c"].join("\r")
54
+ expected_value = ["a", "b", "c"].join("\r\n")
55
+
56
+ helper_print_start_values(start_value, expected_value)
48
57
  helper_write_file(@file1, start_value)
49
- helper_for_hexdump("start", @file1) if @debug
50
- fs= @tle.output_to_string(@file1, LineEnder::Ending::Windows)
51
- assert(fs == fixed_value, "#{__method__} ... fixed file is not a match to expected value")
58
+ helper_for_hexdump("start", @file1)
59
+ output_string= @tle.output_to_string(@file1, LineEnder::Ending::Windows)
60
+ helper_print_output_value(output_string)
61
+
62
+ assert(output_string == expected_value, "#{__method__} ... output string is not a match to expected value")
63
+
64
+ end
65
+
66
+ def helper_print_start_values(start_value, expected_value)
67
+ puts("start value ... getting pushed into file1.txt: #{start_value.inspect}")
68
+ puts("expected value: #{expected_value.inspect}")
69
+ end
70
+
71
+ def helper_print_output_value(output_value)
72
+ puts("output value (as string): #{output_value.inspect}")
52
73
  end
53
74
 
54
- def helper_for_file_test(start_value, fixed_value, ending_to_use, new_output_file = false)
75
+ def helper_for_file_test(start_value, expected_value, ending_to_use, new_output_file = false)
55
76
  helper_write_file(@file1, start_value)
56
- helper_for_hexdump("start", @file1) if @debug
77
+ helper_for_hexdump("start", @file1)
57
78
  if new_output_file
58
79
  @tle.output_to_file( @file1, ending_to_use, @file2 )
59
- fixed_file = @file2
80
+ output_file = @file2
60
81
  else
61
82
  @tle.output_to_file( @file1, ending_to_use)
62
- fixed_file = @file1
83
+ output_file = @file1
63
84
  end
64
- helper_for_hexdump("fixed", fixed_file) if File.exists?(fixed_file) && @debug
65
- helper_read_file(fixed_file)
85
+ helper_for_hexdump("output", output_file) if File.exists?(output_file)
86
+ helper_read_file(output_file) if File.exists?(output_file)
66
87
  end
67
88
 
68
89
  def helper_write_file(file_name, value)
@@ -78,7 +99,7 @@ class TestLineEnder < Test:: Unit::TestCase
78
99
  file = File.open(file_name, "rb")
79
100
  file.binmode
80
101
  file_string = file.read
81
- puts "contents of file #{file_name} as a string ... #{file_string.inspect}" if @debug
102
+ #puts "contents of file #{file_name} as a string ... #{file_string.inspect}"
82
103
  end
83
104
  file_string
84
105
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: line_ender
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rich Krueger
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2013-08-28 00:00:00 Z
12
+ date: 2014-06-28 00:00:00 Z
13
13
  dependencies: []
14
14
 
15
15
  description: A little mixin to change the line endings of files. Supports Mac, Unix, and Windows line ending conversions.
@@ -44,8 +44,8 @@ required_ruby_version: !ruby/object:Gem::Requirement
44
44
  required_rubygems_version: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - *id001
47
- requirements: []
48
-
47
+ requirements:
48
+ - trollop, v2.0
49
49
  rubyforge_project:
50
50
  rubygems_version: 2.0.7
51
51
  signing_key: