#compdef play
# ------------------------------------------------------------------------------
# Copyright (c) 2011 zsh-users
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the zsh-users nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
#  Zsh completion script for Play! framework (https://www.playframework.com).
#
#  Status: See FIXME and TODO tags.
#
#  Source: https://github.com/zsh-users/zsh-completions
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
#  * Julien Nicoulaud <julien.nicoulaud@gmail.com>
#
# ------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# ------------------------------------------------------------------------------


_play() {
  local context curcontext="$curcontext" state line ret=1
  typeset -A opt_args

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

  case $state in
    (args)
      curcontext="${curcontext%:*:*}:play-cmd-$words[1]:"
      case $line[1] in
        (build-module|list-modules|lm|check|id)
          _message 'no more arguments' && ret=0
        ;;
        (dependencies|deps)
          _arguments \
            '1:: :_play_apps' \
            '(--debug)--debug[Debug mode (even more informations logged than in verbose mode)]' \
            '(--jpda)--jpda[Listen for JPDA connection. The process will  suspended until a client is plugged to the JPDA port.]' \
            '(--sync)--sync[Keep lib/ and modules/ directory synced. Delete unknow dependencies.]' \
            '(--verbose)--verbose[Verbose Mode]' \
          && ret=0
        ;;
        (clean|javadoc|jd|out|pid|secret|stop)
          _arguments '1:: :_play_apps' && ret=0
        ;;
        (help)
          _arguments '1: :_play_cmds -F "(cp deps ec idea jd st lm nb nm help antify evolutions evolutions:apply evolutions:markApplied evolutions:resolve)"' && ret=0
        ;;
        (status|st)
          _arguments \
            '1:: :_play_apps' \
            '(--url)--url[If you want to monitor an application running on a remote server, specify the application URL using this option]:URL:_urls' \
            '(--secret)--secret[You can provide your own secret key using this option]:Secret key' \
          && ret=0
        ;;
        (new)
          _arguments \
            '1: :_play_apps' \
            '(--with)--with[Automatically enable this set of module for the newly created application]:Modules list:_play_modules_list' \
          && ret=0
        ;;
        (install)
          _arguments '1:Play! module:_play_modules_dash_versions' && ret=0
        ;;
        (new-module)
          _arguments '1:Module directory:_files -/' && ret=0
        ;;
        (test|precompile|run|start|war|auto-test|classpath|cp|eclipsify|ec|idealize|idea|modules|netbeansify|nb)
          local cmd_args; cmd_args=(
            '1:: :_play_apps'
            '(--deps)--deps[Resolve and install dependencies before running the command]'
          )
          case $line[1] in
            (precompile|run|start|restart|war)
              local app_dir="$line[2]"
              [[ -d "$app_dir" ]] || app_dir=.
              [[ -f "$app_dir/conf/application.conf" ]] && cmd_args+=('--'${(u)${(M)$(<$app_dir/conf/application.conf):#%*}%%.*}'[Use this ID to run the application (override the default framework ID)]')
            ;|
            (test|run)
              cmd_args+=('(-f)-f[Disable the JPDA port checking and force the jpda.port value]')
            ;|
            (war)
              cmd_args+=(
                '(-o --output)'{-o,--output}'[The path where the WAR directory will be created. The contents of this directory will first be deleted]:output directory:_files -/'
                '(--zip)--zip[By default, the script creates an exploded WAR. If you want a zipped archive, specify the --zip option]'
                '(--exclude)--exclude[Excludes a list of colon separated directories]:excluded directories list:_play_colon_dirs_list'
              )
            ;|
            (test|run|start|restart|war)
              cmd_args+=('*:Java option')
            ;;
          esac
          _arguments "$cmd_args[@]" && ret=0
        ;;
        *)
          _call_function ret _play_cmd_$words[1] && ret=0
          (( ret )) && _message 'no more arguments'
        ;;
      esac
    ;;
  esac
}

# FIXME Completes only core commands, some modules add commands too (eg Maven). Where do we get them ?
# FIXME Parse 'play help' and 'play help <command>' (for aliases) instead of hard-coding.
_play_cmds() {
  local commands; commands=(
    'antify:Create a build.xml file for this project'
    'auto-test:Automatically run all application tests'
    'build-module:Build and package a module'
    'check:Check for a release newer than the current one'
    {classpath,cp}':Display the computed classpath'
    'clean:Delete temporary files (including the bytecode cache)'
    {dependencies,deps}':Resolve and retrieve project dependencies'
    {eclipsify,ec}':Create all Eclipse configuration files'
    'evolutions:Run the evolution check'
    'evolutions\:apply:Automatically apply pending evolutions'
    'evolutions\:mark:AppliedMark pending evolutions as manually applied'
    'evolutions\:resolve:Resolve partially applied evolution'
    'help:Display help on a specific command'
    'id:Define the framework ID'
    {idealize,idea}':Create all IntelliJ Idea configuration files'
    'install:Install a module'
    {javadoc,jd}':Generate your application Javadoc'
    {list-modules,lm}':List modules available from the central modules repository'
    'modules:Display the computed modules list'
    {netbeansify,nb}':Create all NetBeans configuration files'
    'new:Create a new application'
    {new-module,nm}':Create a module'
    'out:Follow logs/system.out file'
    'pid:Show the PID of the running application'
    'precompile:Precompile all Java sources and templates to speed up application start-up'
    'restart:Restart the running application'
    'run:Run the application in the current shell'
    'secret:Generate a new secret key'
    'start:Start the application in the background'
    {status,st}':Display the running application status'
    'stop:Stop the running application'
    'test:Run the application in test mode in the current shell'
    'war:Export the application as a standalone WAR archive'
  )
  _describe -t commands 'Play! command' commands "$@"
}

_play_apps() {
  _wanted application expl 'Play! application directory' _files -/
}

_play_modules() {
  local modules; modules=(${(ps:,:)${${${(S)${(f)$(_call_program modules play list-modules)}//\]*\[/,}%%\]*}##*\[}})
  _describe -t modules 'Play! module' modules "$@"
}

_play_modules_dash_versions() {
  local ret=1
  if compset -P '*-'; then
    local versions; versions=(${(ps:,:)${${${${${(f)$(_call_program versions play list-modules)}##*${IPREFIX%-}\]}#*Versions:}%%"~"*}//[[:space:]]/}})
    _describe -t module-versions "${IPREFIX%-} module versions" versions && ret=0
  else
    _wanted modules expl 'Play! module' _play_modules -qS- && ret=0
  fi
}

_play_modules_list() {
  compset -P '*,'; compset -S ',*'
  _wanted module-list expl 'Play! modules list' _play_modules -qS,
}

_play_colon_dirs_list() {
  compset -P '*:'; compset -S ':*'
  _wanted directories-list expl 'Directories list' _files -/ -qS:
}

_play "$@"
