-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_config.rb
129 lines (97 loc) · 2.77 KB
/
_config.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require 'json'
require "colorize"
require "base64"
module MIT
HEADER = [
"#================================================================",
"# The MIT License",
"# Copyright (c) 2020 biud436",
"# ---------------------------------------------------------------",
"# Free for commercial and non commercial use.",
"#================================================================",
].join("\n")
end
class App
def initialize
if ARGV.include? "-r"
rebase
else
run
end
end
def read_folder(project_root = nil, files = [])
project_root = File.dirname(File.absolute_path(__FILE__)) if !project_root
Dir.glob([project_root + "/*.rb", project_root + "/*"]) do |file|
if File.directory?(file)
read_folder(file, files)
elsif File.extname(file) == ".rb"
files.push(file)
end
end
return files
end
def config_file
"config.json"
end
def load_config
# 설정 파일을 불러옵니다
config = if File.exist?(config_file)
puts "config.json file is found in this directory".red
json = File.open(config_file, "r+").read
parsed = JSON.parse(json)
else
puts "config.json file cannot find in this directory".red
JSON.parse "{}"
end
config["_config.rb"] = true
return config
end
def save_config(config)
f = File.open(config_file, "w+")
f.puts JSON.pretty_generate(config)
f.close
puts "config.json file has changed".blue.on_red.uncolorize
end
def run
# 설정 파일을 로드합니다.
config = load_config
# 모든 루비 파일을 탐색한다.
read_folder.uniq.each do |file|
# 헤더가 이미 작성되어있다면 다음으로
next if config[File.basename(file)]
puts "validation ==> #{file}".blue.on_red.blink
f = File.open(file, 'r+')
contents = f.read
f.close
f = File.open(file, 'w+')
f << MIT::HEADER
f << "\n"
f << contents
f.close
config[File.basename(file)] = Base64.encode64(contents)
puts "#{File.basename(file)} is written successfully".colorize(:light_blue)
end
save_config(config)
end
# 이전 파일로 복구합니다.
def rebase
# 설정 파일을 로드합니다.
config = load_config
# 모든 루비 파일을 탐색한다.
read_folder.uniq.each do |file|
name = File.basename(file)
next if name == "_config.rb"
next if !config[name]
puts "validation ==> #{file}".black.on_white.blink
enc = config[name]
plain = Base64.decode64(enc)
f = File.open(file, 'w+')
f << plain
f.close
config.delete(name)
end
# 설정 파일을 저장합니다.
save_config(config)
end
end
app = App.new