Debian Codegraph @

bin/sow

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/env ruby -w
2
3 require "optparse"
4 require "rubygems"
5 require "hoe"
6 require "fileutils"
7 require "erb"
8
9 XIF = "FI" + "X" # prevents extra hits on my TAG reporter
10
11 option = {
12 :style => "default",
13 :subdir => nil,
14 }
15
16 def check_subdir option
17 return unless option[:subdir]
18
19 warn "ERROR: you can't specify multiple subdirs"
20 abort opts.to_s
21 end
22
23 def make_sub_modules klass
24 last = nil
25 result = []
26
27 klass.split("::")[0..-2].each do |part|
28 last = [last, part].compact.join("::")
29 result << "module #{last}; end\n"
30 end
31 result << "\n" unless result.empty?
32 result.join
33 end
34
35 op = OptionParser.new do |opts|
36 opts.banner = "Usage: sow [options] project_name"
37
38 opts.separator "Standard options:"
39
40 opts.on("-t", "--trunk", "Add project to subdir under 'trunk'.") do
41 check_subdir option
42 option[:subdir] = "trunk"
43 end
44
45 opts.on("-d", "--dev", "Add project to subdir under 'dev'.") do
46 check_subdir option
47 option[:subdir] = "dev"
48 end
49
50 opts.on "-C dir", String, "Change directory to dir before creating project." do |d|
51 Dir.chdir d
52 end
53
54 opts.on("-s style", "--style style", String, "Use template <style>.") do |s|
55 option[:style] = s
56 end
57
58 opts.on("-h", "--help", "Show this message.") do
59 puts opts
60 exit
61 end
62 end
63
64 op.parse!
65
66 include FileUtils::Verbose
67
68 # variables for erb:
69 template_dir = File.expand_path("~/.hoe_template")
70 template_path = File.join template_dir, option[:style]
71 source_path = File.join(File.dirname(File.dirname(__FILE__)), "template")
72 default_dir = File.join template_dir, "default"
73
74 if File.directory? template_dir and not File.directory? default_dir then
75 warn "Detected old #{template_dir}"
76 warn "Moving to #{default_dir}"
77 tmp = "#{template_dir}.#{$$}"
78 File.rename template_dir, tmp
79 FileUtils.mkdir template_dir
80 File.rename tmp, default_dir
81 end
82
83 unless File.directory? template_path then
84 warn "Creating missing #{option[:style]} template."
85 FileUtils.mkdir_p File.dirname(template_path)
86 FileUtils.cp_r source_path, template_path
87 paths = (Dir["#{template_path}/**/*"] +
88 Dir["#{template_path}/**/.*"]).select { |f| File.file? f }
89 FileUtils.chmod 0644, paths
90 FileUtils.chmod 0755, paths.grep(/bin\//)
91 end
92
93 project = ARGV.shift
94
95 abort op.to_s unless project
96 abort "Project #{project} seems to exist" if File.directory? project
97
98 _, file_name, klass, test_klass = Hoe.normalize_names project
99 ext_name = File.basename file_name # minitest/plugin => plugin
100 so_name = RbConfig::CONFIG["DLEXT"]
101
102 klass = klass # quell unused warnings if they're not used in templates
103 test_klass = test_klass # ditto
104 so_name = so_name # ditto
105
106 FileUtils.cp_r template_path, project
107
108 Dir.chdir project do
109 dirs = Dir["**/*"].select { |f| File.directory? f }.sort.reverse
110 dirs.grep(/file_name$/).each do |file|
111 target = file.sub(/file_name$/, file_name)
112 FileUtils.mkdir_p File.dirname(target)
113 FileUtils.mv file, target
114 end
115
116 dirs = Dir["**/*"].select { |f| File.directory? f }.sort.reverse
117 dirs.grep(/ext_name$/).each do |file|
118 target = file.sub(/ext_name$/, ext_name)
119 FileUtils.mkdir_p File.dirname(target)
120 FileUtils.mv file, target
121 end
122
123 paths = (Dir["**/*"] + Dir["**/.*"]).select { |f| File.file? f }.sort
124 paths.each do |path|
125 file = File.read path
126
127 warn "erb: #{path}"
128
129 File.open path, "w" do |io|
130 if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
131 erb = ERB.new file, trim_mode: "<>"
132 else
133 erb = ERB.new file, nil, "<>"
134 end
135 erb.filename = path
136 io.puts erb.result(binding)
137 end
138 end
139
140 paths.grep(/(file|ext)_name|\.erb$/).each do |file|
141 new_file = file.
142 sub(/(test_)?file_name/, file_name).
143 sub(/(test_)?ext_name/, ext_name).
144 sub(/\.erb$/, "")
145
146 case file
147 when /^bin/ then
148 dir, *rest = new_file.split File::SEPARATOR
149
150 new_file = File.join dir, rest.join("_")
151 when /^test/ then
152 dir, *rest = new_file.split File::SEPARATOR
153
154 rest.last.sub!(/^/, "test_")
155
156 new_file = File.join dir, *rest
157 end
158
159 FileUtils.mkdir_p File.dirname new_file
160 FileUtils.mv file, new_file
161 end
162 end
163
164 if option[:subdir] then
165 temp_dir = "#{project}.#{$$}"
166 FileUtils.mv project, temp_dir
167 FileUtils.mkdir project
168 FileUtils.mv temp_dir, "#{project}/#{option[:subdir]}"
169 end
170
171 puts
172 puts "... done, now go fix all occurrences of '#{XIF}':"
173 if Hoe::WINDOZE then
174 puts `findstr /N /S /C:#{XIF} #{project}\\*`
175 else
176 puts `find #{project} -type f | xargs grep -n #{XIF}`.gsub(/\A|\n/, "\n ")
177 end