Skip to content

Commit

Permalink
Merge pull request #1880 from adamrtalbot/1879_sync_template_yaml
Browse files Browse the repository at this point in the history
NF-Core Sync includes template.yaml as option
  • Loading branch information
mirpedrol authored Oct 12, 2022
2 parents 6d7625a + 2898626 commit 33726de
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### General

- Fix error in tagging GitPod docker images during releases
- `nf-core sync` now supports the template YAML file using `-t/--template-yaml`.
- Fix bug when updating modules from old version in old folder structure
- Don't remove local copy of modules repo, only update it with fetch ([#1881](https://github.com/nf-core/tools/pull/1881))
- Add subworkflow commands create-test-yml, create and install ([#1897](https://github.com/nf-core/tools/pull/1897))
Expand Down
5 changes: 3 additions & 2 deletions nf_core/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,8 @@ def bump_version(new_version, dir, nextflow):
@click.option("-p", "--pull-request", is_flag=True, default=False, help="Make a GitHub pull-request with the changes.")
@click.option("-g", "--github-repository", type=str, help="GitHub PR: target repository.")
@click.option("-u", "--username", type=str, help="GitHub PR: auth username.")
def sync(dir, from_branch, pull_request, github_repository, username):
@click.option("-t", "--template-yaml", help="Pass a YAML file to customize the template")
def sync(dir, from_branch, pull_request, github_repository, username, template_yaml):
"""
Sync a pipeline [cyan i]TEMPLATE[/] branch with the nf-core template.
Expand All @@ -1214,7 +1215,7 @@ def sync(dir, from_branch, pull_request, github_repository, username):
nf_core.utils.is_pipeline_directory(dir)

# Sync the given pipeline dir
sync_obj = nf_core.sync.PipelineSync(dir, from_branch, pull_request, github_repository, username)
sync_obj = nf_core.sync.PipelineSync(dir, from_branch, pull_request, github_repository, username, template_yaml)
try:
sync_obj.sync()
except (nf_core.sync.SyncException, nf_core.sync.PullRequestException) as e:
Expand Down
1 change: 1 addition & 0 deletions nf_core/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class PipelineCreate(object):
force (bool): Overwrites a given workflow directory with the same name. Defaults to False.
May the force be with you.
outdir (str): Path to the local output directory.
template_yaml (str): Path to template.yml file for pipeline creation settings.
"""

def __init__(
Expand Down
43 changes: 32 additions & 11 deletions nf_core/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class PipelineSync(object):
required_config_vars (list): List of nextflow variables required to make template pipeline
gh_username (str): GitHub username
gh_repo (str): GitHub repository name
template_yaml (str): Path to template.yml file for pipeline creation settings.
"""

def __init__(
Expand All @@ -61,6 +62,7 @@ def __init__(
make_pr=False,
gh_repo=None,
gh_username=None,
template_yaml_path=None,
):
"""Initialise syncing object"""

Expand All @@ -77,6 +79,12 @@ def __init__(
self.gh_repo = gh_repo
self.pr_url = ""

self.template_yaml_path = template_yaml_path
# Save contents of template.yml for using outside of git.
if self.template_yaml_path is not None:
with open(self.template_yaml_path, "r") as template_yaml:
self.template_yaml_cache = template_yaml.read()

# Set up the API auth if supplied on the command line
self.gh_api = nf_core.utils.gh_api
self.gh_api.lazy_init()
Expand Down Expand Up @@ -204,7 +212,7 @@ def delete_template_branch_files(self):
# Delete everything
log.info("Deleting all files in 'TEMPLATE' branch")
for the_file in os.listdir(self.pipeline_dir):
if the_file == ".git":
if the_file == ".git" or the_file == self.template_yaml_path:
continue
file_path = os.path.join(self.pipeline_dir, the_file)
log.debug(f"Deleting {file_path}")
Expand All @@ -225,16 +233,29 @@ def make_template_pipeline(self):
# Only show error messages from pipeline creation
logging.getLogger("nf_core.create").setLevel(logging.ERROR)

nf_core.create.PipelineCreate(
name=self.wf_config["manifest.name"].strip('"').strip("'"),
description=self.wf_config["manifest.description"].strip('"').strip("'"),
version=self.wf_config["manifest.version"].strip('"').strip("'"),
no_git=True,
force=True,
outdir=self.pipeline_dir,
author=self.wf_config["manifest.author"].strip('"').strip("'"),
plain=True,
).init_pipeline()
# Re-write the template yaml from cache which may have been updated
if self.template_yaml_path and self.template_yaml_cache:
with open(self.template_yaml_path, "w") as template_path:
template_path.write(self.template_yaml_cache)

try:
nf_core.create.PipelineCreate(
name=self.wf_config["manifest.name"].strip('"').strip("'"),
description=self.wf_config["manifest.description"].strip('"').strip("'"),
version=self.wf_config["manifest.version"].strip('"').strip("'"),
no_git=True,
force=True,
outdir=self.pipeline_dir,
author=self.wf_config["manifest.author"].strip('"').strip("'"),
template_yaml_path=self.template_yaml_path,
plain=True,
).init_pipeline()
except Exception as err:
# If sync fails, remove template_yaml_path before raising error.
os.remove(self.template_yaml_path)
# Reset to where you were to prevent git getting messed up.
self.repo.git.reset("--hard")
raise SyncException(f"Failed to rebuild pipeline from template with error:\n{err}")

def commit_template_changes(self):
"""If we have any changes with the new template files, make a git commit"""
Expand Down

0 comments on commit 33726de

Please sign in to comment.