| 95 | |
| 96 | |
| 97 | class SourceFiles: |
| 98 | def __init__(self, black_repo_dir: Path): |
| 99 | # File path fun all pathlib to be platform agnostic |
| 100 | self.black_repo_path = black_repo_dir |
| 101 | self.changes_path = self.black_repo_path / "CHANGES.md" |
| 102 | self.docs_path = self.black_repo_path / "docs" |
| 103 | self.version_doc_paths = ( |
| 104 | self.docs_path / "integrations" / "source_version_control.md", |
| 105 | self.docs_path / "usage_and_configuration" / "the_basics.md", |
| 106 | self.docs_path / "guides" / "using_black_with_jupyter_notebooks.md", |
| 107 | ) |
| 108 | self.current_version = self.get_current_version() |
| 109 | self.next_version = self.get_next_version() |
| 110 | |
| 111 | def __str__(self) -> str: |
| 112 | return f"""\ |
| 113 | > SourceFiles ENV: |
| 114 | Repo path: {self.black_repo_path} |
| 115 | CHANGES.md path: {self.changes_path} |
| 116 | docs path: {self.docs_path} |
| 117 | Current version: {self.current_version} |
| 118 | Next version: {self.next_version} |
| 119 | """ |
| 120 | |
| 121 | def add_template_to_changes(self) -> int: |
| 122 | """Add the template to CHANGES.md if it does not exist""" |
| 123 | LOG.info(f"Adding template to {self.changes_path}") |
| 124 | |
| 125 | with self.changes_path.open("r", encoding="utf-8") as cfp: |
| 126 | changes_string = cfp.read() |
| 127 | |
| 128 | if "## Unreleased" in changes_string: |
| 129 | LOG.error(f"{self.changes_path} already has unreleased template") |
| 130 | return 1 |
| 131 | |
| 132 | templated_changes_string = changes_string.replace( |
| 133 | "# Change Log\n", |
| 134 | f"# Change Log\n\n{NEW_VERSION_CHANGELOG_TEMPLATE}", |
| 135 | ) |
| 136 | |
| 137 | with self.changes_path.open("w", encoding="utf-8") as cfp: |
| 138 | cfp.write(templated_changes_string) |
| 139 | |
| 140 | LOG.info(f"Added template to {self.changes_path}") |
| 141 | return 0 |
| 142 | |
| 143 | def cleanup_changes_template_for_release(self) -> None: |
| 144 | LOG.info(f"Cleaning up {self.changes_path}") |
| 145 | |
| 146 | with self.changes_path.open("r", encoding="utf-8") as cfp: |
| 147 | changes_string = cfp.read() |
| 148 | |
| 149 | # Change Unreleased to next version |
| 150 | changes_string = changes_string.replace( |
| 151 | "## Unreleased", f"## Version {self.next_version}" |
| 152 | ) |
| 153 | |
| 154 | # Remove all comments |