7568bbd9f57e0d7d90dd4a15abffc7e8a269f88f

Author
Marc Cornellà <hello@mcornella.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(ng): get `ng` completion from parsed help output (#10294)

Co-authored-by: Yannick Galatol <yannick.galatol@gmail.com>

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/ng/_ng b/plugins/ng/_ng
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..9c96cf7e3b905286d7336405be33f10c9720c8ad
  4--- /dev/null
  5+++ b/plugins/ng/_ng
  6@@ -0,0 +1,56 @@
  7+#compdef ng
  8+
  9+setopt localoptions extendedglob
 10+
 11+if (( CURRENT == 2 )); then
 12+  local -a cmds alias
 13+  # Sample output (ng help):
 14+  # Available Commands:
 15+  #   add Adds support for an external library to your project.
 16+  for line in ${(@f)"$(ng help 2>/dev/null | sed -n '/^  /p')"}; do
 17+    if [[ "$line" =~ '^  ([^ ]+) \(([^)]+)\) (.*)$' ]]; then
 18+      alias=(${match[1]} ${(s:, :)match[2]})
 19+      cmds+=(${^alias}:"${match[3]//:/}")
 20+    elif [[ "$line" =~ '^  ([^ ]+) (.*)$' ]]; then
 21+      cmds+=("${match[1]}:${match[2]//:/}")
 22+    fi
 23+  done
 24+  _describe commands cmds && return 0
 25+elif (( CURRENT == 3 )); then
 26+  local -a flags args
 27+  local section description
 28+  # Sample output (ng build --help):
 29+  # --configuration (-c)
 30+  #   One or more named builder configurations as a comma-separated list as specified in the "configurations" section of angular.json.
 31+  #   The builder uses the named configurations to run the given target.
 32+  #   For more information, see https://angular.io/guide/workspace-config#alternate-build-configurations.
 33+  # Prefix --flags with literal \0, and split on that to get each flag section
 34+  for section in ${(s:\0:)"$(ng ${words[2]} --help 2>/dev/null | sed -e '1,/^options/ d;s/^  --/\\0--/')"}; do
 35+    # Split by newline and discard extra description lines (lines > 2)
 36+    for args description in ${${(@f)section}[1,2]}; do
 37+      args=(${(s: :)${${args%% #}## #}//[(),]/})
 38+      description=${${description%% #}## #}
 39+      flags+=(${^args}":$description")
 40+    done
 41+  done
 42+  _describe flags flags
 43+
 44+  case "$words[2]" in
 45+  b|build|l|lint|t|test)
 46+    # Sample output (ng config projects):
 47+    # {
 48+    #   "angular-project-1": {
 49+    #     ...
 50+    #   },
 51+    #   "angular-project-2": {
 52+    #     ...
 53+    #   }
 54+    # }
 55+    # In abscence of a proper JSON parser, just grab the lines with
 56+    # a 2-space indentation and only the stuff inside quotes
 57+    local -a projects
 58+    projects=(${(@f)"$(ng config projects 2>/dev/null | sed -n 's/^  "\([^"]\+\)".*$/\1/p')"})
 59+    _describe projects projects
 60+    ;;
 61+  esac
 62+fi
 63diff --git a/plugins/ng/ng.plugin.zsh b/plugins/ng/ng.plugin.zsh
 64deleted file mode 100644
 65index 44102e2a6845fbbbf7b8ae45181bd200fb4153ad..0000000000000000000000000000000000000000
 66--- a/plugins/ng/ng.plugin.zsh
 67+++ /dev/null
 68@@ -1,78 +0,0 @@
 69-ng_opts='addon asset-sizes b build completion d destroy doc e2e g generate get github-pages:deploy gh-pages:deploy h help i init install lint make-this-awesome new s serve server set t test update v version -h --help'
 70-
 71-_ng_completion () {
 72-  local words cword opts
 73-  read -Ac words
 74-  read -cn cword
 75-  let cword-=1
 76-
 77-  case $words[cword] in
 78-    addon )
 79-      opts='-b --blueprint -d -dir --directory --dry-run -sb --skip-bower -sg --skip-git -sn --skip-npm -v --verbose'
 80-      ;;
 81-
 82-    asset-sizes )
 83-      opts='-o --output-path'
 84-      ;;
 85-
 86-    b | build )
 87-      opts='--environment --output-path --suppress-sizes --target --watch --watcher -dev -e -prod'
 88-      ;;
 89-
 90-    d | destroy )
 91-      opts='--dry-run --verbose --pod --classic --dummy --in-repo --in-repo-addon -d -v -p -c -dum -id -ir'
 92-      ;;
 93-
 94-    g | generate )
 95-      opts='class component directive enum module pipe route service --generate -d --dry-run --verbose -v --pod -p --classic -c --dummy -dum -id --in-repo --in-repo-addon -ir'
 96-      ;;
 97-
 98-    gh-pages:deploy | github-pages:deploy )
 99-      opts='--environment --gh-token --gh-username --skip-build --user-page --message'
100-      ;;
101-
102-    h | help | -h | --help)
103-      opts='--json --verbose -v'
104-      ;;
105-
106-    init )
107-      opts='--blueprint --dry-run --link-cli --mobile --name --prefix --skip-bower --skip-npm --source-dir --style --verbose -b -d -lc -n -p -sb -sd -sn -v'
108-      ;;
109-
110-    new )
111-      opts='--blueprint --directory --dry-run --link-cli --mobile --prefix --skip-bower --skip-git --skip-npm --source-dir --style --verbose -b -d -dir -lc -p -sb -sd -sg -sn -v'
112-      ;;
113-
114-    s | serve | server )