-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathptidy
executable file
·157 lines (134 loc) · 2.84 KB
/
ptidy
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
150
151
152
153
154
155
156
#!/usr/bin/env perl
use strict;
use warnings;
#
# call language-appropriate tidier.
#
use File::Slurp::Tiny qw( read_file );
if ( !@ARGV ) {
die "$0 filename\n";
}
my $debug = 1;
for my $file (@ARGV) {
my $is_x = -x $file;
my $buf = read_file($file);
if ( is_php( $buf, $file ) ) {
run_php_tidy($file);
}
elsif ( is_perl( $buf, $file ) ) {
run_perl_tidy($file);
}
elsif ( is_ruby( $buf, $file ) ) {
run_ruby_tidy($file);
}
elsif ( is_js( $buf, $file ) ) {
run_js_prettier($file);
}
elsif ( is_python( $buf, $file ) ) {
run_python_prettier($file);
}
elsif (is_markdown( $buf, $file )
or is_handlebars( $buf, $file )
or is_yaml( $buf, $file ) )
{
run_markdown_prettier($file);
}
run("chmod +x $file") if $is_x;
}
sub is_yaml {
my ( $buf, $file ) = @_;
return $file =~ m/\.ya?ml$/;
}
sub is_php {
my $buf = shift;
my $file = shift;
if ( $buf =~ m/<\?php/ ) {
return 1;
}
if ( $file =~ m/\.php$/ ) {
return 1;
}
return 0;
}
sub is_perl {
my $buf = shift;
my $file = shift;
if ( $file =~ m/\.(pm|pl)$/ ) {
return 1;
}
if ( $buf =~ m!use strict;! ) {
return 1;
}
if ( $buf =~ m!/usr/bin/env perl! ) {
return 1;
}
if ( $buf =~ m!/usr/bin/perl! ) {
return 1;
}
if ( $buf =~ m/package \w+/ ) {
return 1;
}
return 0;
}
sub is_ruby {
my $buf = shift;
my $file = shift;
if ( $file =~ m/\.(rake|rb)$/ ) {
return 1;
}
if ( $buf =~ m!/bin/ruby! ) {
return 1;
}
if ( $buf =~ m!require (['"])rubygems\1! ) {
return 1;
}
}
sub is_js {
my ( $buf, $file ) = @_;
return 1 if $buf =~ /env node/;
return $file =~ /\.jsx?$/;
}
sub is_python {
my ( $buf, $file ) = @_;
return 1 if $buf =~ /env python/;
return $file =~ /\.py$/;
}
sub is_markdown {
my ( $buf, $file ) = @_;
return $file =~ /\.md$/;
}
sub is_handlebars {
my ( $buf, $file ) = @_;
return $file =~ /\.hbs$/;
}
sub run {
my $cmd = shift;
#warn $cmd if $debug;
system($cmd) and die "$cmd failed: $!";
}
sub run_perl_tidy {
my $file = shift;
run("perltidy -pbp $file > $file.tdy && mv $file.tdy $file");
}
sub run_php_tidy {
my $file = shift;
run("phptidy replace $file");
}
sub run_ruby_tidy {
my $file = shift;
run("rbeautify $file > $file.tdy && mv $file.tdy $file");
}
sub run_js_prettier {
my $file = shift;
#run("eslint --fix $file");
run("npx prettier -w --single-quote --print-width 100 $file");
run("eslint --fix $file");
}
sub run_python_prettier {
my $file = shift;
run("black $file");
}
sub run_markdown_prettier {
my $file = shift;
run("prettier -w --print-width 100 --no-config $file");
}