-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew-module.rb
66 lines (52 loc) · 1.99 KB
/
new-module.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require "down"
require "zip"
require "fileutils"
require_relative "./spinner"
MODULE_ZIP_URL = "https://github.com/ChillyCheesy/SkeletonModule/archive/refs/heads/master.zip"
def new_module(name)
SPINNER.start
tempfile = Down.download(MODULE_ZIP_URL)
Zip::File.open(tempfile.path) do |zip_file|
zip_file.each do |f|
f_path=File.join("./", f.name.sub(/([^\/])*\//, name + "/"))
FileUtils.mkdir_p(File.dirname(f_path))
zip_file.extract(f, f_path) unless File.exist?(f_path)
end
end
replace_in_folder("./" + name, "skeleton", name)
FileUtils.mv("./" + name, "./" + name + "Module")
SPINNER.stop("Module #{name}Module successfully created, you can now start working on it!")
end
def renameFile(file, name)
# Rename the file
if file.include?("Skeleton")
FileUtils.mv(file, file.gsub("Skeleton", name.capitalize))
end
if file.include?("skeleton")
FileUtils.mv(file, file.gsub("skeleton", name[0].downcase + name[1..-1]))
end
end
def snake_case(str)
str.split(/(?=[A-Z])/).join("_").downcase
end
def replace_in_file(file, old_word, new_word)
text = File.read(file)
new_contents = text.gsub(old_word, new_word).gsub(old_word[0].upcase + old_word[1..-1], new_word[0].upcase + new_word[1..-1])
new_file = file.gsub(old_word, new_word).gsub(old_word[0].upcase + old_word[1..-1], new_word[0].upcase + new_word[1..-1])
File.rename(file, new_file)
File.open(new_file, "w") { |file| file.puts new_contents }
end
def replace_in_folder(folder, old_word, new_word)
Dir.glob("#{folder}/**/*").each do |file|
if File.directory?(file)
new_name = file.gsub(old_word, snake_case(new_word))
FileUtils.mv(file, new_name) unless file == new_name
if new_name != file
replace_in_folder(new_name, old_word, new_word)
end
end
if File.file?(file)
replace_in_file(file, old_word, new_word)
end
end
end