Skip to content

Commit

Permalink
Merge pull request #3188 from mashehu/fix-run-prettier
Browse files Browse the repository at this point in the history
Fix run_prettier on rendered template
  • Loading branch information
mashehu authored Oct 3, 2024
2 parents d52969b + 93c2def commit 38db8cb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion nf_core/pipelines/create/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def render_template(self) -> None:
log.debug(f"Dumping pipeline template yml to pipeline config file '{config_fn.name}'")

# Run prettier on files
run_prettier_on_file(self.outdir)
run_prettier_on_file([str(f) for f in self.outdir.glob("**/*")])

def fix_linting(self):
"""
Expand Down
30 changes: 23 additions & 7 deletions nf_core/pipelines/lint_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import logging
import subprocess
from pathlib import Path
from typing import List
from typing import List, Union

import rich
import yaml
from rich.console import Console
from rich.table import Table

Expand Down Expand Up @@ -69,7 +70,7 @@ def print_fixes(lint_obj):
)


def run_prettier_on_file(file):
def run_prettier_on_file(file: Union[Path, str, List[str]]) -> None:
"""Run the pre-commit hook prettier on a file.
Args:
Expand All @@ -80,12 +81,15 @@ def run_prettier_on_file(file):
"""

nf_core_pre_commit_config = Path(nf_core.__file__).parent / ".pre-commit-prettier-config.yaml"
args = ["pre-commit", "run", "--config", str(nf_core_pre_commit_config), "prettier"]
if isinstance(file, List):
args.extend(["--files", *file])
else:
args.extend(["--files", str(file)])

try:
subprocess.run(
["pre-commit", "run", "--config", nf_core_pre_commit_config, "prettier", "--files", file],
capture_output=True,
check=True,
)
subprocess.run(args, capture_output=True, check=True)
log.debug(f"${subprocess.STDOUT}")
except subprocess.CalledProcessError as e:
if ": SyntaxError: " in e.stdout.decode():
log.critical(f"Can't format {file} because it has a syntax error.\n{e.stdout.decode()}")
Expand All @@ -111,6 +115,18 @@ def dump_json_with_prettier(file_name, file_content):
run_prettier_on_file(file_name)


def dump_yaml_with_prettier(file_name: Union[Path, str], file_content: dict) -> None:
"""Dump a YAML file and run prettier on it.
Args:
file_name (Path | str): A file identifier as a string or pathlib.Path.
file_content (dict): Content to dump into the YAML file
"""
with open(file_name, "w") as fh:
yaml.safe_dump(file_content, fh)
run_prettier_on_file(file_name)


def ignore_file(lint_name: str, file_path: Path, dir_path: Path) -> List[List[str]]:
"""Ignore a file and add the result to the ignored list. Return the passed, failed, ignored and ignore_configs lists."""

Expand Down
4 changes: 2 additions & 2 deletions nf_core/pipelines/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import nf_core.pipelines.create.create
import nf_core.pipelines.list
import nf_core.utils
from nf_core.pipelines.lint_utils import dump_yaml_with_prettier

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -290,8 +291,7 @@ def make_template_pipeline(self):
self.config_yml.template.outdir = "."
# Update nf-core version
self.config_yml.nf_core_version = nf_core.__version__
with open(self.config_yml_path, "w") as config_path:
yaml.safe_dump(self.config_yml.model_dump(), config_path)
dump_yaml_with_prettier(self.config_yml_path, self.config_yml.model_dump())

except Exception as err:
# Reset to where you were to prevent git getting messed up.
Expand Down

0 comments on commit 38db8cb

Please sign in to comment.