#compdef carthage
#
# Zsh completion script for Carthage.

__subcommands() {
    local -a commands
    commands=(
        'archive'
        'bootstrap'
        'build'
        'checkout'
        'copy-frameworks'
        'fetch'
        'help'
        'outdated'
        'update'
        'validate'
        'version'
    )
    _values Commands $commands
}

__carthage_dependencies() {
   local -a _dependencies
   _dependencies=( $(egrep '^\s*git' Cartfile | awk -F '"' '{print $2}' | sed -e 's/.*\///' | sed -e 's/\.git$//') )
   _describe -t dependencies Dependencies _dependencies
}

_carthage() {
    typeset -A opt_args
    local ret=1 state

    _arguments -C \
        '1: :__subcommands' \
        '*:: :->args' \
        && ret=0

    case $state in
        (args)
            case $words[1] in
                (archive)
                    _arguments \
                        '--output: :_files' \
                        '--project-directory: :_directories' \
                        '--color: :(auto always never)' \
                        '(-)*:: :->null_state' \
                        && ret=0
                        ;;
                (bootstrap|update)
                    _arguments -C \
                        '--no-checkout' \
                        '--no-build' \
                        '--verbose' \
                        '--configuration: :(Release Debug)' \
                        '--platform: :(all macOS iOS watchOS tvOS)' \
                        '--toolchain: :(com.apple.dt.toolchain.Swift_2_3 com.apple.dt.toolchain.XcodeDefault)' \
                        '--derived-data: :_directories' \
                        '--use-ssh' \
                        '--use-submodules' \
                        '--no-use-binaries' \
                        '--color: :(auto always never)' \
                        '--project-directory: :_directories' \
                        '--cache-builds' \
                        '*: :__carthage_dependencies' \
                        && ret=0
                        ;;
                (build)
                    _arguments \
                        '--configuration: :(Release Debug)' \
                        '--platform: :(all macOS iOS watchOS tvOS)' \
                        '--toolchain: :(com.apple.dt.toolchain.Swift_2_3 com.apple.dt.toolchain.XcodeDefault)' \
                        '--derived-data: :_directories' \
                        '--no-skip-current' \
                        '--color: :(auto always never)' \
                        '--verbose' \
                        '--project-directory: :_directories' \
                        '--cache-builds' \
                        '(-)*:: :->null_state' \
                        && ret=0
                        ;;
                (checkout)
                    _arguments \
                        '--use-ssh' \
                        '--use-submodules' \
                        '--no-use-binaries' \
                        '--color: :(auto always never)' \
                        '--project-directory: :_directories' \
                        '(-)*:: :->null_state' \
                        && ret=0
                        ;;
                (fetch)
                    _arguments \
                        '--color: :(auto always never)' \
                        '(-)*:: :->null_state' \
                        && ret=0
                        ;;
                (help)
                    if (( $CURRENT == 2 ))
                    then
                        __subcommands && ret=0
                    fi
                    ;;
                (outdated)
                    _arguments \
                        '--use-ssh' \
                        '--verbose' \
                        '--color: :(auto always never)' \
                        '--project-directory: :_directories' \
                        && ret=0
                        ;;
            esac
            ;;
    esac

    return ret
}
