blufin-lib 1.4.0
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.
- checksums.yaml +7 -0
- data/lib/blufin-lib.rb +22 -0
- data/lib/blufin-lib/core/arrays.rb +18 -0
- data/lib/blufin-lib/core/browser.rb +30 -0
- data/lib/blufin-lib/core/datetime_utils.rb +14 -0
- data/lib/blufin-lib/core/encryptor.rb +53 -0
- data/lib/blufin-lib/core/files.rb +385 -0
- data/lib/blufin-lib/core/network.rb +25 -0
- data/lib/blufin-lib/core/numbers.rb +15 -0
- data/lib/blufin-lib/core/routes.rb +85 -0
- data/lib/blufin-lib/core/ssh.rb +145 -0
- data/lib/blufin-lib/core/strings.rb +109 -0
- data/lib/blufin-lib/core/terminal.rb +413 -0
- data/lib/blufin-lib/core/tools.rb +108 -0
- data/lib/blufin-lib/test/TestEnvironmentValidator.rb +98 -0
- data/lib/version.rb +1 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 28d9f465ff5230d850d2c1d876a4e105bf077559
|
4
|
+
data.tar.gz: ff911b1d347254a0054140b7fcd148a5641cfaa3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 34aa1c67ceae7a75988e3169616ede84b5a21b91d46f09266e47fb35a8bb85e3adfe51860d7a62ffea085fe9df7cce11467af4ee27f33a4bea4380bc5e7001bd
|
7
|
+
data.tar.gz: 5745185edb426f42e9d7eb6c62b3b7c26507cff9de7ea4b4e14633206d00e79b8aaf1e78ba27d2c9ef4fefe4b6543ce051e11c6fde6549e222848615a1ad62c0
|
data/lib/blufin-lib.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Blufin
|
2
|
+
|
3
|
+
autoload :Arrays, 'blufin-lib/core/arrays'
|
4
|
+
autoload :Browser, 'blufin-lib/core/browser'
|
5
|
+
autoload :DateTimeUtils, 'blufin-lib/core/datetime_utils'
|
6
|
+
autoload :Encryptor, 'blufin-lib/core/encryptor'
|
7
|
+
autoload :Files, 'blufin-lib/core/files'
|
8
|
+
autoload :Network, 'blufin-lib/core/network'
|
9
|
+
autoload :Numbers, 'blufin-lib/core/numbers'
|
10
|
+
autoload :Routes, 'blufin-lib/core/routes'
|
11
|
+
autoload :SSH, 'blufin-lib/core/ssh'
|
12
|
+
autoload :Strings, 'blufin-lib/core/strings'
|
13
|
+
autoload :Terminal, 'blufin-lib/core/terminal'
|
14
|
+
autoload :Tools, 'blufin-lib/core/tools'
|
15
|
+
|
16
|
+
module Test
|
17
|
+
|
18
|
+
autoload :EnvironmentValidator, 'blufin-lib/test/TestEnvironmentValidator'
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Blufin
|
2
|
+
|
3
|
+
class Arrays
|
4
|
+
|
5
|
+
# Checks if an array has duplicates.
|
6
|
+
# @return boolean
|
7
|
+
def self.array_has_duplicate(array, ignore_case = false)
|
8
|
+
raise RuntimeError, "Expected Array, but got #{array.class}" unless array.is_a?(Array)
|
9
|
+
array = array.dup
|
10
|
+
if ignore_case
|
11
|
+
array.map! { |name| name.downcase }
|
12
|
+
end
|
13
|
+
!(array.uniq.length == array.length)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'watir-webdriver'
|
2
|
+
|
3
|
+
module Blufin
|
4
|
+
|
5
|
+
class Browser
|
6
|
+
|
7
|
+
# Get a Watir Browser object.
|
8
|
+
# @return [Watir::Browser]
|
9
|
+
def self.get(headless = false)
|
10
|
+
|
11
|
+
# Set the path to phantomjs executable
|
12
|
+
Selenium::WebDriver::PhantomJS.path = '/usr/local/bin/phantomjs'
|
13
|
+
|
14
|
+
browser = Watir::Browser.new (headless ? :phantomjs : :chrome), :switches => %w(--ignore-certificate-errors --test-type)
|
15
|
+
|
16
|
+
width = 1440
|
17
|
+
height = 2000
|
18
|
+
x = 0
|
19
|
+
y = -0
|
20
|
+
|
21
|
+
browser.window.move_to(x, y)
|
22
|
+
browser.window.resize_to(width, height)
|
23
|
+
browser.window.use
|
24
|
+
browser
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Blufin
|
2
|
+
|
3
|
+
class DateTimeUtils
|
4
|
+
|
5
|
+
# Returns the current DateTime as string. Default format is: %d/%m/%Y %H:%M:%S
|
6
|
+
# @return String
|
7
|
+
def self.now(format = nil)
|
8
|
+
format = '%Y/%m/%d %H:%M:%S' if format.nil?
|
9
|
+
Time.now.strftime(format)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'base64'
|
3
|
+
|
4
|
+
module Blufin
|
5
|
+
|
6
|
+
class Encryptor
|
7
|
+
|
8
|
+
def initialize(key = nil, hex = nil)
|
9
|
+
|
10
|
+
@key = key
|
11
|
+
@hex = hex
|
12
|
+
|
13
|
+
raise RuntimeError, "Encryptor hasn't been properly initialized. You forgot to pass the CRYPT_KEY + CRYPT_HEX to the constructor when instantiating the class." if @key.nil? || @hex.nil?
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
def encrypt(string)
|
18
|
+
|
19
|
+
c = OpenSSL::Cipher.new('aes-256-cbc')
|
20
|
+
c.encrypt
|
21
|
+
c.key = Digest::SHA1.hexdigest(@hex)[0..31]
|
22
|
+
begin
|
23
|
+
d = c.update(string)
|
24
|
+
d << c.final
|
25
|
+
return Base64.encode64(d)
|
26
|
+
rescue => e
|
27
|
+
Blufin::Terminal::error('Encryption failed', "Could not encrypt string \xe2\x86\x92 #{string == '' ? '[blank]' : Blufin::Terminal::format_invalid(string)}", false)
|
28
|
+
Blufin::Terminal::print_exception(e)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
def decrypt(string)
|
34
|
+
|
35
|
+
string_decoded = Base64.decode64(string)
|
36
|
+
c = OpenSSL::Cipher.new('aes-256-cbc')
|
37
|
+
c.decrypt
|
38
|
+
c.key = Digest::SHA1.hexdigest(@hex)[0..31]
|
39
|
+
begin
|
40
|
+
d = c.update(string_decoded)
|
41
|
+
d << c.final
|
42
|
+
return d
|
43
|
+
rescue => e
|
44
|
+
Blufin::Terminal::error('Decryption failed', "Could not decrypt string \xe2\x86\x92 #{string == '' ? '[blank]' : Blufin::Terminal::format_invalid(string)}", false)
|
45
|
+
Blufin::Terminal::print_exception(e)
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,385 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'parseconfig'
|
3
|
+
|
4
|
+
module Blufin
|
5
|
+
|
6
|
+
class Files
|
7
|
+
|
8
|
+
JAVA_AUTO_GENERATED_EVERY_RUN = 'java_auto_generated_every_run'
|
9
|
+
JAVA_AUTO_GENERATED_ONCE = 'java_auto_generated_once'
|
10
|
+
|
11
|
+
# Same as write_file() but for Java files. If arrange_imports = TRUE (default) it
|
12
|
+
# will sort the import statements in the same order as IntelliJ would.
|
13
|
+
# @return String
|
14
|
+
def self.write_file_java(path_and_file, array_of_lines, auto_generated = JAVA_AUTO_GENERATED_EVERY_RUN)
|
15
|
+
raise RuntimeError, "Expected String, instead got:#{path_and_file.class}" unless path_and_file.is_a?(String)
|
16
|
+
auto_generated_valid = [JAVA_AUTO_GENERATED_EVERY_RUN, JAVA_AUTO_GENERATED_ONCE]
|
17
|
+
raise RuntimeError, "Expected .java file, instead got: #{path_and_file}" unless path_and_file =~ /\.java\z/
|
18
|
+
raise RuntimeError, "auto_generated must be one of the following: #{auto_generated_valid.join(', ')}" unless auto_generated_valid.include?(auto_generated)
|
19
|
+
package = nil
|
20
|
+
import_statements_one = []
|
21
|
+
import_statements_two = []
|
22
|
+
previous_was_blank = false
|
23
|
+
annotations_one = []
|
24
|
+
annotations_two = []
|
25
|
+
contents = []
|
26
|
+
array_of_lines.each do |line|
|
27
|
+
if contents.any?
|
28
|
+
next if line.strip == '' && previous_was_blank
|
29
|
+
previous_was_blank = false
|
30
|
+
contents << line
|
31
|
+
previous_was_blank = true if line.strip == ''
|
32
|
+
next
|
33
|
+
end
|
34
|
+
if package.nil? && line.strip =~ /^package\s+[A-Za-z0-9\._-]+;$/
|
35
|
+
# Get package.
|
36
|
+
raise RuntimeError, "Package has a hyphen in it \xe2\x86\x92 \x1B[38;5;184m#{line.strip}\x1B[0m. When this file's contents were generated you forgot a .gsub() statement to replace it." if line.strip =~ /-/
|
37
|
+
package = line.strip
|
38
|
+
next
|
39
|
+
elsif line.strip =~ /^import\s+[A-Za-z0-9\.*_-]+;$/ || line.strip =~ /^import\s+static\s+[A-Za-z0-9\.*_-]+;$/
|
40
|
+
# Get import statements.
|
41
|
+
raise RuntimeError, "Import has a hyphen in it \xe2\x86\x92 \x1B[38;5;184m#{line.strip}\x1B[0m. When this file's contents were generated you forgot a .gsub() statement to replace it." if line.strip =~ /-/
|
42
|
+
import_statements_one << line.strip
|
43
|
+
next
|
44
|
+
elsif !contents.any? && line.strip =~ /^@(.)+$/
|
45
|
+
annotations_one << line.strip
|
46
|
+
elsif !contents.any? && line.strip =~ /^(public|private|protected)\s+.*(class|enum)\s+.*\{$/
|
47
|
+
contents << line.strip
|
48
|
+
next
|
49
|
+
end
|
50
|
+
end
|
51
|
+
raise RuntimeError, "Couldn't parse content for: #{path_and_file}" unless contents.any?
|
52
|
+
# Add @AutoGenerated + @TestNotRequired stuff.
|
53
|
+
import_statements_one << 'import org.blufin.base.annotations.AutoGenerated;'
|
54
|
+
import_statements_one << 'import org.blufin.base.annotations.TestNotRequired;'
|
55
|
+
import_statements_one << 'import org.blufin.base.annotations.helper.ON;'
|
56
|
+
annotations_one.each do |annotation|
|
57
|
+
next if %w(@TestNotRequired @AutoGenerated @AutoGenerated(ON.EVERY_RUN) @AutoGenerated(ON.CREATION_ONLY)).include?(annotation)
|
58
|
+
annotations_two << annotation
|
59
|
+
end
|
60
|
+
annotations_two << '@TestNotRequired'
|
61
|
+
annotations_two << ((auto_generated == JAVA_AUTO_GENERATED_EVERY_RUN) ? '@AutoGenerated(ON.EVERY_RUN)' : '@AutoGenerated(ON.CREATION_ONLY)')
|
62
|
+
annotations_two.uniq!
|
63
|
+
import_statements_one.uniq!
|
64
|
+
import_statements_one.sort!
|
65
|
+
package_path = package.gsub(/package\s/, '').gsub(/;/, '').strip
|
66
|
+
|
67
|
+
# Figure out which import statements to "keep" -- IE: Are they being used?
|
68
|
+
import_statements_one.each do |import_statement|
|
69
|
+
|
70
|
+
import_statement_path = []
|
71
|
+
import_statement_split = import_statement.gsub(/import\s/, '').gsub(/;/, '').strip.split('.')
|
72
|
+
import_statement_split.each_with_index { |n, idx| import_statement_path << n unless idx == (import_statement_split.length - 1) }
|
73
|
+
|
74
|
+
# Skip import if we're in the same package.
|
75
|
+
next if import_statement_path.join('.') == package_path
|
76
|
+
|
77
|
+
found = false
|
78
|
+
is = import_statement.split('.')
|
79
|
+
is = is[is.length - 1].gsub(';', '').strip
|
80
|
+
if is == '*'
|
81
|
+
import_statements_two << import_statement
|
82
|
+
next
|
83
|
+
end
|
84
|
+
annotations_two.each do |annotation|
|
85
|
+
if annotation =~ /@#{is}/ || annotation =~ /[\(\.]#{is}[\)\.]/
|
86
|
+
found = true
|
87
|
+
break
|
88
|
+
end
|
89
|
+
end
|
90
|
+
unless found
|
91
|
+
contents.each do |content_line|
|
92
|
+
if content_line =~ /[<\(\s{]#{is}[\.<>,\s\(\)]/ || content_line =~ /@#{is}[\(]?/
|
93
|
+
found = true
|
94
|
+
break
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
import_statements_two << import_statement if found
|
99
|
+
end
|
100
|
+
import_statement_counter = {}
|
101
|
+
import_statement_tracker = {}
|
102
|
+
# Convert multiple imports to .*
|
103
|
+
import_statements_two.each do |import_statement|
|
104
|
+
is = import_statement.split('.')
|
105
|
+
is = is.first(is.length - 1).join('.')
|
106
|
+
import_statement_counter[is] = 0 if import_statement_counter[is].nil?
|
107
|
+
import_statement_counter[is] += 1
|
108
|
+
import_statement_tracker[is] = [] if import_statement_tracker[is].nil?
|
109
|
+
import_statement_tracker[is] << import_statement
|
110
|
+
end
|
111
|
+
import_statements_one = []
|
112
|
+
import_statements_two.each do |import_statement|
|
113
|
+
is = import_statement.split('.')
|
114
|
+
is = is.first(is.length - 1).join('.')
|
115
|
+
if import_statement_counter[is] > 5
|
116
|
+
import_statements_one << "#{is}.*;"
|
117
|
+
else
|
118
|
+
import_statements_one << import_statement
|
119
|
+
end
|
120
|
+
end
|
121
|
+
import_statements_one.uniq!
|
122
|
+
import_statements_one.sort!
|
123
|
+
# Re-assemble the file.
|
124
|
+
final_written_java_non = 0
|
125
|
+
final_written_java = 0
|
126
|
+
final_contents = [package, '']
|
127
|
+
# Write NON-JAVA statements.
|
128
|
+
import_statements_one.each do |import_statement|
|
129
|
+
unless import_statement =~ /^import\s+java/ || import_statement =~ /^import\s+static/
|
130
|
+
final_contents << import_statement
|
131
|
+
final_written_java_non += 1
|
132
|
+
end
|
133
|
+
end
|
134
|
+
static_statement_found = false
|
135
|
+
# Write STATIC statements.
|
136
|
+
import_statements_one.each do |import_statement|
|
137
|
+
if import_statement =~ /^import\s+static/
|
138
|
+
unless static_statement_found
|
139
|
+
final_contents << ''
|
140
|
+
static_statement_found = true
|
141
|
+
end
|
142
|
+
final_contents << import_statement
|
143
|
+
final_written_java += 1
|
144
|
+
end
|
145
|
+
end
|
146
|
+
final_contents << '' if final_written_java_non > 0
|
147
|
+
# Write JAVA statements.
|
148
|
+
import_statements_one.each do |import_statement|
|
149
|
+
if import_statement =~ /^import\s+java/
|
150
|
+
final_contents << import_statement
|
151
|
+
final_written_java += 1
|
152
|
+
end
|
153
|
+
end
|
154
|
+
final_contents << '' if final_written_java > 0
|
155
|
+
annotations_two.each { |line| final_contents << line } if annotations_two.any?
|
156
|
+
contents.each { |line| final_contents << line } if contents.any?
|
157
|
+
return self.write_file(path_and_file, final_contents)
|
158
|
+
end
|
159
|
+
|
160
|
+
# Write an "Array of Lines" to a file -- overwrites file if exists!
|
161
|
+
# @return String
|
162
|
+
def self.write_file(path_and_file, array_of_lines)
|
163
|
+
raise RuntimeError, "Expected String, instead got:#{path_and_file.class}" unless path_and_file.is_a?(String)
|
164
|
+
path_and_file = File.expand_path(path_and_file)
|
165
|
+
raise RuntimeError, "Expected an array of lines to write to file, instead got: #{array_of_lines.class}" unless array_of_lines.is_a? Array
|
166
|
+
prepare_for_file_write(path_and_file)
|
167
|
+
begin
|
168
|
+
File.open(path_and_file, 'w') { |file|
|
169
|
+
array_of_lines.each_with_index do |line, index|
|
170
|
+
if index == array_of_lines.size - 1
|
171
|
+
file.write("#{line}") unless line.to_s.strip == ''
|
172
|
+
else
|
173
|
+
file.write("#{line}\n")
|
174
|
+
end
|
175
|
+
end
|
176
|
+
file.close
|
177
|
+
}
|
178
|
+
rescue Exception => e
|
179
|
+
Blufin::Terminal::print_exception(e)
|
180
|
+
end
|
181
|
+
path_and_file
|
182
|
+
end
|
183
|
+
|
184
|
+
# Write a "String" to a file -- overwrites file if exists!
|
185
|
+
# @return void
|
186
|
+
def self.write_file_string(path_and_file, content)
|
187
|
+
raise RuntimeError, "Expected String, instead got:#{path_and_file.class}" unless path_and_file.is_a?(String)
|
188
|
+
path_and_file = File.expand_path(path_and_file)
|
189
|
+
raise RuntimeError, "Expected String to write to file, instead got: #{content.class}" unless content.is_a? String
|
190
|
+
prepare_for_file_write(path_and_file)
|
191
|
+
begin
|
192
|
+
File.open(path_and_file, 'w') { |file|
|
193
|
+
file.write("#{content}")
|
194
|
+
file.close
|
195
|
+
}
|
196
|
+
rescue Exception => e
|
197
|
+
Blufin::Terminal::print_exception(e)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
# Writes a line to a file.
|
202
|
+
# regex -> If a value exists the program checks for this regex and writes on the line AFTER match(es). if nil (or not found), writes at the end of file.
|
203
|
+
# only_if_not_exists -> If TRUE, will add line only if it doesn't already exist.
|
204
|
+
# replace -> If TRUE, will replace file rather than writing on next line after regex.
|
205
|
+
# @return void
|
206
|
+
def self.write_line_to_file(path_and_file, line, regex = nil, only_if_not_exists = true, replace = false)
|
207
|
+
raise RuntimeError, "Expected String, instead got:#{path_and_file.class}" unless path_and_file.is_a?(String)
|
208
|
+
path_and_file = File.expand_path(path_and_file)
|
209
|
+
raise RuntimeError, "File not found: #{path_and_file}" unless file_exists(path_and_file)
|
210
|
+
raise RuntimeError, "Expected String to write to file, instead got: #{line.class}" unless line.is_a? String
|
211
|
+
raise RuntimeError, "Expected Regexp to match to line, instead got: #{regex.class}" unless regex.nil? || regex.is_a?(Regexp)
|
212
|
+
raise RuntimeError, 'Cannot set replace to TRUE when regex is nil.' if regex.nil? && replace
|
213
|
+
raise RuntimeError, 'only_if_not_exists must be set to FALSE when replace is TRUE.' if only_if_not_exists && replace
|
214
|
+
new_lines = []
|
215
|
+
if only_if_not_exists
|
216
|
+
line_found = false
|
217
|
+
read_file(path_and_file).each do |line_content|
|
218
|
+
line_content.gsub!(/\n\z/, '')
|
219
|
+
line_found = true if line_content == line
|
220
|
+
end
|
221
|
+
return if line_found
|
222
|
+
end
|
223
|
+
if !regex.nil? && !replace
|
224
|
+
regex_found = false
|
225
|
+
line_inserted = false
|
226
|
+
read_file(path_and_file).each do |line_content|
|
227
|
+
line_content.gsub!(/\n\z/, '')
|
228
|
+
regex_found = true if line_content =~ regex
|
229
|
+
if regex_found && line_content !~ regex && !line_inserted
|
230
|
+
new_lines << line
|
231
|
+
line_inserted = true
|
232
|
+
end
|
233
|
+
new_lines << line_content
|
234
|
+
end
|
235
|
+
new_lines << line unless line_inserted
|
236
|
+
elsif !regex.nil? && replace
|
237
|
+
regex_found = false
|
238
|
+
read_file(path_and_file).each do |line_content|
|
239
|
+
line_content.gsub!(/\n\z/, '')
|
240
|
+
if !regex_found && line_content =~ regex
|
241
|
+
regex_found = true
|
242
|
+
new_lines << line
|
243
|
+
else
|
244
|
+
new_lines << line_content
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
write_file(path_and_file, new_lines)
|
249
|
+
end
|
250
|
+
|
251
|
+
# Removes a line from a file.
|
252
|
+
# multiple_occurrences -> If set to TRUE, will remove ALL occurrences from file.
|
253
|
+
# @return void
|
254
|
+
def self.remove_line_from_file(path_and_file, regex, multiple_occurrences = false)
|
255
|
+
raise RuntimeError, "Expected String, instead got:#{path_and_file.class}" unless path_and_file.is_a?(String)
|
256
|
+
path_and_file = File.expand_path(path_and_file)
|
257
|
+
raise RuntimeError, "Expected Regexp to match to line, instead got: #{regex.class}" unless regex.is_a? Regexp
|
258
|
+
raise RuntimeError, "File not found: #{path_and_file}" unless file_exists(path_and_file)
|
259
|
+
new_lines = []
|
260
|
+
line_removed = false
|
261
|
+
read_file(path_and_file).each do |line_content|
|
262
|
+
line_content.gsub!(/\n\z/, '')
|
263
|
+
line_content_to_match = line_content.strip
|
264
|
+
if line_content_to_match =~ regex
|
265
|
+
if multiple_occurrences
|
266
|
+
next
|
267
|
+
else
|
268
|
+
unless line_removed
|
269
|
+
line_removed = true
|
270
|
+
next
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
new_lines << line_content
|
275
|
+
end
|
276
|
+
write_file(path_and_file, new_lines)
|
277
|
+
end
|
278
|
+
|
279
|
+
# Get content of a file as an "Array of Lines"
|
280
|
+
# @return Array
|
281
|
+
def self.read_file(path_and_file, start_line = nil, end_line = nil)
|
282
|
+
raise RuntimeError, "Expected String, instead got:#{path_and_file.class}" unless path_and_file.is_a?(String)
|
283
|
+
line_count = 0
|
284
|
+
path_and_file = File.expand_path(path_and_file)
|
285
|
+
raise RuntimeError, "File not found: #{path_and_file}" unless file_exists(path_and_file)
|
286
|
+
raise RuntimeError, "start_line (#{start_line}) cannot be bigger than end_line (#{end_line})." if (!start_line.nil? && !end_line.nil?) && (start_line > end_line)
|
287
|
+
file_content = []
|
288
|
+
file = File.open(path_and_file).read
|
289
|
+
file.gsub!(/\r\n?/, "\n")
|
290
|
+
file.each_line do |line|
|
291
|
+
line_count += 1
|
292
|
+
next if !start_line.nil? && start_line > line_count
|
293
|
+
next if !end_line.nil? && end_line < line_count
|
294
|
+
file_content << line
|
295
|
+
end
|
296
|
+
file_content
|
297
|
+
end
|
298
|
+
|
299
|
+
# Deletes a file (if exists)
|
300
|
+
# @return void
|
301
|
+
def self.delete_file(path_and_file)
|
302
|
+
raise RuntimeError, "Expected String, instead got:#{path_and_file.class}" unless path_and_file.is_a?(String)
|
303
|
+
FileUtils.rm(File.expand_path(path_and_file)) if file_exists(path_and_file)
|
304
|
+
end
|
305
|
+
|
306
|
+
# Returns TRUE or FALSE depending whether a path exists.
|
307
|
+
# @return void
|
308
|
+
def self.path_exists(path)
|
309
|
+
raise RuntimeError, "Expected String, instead got:#{path.class}" unless path.is_a?(String)
|
310
|
+
File.directory?(File.expand_path(path))
|
311
|
+
end
|
312
|
+
|
313
|
+
# Returns TRUE or FALSE depending whether a path exists.
|
314
|
+
# @return void
|
315
|
+
def self.file_exists(path_and_file)
|
316
|
+
raise RuntimeError, "Expected String, instead got:#{path_and_file.class}" unless path_and_file.is_a?(String)
|
317
|
+
File.exist?(File.expand_path(path_and_file))
|
318
|
+
end
|
319
|
+
|
320
|
+
# Get full path, IE: '~/Repos' would become '/Users/Albert/Repos'
|
321
|
+
# @return String
|
322
|
+
def self.get_full_path(path)
|
323
|
+
raise RuntimeError, "Expected String, instead got:#{path.class}" unless path.is_a?(String)
|
324
|
+
"/#{Blufin::Strings.remove_surrounding_slashes(File.expand_path(path))}"
|
325
|
+
end
|
326
|
+
|
327
|
+
# Get and array of files in a directory.
|
328
|
+
# @return Array
|
329
|
+
def self.get_files_in_dir(path, only_with_extension = nil)
|
330
|
+
raise RuntimeError, "Expected String, instead got:#{path.class}" unless path.is_a?(String)
|
331
|
+
path = "/#{Blufin::Strings.remove_surrounding_slashes(File.expand_path(path))}"
|
332
|
+
raise RuntimeError, "Directory doesn't exist: #{path}" unless path_exists(path)
|
333
|
+
files = Dir.glob("#{path}/**/*.#{only_with_extension.nil? ? '*' : only_with_extension}")
|
334
|
+
files.uniq.sort
|
335
|
+
end
|
336
|
+
|
337
|
+
# Get and array of directories in a directory.
|
338
|
+
# @return Array
|
339
|
+
def self.get_dirs_in_dir(path, recursive = false)
|
340
|
+
raise RuntimeError, "Expected String, instead got:#{path.class}" unless path.is_a?(String)
|
341
|
+
path = "/#{Blufin::Strings.remove_surrounding_slashes(File.expand_path(path))}"
|
342
|
+
raise RuntimeError, "Directory doesn't exist: #{path}" unless path_exists(path)
|
343
|
+
dirs = Dir.glob("#{path}/**/*/")
|
344
|
+
unless recursive
|
345
|
+
root_dirs = []
|
346
|
+
root_length = path.split('/').length
|
347
|
+
dirs.each do |dir|
|
348
|
+
root_dirs << dir if dir.split('/').length == root_length + 1
|
349
|
+
end
|
350
|
+
dirs = root_dirs
|
351
|
+
end
|
352
|
+
dirs.map! { |value| value.gsub(/\/\z/, '') }
|
353
|
+
dirs
|
354
|
+
end
|
355
|
+
|
356
|
+
# Create a directory recursively (if it doesn't already exist).
|
357
|
+
# @return String
|
358
|
+
def self.create_directory(path)
|
359
|
+
begin
|
360
|
+
path = File.expand_path(path)
|
361
|
+
FileUtils.mkpath(path) unless path_exists(path)
|
362
|
+
path
|
363
|
+
rescue => e
|
364
|
+
Blufin::Terminal::print_exception(e)
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
# Proxy function for the above.
|
369
|
+
# @return void
|
370
|
+
def self.create_path(path)
|
371
|
+
create_directory(path)
|
372
|
+
end
|
373
|
+
|
374
|
+
private
|
375
|
+
|
376
|
+
# Creates path (if not exists) and deletes file (if exists).
|
377
|
+
# @return void
|
378
|
+
def self.prepare_for_file_write(path_and_file)
|
379
|
+
FileUtils::mkdir_p(File.dirname(path_and_file)) unless path_exists(File.dirname(path_and_file))
|
380
|
+
File.delete(path_and_file) if file_exists(path_and_file)
|
381
|
+
end
|
382
|
+
|
383
|
+
end
|
384
|
+
|
385
|
+
end
|