joe_utils 0.0.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/Rakefile +8 -0
- data/lib/joe_utils/console_helper.rb +11 -0
- data/lib/joe_utils/files_helper.rb +107 -0
- data/lib/joe_utils/name_helper.rb +16 -0
- data/lib/joe_utils/script.rb +0 -0
- data/lib/joe_utils/script_editor_helper.rb +16 -0
- data/lib/joe_utils/script_renderer.rb +54 -0
- data/lib/joe_utils/socket_helper.rb +17 -0
- data/lib/joe_utils.rb +9 -0
- data/test/test_joe_utils.rb +9 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b75efe7a08baf939992f91c589eb9fab2844cea6
|
4
|
+
data.tar.gz: 4322b777483856910f9d2521101a418bfd3418a5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2bb3594a0d0fb7133eb63f49abb9ee821ea2ea9ddae9072621e8332f753ff44a691dd472281fbba4f9da4bf5ef999679d6b19386af22fd1b8803af6963d48656
|
7
|
+
data.tar.gz: 9cf96dd6fa6e2d3bcb4c2d884583061d395f474b8561a0e94a761aacd7a674f11348296270269bc8102beec2e429eeb01506f07930ac2c140ac410b3815caaef
|
data/Rakefile
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'fileutils'
|
4
|
+
require 'zip'
|
5
|
+
|
6
|
+
module FilesHelper
|
7
|
+
|
8
|
+
def dir_exists?(location)
|
9
|
+
File.directory?(location) ? true : puts("Directory #{location} doesn't exist.")
|
10
|
+
end
|
11
|
+
|
12
|
+
def file_exists?(location)
|
13
|
+
File.exist?(location) ? true : puts("The file #{location} doesn't exist.")
|
14
|
+
end
|
15
|
+
|
16
|
+
def remove_dir(location)
|
17
|
+
FileUtils.rm_rf(location)
|
18
|
+
puts "Directory #{location} removed."
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_dir(location, options = {})
|
22
|
+
if dir_exists?(location)
|
23
|
+
if options[:overwrite]
|
24
|
+
remove_dir(location)
|
25
|
+
else
|
26
|
+
puts "Directory #{location} already exists."
|
27
|
+
return
|
28
|
+
end
|
29
|
+
end
|
30
|
+
FileUtils.mkdir_p(location)
|
31
|
+
puts "Directory #{location} created."
|
32
|
+
end
|
33
|
+
|
34
|
+
def for_all_in_dir(location, options = {})
|
35
|
+
Dir.glob(File.join(File.join(location, options[:recursive] && '**'), options[:matching] || '*')) { |f| yield f }
|
36
|
+
end
|
37
|
+
|
38
|
+
def unzip(zip_file, location)
|
39
|
+
create_dir(location)
|
40
|
+
Zip::File.open(zip_file) { |content| content.each { |f| content.extract(f, File.join(location, f.name)) } }
|
41
|
+
end
|
42
|
+
|
43
|
+
def zip(zip_file)
|
44
|
+
Zip::File.open(zip_file, Zip::File::CREATE) { |zip| yield zip }
|
45
|
+
end
|
46
|
+
|
47
|
+
def zip_folder(zip_file, to_zip, options = {})
|
48
|
+
zip(zip_file) do |zip|
|
49
|
+
for_all_in_dir(to_zip, matching: options[:matching], recursive: true) { |file| zip.add(File.basename(file), file) }
|
50
|
+
yield zip if block_given?
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def add_to_open_zip(zip, file)
|
55
|
+
zip.add(File.basename(file), file)
|
56
|
+
end
|
57
|
+
|
58
|
+
def edit_file(address)
|
59
|
+
new_file_content = yield File.open(address, 'r') { |f| f.read }
|
60
|
+
File.open(address, 'w+') { |f| f.write(new_file_content) }
|
61
|
+
end
|
62
|
+
|
63
|
+
def create_file_by_template(template_path, path)
|
64
|
+
text = load_file_text(template_path)
|
65
|
+
yield text if block_given?
|
66
|
+
create_file(path, text)
|
67
|
+
end
|
68
|
+
|
69
|
+
def load_file_text(path)
|
70
|
+
File.open(path, 'r') { |f| f.read } if file_exists?(path)
|
71
|
+
end
|
72
|
+
|
73
|
+
def create_file(path, text)
|
74
|
+
File.open(path, 'w+') { |f| f.write(text) }
|
75
|
+
puts "File #{path} created."
|
76
|
+
end
|
77
|
+
|
78
|
+
# Replaces text in all files in the folder address
|
79
|
+
# @param text [String] text to be replaced
|
80
|
+
# @param folder_address [String] folder address to search in
|
81
|
+
# @return nil
|
82
|
+
def replace_in_all(text, folder_address)
|
83
|
+
Dir[File.join(folder_address, '*')].each do |address|
|
84
|
+
content = File.open(address, 'r') { |f| f.read }
|
85
|
+
if content.gsub!(text) { |match| yield match }
|
86
|
+
File.open(address, 'w+') { |f| f.write(content) }
|
87
|
+
puts "Replaced all in: #{address}."
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# Puts all file addresses which contain the text
|
93
|
+
# @param text [String] searched text
|
94
|
+
# @param location [String] folder address searched
|
95
|
+
# @return nil
|
96
|
+
def find_in_all(text, location)
|
97
|
+
Dir[File.join(location, '*')].each do |address|
|
98
|
+
File.open(address, 'r') { |f| puts "Found in file: #{f}" if f.read =~ /#{text}/ }
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def catch_errno_enoent
|
103
|
+
yield
|
104
|
+
rescue Errno::ENOENT => e
|
105
|
+
# Nothing bad happened
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module NameHelper
|
4
|
+
|
5
|
+
def create_nice_file_name(file_content)
|
6
|
+
class_name = file_content.match(/#{@app_structure.vendor}::[A-Z_]+::[a-zA-Z]+/).to_s.split('::').last
|
7
|
+
class_name.gsub!(/[A-Z]+/) do |capital_letters|
|
8
|
+
if capital_letters.size > 1 && class_name[-capital_letters.size..-1] != capital_letters
|
9
|
+
"_#{capital_letters[0..-2].downcase}_#{capital_letters[-1].downcase}"
|
10
|
+
else
|
11
|
+
"_#{capital_letters.downcase}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
class_name
|
15
|
+
end
|
16
|
+
end
|
File without changes
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module ScriptEditorHelper
|
4
|
+
# @param pars [Array]
|
5
|
+
# @return [nil]
|
6
|
+
def add_pars_to_controllers(pars)
|
7
|
+
Dir["#{@app_structure.controllers_fld}/*"].each do |controller|
|
8
|
+
content = load_file_text(controller)
|
9
|
+
content.sub!(/CONFIG = \{[\s\w:',.\/\?=\-]*?\s\s}/) { |config|
|
10
|
+
pars.each { |par| config.sub!(/CONFIG = \{\n/, "CONFIG = {\n\t\t\t#{par},\n") unless content[par] || content[par[/['"][\s\w:,.\/\?=\-]+?['"]/]] }
|
11
|
+
config
|
12
|
+
}
|
13
|
+
create_file(controller, content)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module ScriptRenderer
|
2
|
+
|
3
|
+
# @param [String] file_path
|
4
|
+
def render_script(file_path)
|
5
|
+
lines = File.open(file_path, 'r') { |f| f.readlines }
|
6
|
+
|
7
|
+
structures, requires, comments, modules, klass, methods, class_name, full_class_name = [], [], [], [], '', {}, '', ''
|
8
|
+
class_began, private_began, public_began, methods_began, structure_began = false, false, true, false, false
|
9
|
+
|
10
|
+
lines.each do |line|
|
11
|
+
line.strip!
|
12
|
+
if line[0] == '#'
|
13
|
+
comments << line[1..-1]
|
14
|
+
elsif line =~ /^require /
|
15
|
+
match = line.match(/\brequire ['"]/)
|
16
|
+
requires << match.post_match[0..-2]
|
17
|
+
elsif line =~ /^module /
|
18
|
+
match = line.match(/\bmodule /)
|
19
|
+
modules << match.post_match[/\b[A-Z]+(::[A-Z_]+)*\b/]
|
20
|
+
elsif line =~ /^class /
|
21
|
+
class_began = true
|
22
|
+
klass = line[/^class [A-Za-z_]+(::[A-Za-z_]+)*/][6..-1].split('::').last
|
23
|
+
elsif line =~ /^def initialize/
|
24
|
+
methods_began = true
|
25
|
+
elsif line =~ /^def /
|
26
|
+
methods[line[/^def \w+/][4..-1]] = {arguments: []}
|
27
|
+
elsif line =~ /^private /
|
28
|
+
private_began = true
|
29
|
+
elsif line =~ /^[A-Z]+\b = {/ && class_began && !methods_began
|
30
|
+
structure_began = true
|
31
|
+
structures << {name: line[/^[A-Z]+\b/] }
|
32
|
+
structures.last[:content] = {}
|
33
|
+
elsif line =~ /^[a-z_]+: ?/ && structure_began
|
34
|
+
match = line.match(/^[a-z_]+: ?/)
|
35
|
+
structures.last[:content][match.to_s[0..-3].to_sym] = "'\"".include?(match.post_match[0]) ? match.post_match[1..-3] : match.post_match[0..-2]
|
36
|
+
elsif line =~ /^:[a-z]+ ?=> ?/ && structure_began
|
37
|
+
match = line.match(/^:[a-z]+ ?=> ?/)
|
38
|
+
structures.last[:content][match.to_s[1..-5].to_sym] = "'\"".include?(match.post_match[0]) ? match.post_match[1..-3] : match.post_match[0..-2]
|
39
|
+
elsif line == '}'
|
40
|
+
structure_began = false
|
41
|
+
else
|
42
|
+
# puts line
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_whole_class_name(file_content)
|
48
|
+
file_content[/#{@app_structure.vendor}::#{@app_structure.app_name_unof}::[a-zA-Z\d]+/]
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_class_name(file_content)
|
52
|
+
get_whole_class_name(file_content).split('::').last
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'timeout'
|
3
|
+
|
4
|
+
module SocketHelper
|
5
|
+
def is_port_open?(ip, port)
|
6
|
+
begin
|
7
|
+
Timeout::timeout(1) do
|
8
|
+
s = TCPSocket.new(ip, port)
|
9
|
+
s.close
|
10
|
+
return true
|
11
|
+
end
|
12
|
+
rescue Timeout::Error, Errno::ECONNREFUSED, Errno::EHOSTUNREACH
|
13
|
+
# don't do anything
|
14
|
+
end
|
15
|
+
false
|
16
|
+
end
|
17
|
+
end
|
data/lib/joe_utils.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'joe_utils/config_helper'
|
2
|
+
require 'joe_utils/console_helper'
|
3
|
+
require 'joe_utils/files_helper'
|
4
|
+
require 'joe_utils/name_helper'
|
5
|
+
require 'joe_utils/script_editor_helper'
|
6
|
+
require 'joe_utils/script_renderer'
|
7
|
+
require 'joe_utils/script'
|
8
|
+
require 'joe_utils/socket_helper'
|
9
|
+
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: joe_utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Josef Erneker
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-26 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Overall personal utils gem
|
14
|
+
email: josef.erneker@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- Rakefile
|
20
|
+
- lib/joe_utils.rb
|
21
|
+
- lib/joe_utils/console_helper.rb
|
22
|
+
- lib/joe_utils/files_helper.rb
|
23
|
+
- lib/joe_utils/name_helper.rb
|
24
|
+
- lib/joe_utils/script.rb
|
25
|
+
- lib/joe_utils/script_editor_helper.rb
|
26
|
+
- lib/joe_utils/script_renderer.rb
|
27
|
+
- lib/joe_utils/socket_helper.rb
|
28
|
+
- test/test_joe_utils.rb
|
29
|
+
homepage: http://rubygems.org/gems/joeutils
|
30
|
+
licenses:
|
31
|
+
- GPL-3.0
|
32
|
+
metadata: {}
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 2.4.5
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: Utils for work with files, sockets etc.
|
53
|
+
test_files:
|
54
|
+
- test/test_joe_utils.rb
|