o
    i                     @   sl  d dl Z d dlZd dlmZ d dlmZ d dlZd dlmZ ddl	m
Z
 ddlmZmZmZmZ dd	lmZmZmZmZmZmZ e
jZe
jZd
dgZededddeddddeeddddeddddde dedddddfdejdedee d ed!ee d"e fd#d$Z!e
je
jded%deded ed!ed"e d&ed'dfd(d)Z"deded ed'dfd*d+Z#dS ),    N)Path)Optional)msg   )about)ensure_pathget_git_versiongit_checkoutgit_repo_branch_exists   )COMMANDPROJECT_FILEArgOpt_get_parent_commandappmainmasterclone.z!The name of the template to clone)helpzAWhere to clone the project. Defaults to current working directoryF)r   existsz--repoz-rzThe repository to clone fromz--branchz-bz8The branch to clone from. If not provided, will attempt , z--sparsez-SzZUse sparse Git checkout to only check out and clone the files needed. Requires Git v22.2+.ctxnamedestrepobranchsparse_checkoutc           	      C   s   |du rt  t |jd  }|tkr|du rt}|du rEtD ]}t||r*|} nq|du rDddd tD }tj	d| ddd	 nt||sXtj	d
| d| ddd	 t
|ts_J t| }t||||||d dS )aW  Clone a project template from a repository. Calls into "git" and will
    only download the files from the given subdirectory. The GitHub repo
    defaults to the official Weasel template repo, but can be customized
    (including using a private repo).

    DOCS: https://github.com/explosion/weasel/tree/main/docs/cli.md#clipboard-clone
    Nr   c                 s   s    | ]	}d | d V  qdS )'N ).0br    r    D/home/ubuntu/.local/lib/python3.10/site-packages/weasel/cli/clone.py	<genexpr>/   s    z$project_clone_cli.<locals>.<genexpr>z2No branch provided and attempted default branches z do not exist.r   exitszrepo: z
 (branch: z) does not exist.r   r   r   parent_command)r   cwdpartsDEFAULT_REPODEFAULT_PROJECTS_BRANCHDEFAULT_BRANCHESr
   joinr   fail
isinstancestrr   project_clone)	r   r   r   r   r   r   default_branchdefault_branches_msgr(   r    r    r#   project_clone_cli   s>   


r5   r'   r(   returnc          	   
   C   s   t |}t| || | }tdd|}zt|| |||d W n tjy=   d|  d| d| d}tj	|dd	 Y nw t
d
|  d| d| d| |t  s_tdt d dS t
d td| d|  dS )zClone a project template from a repository.

    name (str): Name of subdirectory to clone.
    dest (Path): Destination path of cloned project.
    repo (str): URL of Git repo containing project templates.
    branch (str): The branch to clone from
    z(http(s?)):\/\/github.com/ )r   sparsezCould not clone 'z' from repo 'z' (branch 'z')r   r%   zCloned 'z' from 'zNo z found in directoryzYour project is now ready!zTo fetch the assets, run:
z assets N)r   check_cloneresolveresubr	   
subprocessCalledProcessErrorr   r/   goodr   r   warnprint)	r   r   r   r   r   r(   project_dir	repo_nameerrr    r    r#   r2   D   s    
r2   c                 C   s   d|  d| d| d}t |d |stjd| dd | r+tjd	| dd |j s>tjd
|j ddd dS dS )a=  Check and validate that the destination path can be used to clone. Will
    check that Git is available and that the destination path is suitable.

    name (str): Name of the directory to clone from the repo.
    dest (Path): Local destination of cloned directory.
    repo (str): URL of the repo to clone from.
    z~Cloning Weasel project templates requires Git and the 'git' command. To clone a project without Git, copy the files from the 'z' directory in the z to z
 manually.)errorz(Not a valid directory to clone project: r   r%   z/Can't clone project, directory already exists: z5Can't clone project, parent directory doesn't exist: z9. Create the necessary folder(s) first before continuing.N)r   r   r/   r   parent)r   r   r   git_errr    r    r#   r9   e   s&   	


r9   )$r;   r=   pathlibr   typingr   typerwasabir   r7   r   utilr   r   r	   r
   r   r   r   r   r   r   r   __projects__r+   __projects_branch__r,   r-   commandr.   Contextr1   boolr5   r2   r9   r    r    r    r#   <module>   sf     
5
!