-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRakefile
149 lines (122 loc) · 3.11 KB
/
Rakefile
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
# encoding: UTF-8
# frozen_string_literal: true
###
# Usage:
# rake -T
# rake build -- cpp/build_tower.cc
# rake run -- cpp/build_tower.cc 33
# rake build -- java/SimplePigLatin.java
# rake run -- java/SimplePigLatin.java 'Monkey Burger'
# rake clean[t] # Dry run.
# rake clean
#
# @author Jonathan Bradley Whited
###
BUILD_DIR = 'build'
CLEAN_FILES = Rake::FileList[
"#{BUILD_DIR}/",
'**/*~','**/*.{class,o,out}',
].exclude(
'{.git,stock}/**',
)
module Codewars
@args = nil
def self.slice_argv!
return @args unless @args.nil?
i = ARGV.index('--')
if i.nil?
@args = []
else
@args = ARGV.slice!(i..-1)
@args = @args[1..-1] # Remove '--'.
end
return @args
end
def self.process_file(task_name)
args = slice_argv!
filename = args[0].to_s.strip
basename = File.basename(filename,'.*').strip
extname = File.extname(filename).strip.downcase
# Chop off the file.
args = args[1..-1] unless args.empty?
if !File.file?(filename)
abort "File not found: #{filename.inspect}"
end
cmd = yield(args,filename,basename,extname)
if !cmd
abort "Unimplemented file ext: #{filename.inspect}"
end
Rake.sh(*cmd) unless cmd.empty?
# Was this the last task?
if ARGV[-1].to_s.strip == task_name
# Keep Rake quiet.
exit
end
end
end
# Could do 'git clean -nX' instead.
# However, do NOT use '-d' which would delete 'stock/'.
desc 'Clean artifacts'
task :clean,[:dryrun?] do |t,args|
dryrun = !args.dryrun?.to_s.strip.empty?
CLEAN_FILES.each do |filename|
filename = filename.strip
if filename.empty? || File.symlink?(filename.sub(%r{[/\\]+\z},''))
puts "[SKIP] #{filename.inspect}"
next
end
if dryrun
puts "[DRY ] #{filename}"
else
if File.file?(filename)
puts "[RM ] #{filename}"
rm filename,verbose: false
elsif File.directory?(filename)
puts "[RMr ] #{filename}"
rm_r filename,verbose: false,secure: true
end
end
end
puts "\n=> DRY RUN" if dryrun
end
desc 'Build file: rake build -- <file> <...args>'
task :build do |t|
Codewars.process_file(t.name) do |args,filename,basename,extname|
mkdir(BUILD_DIR,verbose: true) unless File.directory?(BUILD_DIR)
case extname
when '.cc'
['g++','-o',File.join(BUILD_DIR,"#{basename}.o"),filename]
when '.java'
['javac','-d',BUILD_DIR,filename]
else
false
end
end
end
desc 'Run file: rake run -- <file> <...args>'
task :run do |t|
Codewars.process_file(t.name) do |args,filename,basename,extname|
case extname
when '.coffee'
['coffee',filename,*args]
when '.cc'
[File.join(BUILD_DIR,"#{basename}.o"),*args]
when '.cr'
['crystal',filename,*args]
when '.java'
['java','-cp',File.join(BUILD_DIR,''),basename,*args]
when '.js'
['node',filename,*args]
when '.kts'
['kotlinc','-script',filename,*args]
when '.php'
['php',filename,*args]
when '.py'
['python',filename,*args]
when '.rb'
['ruby',filename,*args]
else
false
end
end
end