line_ender 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/line_ender.rb +112 -0
  3. data/lib/test_line_ender.rb +63 -0
  4. metadata +54 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA512:
3
+ data.tar.gz: bc0949d7421ee2d59e9b66c7c8e283add063a350aa984007bd448df84e0ab7bfe469f8570327f9c98b2b88c5ef28708667f259bfb08874b1bfce3508c15fdddc
4
+ metadata.gz: 4eb928bd0f1aa81e6421b4979fbf5295ed035e9f12abb848f9dda8d06661d8937f2ec04d42f32d6b2b855ff294c9ff54006dfc3887493af23fb470da6e511f72
5
+ SHA1:
6
+ data.tar.gz: f87a113ebfe033f1cdd1c307d51e9823ed3e48ad
7
+ metadata.gz: e8606ceac15be785b5aeec4dab2aded01e557bb1
data/lib/line_ender.rb ADDED
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+
4
+ module LineEnder
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
+ 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 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 get_output_filepath(input_filepath, output_filename)
51
+ input_path = File.expand_path(input_filepath).split(::File::Separator)
52
+ input_filename = input_path.last
53
+ puts "in get_output_filepath ... input_path = #{input_path}" if @debug
54
+ puts "in get_output_filepath ... input_filename = #{input_filename}" if @debug
55
+ output_filepath = ''
56
+ if output_filename.empty?
57
+ output_filepath = input_filepath
58
+ else
59
+ output_path = output_filename.split(::File::Separator)
60
+ output_filename = output_path.last
61
+ if output_path.first == output_filename
62
+ if input_filename == output_filename
63
+ output_filepath = input_filepath
64
+ else
65
+ input_path.pop
66
+ output_filepath = File.join(input_path,output_filename)
67
+ puts "in get_output_filepath ... output_filepath = #{output_filepath}" if @debug
68
+ end
69
+ else
70
+ #the path provided in output_filename is a path ... lets expand it
71
+ output_path = File.expand_path(output_filename).split(::File::Separator)
72
+ output_filepath = File.join(output_path)
73
+ end
74
+ end
75
+ output_filepath
76
+ end
77
+
78
+ def string_to_file(fixed_file_string, output_filepath)
79
+ File.delete(output_filepath) if File.exists?(output_filepath)
80
+ File.open(output_filepath, "wb"){|out_file| out_file.write(fixed_file_string)}
81
+ end
82
+
83
+ def change_line_endings(file_as_string, ending)
84
+ fixed_file_string = ''
85
+ fixed_file_string = file_as_string.gsub(/(\n)|(\r\n?)/, "\n") if ending == Ending::Unix
86
+ fixed_file_string = file_as_string.gsub(/(\n)|(\r\n?)/, "\r") if ending == Ending::Mac
87
+ fixed_file_string = file_as_string.gsub(/(\n)|(\r\n?)/, "\r\n") if ending == Ending::Windows
88
+ fixed_file_string
89
+ end
90
+
91
+ def file_to_string(filepath)
92
+ file = File.open(filepath, "rb")
93
+ file_to_string = file.read
94
+ file.close
95
+ file_to_string.chomp
96
+ end
97
+
98
+ def valid_ending?(ending)
99
+ raise RuntimeError, "Ending parameter is required!" unless ending
100
+ raise RuntimeError, "Ending parameter must be of type Ending" unless Ending.valid?(ending)
101
+ return true
102
+ end
103
+
104
+ def valid_filepath?(filepath)
105
+ raise RuntimeError, "Filepath parameter is required!" unless filepath
106
+ raise RuntimeError, "File with that filepath is not found!" unless File.exists?(filepath)
107
+ raise RuntimeError, "File is not readable!" unless File.readable?(filepath)
108
+ return true
109
+ end
110
+
111
+
112
+ end
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'line_ender'
4
+ require 'test/unit'
5
+
6
+ class TestLineEnder < Test:: Unit::TestCase
7
+
8
+ class TLE
9
+ include LineEnder
10
+ end
11
+
12
+ def setup
13
+ puts 'setup'
14
+ @debug = false
15
+ @tle = TLE.new
16
+ @file1 = "file1.txt"
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
+ end
23
+
24
+ def teardown
25
+ puts 'teardown'
26
+ @tle = nil
27
+ system "rm #{@file1}" if File.exists?(@file1)
28
+ system "rm #{@file2}" if File.exists?(@file2)
29
+ end
30
+
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 ..."
59
+ fs= @tle.output_to_string(@file1, LineEnder::Ending::Windows)
60
+ assert(fs == @le_windows)
61
+ end
62
+
63
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: line_ender
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rich Krueger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2013-08-22 00:00:00 Z
13
+ dependencies: []
14
+
15
+ description: A little mixin to change line-endings of files. Supports Mac, Unix, and Windows line ending conversions.
16
+ email:
17
+ - rkrueger@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/line_ender.rb
26
+ - lib/test_line_ender.rb
27
+ homepage: http://www.hqual.net
28
+ licenses: []
29
+
30
+ metadata: {}
31
+
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - &id001
40
+ - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - *id001
46
+ requirements: []
47
+
48
+ rubyforge_project:
49
+ rubygems_version: 2.0.7
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Mixin to fix LineEndings of files
53
+ test_files: []
54
+