-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.rb
156 lines (142 loc) · 3.32 KB
/
add.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
require 'fileutils'
def app_settings
settings = {
"file" => "index.scss",
"open" => "true",
"editor" => "nvim"
}
settings
end
def welcome
puts "Select an option:"
puts "1 - Setting"
puts "2 - Tool"
puts "3 - Generic"
puts "4 - Element"
puts "5 - Object"
puts "6 - Component"
puts "7 - Utility"
gets.to_i
end
def get_name
puts "enter the name(without o- or c- prefix)"
gets.chomp
end
def get_prefix(mode)
prefix = {
"setting" => "setting",
"tool" => "tool",
"generic" => "generic",
"element" => "element",
"object" => "o",
"component" => "c",
"utility" => "utility",
}
prefix[mode]
end
def lookup(num)
index = {
1 => "setting",
2 => "tool",
3 => "generic",
4 => "element",
5 => "object",
6 => "component",
7 => "utility",
}
if index[num]
return index[num]
else
puts 'Incorrect input'
exit!
end
end
def get_plural(mode)
plural = {
"setting" => "settings",
"tool" => "tools",
"generic" => "generic",
"element" => "elements",
"object" => "objects",
"component" => "components",
"utility" => "utilities"
}
plural[mode]
end
def update_stylesheet(name, choice, mode, index)
if File.exists?(index)
plural = get_plural(mode)
upcase = mode.upcase
text = File.read(index)
new_lines = <<~SCSS
@import './#{choice}-#{plural}/#{mode}.#{name}';
// ---#{upcase} - PLACHOLDER --- //
SCSS
placeholder_line = "// ---#{upcase} - PLACHOLDER --- //"
updated_file = text.gsub(/#{placeholder_line}/, new_lines)
File.open(index, "w") {|file| file.puts updated_file }
end
end
def create_file(name, choice, mode)
plural = get_plural(mode)
target_file = "./#{choice}-#{plural}/_#{mode}.#{name}.scss"
if !File.exists?(target_file)
FileUtils.touch(target_file)
else
Puts "#{mode} already exists"
kill!
end
target_file
end
def update_new_file(text, name, mode)
if mode == "object" || mode == "component"
prefix = get_prefix(mode)
new_lines = <<~SCSS
.#{prefix}-#{name} {
$this: '.#{prefix}-#{name}';
@extend %bem-block;
// PLACEHOLDER
}
SCSS
File.open(text, "w") {|file| file.puts new_lines }
end
text
end
def build_children(mode, name, text)
if mode == "object" || mode == "component"
puts "Add children to #{mode}? Separate with spaces, otherwise leave empty."
children = gets.chomp.split(' ')
if children != ''
children_template = ''
children.each do |child|
children_template += <<~SCSS
\t&__#{child} {
\t}
SCSS
end
else
children_template = ''
end
doc = File.read(text)
placeholder_line = "// PLACEHOLDER"
updated_file = doc.gsub(/#{placeholder_line}/, children_template)
File.open(text, "w") {|file| file.puts updated_file }
end
end
def open(set_to_open, editor, new_file)
if set_to_open
system("#{editor}", "#{new_file}")
end
end
def app
settings = app_settings
choice = welcome
mode = lookup(choice)
name = get_name
new_file = create_file(name, choice, mode)
update_stylesheet(name, choice, mode, settings['file'])
update_new_file(new_file, name, mode)
build_children(mode, name, new_file)
open(settings['open'], settings['editor'], new_file)
end
app