Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
8c8497745b | |||
cc158ba3b7 |
10
README.md
10
README.md
@ -3,18 +3,10 @@
|
|||||||
## Requirements
|
## Requirements
|
||||||
- zsh
|
- zsh
|
||||||
- astyle 2.04+
|
- astyle 2.04+
|
||||||
- perl
|
|
||||||
- doxygen (optional, for checking if all code is documented)
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
```sh
|
```sh
|
||||||
cd path/with/code/
|
cd path/with/code/
|
||||||
git clone https://github.ncsu.edu/agadler/csc230-autoformat ../autoformat
|
git clone https://austenwares.com/gogs/autoformat ../autoformat
|
||||||
../autoformat/autoformat.sh
|
../autoformat/autoformat.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
## Known Issues
|
|
||||||
- Automatically formatting this doesn't work (`(true)` doesn't get placed after `while ``): ```
|
|
||||||
while
|
|
||||||
(true) {}
|
|
||||||
```
|
|
||||||
|
@ -1,13 +1,18 @@
|
|||||||
#!/bin/zsh
|
#!/bin/zsh
|
||||||
# Format all c files
|
# For support:
|
||||||
GLOB=( **/*.c )
|
# https://austenwares.com/gogs/stonewareslord/autoformat/src/java
|
||||||
|
#
|
||||||
|
# Format all java files
|
||||||
|
GLOB=( **/*.java )
|
||||||
# Astyle options:
|
# Astyle options:
|
||||||
# --mode=c : c formatting
|
# --mode=java : java formatting
|
||||||
# -xc : Brace attached to class names
|
# -xc : Brace attached to class names
|
||||||
# --style=stroustrup : stroustrup style (similar to 1tbs)
|
# --style=google : google style (similar to 1tbs)
|
||||||
# -j : Always add brackets (even on one line if statements)
|
# -j : Always add brackets (even on one line if statements)
|
||||||
|
# -z2 : Force Linux lineending
|
||||||
# -s2 : Three spaces
|
# -s2 : Three spaces
|
||||||
# -xG : Indent modifiers
|
# -xG : Indent modifiers
|
||||||
|
# -xe : Erase blank lines
|
||||||
# -S : Indent switches
|
# -S : Indent switches
|
||||||
# -K : Indent cases
|
# -K : Indent cases
|
||||||
# -N : Indent namespaces
|
# -N : Indent namespaces
|
||||||
@ -16,26 +21,10 @@ GLOB=( **/*.c )
|
|||||||
# -n : Don't make a backup
|
# -n : Don't make a backup
|
||||||
# -p : Pad operators
|
# -p : Pad operators
|
||||||
# -H : Pad header (space after if, for, while)
|
# -H : Pad header (space after if, for, while)
|
||||||
OPTS=( --mode=c -xc --style=stroustrup -j -s2 -xG -S -K -N -xn -xl -n -p -H )
|
OPTS=( --mode=java -xc --style=google -j -z2 -s2 -xG -xe -S -K -N -xn -xl -n -p -H )
|
||||||
# Process the c files by adding the comment and removing newlines
|
|
||||||
perl "${0:h}/process.pl" $GLOB
|
|
||||||
# Colorize output if you can
|
# Colorize output if you can
|
||||||
if which colout>/dev/null; then
|
if which colout>/dev/null; then
|
||||||
astyle $OPTS $GLOB|\grep -P '^(?!Unchanged)'|colout '(Formatted)' green||true
|
astyle $OPTS $GLOB|\grep -P '^(?!Unchanged)'|colout '(Formatted)' green||true
|
||||||
else
|
else
|
||||||
astyle $OPTS $GLOB|\grep -P '^(?!Unchanged)'||true
|
astyle $OPTS $GLOB|\grep -P '^(?!Unchanged)'||true
|
||||||
fi
|
fi
|
||||||
# Now check for documentation
|
|
||||||
# Run doxygen, redirecting stdout to dev null, and setting stderr to $OUTPUT
|
|
||||||
if command -v doxygen >/dev/null; then
|
|
||||||
OUTPUT=$((doxygen "${0:h}/Doxyfile" > /dev/null) 2>&1)
|
|
||||||
# If there is output, something went wrong
|
|
||||||
if [ ! -z "$OUTPUT" ]; then
|
|
||||||
printf "$OUTPUT\n" >&2
|
|
||||||
return 2
|
|
||||||
fi
|
|
||||||
#TODO: We might need this in the future, bur for now, delete html directory
|
|
||||||
rm -r html || true
|
|
||||||
else
|
|
||||||
printf "You don't have doxygen installed. Can't check documentation\n" >&2
|
|
||||||
fi
|
|
||||||
|
59
process.pl
59
process.pl
@ -1,59 +0,0 @@
|
|||||||
#!/usr/bin/perl
|
|
||||||
use strict;
|
|
||||||
use autodie;
|
|
||||||
use warnings;
|
|
||||||
use File::Compare;
|
|
||||||
use File::Basename;
|
|
||||||
no warnings 'once';
|
|
||||||
# For every argument (file)
|
|
||||||
foreach my $file (@ARGV) {
|
|
||||||
# If it exists
|
|
||||||
if (-e $file) {
|
|
||||||
# Load the autoformatted data into $data
|
|
||||||
my ($data, $old_data) = process_file($file);
|
|
||||||
# Check if original data matches new data, if it doesn't, write it
|
|
||||||
# If it matches (there were no changes made), do nothing
|
|
||||||
if (not $data eq $old_data) {
|
|
||||||
# Write out the data
|
|
||||||
open(MYOUTPUTFILE, "+>$file") or die "Error opening output file $!";
|
|
||||||
print MYOUTPUTFILE $data;
|
|
||||||
close(MYOUTPUTFILE);
|
|
||||||
# Tell the user that we changed the file
|
|
||||||
print "Processed $file\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
# Creates the automatically formatted file
|
|
||||||
sub process_file {
|
|
||||||
my($filename) = "$_[0]";
|
|
||||||
open(MYINPUTFILE, "<$filename") or die "x $!";
|
|
||||||
# The formatted file
|
|
||||||
my($data) = "";
|
|
||||||
# The original file (used for comparison to see if changes were made)
|
|
||||||
my($old_data) = "";
|
|
||||||
# Loop through every line in the file
|
|
||||||
while(<MYINPUTFILE>) {
|
|
||||||
# Add data to $old_data
|
|
||||||
$old_data .= $_;
|
|
||||||
# Get rid of trailing whitespace
|
|
||||||
s/\s*$//;
|
|
||||||
my($line) = $_;
|
|
||||||
# If it is not a whitespace only line, append it to $data
|
|
||||||
if($line !~ /^\s*$/) {
|
|
||||||
$data = $data . "$line\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
close(MYINPUTFILE);
|
|
||||||
# Get rid of final newline
|
|
||||||
chomp($data);
|
|
||||||
if ($data !~ /^\s*\/\*\*/) {
|
|
||||||
# Get the basename of the file, this prevents making the block comment say:
|
|
||||||
# @file src/folders/code.c
|
|
||||||
# And instead uses
|
|
||||||
# @file code.c
|
|
||||||
my $basename = basename($filename);
|
|
||||||
$data = "/**\n * \@file $basename\n * \@author Austen Adler (agadler)\n * TODO: Description\n */\n$data";
|
|
||||||
}
|
|
||||||
$data .= "\n";
|
|
||||||
return ($data, $old_data);
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user