flake-modules/new-plugin: add dry run argument

Just to see the generated output in console instead of having to go find
the file.
This commit is contained in:
Austin Horstman 2025-05-07 23:37:41 -05:00
parent d22e7878ec
commit e656464da4

View file

@ -83,6 +83,7 @@ def create_nix_file(
package, package,
maintainer, maintainer,
is_default_maintainer=False, is_default_maintainer=False,
dry_run=False,
): ):
""" """
Create a nix file from a template. Create a nix file from a template.
@ -95,6 +96,7 @@ def create_nix_file(
package (str): The package name of the plugin. package (str): The package name of the plugin.
maintainer (str): The maintainer name from lib.maintainers. maintainer (str): The maintainer name from lib.maintainers.
is_default_maintainer (bool): Whether the maintainer is the default value. is_default_maintainer (bool): Whether the maintainer is the default value.
dry_run (bool): If True, only print what would be written instead of actually writing.
""" """
# Add a TODO comment if using the default maintainer # Add a TODO comment if using the default maintainer
maintainer_todo = ( maintainer_todo = (
@ -108,10 +110,10 @@ def create_nix_file(
maintainer=maintainer, maintainer=maintainer,
maintainer_todo=maintainer_todo, maintainer_todo=maintainer_todo,
) )
write_to_file(file_path, content) write_to_file(file_path, content, dry_run)
def create_test_file(file_path, template, name): def create_test_file(file_path, template, name, dry_run=False):
""" """
Create a test file from a template. Create a test file from a template.
@ -119,19 +121,28 @@ def create_test_file(file_path, template, name):
file_path (str): The path to the file to create. file_path (str): The path to the file to create.
template (str): The template string to use for the file content. template (str): The template string to use for the file content.
name (str): The name of the plugin. name (str): The name of the plugin.
dry_run (bool): If True, only print what would be written instead of actually writing.
""" """
content = template.format(name=name) content = template.format(name=name)
write_to_file(file_path, content) write_to_file(file_path, content, dry_run)
def write_to_file(file_path, content: str): def write_to_file(file_path, content: str, dry_run=False):
""" """
Makes sure directories exist and write content to a file. Makes sure directories exist and write content to a file.
Args: Args:
file_path (str): The path to the file to write. file_path (str): The path to the file to write.
content (str): The content to write to the file. content (str): The content to write to the file.
dry_run (bool): If True, only print what would be written instead of actually writing.
""" """
if dry_run:
print(f"Would write to {file_path}:")
print("=" * 40)
print(content)
print("=" * 40)
return
os.makedirs(os.path.dirname(file_path), exist_ok=True) os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, "w") as f: with open(file_path, "w") as f:
f.write(content) f.write(content)
@ -175,6 +186,12 @@ def main():
help="Maintainer name (from lib.maintainers)", help="Maintainer name (from lib.maintainers)",
default=DEFAULT_MAINTAINER, default=DEFAULT_MAINTAINER,
) )
parser.add_argument(
"--dry-run",
"-d",
action="store_true",
help="Show what would be written without actually creating files",
)
args = parser.parse_args() args = parser.parse_args()
# Calculate name - convert to kebab case and strip nvim # Calculate name - convert to kebab case and strip nvim
@ -202,11 +219,13 @@ def main():
package, package,
args.maintainer, args.maintainer,
is_default_maintainer, is_default_maintainer,
args.dry_run,
) )
create_test_file( create_test_file(
test_path, test_path,
test_nix_template, test_nix_template,
name, name,
args.dry_run,
) )