Skip to content

Commit

Permalink
refactor(nms): Make TypeScript generation script executable and fix P…
Browse files Browse the repository at this point in the history
…ackageType usage

Signed-off-by: Thomas Schmitt <thomas.schmitt@tngtech.com>
  • Loading branch information
thmsschmitt committed May 19, 2022
1 parent ee21d21 commit b49efa5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 32 deletions.
4 changes: 2 additions & 2 deletions nms/app/views/equipment/GatewayDetailConfigEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import type {
apn_resources,
challenge_key,
distribution_package,
enodeb_serials,
gateway_device,
gateway_dns_configs,
Expand All @@ -25,7 +26,6 @@ import type {
gateway_ran_configs,
lte_gateway,
magmad_gateway_configs,
package_type,
} from '../../../generated/MagmaAPIBindings';

import Accordion from '@material-ui/core/Accordion';
Expand Down Expand Up @@ -344,7 +344,7 @@ type Props = {
onSave: lte_gateway => void,
};

type VersionType = $PropertyType<package_type, 'version'>;
type VersionType = $PropertyType<distribution_package, 'version'>;

export function ConfigEdit(props: Props) {
const enqueueSnackbar = useEnqueueSnackbar();
Expand Down
82 changes: 55 additions & 27 deletions nms/scripts/generateAPIFromSwaggerForTypeScript.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
################################################################################
# Copyright 2022 The Magma Authors.

Expand All @@ -12,32 +12,58 @@
# limitations under the License.
################################################################################

set -e
set -euo pipefail

USAGE="generateAPIFromSwaggerForTypeScript.sh
— generates TypeScript NMS API bindings for swagger spec
usage() {
cat <<EOF
generateAPIFromSwaggerForTypeScript.sh
— generates TypeScript NMS API bindings for swagger spec
Usage:
generateAPIFromSwaggerForTypeScript.sh <input> <output>
Usage:
generateAPIFromSwaggerForTypeScript.sh [-f] <input> <output>
Options:
<input> Input swagger.yml file to read.
<input> Output file for js bindings.
"
Options:
if [ $# -ne 2 ]; then
echo "$USAGE"
exit 1
fi
-i swagger.yml file
-o Output directory for ts bindings
-f Overwrite files without confirmation
<input> Input swagger.yml file to read.
<output> Output directory for js bindings.
EOF
exit 2
}

FORCE=false
INPUT=""
OUTPUT=""
while getopts 'hfi:o:' option; do
case "$option" in
f) FORCE=true ;;
i) INPUT="${OPTARG}" ;;
o) OUTPUT="${OPTARG}" ;;
h | *) usage ;;
esac
done

INPUT=${1}
OUTPUT=${2}
echo "Input file: $INPUT";
echo "Output directory: $OUTPUT";
[[ -z "${INPUT}" ]] || [[ -z "${OUTPUT}" ]] && usage

yarn --silent openapi -i "${INPUT}" -o "${OUTPUT}"
if [ "$FORCE" = false ] && [ -d "${OUTPUT}" ] ; then
read -r -p "Are you sure you want to overwrite all files in ${OUTPUT} (y/N)? " REPLY
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
fi

(set -x;
yarn --silent openapi --client axios --useUnionTypes -i "${INPUT}" -o "${OUTPUT}")

HEADER='/**
addHeader() {
local file="$1"
TEMPORARY_FILE=$(mktemp)
cat <<EOF > "${TEMPORARY_FILE}"
/**
* Copyright 2022 The Magma Authors.
*
* This source code is licensed under the BSD-style license found in the
Expand All @@ -52,12 +78,14 @@ HEADER='/**
* @generated
* This file is generated by "nms/scripts/generateAPIFromSwaggerForTypeScript.sh".
*/
'
$(cat "${file}")
EOF
mv "${TEMPORARY_FILE}" "${file}"
}

# shellcheck disable=SC2044
for file in $(find "${OUTPUT}" -type f); do
TEMPORARY_FILE=$(mktemp)
(echo "${HEADER}"; cat "${file}") > "${TEMPORARY_FILE}" && mv "${TEMPORARY_FILE}" "${file}"
sed -i -e 's/?view=full(/_view_full(/g' "${file}"
sed -i -e 's/?verbose=false(/_verbose_false(/g' "${file}"
for file in "${OUTPUT}"/*.ts "${OUTPUT}"/**/*.ts; do
addHeader "${file}"
# TODO #12768 remove sed replacements
sed -i 's/?view=full(/_view_full(/g' "${file}"
sed -i 's/?verbose=false(/_verbose_false(/g' "${file}"
done
2 changes: 1 addition & 1 deletion orc8r/cloud/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,4 @@ nms_prereqs_osx:
nms_gen:
# generate Swagger API bindings for NMS
$(MAGMA_ROOT)/nms/scripts/generateAPIFromSwagger.sh $(SWAGGER_V1_YML) $(SWAGGER_NMS_OUT)
$(MAGMA_ROOT)/nms/scripts/generateAPIFromSwaggerForTypeScript.sh $(SWAGGER_V1_YML) $(SWAGGER_NMS_OUT_TS)
$(MAGMA_ROOT)/nms/scripts/generateAPIFromSwaggerForTypeScript.sh -f -i $(SWAGGER_V1_YML) -o $(SWAGGER_NMS_OUT_TS)
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func NewDefaultGatewayStatus(hardwareID string) *GatewayStatus {
},
PlatformInfo: &PlatformInfo{
VpnIP: "facebook.com",
Packages: []*Package{
Packages: []*DistributionPackage{
{
Name: "magma",
Version: "0.0.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ magma-gen-meta:
filename: machine_info_swaggergen.go
- go-struct-name: NetworkInterface
filename: network_interface_swaggergen.go
- go-struct-name: Package
- go-struct-name: DistributionPackage
filename: distribution_package_swaggergen.go
- go-struct-name: PlatformInfo
filename: platform_info_swaggergen.go
Expand Down

0 comments on commit b49efa5

Please sign in to comment.