Skip to content

Commit d990732

Browse files
committed
Update notice script to derive copyright years from git
- Compute per-file year or year range from git history and update existing headers - Resolve repo root automatically and process Java files from project root - Stop adding armor/offhand indicators in the load-kit GUI view
1 parent 0a509ab commit d990732

3 files changed

Lines changed: 101 additions & 21 deletions

File tree

src/main/java/dev/noah/perplayerkit/gui/GUI.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ public Menu ViewPublicKitMenu(Player p, String id) {
326326
menu.getSlot(i + 9).setItem(kit[i]);
327327
}
328328

329-
setArmorAndOffhandIndicators(menu);
330329
menu.getSlot(CLEAR_SLOT).setItem(createItem(Material.APPLE, 1, "<green><b>LOAD KIT</b></green>"));
331330
menu.getSlot(BACK_SLOT).setItem(createItem(Material.OAK_DOOR, 1, "<red><b>BACK</b></red>"));
332331
addPublicKitMenu(menu.getSlot(BACK_SLOT));
6.04 KB
Binary file not shown.

tools/notice.py

Lines changed: 101 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import os
2+
import re
3+
import subprocess
4+
from datetime import datetime
25

3-
# Define the copyright notice to be added
4-
COPYRIGHT_NOTICE = """/*
5-
* Copyright 2022-2025 Noah Ross
6+
7+
NOTICE_TEMPLATE = """/*
8+
* Copyright {years} Noah Ross
69
*
710
* This file is part of PerPlayerKit.
811
*
@@ -20,33 +23,111 @@
2023
* along with PerPlayerKit. If not, see <https://www.gnu.org/licenses/>.
2124
*/"""
2225

23-
def add_copyright_to_file(file_path):
26+
COPYRIGHT_LINE_RE = re.compile(
27+
r"^ \* Copyright (?P<years>\d{4}(?:-\d{4})?) Noah Ross$",
28+
re.MULTILINE,
29+
)
30+
31+
32+
def get_repo_root(start_path):
33+
try:
34+
result = subprocess.run(
35+
["git", "rev-parse", "--show-toplevel"],
36+
cwd=start_path,
37+
capture_output=True,
38+
text=True,
39+
check=True,
40+
)
41+
except (FileNotFoundError, subprocess.CalledProcessError):
42+
return None
43+
44+
return result.stdout.strip()
45+
46+
47+
def get_copyright_years(file_path, repo_root):
48+
if repo_root is None:
49+
return str(datetime.now().year)
50+
51+
relative_path = os.path.relpath(file_path, repo_root)
52+
53+
try:
54+
result = subprocess.run(
55+
[
56+
"git",
57+
"-C",
58+
repo_root,
59+
"log",
60+
"--follow",
61+
"--format=%ad",
62+
"--date=format:%Y",
63+
"--",
64+
relative_path,
65+
],
66+
capture_output=True,
67+
text=True,
68+
check=True,
69+
)
70+
except (FileNotFoundError, subprocess.CalledProcessError, ValueError):
71+
return str(datetime.now().year)
72+
73+
years = [line.strip() for line in result.stdout.splitlines() if line.strip()]
74+
if not years:
75+
return str(datetime.now().year)
76+
77+
newest_year = years[0]
78+
oldest_year = years[-1]
79+
if newest_year == oldest_year:
80+
return newest_year
81+
82+
return f"{oldest_year}-{newest_year}"
83+
84+
85+
def write_updated_content(file_path, content):
86+
with open(file_path, "w", encoding="utf-8") as file:
87+
file.write(content)
88+
89+
90+
def add_copyright_to_file(file_path, repo_root):
2491
"""
25-
Add the copyright notice to a Java file if it doesn't already exist.
92+
Add or update the copyright notice on a Java file.
2693
"""
27-
with open(file_path, 'r+') as file:
94+
with open(file_path, "r", encoding="utf-8") as file:
2895
content = file.read()
29-
# Check if the copyright notice is already present
30-
if COPYRIGHT_NOTICE in content:
31-
print(f"Copyright notice already exists in: {file_path}")
96+
97+
years = get_copyright_years(file_path, repo_root)
98+
notice = NOTICE_TEMPLATE.format(years=years)
99+
100+
match = COPYRIGHT_LINE_RE.search(content)
101+
if match:
102+
if match.group("years") == years:
103+
print(f"Copyright notice already up to date in: {file_path}")
32104
return
33-
# Prepend the copyright notice
34-
file.seek(0)
35-
file.write(COPYRIGHT_NOTICE + "\n" + content)
36-
print(f"Added copyright notice to: {file_path}")
37105

38-
def process_java_files(directory):
106+
updated_content = COPYRIGHT_LINE_RE.sub(
107+
f" * Copyright {years} Noah Ross",
108+
content,
109+
count=1,
110+
)
111+
write_updated_content(file_path, updated_content)
112+
print(f"Updated copyright notice in: {file_path}")
113+
return
114+
115+
write_updated_content(file_path, notice + "\n" + content)
116+
print(f"Added copyright notice to: {file_path}")
117+
118+
119+
def process_java_files(directory, repo_root):
39120
"""
40121
Recursively process all Java files in the given directory.
41122
"""
42123
for root, _, files in os.walk(directory):
43124
for file in files:
44-
if file.endswith('.java'):
125+
if file.endswith(".java"):
45126
file_path = os.path.join(root, file)
46-
add_copyright_to_file(file_path)
127+
add_copyright_to_file(file_path, repo_root)
47128

48-
if __name__ == "__main__":
49-
# Replace this with the path to your project directory
50-
project_directory = "./"
51-
process_java_files(project_directory)
52129

130+
if __name__ == "__main__":
131+
project_directory = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
132+
repo_root = get_repo_root(project_directory)
133+
process_java_files(project_directory, repo_root)

0 commit comments

Comments
 (0)