|
1 | 1 | import os |
2 | 2 | import shutil |
3 | | -import tempfile |
4 | | -import time |
5 | 3 | from copy import deepcopy |
6 | 4 | from pathlib import Path |
7 | 5 | from typing import Optional |
|
23 | 21 | stage_and_commit_all, |
24 | 22 | update_local_branches_from_remote_without_checkout, |
25 | 23 | ) |
26 | | -from flexlate.path_ops import ( |
27 | | - location_relative_to_new_parent, |
28 | | -) |
| 24 | +from flexlate.path_ops import location_relative_to_new_parent |
29 | 25 | from flexlate.render.multi import MultiRenderer |
30 | | -from flexlate.styles import SUCCESS_STYLE, INFO_STYLE, console, styled, print_styled |
| 26 | +from flexlate.styles import INFO_STYLE, SUCCESS_STYLE, console, print_styled, styled |
31 | 27 | from flexlate.syncer import Syncer |
| 28 | +from flexlate.temp_path import create_temp_path |
32 | 29 | from flexlate.template.base import Template |
33 | 30 | from flexlate.template_data import TemplateData |
34 | 31 | from flexlate.transactions.transaction import ( |
@@ -344,10 +341,8 @@ def init_project_from_template_source_path( |
344 | 341 | renderer: MultiRenderer = MultiRenderer(), |
345 | 342 | syncer: Syncer = Syncer(), |
346 | 343 | ) -> str: |
347 | | - with tempfile.TemporaryDirectory() as temp_dir: |
348 | | - temp_path = Path(temp_dir) |
349 | | - |
350 | | - repo = Repo.init(temp_dir) |
| 344 | + with create_temp_path() as temp_path: |
| 345 | + repo = Repo.init(temp_path) |
351 | 346 |
|
352 | 347 | def sync(): |
353 | 348 | syncer.sync_local_changes_to_flexlate_branches( |
@@ -491,31 +486,34 @@ def move_source_path_to_be_relative_to_destination( |
491 | 486 | move_source_path_to_be_relative_to_destination, |
492 | 487 | project_root=final_out_path, |
493 | 488 | ) |
494 | | - stage_and_commit_all( |
495 | | - repo, |
496 | | - "Move template source path to match permanent destination of project", |
497 | | - ) |
498 | | - sync() |
| 489 | + # If the final output folder is at the same |
| 490 | + # level as the temp folder, the relative path will be unchanged, so there will be no changes |
| 491 | + if repo.is_dirty(): |
| 492 | + stage_and_commit_all( |
| 493 | + repo, |
| 494 | + "Move template source path to match permanent destination of project", |
| 495 | + ) |
| 496 | + sync() |
499 | 497 |
|
500 | 498 | return folder_name |
501 | 499 |
|
502 | 500 |
|
503 | 501 | def _add_template_commit_message( |
504 | 502 | template: Template, out_root: Path, project_root: Path |
505 | 503 | ) -> str: |
506 | | - relative_path = out_root.absolute().relative_to(project_root) |
| 504 | + relative_path = Path(os.path.relpath(out_root.absolute(), project_root)) |
507 | 505 | return f"Applied template {template.name} to {relative_path}" |
508 | 506 |
|
509 | 507 |
|
510 | 508 | def _move_applied_template_config_message( |
511 | 509 | template: Template, out_root: Path, project_root: Path |
512 | 510 | ) -> str: |
513 | | - relative_path = out_root.absolute().relative_to(project_root) |
| 511 | + relative_path = Path(os.path.relpath(out_root.absolute(), project_root)) |
514 | 512 | return f"Moved config for {template.name} to {relative_path}" |
515 | 513 |
|
516 | 514 |
|
517 | 515 | def _add_template_source_commit_message( |
518 | 516 | template: Template, out_root: Path, project_root: Path |
519 | 517 | ) -> str: |
520 | | - relative_path = out_root.absolute().relative_to(project_root) |
| 518 | + relative_path = Path(os.path.relpath(out_root.absolute(), project_root)) |
521 | 519 | return f"Added template source {template.name} to {relative_path}" |
0 commit comments