-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add beet.contrib.strip_final_newlines
- Loading branch information
Showing
8 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""Plugin that strips final newlines in text files.""" | ||
|
||
|
||
__all__ = [ | ||
"StripFinalNewlinesSerializer", | ||
"StripFinalNewlinesOptions", | ||
"strip_final_newlines", | ||
] | ||
|
||
|
||
from dataclasses import dataclass | ||
from typing import Any, Callable | ||
|
||
from pydantic import BaseModel | ||
|
||
from beet import Context, ListOption, TextFileBase, configurable | ||
|
||
|
||
class StripFinalNewlinesOptions(BaseModel): | ||
extensions: ListOption[str] = ListOption() | ||
|
||
|
||
def beet_default(ctx: Context): | ||
ctx.require(strip_final_newlines) | ||
|
||
|
||
@dataclass | ||
class StripFinalNewlinesSerializer: | ||
"""Wrap existing serializer and strip final newline.""" | ||
|
||
serializer: Callable[[Any], str] | ||
|
||
def __call__(self, content: Any) -> str: | ||
raw = self.serializer(content) | ||
if raw.endswith("\n"): | ||
raw = raw[:-1] | ||
return raw | ||
|
||
|
||
@configurable(validator=StripFinalNewlinesOptions) | ||
def strip_final_newlines(ctx: Context, opts: StripFinalNewlinesOptions): | ||
"""Plugin that strips final newlines in text files.""" | ||
for pack in ctx.packs: | ||
for _, text_file in pack.list_files( | ||
*opts.extensions.entries(), | ||
extend=TextFileBase[Any], | ||
): | ||
text_file.serializer = StripFinalNewlinesSerializer(text_file.serializer) | ||
if isinstance(raw := text_file.get_content(), str) and raw.endswith("\n"): | ||
text_file.text = raw[:-1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
data_pack: | ||
load: "src" | ||
pipeline: | ||
- beet.contrib.strip_final_newlines |
1 change: 1 addition & 0 deletions
1
examples/load_strip_final_newlines/src/data/demo/functions/foo.mcfunction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
say hello |
3 changes: 3 additions & 0 deletions
3
examples/load_strip_final_newlines/src/data/minecraft/tags/functions/tick.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"values": ["demo:foo"] | ||
} |
1 change: 1 addition & 0 deletions
1
...examples__build_load_strip_final_newlines__0.data_pack/data/demo/functions/foo.mcfunction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
say hello |
3 changes: 3 additions & 0 deletions
3
...les__build_load_strip_final_newlines__0.data_pack/data/minecraft/tags/functions/tick.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"values": ["demo:foo"] | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/snapshots/examples__build_load_strip_final_newlines__0.data_pack/pack.mcmeta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"pack": { | ||
"pack_format": 10, | ||
"description": "" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/snapshots/examples__build_load_strip_final_newlines__1.resource_pack/pack.mcmeta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"pack": { | ||
"pack_format": 9, | ||
"description": "" | ||
} | ||
} |