Skip to content

Commit

Permalink
Merge pull request #3371 from mirpedrol/run-prettier-on-sync
Browse files Browse the repository at this point in the history
run pre-comit when rendering template for pipelines sync
  • Loading branch information
mirpedrol authored Jan 13, 2025
2 parents b685800 + 80e06dc commit d4d3f36
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# nf-core/tools: Changelog

## v3.2.0dev
## v3.1.2dev

### Template

Expand All @@ -15,6 +15,7 @@
### General

- Parameters schema validation: allow oneOf, anyOf and allOf with `required` ([#3386](https://github.com/nf-core/tools/pull/3386))
- Run pre-comit when rendering template for pipelines sync ([#3371](https://github.com/nf-core/tools/pull/3371))

### Version updates

Expand Down
3 changes: 2 additions & 1 deletion nf_core/pipeline-template/conf/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ params {
// TODO nf-core: Give any required params for the test so that command line flags are not needed
input = params.pipelines_testdata_base_path + 'viralrecon/samplesheet/samplesheet_test_illumina_amplicon.csv'

{% if igenomes -%}
{%- if igenomes -%}

// Genome references
genome = 'R64-1-1'
{%- endif %}
Expand Down
4 changes: 4 additions & 0 deletions nf_core/pipelines/create/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ def render_template(self) -> None:
yaml.dump(config_yml.model_dump(exclude_none=True), fh, Dumper=custom_yaml_dumper())
log.debug(f"Dumping pipeline template yml to pipeline config file '{config_fn.name}'")

# Run prettier on files for pipelines sync
log.debug("Running prettier on pipeline files")
run_prettier_on_file([str(f) for f in self.outdir.glob("**/*")])

def fix_linting(self):
"""
Updates the .nf-core.yml with linting configurations
Expand Down
44 changes: 29 additions & 15 deletions nf_core/pipelines/lint_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ def print_fixes(lint_obj):
)


def check_git_repo() -> bool:
"""Check if the current directory is a git repository."""
try:
subprocess.check_output(["git", "rev-parse", "--is-inside-work-tree"])
return True
except subprocess.CalledProcessError:
return False


def run_prettier_on_file(file: Union[Path, str, List[str]]) -> None:
"""Run the pre-commit hook prettier on a file.
Expand All @@ -80,28 +89,33 @@ def run_prettier_on_file(file: Union[Path, str, List[str]]) -> None:
If Prettier is not installed, a warning is logged.
"""

is_git = check_git_repo()

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(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()}")
elif "files were modified by this hook" in e.stdout.decode():
all_lines = [line for line in e.stdout.decode().split("\n")]
files = "\n".join(all_lines[3:])
log.debug(f"The following files were modified by prettier:\n {files}")
else:
log.warning(
"There was an error running the prettier pre-commit hook.\n"
f"STDOUT: {e.stdout.decode()}\nSTDERR: {e.stderr.decode()}"
)
if is_git:
try:
proc = subprocess.run(args, capture_output=True, check=True)
log.debug(f"{proc.stdout.decode()}")
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()}")
elif "files were modified by this hook" in e.stdout.decode():
all_lines = [line for line in e.stdout.decode().split("\n")]
files = "\n".join(all_lines[3:])
log.debug(f"The following files were modified by prettier:\n {files}")
else:
log.warning(
"There was an error running the prettier pre-commit hook.\n"
f"STDOUT: {e.stdout.decode()}\nSTDERR: {e.stderr.decode()}"
)
else:
log.debug("Not in a git repository, skipping pre-commit hook.")


def dump_json_with_prettier(file_name, file_content):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from setuptools import find_packages, setup

version = "3.2.0dev"
version = "3.1.2dev"

with open("README.md") as f:
readme = f.read()
Expand Down

0 comments on commit d4d3f36

Please sign in to comment.