line_ender 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA512:
3
- data.tar.gz: bc0949d7421ee2d59e9b66c7c8e283add063a350aa984007bd448df84e0ab7bfe469f8570327f9c98b2b88c5ef28708667f259bfb08874b1bfce3508c15fdddc
4
- metadata.gz: 4eb928bd0f1aa81e6421b4979fbf5295ed035e9f12abb848f9dda8d06661d8937f2ec04d42f32d6b2b855ff294c9ff54006dfc3887493af23fb470da6e511f72
3
+ data.tar.gz: 9186cd233ab899bfb47b425f4039fda71d7d840692e20bdc355ee0bc878c0ab3b945f5905a10bfbf0b1d74974b0b8d917e1c5ee7e37955f978e6a2ac89aff24e
4
+ metadata.gz: 3394b3cdaa70f8a5d7542251a514d45ff1f04338c5f58a99dffe920c9cf97d4e0856ca7dd35dc81be9b8eb541ef07f128e3d8330a1133a967bb921b7cd69d724
5
5
  SHA1:
6
- data.tar.gz: f87a113ebfe033f1cdd1c307d51e9823ed3e48ad
7
- metadata.gz: e8606ceac15be785b5aeec4dab2aded01e557bb1
6
+ data.tar.gz: ea6ae3546c2020c688b55afcc63dcd37a043f294
7
+ metadata.gz: 0ae0ea43faecf40c84ce5f6d8610733bfdd2030f
data/lib/le ADDED
@@ -0,0 +1,78 @@
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
+
24
+ require 'rubygems'
25
+ require 'getoptlong'
26
+ require 'rdoc/usage'
27
+ require 'line_ender'
28
+
29
+ include LineEnder
30
+
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
58
+ end
59
+
60
+ ending = LineEnder::Ending::Unix unless ending
61
+
62
+ if ARGV.length != 1
63
+ puts "***---Missing INPUTFILE argument---***"
64
+ puts
65
+ RDoc::usage
66
+ exit 0
67
+ end
68
+
69
+ infile = ARGV.shift
70
+
71
+ if outfile
72
+ output_to_file( infile, ending, outfile )
73
+ else
74
+ output_to_file( infile, ending )
75
+ end
76
+
77
+
78
+
data/lib/line_ender.rb CHANGED
@@ -27,6 +27,8 @@ module LineEnder
27
27
  raise RuntimeError, "Something is wrong with the 'Ending' parameter passed in!" unless valid_ending?(ending)
28
28
  output_filepath = get_output_filepath(input_filepath, output_filename)
29
29
  puts "output filepath = #{output_filepath}" if @debug
30
+
31
+ #this will only happen for an inplace file update
30
32
  if (output_filepath == input_filepath)
31
33
  raise RuntimeError, "File is not writeable!" unless File.writable?(output_filepath)
32
34
  end
@@ -67,7 +69,7 @@ module LineEnder
67
69
  puts "in get_output_filepath ... output_filepath = #{output_filepath}" if @debug
68
70
  end
69
71
  else
70
- #the path provided in output_filename is a path ... lets expand it
72
+ #the output_filename is a path ... lets expand it
71
73
  output_path = File.expand_path(output_filename).split(::File::Separator)
72
74
  output_filepath = File.join(output_path)
73
75
  end
@@ -90,9 +92,9 @@ module LineEnder
90
92
 
91
93
  def file_to_string(filepath)
92
94
  file = File.open(filepath, "rb")
93
- file_to_string = file.read
95
+ file_string = file.read
94
96
  file.close
95
- file_to_string.chomp
97
+ file_string
96
98
  end
97
99
 
98
100
  def valid_ending?(ending)
@@ -11,14 +11,10 @@ class TestLineEnder < Test:: Unit::TestCase
11
11
 
12
12
  def setup
13
13
  puts 'setup'
14
- @debug = false
14
+ @debug = true
15
15
  @tle = TLE.new
16
16
  @file1 = "file1.txt"
17
17
  @file2 = "file2.txt"
18
- @le_mac = "a\\\\rb\\\\rc"
19
- @le_windows = "a\r\nb\r\nc"
20
- @le_unix = "a\nb\nc"
21
- system "echo #{@le_mac} > #{@file1}"
22
18
  end
23
19
 
24
20
  def teardown
@@ -28,36 +24,69 @@ class TestLineEnder < Test:: Unit::TestCase
28
24
  system "rm #{@file2}" if File.exists?(@file2)
29
25
  end
30
26
 
31
- def test_new_file
32
- puts "from test_new_file ..."
33
- @tle.output_to_file( @file1, LineEnder::Ending::Unix, @file2 )
34
- assert(File.exists?(@file2))
35
- system "hexdump -C #{@file2} | head -n 40" if @debug
36
- puts "" if @debug
37
- puts "compare le_unix to #{@le_unix}" if @debug
38
- fl = File.open(@file2, "rb")
39
- fs = fl.read
40
- puts "to file_to_string ... #{fs}" if @debug
41
- assert(fs == @le_unix)
42
- end
43
-
44
- def test_inplace_fix
45
- puts "from test_inplace_fix ..."
46
- @tle.output_to_file( @file1, LineEnder::Ending::Windows)
47
- system "hexdump -C #{@file1} | head -n 40" if @debug
48
- puts "" if @debug
49
- puts "compare le_windows to #{@le_windows}" if @debug
50
- fl = File.open(@file1, "rb")
51
- fs = fl.read
52
- puts "to file_to_string ... #{fs}" if @debug
53
- assert(fs == @le_windows)
54
- end
55
-
56
-
57
- def test_string_convert_to_windows
58
- puts "from test_string_convert_to_windows ..."
27
+ 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)
32
+ 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)
34
+ end
35
+
36
+ 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")
42
+ end
43
+
44
+ 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"
48
+ helper_write_file(@file1, start_value)
49
+ helper_for_hexdump("start", @file1) if @debug
59
50
  fs= @tle.output_to_string(@file1, LineEnder::Ending::Windows)
60
- assert(fs == @le_windows)
51
+ assert(fs == fixed_value, "#{__method__} ... fixed file is not a match to expected value")
61
52
  end
62
53
 
54
+ def helper_for_file_test(start_value, fixed_value, ending_to_use, new_output_file = false)
55
+ helper_write_file(@file1, start_value)
56
+ helper_for_hexdump("start", @file1) if @debug
57
+ if new_output_file
58
+ @tle.output_to_file( @file1, ending_to_use, @file2 )
59
+ fixed_file = @file2
60
+ else
61
+ @tle.output_to_file( @file1, ending_to_use)
62
+ fixed_file = @file1
63
+ end
64
+ helper_for_hexdump("fixed", fixed_file) if File.exists?(fixed_file) && @debug
65
+ helper_read_file(fixed_file)
66
+ end
67
+
68
+ def helper_write_file(file_name, value)
69
+ file = File.new(file_name, "wb")
70
+ file.binmode
71
+ file.write(value)
72
+ file.close
73
+ end
74
+
75
+ def helper_read_file(file_name)
76
+ file_string = ""
77
+ if File.exists?(file_name)
78
+ file = File.open(file_name, "rb")
79
+ file.binmode
80
+ file_string = file.read
81
+ puts "contents of file #{file_name} as a string ... #{file_string.inspect}" if @debug
82
+ end
83
+ file_string
84
+ end
85
+
86
+ def helper_for_hexdump(which_file, file_name)
87
+ puts "#{which_file} file looks like this ..."
88
+ system "hexdump -C #{file_name}"
89
+ end
90
+
91
+
63
92
  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.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rich Krueger
@@ -9,10 +9,10 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2013-08-22 00:00:00 Z
12
+ date: 2013-08-28 00:00:00 Z
13
13
  dependencies: []
14
14
 
15
- description: A little mixin to change line-endings of files. Supports Mac, Unix, and Windows line ending conversions.
15
+ description: A little mixin to change the line endings of files. Supports Mac, Unix, and Windows line ending conversions.
16
16
  email:
17
17
  - rkrueger@gmail.com
18
18
  executables: []
@@ -24,6 +24,7 @@ extra_rdoc_files: []
24
24
  files:
25
25
  - lib/line_ender.rb
26
26
  - lib/test_line_ender.rb
27
+ - lib/le
27
28
  homepage: http://www.hqual.net
28
29
  licenses: []
29
30
 
@@ -49,6 +50,6 @@ rubyforge_project:
49
50
  rubygems_version: 2.0.7
50
51
  signing_key:
51
52
  specification_version: 4
52
- summary: Mixin to fix LineEndings of files
53
+ summary: Mixin to fix line endings of files
53
54
  test_files: []
54
55