-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexport.rb
executable file
·83 lines (71 loc) · 2 KB
/
export.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
#!/usr/bin/env ruby
require 'fileutils'
def usage
puts "usage: ./export.rb version dir"
exit 1
end
usage if ARGV.size != 2
HOME = File.dirname(__FILE__)
FILES = ['CMakeLists.txt', 'LICENSE', 'local', 'src', 'README.md', 'tests',
'.gitignore', 'assignments']
IGNORE = [/.*swp/]
EXPORT_DIR = ARGV[1]
VERSION = Integer(ARGV[0])
if Dir.exists? EXPORT_DIR
puts "#{EXPORT_DIR} already exists. Overwriting? (y/n)"
res = STDIN.gets.strip
unless res == "y"
puts "abort"
exit 1
end
FILES.each do |f|
trg = File.join(EXPORT_DIR, f)
FileUtils.rm_rf(trg)
end
end
FileUtils.mkdir_p(EXPORT_DIR)
def crawl(f)
src = File.join(HOME, f)
trg = File.join(EXPORT_DIR, f)
IGNORE.each do |i|
return if i =~ src
end
begin
if File.directory?(src)
FileUtils.mkdir_p(trg)
Dir.foreach(src) do |g|
next if g == '.' or g == '..'
crawl(File.join(f,g))
end
else
output = ""
print = [true]
File.readlines(src).each do |line|
if line =~ /#if (VERSION (<=|>=|<|>|==) \d+)/
print << (eval($1) && print.last)
elsif line =~ /#endif .*VERSION/
if (print.size == 1)
puts "Unbalanced endif in #{src}"
exit 1
end
print.pop
else
output << line if print.last
end
end
if (print.size != 1)
puts "Unclosed endif in #{src}"
exit 1
end
File.open(trg, 'w') { |file| file.write(output) } if output != ""
end
rescue
puts "Error in File #{f}"
exit 1
end
end
FILES.each do |f|
puts "Cannot export #{f}: does not exist" unless File.exists? f
crawl f
end
File.open(File.join(EXPORT_DIR, '.version'), 'w') { |file| file.write(VERSION) }