Skip to content

Commit 5cc1807

Browse files
committed
Add a workflow to check file formatting.
Perltidy is used for Perl files, and prettier for javascript, css, html, yml, and md files. The files are not formatted yet, this commit just adds the infrastructure.
1 parent fe40758 commit 5cc1807

File tree

8 files changed

+222
-7
lines changed

8 files changed

+222
-7
lines changed

.editorconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
max_line_length = 120
8+
trim_trailing_whitespace = true
9+
indent_style = tab
10+
indent_size = 4
11+
12+
[*.{yml,md}]
13+
indent_style = space
14+
indent_size = 2
15+
16+
[*.pg]
17+
trim_trailing_whitespace = false
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
name: Check Formatting of Code Base
3+
4+
defaults:
5+
run:
6+
shell: bash
7+
8+
on:
9+
push:
10+
branches-ignore: [main, develop]
11+
pull_request:
12+
13+
jobs:
14+
perltidy:
15+
name: Check Perl file formatting with perltidy
16+
runs-on: ubuntu-24.04
17+
container:
18+
image: perl:5.38
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v4
22+
- name: Install dependencies
23+
run: cpanm -n Perl::Tidy@20240903
24+
- name: Run perltidy
25+
shell: bash
26+
run: |
27+
git config --global --add safe.directory "$GITHUB_WORKSPACE"
28+
shopt -s extglob globstar nullglob
29+
perltidy --pro=./.perltidyrc -b -bext='/' ./**/*.p[lm] ./**/*.t && git diff --exit-code
30+
31+
prettier:
32+
name: Check JavaScript, style, and HTML file formatting with prettier
33+
runs-on: ubuntu-24.04
34+
steps:
35+
- name: Checkout code
36+
uses: actions/checkout@v4
37+
- name: Install Node
38+
uses: actions/setup-node@v4
39+
with:
40+
node-version: '20'
41+
- name: Install Dependencies
42+
run: cd public && npm ci --ignore-scripts
43+
- name: Check formatting with prettier
44+
run: cd public && npm run prettier-check

.gitignore

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
*.DS_Store
22
render_app.conf
33
lib/.pls-tmp-*
4-
lib/WeBWorK/htdocs/tmp/renderer/gif/*
5-
lib/WeBWorK/htdocs/tmp/renderer/images/*
6-
lib/WeBWorK/htdocs/DATA/*.json
7-
lib/WeBWorK/bin/*
8-
webwork-open-problem-library/
9-
private/
4+
webwork-open-problem-library
5+
private
106
tmp/*
117
!tmp/.gitkeep
128
logs/*

.perltidyrc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# PBP .perltidyrc file
2+
-l=120 # Max line width is 120 cols
3+
-et=4 # Use tabs instead of spaces.
4+
-i=4 # Indent level is 4 cols
5+
-ci=4 # Continuation indent is 4 cols
6+
-b # Write the file inline and create a .bak file
7+
-vt=0 # Minimal vertical tightness
8+
-cti=0 # No extra indentation for closing brackets
9+
-pt=2 # Maximum parenthesis tightness
10+
-bt=1 # Medium brace tightness
11+
-sbt=1 # Medium square bracket tightness
12+
-bbt=1 # Medium block brace tightness
13+
-nsfs # No space before semicolons
14+
-nolq # Don't outdent long quoted strings
15+
-mbl=1 # Do not allow multiple empty lines
16+
-ce # Cuddled else
17+
-cb # Cuddled blocks
18+
-nbbc # Do not add blank lines before full length comments
19+
-nbot # No line break on ternary
20+
-nlop # No logical padding (this causes mixed tabs and spaces)
21+
-wn # Weld nested containers
22+
-xci # Extended continuation indentation

.prettierrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"arrowParens": "always",
3+
"bracketSpacing": true,
4+
"printWidth": 120,
5+
"semi": true,
6+
"singleQuote": true,
7+
"trailingComma": "none"
8+
}

bin/run-perltidy.pl

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env perl
2+
3+
=head1 NAME
4+
5+
run-perltidy.pl -- Run perltidy on the renderer source files.
6+
7+
=head1 SYNOPSIS
8+
9+
run-perltidy.pl [options] file1 file2 ...
10+
11+
=head1 DESCRIPTION
12+
13+
Run perltidy on the renderer source files.
14+
15+
=head1 OPTIONS
16+
17+
For this script to work the .perltidyrc file in the renderer root directory
18+
must be readable. Note that the renderer root directory is automatically
19+
detected.
20+
21+
This script accepts all of the options that are accepted by perltidy. See the
22+
perltidy documentation for details.
23+
24+
However, the C<-pro> option is not allowed. This script will use the
25+
.perltidyrc file in the renderer root directory for this option instead.
26+
27+
In addition the default value of C<-bext> for this script is C<'/'>, which means
28+
that backup files will be created with the C<.bak> extension, and will be
29+
deleted if there are no errors. Note that this behavior may be changed by
30+
passing a different value for the C<-bext> option.
31+
32+
Note that the C<-v> flag makes this script verbose, and does not output the
33+
perltidy version as it would usually do for perltidy.
34+
35+
Finally, if no files are passed on the command line, then perltidy will be
36+
executed on all files with the extensions C<.pl>, C<.pm>, or C<.t> in the
37+
renderer directory. If files are passed on the command line, then perltidy
38+
will only be executed on the listed files.
39+
40+
=cut
41+
42+
use strict;
43+
use warnings;
44+
use feature 'say';
45+
46+
use Perl::Tidy;
47+
use File::Find qw(find);
48+
use Mojo::File qw(curfile);
49+
50+
my $renderer_root = curfile->dirname->dirname;
51+
52+
die "Version 20240903 of perltidy is required for this script.\nThe installed version is $Perl::Tidy::VERSION.\n"
53+
unless $Perl::Tidy::VERSION == 20240903;
54+
die "The .perltidyrc file in the renderer root directory is not readable.\n"
55+
unless -r "$renderer_root/.perltidyrc";
56+
57+
my $verbose = 0;
58+
my (@args, @files);
59+
for (@ARGV) {
60+
if ($_ eq '-v') { $verbose = 1 }
61+
elsif ($_ =~ /^-/) { push(@args, $_) }
62+
else { push(@files, $_) }
63+
}
64+
65+
# Validate options that were passed.
66+
my %options;
67+
my $err = Perl::Tidy::perltidy(argv => \@args, dump_options => \%options);
68+
exit $err if $err;
69+
die "The -pro option is not suppored by this script.\n" if defined $options{profile};
70+
71+
unshift(@args, '-bext=/') unless defined $options{'backup-file-extension'};
72+
73+
if (@files) {
74+
for (@files) {
75+
push(@args, $_);
76+
say "Tidying file: $_" if $verbose;
77+
Perl::Tidy::perltidy(argv => \@args, perltidyrc => "$renderer_root/.perltidyrc");
78+
pop(@args);
79+
}
80+
} else {
81+
find(
82+
{
83+
wanted => sub {
84+
my $path = $File::Find::name;
85+
my $dir = $File::Find::dir;
86+
my ($name) = $path =~ m|^$dir(?:/(.*))?$|;
87+
$name = '' unless defined $name;
88+
89+
if (-d $path && $name =~ /^(\.git|\.github|htdocs|\.vscode|PG)$/) {
90+
$File::Find::prune = 1;
91+
return;
92+
}
93+
94+
return unless $path =~ /\.p[lm]$/ || $path =~ /\.t$/;
95+
96+
say "Tidying file: $path" if $verbose;
97+
98+
push(@args, $path);
99+
Perl::Tidy::perltidy(argv => \@args, perltidyrc => "$renderer_root/.perltidyrc");
100+
pop(@args);
101+
},
102+
no_chdir => 1
103+
},
104+
$renderer_root
105+
);
106+
}
107+
108+
1;

public/package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"license": "GPL-2.0+",
55
"scripts": {
66
"generate-assets": "node generate-assets",
7-
"prepare": "npm run generate-assets"
7+
"prepare": "npm run generate-assets",
8+
"prettier-format": "prettier --ignore-path=../.gitignore --write \"**/*.{js,css,scss,html}\" \"../**/*.yml\"",
9+
"prettier-check": "prettier --ignore-path=../.gitignore --check \"**/*.{js,css,scss,html}\" \"../**/*.yml\""
810
},
911
"repository": {
1012
"type": "git",
@@ -24,6 +26,7 @@
2426
"chokidar": "^3.5.3",
2527
"cssnano": "^6.0.0",
2628
"postcss": "^8.4.21",
29+
"prettier": "^3.5.3",
2730
"rtlcss": "^4.0.0",
2831
"sass": "^1.57.1",
2932
"terser": "^5.16.1",

0 commit comments

Comments
 (0)