Skip to content

Commit 5772028

Browse files
committed
chore: Add Ruby and C code formatting checks
1 parent 16773a2 commit 5772028

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

rakelib/format.rake

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# frozen_string_literal: true
2+
3+
require 'rake/clean'
4+
5+
begin
6+
require 'rubocop/rake_task'
7+
8+
module AstyleHelper
9+
class << self
10+
def run(files)
11+
assert
12+
command = ['astyle', args, files].flatten.shelljoin
13+
system(command)
14+
end
15+
16+
def assert
17+
require 'mkmf'
18+
find_executable0('astyle') || raise("Could not find command 'astyle'")
19+
end
20+
21+
def args
22+
indentation_args + bracket_args + space_args + pointer_args + function_args + limit_args + quiet_args
23+
end
24+
25+
def indentation_args
26+
[
27+
'--indent=spaces=4',
28+
'--indent-switches'
29+
]
30+
end
31+
32+
def bracket_args
33+
[
34+
'--style=1tbs',
35+
'--style=allman',
36+
'--keep-one-line-blocks'
37+
]
38+
end
39+
40+
def space_args
41+
[
42+
'--unpad-paren',
43+
'--pad-header',
44+
'--pad-oper',
45+
'--pad-comma'
46+
]
47+
end
48+
49+
def pointer_args
50+
[
51+
'--align-pointer=name'
52+
]
53+
end
54+
55+
def function_args
56+
[
57+
'--attach-return-type',
58+
'--attach-return-type-decl'
59+
]
60+
end
61+
62+
def limit_args
63+
[
64+
'--max-code-length=120'
65+
]
66+
end
67+
68+
def quiet_args
69+
[
70+
'--formatted',
71+
'--verbose'
72+
]
73+
end
74+
75+
def c_files
76+
Dir.glob('ext/chdb/**/*.{c,h}')
77+
end
78+
end
79+
end
80+
81+
namespace 'format' do
82+
desc 'Format C code'
83+
task 'c' do
84+
puts 'Running astyle on C files ...'
85+
AstyleHelper.run(AstyleHelper.c_files)
86+
end
87+
88+
CLEAN.add(AstyleHelper.c_files.map { |f| "#{f}.orig" })
89+
90+
desc 'Format Ruby code'
91+
task 'ruby' => 'rubocop:autocorrect'
92+
end
93+
94+
RuboCop::RakeTask.new
95+
96+
task 'format' => ['format:c', 'format:ruby']
97+
rescue LoadError => e
98+
puts "NOTE: Rubocop is not available in this environment: #{e.message}"
99+
end

0 commit comments

Comments
 (0)