-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.rb
executable file
·71 lines (59 loc) · 1.71 KB
/
rename.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
#!/usr/bin/env ruby
require 'find'
require 'fileutils'
class String
def camel_case
return self if self !~ /_/ && self =~ /[A-Z]+.*/
split('_').map(&:capitalize).join
end
end
def usage
puts 'This script renames the template plugin to a name of your choice'
puts 'Please supply the desired plugin name in snake_case, e.g.'
puts ''
puts ' rename.rb my_awesome_plugin'
puts ''
exit 0
end
usage if ARGV.size != 1
snake = ARGV[0]
camel = snake.camel_case
if snake == camel
puts "Could not camelize '#{snake}' - exiting"
exit 1
end
old_dirs = []
Find.find('.') do |path|
next unless File.file?(path)
next if path =~ /\.git/
next if path == './rename.rb'
# Change content on all files
tmp_file = "#{path}.tmp"
system(%(sed 's/foreman_plugin_template/#{snake}/g' #{path} > #{tmp_file}))
system(%(sed 's/ForemanPluginTemplate/#{camel}/g' #{tmp_file} > #{path}))
system(%(rm #{tmp_file}))
end
Find.find('.') do |path|
# Change all the paths to the new snake_case name
if path =~ /foreman_plugin_template/i
new = path.gsub('foreman_plugin_template', snake)
# Recursively copy the directory and store the original for deletion
# Check for $ because we don't need to copy template/hosts for example
if File.directory?(path) && path =~ /foreman_plugin_template$/i
FileUtils.cp_r(path, new)
old_dirs << path
else
# gsub replaces all instances, so it will work on the new directories
FileUtils.mv(path, new)
end
end
end
# Clean up
FileUtils.rm_rf(old_dirs)
FileUtils.mv('README.plugin.md', 'README.md')
puts 'All done!'
puts "Add this to Foreman's bundler configuration:"
puts ''
puts " gem '#{snake}', :path => '#{Dir.pwd}'"
puts ''
puts 'Happy hacking!'