Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ jobs:
rm -rf branch-test
git config --global init.defaultBranch develop
mkdir branch-test && cd branch-test && git init
touch .styleguide && git add .styleguide && git commit -q -m "Initial commit"
touch .wpiformat && git add .wpiformat && git commit -q -m "Initial commit"
if wpiformat; then
exit 1
fi
Expand All @@ -115,7 +115,7 @@ jobs:
rm -rf branch-test
git config --global init.defaultBranch develop
mkdir branch-test && cd branch-test && git init
touch .styleguide && git add .styleguide && git commit -q -m "Initial commit"
touch .wpiformat && git add .wpiformat && git commit -q -m "Initial commit"
wpiformat -default-branch develop

# Verify wpiformat reports success if main branch exists
Expand All @@ -125,7 +125,7 @@ jobs:
rm -rf branch-test
git config --global init.defaultBranch main
mkdir branch-test && cd branch-test && git init
touch .styleguide && git add .styleguide && git commit -q -m "Initial commit"
touch .wpiformat && git add .wpiformat && git commit -q -m "Initial commit"
wpiformat

# Verify wpiformat reports success if master branch exists
Expand All @@ -135,7 +135,7 @@ jobs:
rm -rf branch-test
git config --global init.defaultBranch master
mkdir branch-test && cd branch-test && git init
touch .styleguide && git add .styleguide && git commit -q -m "Initial commit"
touch .wpiformat && git add .wpiformat && git commit -q -m "Initial commit"
wpiformat

- name: Delete branch-test folder
Expand Down
File renamed without changes.
File renamed without changes.
16 changes: 8 additions & 8 deletions wpiformat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ pip install wpiformat

## Project Setup

To use these tools with a new project, copy `.styleguide`, and `.styleguide-license` from the examples folder into the project and create a new `.clang-format` file based on the desired C/C++ style.
To use these tools with a new project, copy `.wpiformat`, and `.wpiformat-license` from the examples folder into the project and create a new `.clang-format` file based on the desired C/C++ style.

Note: Since wpiformat already handles include ordering, it is recommended to use `SortIncludes: false` in `.clang-format`.

## .styleguide
## .wpiformat

wpiformat checks the current directory for the `.styleguide` file. If one doesn't exist, all parent directories are tried as well. This file contains groups of filename regular expressions.
wpiformat checks the current directory for the `.wpiformat` file. If one doesn't exist, all parent directories are tried as well. This file contains groups of filename regular expressions.
```
groupName {
regex_here
Expand All @@ -36,7 +36,7 @@ The regexes are matched using [re.search()](https://docs.python.org/3/library/re

Empty config groups can be omitted. Directory separators must be "/", not "\\". During processing, they will be replaced internally with an os.sep that is automatically escaped for regexes.

See the `.styleguide` file in the docs/examples directory for all possible groups.
See the `.wpiformat` file in the docs/examples directory for all possible groups.

### Specifying C/C++ files to format

Expand Down Expand Up @@ -73,17 +73,17 @@ For example, given a file at `allwpilib/src/main/native/include/wpiutil/support/

The `repoRootNameOverride` group allows one to override the repository name used in include guards. This is useful for giving subprojects within one repository different repository roots in their include guards. Only specify one name in this group because subsequent names will be ignored.

## .styleguide-license
## .wpiformat-license

This file contains the license header template. It should contain `Copyright (c)` followed by the company name and the string `{year}`. See the `.styleguide-license` file in the docs/examples directory.
This file contains the license header template. It should contain `Copyright (c)` followed by the company name and the string `{year}`. See the `.wpiformat-license` file in the docs/examples directory.

wpiformat checks the currently processed file's directory for a `.styleguide` file first and traverses up the directory tree if one isn't found. This allows templates which are closer to the processed file to override a project's main template.
wpiformat checks the currently processed file's directory for a `.wpiformat` file first and traverses up the directory tree if one isn't found. This allows templates which are closer to the processed file to override a project's main template.

### License header semantics

The license header is always at the beginning of the file and ends after two newlines. If there isn't one, or it doesn't contain the required copyright contents, wpiformat inserts a new one containing the current year.

### `.styleguide-license` special variables
### `.wpiformat-license` special variables

`{year}` is replaced with a year range from the earliest copyright year in the file to the current year. If the earliest year is the current year, only that year will be written.

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion wpiformat/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def test_config():
config_file = Config(os.path.abspath(os.getcwd()), ".styleguide")
config_file = Config(os.path.abspath(os.getcwd()), ".wpiformat")
assert config_file.is_modifiable_file(
"." + os.sep + "wpiformat" + os.sep + "javaguidelink.png"
)
Expand Down
10 changes: 5 additions & 5 deletions wpiformat/test/test_licenseupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,20 @@ def test_licenseupdate():
)

# Ensure excluded files won't be processed
config_file = Config(os.path.abspath(os.getcwd()), ".styleguide")
config_file = Config(os.path.abspath(os.getcwd()), ".wpiformat")
assert not LicenseUpdate().should_process_file(config_file, "./Excluded.h")

# Create git repo to test license years for commits
with OpenTemporaryDirectory():
subprocess.run(["git", "init", "-q"])

# Add base files
with open(".styleguide-license", "w") as file:
with open(".wpiformat-license", "w") as file:
file.write("// Copyright (c) {year}")
with open(".styleguide", "w") as file:
with open(".wpiformat", "w") as file:
file.write("cppSrcFileInclude {\n" + r"\.cpp$")
subprocess.run(["git", "add", ".styleguide-license"])
subprocess.run(["git", "add", ".styleguide"])
subprocess.run(["git", "add", ".wpiformat-license"])
subprocess.run(["git", "add", ".wpiformat"])
subprocess.run(["git", "commit", "-q", "-m", '"Initial commit"'])

# Add file with commit date of last year and range through this year
Expand Down
4 changes: 2 additions & 2 deletions wpiformat/test/test_tasktest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def run_and_check_file(
input_contents = input_contents.replace("\n", os.linesep)
expected_output_contents = expected_output_contents.replace("\n", os.linesep)

config_file = Config(os.path.abspath(os.getcwd()), ".styleguide")
config_file = Config(os.path.abspath(os.getcwd()), ".wpiformat")

if task.should_process_file(config_file, filename):
output, success = task.run_pipeline(config_file, filename, input_contents)
Expand Down Expand Up @@ -59,7 +59,7 @@ def run_and_check_stdout(
expected_output_contents -- expected output file contents
expected_success -- whether run is expected to succeed
"""
config_file = Config(os.path.abspath(os.getcwd()), ".styleguide")
config_file = Config(os.path.abspath(os.getcwd()), ".wpiformat")

if task.should_process_file(config_file, filename):
with redirect_stdout(io.StringIO()) as f:
Expand Down
27 changes: 23 additions & 4 deletions wpiformat/wpiformat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ def proc_pipeline(filename: str) -> bool:
Keyword arguments:
filename -- filename
"""
config_file = Config(os.path.dirname(filename), ".styleguide")
# TODO: Remove handling for deprecated .styleguide file
try:
config_file = Config(os.path.dirname(filename), ".wpiformat")
except OSError:
config_file = Config(os.path.dirname(filename), ".styleguide")

if verbose1 or verbose2:
with print_lock:
print("Processing", filename)
Expand Down Expand Up @@ -130,7 +135,12 @@ def proc_standalone(filename: str) -> bool:
Keyword arguments:
filename -- filename
"""
config_file = Config(os.path.dirname(filename), ".styleguide")
# TODO: Remove handling for deprecated .styleguide file
try:
config_file = Config(os.path.dirname(filename), ".wpiformat")
except OSError:
config_file = Config(os.path.dirname(filename), ".styleguide")

if verbose2:
with print_lock:
print("Processing", filename)
Expand Down Expand Up @@ -180,7 +190,12 @@ def proc_batch(filenames: list[str]) -> bool:
for subtask in task_pipeline:
work = []
for filename in filenames:
config_file = Config(os.path.dirname(filename), ".styleguide")
# TODO: Remove handling for deprecated .styleguide file
try:
config_file = Config(os.path.dirname(filename), ".wpiformat")
except OSError:
config_file = Config(os.path.dirname(filename), ".styleguide")

if subtask.should_process_file(config_file, filename):
work.append(filename)

Expand Down Expand Up @@ -477,7 +492,11 @@ def main():
# Don't run tasks on modifiable or generated files
work = []
for name in files:
config_file = Config(os.path.dirname(name), ".styleguide")
# TODO: Remove handling for deprecated .styleguide file
try:
config_file = Config(os.path.dirname(name), ".wpiformat")
except OSError:
config_file = Config(os.path.dirname(name), ".styleguide")

if config_file.is_modifiable_file(name):
continue
Expand Down
7 changes: 7 additions & 0 deletions wpiformat/wpiformat/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def read_file(directory: str, filename: str) -> tuple[str, list[str]]:
with open(filepath, "r") as file_contents:
contents = file_contents.read().splitlines()
Config.__config_cache[filepath] = contents

# TODO: Remove deprecation message
if filename.startswith(".styleguide"):
print(
f"warning: rename deprecated {filepath} to {filepath.replace('styleguide', 'wpiformat')}"
)

return filepath, contents
except OSError as e:
# .git files are ignored, which are created within submodules
Expand Down
12 changes: 9 additions & 3 deletions wpiformat/wpiformat/licenseupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,15 @@ def run_pipeline(
) -> tuple[str, bool]:
linesep = super().get_linesep(lines)

_, license_template = Config.read_file(
os.path.dirname(os.path.abspath(filename)), ".styleguide-license"
)
# TODO: Remove handling for deprecated .styleguide-license file
try:
_, license_template = Config.read_file(
os.path.dirname(os.path.abspath(filename)), ".wpiformat-license"
)
except OSError:
_, license_template = Config.read_file(
os.path.dirname(os.path.abspath(filename)), ".styleguide-license"
)

# Get year when file was most recently modified in Git history
#
Expand Down