Skip to content

Add clang-format cmake targets and formatting script#81

Closed
th1000s wants to merge 1 commit intoDescentDevelopers:mainfrom
th1000s:fmt
Closed

Add clang-format cmake targets and formatting script#81
th1000s wants to merge 1 commit intoDescentDevelopers:mainfrom
th1000s:fmt

Conversation

@th1000s
Copy link
Contributor

@th1000s th1000s commented Apr 18, 2024

fmt: formats all modified files (what git status lists)
fmtall: all files known to git
checkfmt: checks that all files are formatted correctly

Warn if the version does not match the one specified by the GitHub action.


(the removed version looked at every file, and also truncated them because of $file > $file)

fmt: formats all modified files (what `git status` lists)
fmtall: all files known to git
checkfmt: checks that all files are formatted correctly

Warn if the version does not match the one specified by
the GitHub action.
@Arcnor
Copy link
Contributor

Arcnor commented Apr 19, 2024

The problem is that the formatter causes issues (there was a PR to fix invalid code generated by the formatter) so I think this should be rethought a bit, maybe only apply to newly added lines or similar...

@Arcnor Arcnor closed this Apr 21, 2024
Jayman2000 added a commit to Jayman2000/Descent3-pr that referenced this pull request Jun 30, 2024
This new script has several advantages over the old one. First, it’s
directly executable. The previous script did not use a shebang, so users
would have to invoke “sh tools/formatter.sh”. This new script does
include a shebang, so users can just run “tools/formatter.sh”.

Second, it’s available under the same license as the rest of the repo.
The previous version of the script was released under the MIT License,
but since it’s a part of a larger GPL’d work, users would potentially
have to comply with two licenses at the same time. This new script is
available under the same license as the rest of the repo in order to
make the licensing situation simpler.

Third, it’s a little bit easier to read. The old script put globs for
every possible file extension on a single line, and escaped them in an
unusual way. Additionally, that line in the previous script contained
more than just globs. It also contained the find command and the start
of a for loop. This new script stores the globs in a variable in order
to make the code easier to read by decreasing the amount of stuff that’s
happening on a single line. The new script also makes things easier to
read by using single quotes to escape globs instead multiple
backslashes. It also makes things easier to read by decreasing the total
number of globs (See the final point for details).

Fourth, this new script is more lenient when it comes to the user’s
current working directory. The previous script would only work properly
if the user’s current working directory was the root directory of the
Descent3 repo. This new script will work properly if the user’s current
working directory is any directory that’s part of the Descent3 repo.

Fifth, this new script can handle more filenames. The previous script
would fail if characters like spaces appeared in the names of files.
This new script will work even if filenames contain spaces, newlines,
etc. There’s no files in the Descent3 repo that currently use those
characters in their names, but it’s possible that there will be in the
future.

Sixth, this new script will ignore the same files that Git ignores. The
previous script would process all files, even if they were ignored by
Git.

Seventh, this new script has slightly better code style. The previous
script used tabs for indentation. This new script uses two spaces for
indentation, just like .editorconfig says we should use. The previous
script used semicolons at the end of lines. This new version doesn’t
because semicolons are unnecessary in shell scripts.

Eighth, this new script avoids truncating files. Apparently, part of the
old script was truncating files [1].

Ninth, this new script is agnostic to the version of clang-format that’s
installed on your system. The previous script required clang-format-16
in particular.

Tenth, this new script respects the style settings in .clang-format. The
previous script used “--style=LLVM” which prevents .clang-format from
being loaded [2].

Finally, this new script is faster than the old one. For one thing, this
new script doesn’t waste time looking for files that don’t exist. For
example, the previous script would look for .java files because
clang-format can format code written in Java [2]. This new script
doesn’t bother looking for .java files because there aren’t any in the
Descent3 repo. More importantly, the old script would run clang-format
serially. This new script runs many instances of clang-format in
parallel.

There is a potential disadvantage to this script. It adds additional
dependencies. Specifically, this new script depends on /bin/sh and git.
That being said, users are highly likely to have both of those installed
anyway, so it’s probably not a disadvantage in practices. At the same
time, this new script also removes a dependency (the find command), so
the upsides of this change almost certainly outweigh the downsides.

[1]: <DescentDevelopers#81>
[2]: <https://clang.llvm.org/docs/ClangFormat.html#standalone-tool>

TODO:
• Proofread commit message.
• Test on Linux.
• Test on Windows.
Jayman2000 added a commit to Jayman2000/Descent3-pr that referenced this pull request Jun 30, 2024
This new script has several advantages over the old one. First, it’s
directly executable. The previous script did not use a shebang, so users
would have to invoke “sh tools/formatter.sh”. This new script does
include a shebang, so users can just run “tools/formatter.sh”.

Second, it’s available under the same license as the rest of the repo.
The previous version of the script was released under the MIT License,
but since it’s a part of a larger GPL’d work, users would potentially
have to comply with two licenses at the same time. This new script is
available under the same license as the rest of the repo in order to
make the licensing situation simpler.

Third, it’s a little bit easier to read. The old script put globs for
every possible file extension on a single line, and escaped them in an
unusual way. Additionally, that line in the previous script contained
more than just globs. It also contained the find command and the start
of a for loop. This new script stores the globs in a variable in order
to make the code easier to read by decreasing the amount of stuff that’s
happening on a single line. The new script also makes things easier to
read by using single quotes to escape globs instead multiple
backslashes. It also makes things easier to read by decreasing the total
number of globs (See the final point for details).

Fourth, this new script is more lenient when it comes to the user’s
current working directory. The previous script would only work properly
if the user’s current working directory was the root directory of the
Descent3 repo. This new script will work properly if the user’s current
working directory is any directory that’s part of the Descent3 repo.

Fifth, this new script can handle more filenames. The previous script
would fail if characters like spaces appeared in the names of files.
This new script will work even if filenames contain spaces, newlines,
etc. There’s no files in the Descent3 repo that currently use those
characters in their names, but it’s possible that there will be in the
future.

Sixth, this new script will ignore the same files that Git ignores. The
previous script would process all files, even if they were ignored by
Git.

Seventh, this new script has slightly better code style. The previous
script used tabs for indentation. This new script uses two spaces for
indentation, just like .editorconfig says we should use. The previous
script used semicolons at the end of lines. This new version doesn’t
because semicolons are unnecessary in shell scripts.

Eighth, this new script avoids truncating files. Apparently, part of the
old script was truncating files [1].

Ninth, this new script is agnostic to the version of clang-format that’s
installed on your system. The previous script required clang-format-16
in particular.

Tenth, this new script respects the style settings in .clang-format. The
previous script used “--style=LLVM” which prevents .clang-format from
being loaded [2].

Finally, this new script is faster than the old one. For one thing, this
new script doesn’t waste time looking for files that don’t exist. For
example, the previous script would look for .java files because
clang-format can format code written in Java [2]. This new script
doesn’t bother looking for .java files because there aren’t any in the
Descent3 repo. More importantly, the old script would run clang-format
serially. This new script runs many instances of clang-format in
parallel.

There is a potential disadvantage to this script. It adds additional
dependencies. Specifically, this new script depends on /bin/sh and git.
That being said, users are highly likely to have both of those installed
anyway, so it’s probably not a disadvantage in practices. At the same
time, this new script also removes a dependency (the find command), so
the upsides of this change almost certainly outweigh the downsides.

[1]: <DescentDevelopers#81>
[2]: <https://clang.llvm.org/docs/ClangFormat.html#standalone-tool>

TODO:
• Proofread commit message.
• Test on Windows.
Jayman2000 added a commit to Jayman2000/Descent3-pr that referenced this pull request Jul 2, 2024
This new script has several advantages over the old one. First, it’s
directly executable. The previous script did not use a shebang, so users
would have to invoke “sh tools/formatter.sh”. This new script does
include a shebang, so users can just run “tools/formatter.sh”.

Second, it’s available under the same license as the rest of the repo.
The previous version of the script was released under the MIT License,
but since it’s a part of a larger GPL’d work, users would potentially
have to comply with two licenses at the same time. This new script is
available under the same license as the rest of the repo in order to
make the licensing situation simpler.

Third, it’s a little bit easier to read. The old script put globs for
every possible file extension on a single line, and escaped them in an
unusual way. Additionally, that line in the previous script contained
more than just globs. It also contained the find command and the start
of a for loop. This new script stores the globs in a variable in order
to make the code easier to read by decreasing the amount of stuff that’s
happening on a single line. The new script also makes things easier to
read by using single quotes to escape globs instead multiple
backslashes. It also makes things easier to read by decreasing the total
number of globs (See the final point for details).

Fourth, this new script is more lenient when it comes to the user’s
current working directory. The previous script would only work properly
if the user’s current working directory was the root directory of the
Descent3 repo. This new script will work properly if the user’s current
working directory is any directory that’s part of the Descent3 repo.

Fifth, this new script can handle more filenames. The previous script
would fail if characters like spaces appeared in the names of files.
This new script will work even if filenames contain spaces, newlines,
etc. There’s no files in the Descent3 repo that currently use those
characters in their names, but it’s possible that there will be in the
future.

Sixth, this new script will ignore the same files that Git ignores. The
previous script would process all files, even if they were ignored by
Git.

Seventh, this new script has slightly better code style. The previous
script used tabs for indentation. This new script uses two spaces for
indentation, just like .editorconfig says we should use. The previous
script used semicolons at the end of lines. This new version doesn’t
because semicolons are unnecessary in shell scripts.

Eighth, this new script avoids truncating files. Apparently, part of the
old script was truncating files [1].

Ninth, this new script is agnostic to the version of clang-format that’s
installed on your system. The previous script required clang-format-16
in particular.

Tenth, this new script respects the style settings in .clang-format. The
previous script used “--style=LLVM” which prevents .clang-format from
being loaded [2].

Finally, this new script is faster than the old one. For one thing, this
new script doesn’t waste time looking for files that don’t exist. For
example, the previous script would look for .java files because
clang-format can format code written in Java [2]. This new script
doesn’t bother looking for .java files because there aren’t any in the
Descent3 repo. More importantly, the old script would run clang-format
serially. This new script runs many instances of clang-format in
parallel.

There is a potential disadvantage to this script. It adds additional
dependencies. Specifically, this new script depends on /bin/sh and git.
That being said, users are highly likely to have both of those installed
anyway, so it’s probably not a disadvantage in practices. At the same
time, this new script also removes a dependency (the find command), so
the upsides of this change almost certainly outweigh the downsides.

[1]: <DescentDevelopers#81>
[2]: <https://clang.llvm.org/docs/ClangFormat.html#standalone-tool>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants