-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnew_module.rb
More file actions
160 lines (131 loc) · 3.54 KB
/
new_module.rb
File metadata and controls
160 lines (131 loc) · 3.54 KB
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
157
158
159
160
#! ruby
require 'lib/xcopy'
def copy_template(source, target)
puts '--- copy template:'
if ! File.exist? target
clean_module(source)
Xcopy.copy_tree(source, target)
puts "> copied #{source} to #{target}"
else
puts "> #{target} exists"
end
end
def clean_module(source)
Dir.chdir source
`mvn clean`
Dir.chdir '..'
end
def delete_docs(source, target)
puts '--- delete docs:'
Dir.chdir target
delete_file 'micronaut-cli.yml'
Dir['*.URL'].each { |file| delete_file(file) }
Dir.chdir '..'
end
def delete_file(file)
if File.exist? file
File.delete file
puts "> deleted #{file}"
end
end
def change_packages(source, target)
puts '--- change packages:'
Dir.chdir target
change_package_in('src/main/java', source, target)
change_package_in('src/test/java', source, target)
Dir.chdir '..'
end
Base_package = 'org/codecop/monolith'
def change_package_in(folder, source, target)
Dir.chdir "#{folder}/#{Base_package}"
if File.exist? source
Xcopy.move_tree(source, target)
Dir.delete(source)
puts "> moved #{folder}/.../#{source} to #{target}"
else
puts "> #{folder}/.../#{target} exists"
end
Dir.chdir '../../../../../..'
end
def change_files(source, target)
puts '--- change files:'
Dir.chdir target
(Dir['**/.*'] + Dir['**/*.*']).each do |file|
change_lines(file, source, target)
end
Dir.chdir '..'
end
def change_lines(file_name, source, target)
return unless File.file? file_name
lines = IO.readlines(file_name)
new_lines = lines.map { |line| line.gsub(source, target) }
save_if_changed(file_name, lines, new_lines)
end
def save_if_changed(file_name, lines, new_lines)
if new_lines != lines
save_file(file_name, new_lines)
puts "> changed file #{file_name}"
end
end
def save_file(file_name, new_lines)
File.open(file_name, 'w') { |file|
new_lines.each { |line| file.print line }
}
end
def change_pom(target)
puts '--- add to parent pom:'
file_name = 'pom.xml'
return unless File.file? file_name
lines = IO.readlines(file_name)
if lines.find_all { |line| line =~ /<module>#{target}<\/module>/ }.size > 0
puts "> #{file_name} has module #{target}"
return
end
new_lines = add_module(lines, target)
save_if_changed(file_name, lines, new_lines)
end
def add_module(lines, target)
add = " <module>#{target}</module>\n"
lines.map do |line|
if line =~ /<\/modules>/
puts "> added module #{target}"
line = add + line
else
line
end
end
end
Base_port = 8080
def change_port(target)
puts '--- change port:'
new_port = Base_port + Dir['*'].find_all { |dir| File.directory? dir}.size - 4
change_lines("#{target}/README.md", Base_port.to_s, new_port.to_s)
change_lines("#{target}/src/main/resources/application.yml", Base_port.to_s, new_port.to_s)
end
if __FILE__ == $0
if ARGV.length < 1 or ARGV.length > 2 or ARGV.include?('-help')
puts 'ruby new_module.rb <new-module-name> [<source module name>|playground]'
puts 'copy module \'playground\' to new module <name>'
exit
end
target = ARGV[0]
if ARGV.length < 2
source = 'playground'
else
source = ARGV[1]
end
if ! File.exist? source or ! File.directory? source
puts "Source module #{source} not found/not a directory"
exit
end
if target !~ /^[a-zA-Z][a-zA-Z0-9_]+$/
puts "Target module name #{target} must be an identifier"
exit
end
copy_template(source, target)
delete_docs(source, target)
change_packages(source, target)
change_files(source, target)
change_pom(target)
change_port(target)
end