|
| 1 | +#!/usr/bin/env zsh |
| 2 | + |
| 3 | +builtin emulate -L zsh |
| 4 | +setopt EXTENDED_GLOB |
| 5 | +setopt PUSHD_SILENT |
| 6 | +setopt ERR_EXIT |
| 7 | +setopt ERR_RETURN |
| 8 | +setopt NO_UNSET |
| 9 | +setopt PIPE_FAIL |
| 10 | +setopt NO_AUTO_PUSHD |
| 11 | +setopt NO_PUSHD_IGNORE_DUPS |
| 12 | +setopt FUNCTION_ARGZERO |
| 13 | + |
| 14 | +## Enable for script debugging |
| 15 | +# setopt WARN_CREATE_GLOBAL |
| 16 | +# setopt WARN_NESTED_VAR |
| 17 | +# setopt XTRACE |
| 18 | + |
| 19 | +autoload -Uz is-at-least && if ! is-at-least 5.2; then |
| 20 | + print -u2 -PR "%F{1}${funcstack[1]##*/}:%f Running on Zsh version %B${ZSH_VERSION}%b, but Zsh %B5.2%b is the minimum supported version. Upgrade zsh to fix this issue." |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +invoke_quicktype() { |
| 25 | + local json_parser_file="plugins/obs-services/generated/services-json.hpp" |
| 26 | + |
| 27 | + if (( ${+commands[quicktype]} )) { |
| 28 | + local quicktype_version=($(quicktype --version)) |
| 29 | + |
| 30 | + if ! is-at-least 23.0.0 ${quicktype_version[3]}; then |
| 31 | + log_error "quicktype is not version 23.x.x (found ${quicktype_version[3]}." |
| 32 | + exit 2 |
| 33 | + fi |
| 34 | + |
| 35 | + if is-at-least 24.0.0 ${quicktype_version[3]}; then |
| 36 | + log_error "quicktype is more recent than version 23.x.x (found ${quicktype_version[3]})." |
| 37 | + exit 2 |
| 38 | + fi |
| 39 | + |
| 40 | + } else { |
| 41 | + log_error "No viable quicktype version found (required 23.x.x)" |
| 42 | + exit 2 |
| 43 | + } |
| 44 | + |
| 45 | + quicktype_args+=( |
| 46 | + ## Main schema and its dependencies |
| 47 | + -s schema build-aux/json-schema/obs-services.json |
| 48 | + -S build-aux/json-schema/service.json |
| 49 | + -S build-aux/json-schema/protocolDefs.json |
| 50 | + -S build-aux/json-schema/codecDefs.json |
| 51 | + ## Language and language options |
| 52 | + -l cpp --no-boost |
| 53 | + --code-format with-struct |
| 54 | + --type-style pascal-case |
| 55 | + --member-style camel-case |
| 56 | + --enumerator-style upper-underscore-case |
| 57 | + # Global include for Nlohmann JSON |
| 58 | + --include-location global-include |
| 59 | + # Namespace |
| 60 | + --namespace OBSServices |
| 61 | + # Main struct type name |
| 62 | + -t ServicesJson |
| 63 | + ) |
| 64 | + |
| 65 | + if (( check_only )) { |
| 66 | + if (( _loglevel > 1 )) log_info "Checking obs-services JSON parser code..." |
| 67 | + |
| 68 | + if ! quicktype ${quicktype_args} | diff -q "${json_parser_file}" - &> /dev/null; then |
| 69 | + log_error "obs-services JSON parser code does not match JSON schemas." |
| 70 | + if (( fail_on_error )) return 2; |
| 71 | + else |
| 72 | + if (( _loglevel > 1 )) log_status "obs-services JSON parser code matches JSON schemas." |
| 73 | + fi |
| 74 | + } else { |
| 75 | + if ! quicktype ${quicktype_args} -o "${json_parser_file}"; then |
| 76 | + log_error "An error occured while generating obs-services JSON parser code." |
| 77 | + if (( fail_on_error )) return 2; |
| 78 | + fi |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +run_quicktype() { |
| 83 | + if (( ! ${+SCRIPT_HOME} )) typeset -g SCRIPT_HOME=${ZSH_ARGZERO:A:h} |
| 84 | + |
| 85 | + typeset -g host_os=${${(L)$(uname -s)}//darwin/macos} |
| 86 | + local -i fail_on_error=0 |
| 87 | + local -i check_only=0 |
| 88 | + local -i verbosity=1 |
| 89 | + local -r _version='1.0.0' |
| 90 | + |
| 91 | + fpath=("${SCRIPT_HOME}/.functions" ${fpath}) |
| 92 | + autoload -Uz set_loglevel log_info log_error log_output log_status log_warning |
| 93 | + |
| 94 | + local -r _usage=" |
| 95 | +Usage: %B${functrace[1]%:*}%b <option> |
| 96 | +
|
| 97 | +%BOptions%b: |
| 98 | +
|
| 99 | +%F{yellow} Formatting options%f |
| 100 | + ----------------------------------------------------------------------------- |
| 101 | + %B-c | --check%b Check only, no actual formatting takes place |
| 102 | +
|
| 103 | +%F{yellow} Output options%f |
| 104 | + ----------------------------------------------------------------------------- |
| 105 | + %B-v | --verbose%b Verbose (more detailed output) |
| 106 | + %B--fail-[never|error] Fail script never/on formatting change - default: %B%F{green}never%f%b |
| 107 | + %B--debug%b Debug (very detailed and added output) |
| 108 | +
|
| 109 | +%F{yellow} General options%f |
| 110 | + ----------------------------------------------------------------------------- |
| 111 | + %B-h | --help%b Print this usage help |
| 112 | + %B-V | --version%b Print script version information" |
| 113 | + |
| 114 | + local -a args |
| 115 | + while (( # )) { |
| 116 | + case ${1} { |
| 117 | + --) |
| 118 | + shift |
| 119 | + args+=($@) |
| 120 | + break |
| 121 | + ;; |
| 122 | + -c|--check) check_only=1; shift ;; |
| 123 | + -v|--verbose) (( verbosity += 1 )); shift ;; |
| 124 | + -h|--help) log_output ${_usage}; exit 0 ;; |
| 125 | + -V|--version) print -Pr "${_version}"; exit 0 ;; |
| 126 | + --debug) verbosity=3; shift ;; |
| 127 | + --fail-never) |
| 128 | + fail_on_error=0 |
| 129 | + shift |
| 130 | + ;; |
| 131 | + --fail-error) |
| 132 | + fail_on_error=1 |
| 133 | + shift |
| 134 | + ;; |
| 135 | + --fail-fast) |
| 136 | + fail_on_error=2 |
| 137 | + shift |
| 138 | + ;; |
| 139 | + *) log_error "Unknown option: %B${1}%b"; log_output ${_usage}; exit 2 ;; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + set -- ${(@)args} |
| 144 | + set_loglevel ${verbosity} |
| 145 | + |
| 146 | + invoke_quicktype |
| 147 | +} |
| 148 | + |
| 149 | +run_quicktype ${@} |
0 commit comments