################################################################ # Swaggy K: A Makefile for generating API clients using OpenAPI Generator # https://github.com/cliffano/swaggy-k ################################################################ # The version of Swaggy K SWAGGY_K_VERSION = 0.9.0-pre.0 # The version of Kiota (https://learn.microsoft.com/en-us/openapi/kiota/overview) used for generating the API clients KIOTA_VERSION = 1.11.1 # LANGS_ALL lists the languages supported by the given KIOTA_VERSION LANGS_ALL = csharp go java php python ruby cli swift typescript # LANGS_PRIMARY lists the languages which will be built and published to public package registries LANGS_PRIMARY = typescript python ruby # The location where OpenAPI specification file will be placed within the project LOCAL_SPEC_PATH = stage/specification.yml ################################################################ # User configuration variables # These variables should be stored in swaggy-k.yml config file, # and they will be parsed using yq https://github.com/mikefarah/yq # Example: # --- # spec_uri: specification/someapp.yml # name: someapp # version:1.2.3 # base_dir: # github_actions: /home/runner/work/someapp/someapp # local: /home/someuser/someapp # SPEC_URI is the file path or URL where the OpenAPI specification is located, for example: # - local file path: spec/some-app.yaml # - remote URL: https://some-app.com/some-app.yaml SPEC_URI=$(shell yq .spec_uri swaggy-k.yml) # APP_NAME is name of the application using Swaggy K APP_NAME ?= $(shell yq .name swaggy-k.yml) # APP_VERSION is version of the application using Swaggy K APP_VERSION ?= $(shell yq .version swaggy-k.yml) # Contact details to be amended to the OpenAPI specification .info.contact.* properties CONTACT_NAME ?= $(shell yq .contact.name swaggy-k.yml) CONTACT_URL ?= $(shell yq .contact.url swaggy-k.yml) CONTACT_EMAIL ?= $(shell yq .contact.email swaggy-k.yml) # APP_BASE_DIR is the absolute path where the application base directory is located, for example: # - MacOS user workspace directory: /Users/some-user/some-path/some-app # - GitHub Actions directory: /home/runner/work/some-app/some-app ifdef GITHUB_ACTIONS APP_BASE_DIR=$(shell yq .base_dir.github_actions swaggy-k.yml) else APP_BASE_DIR=$(shell yq .base_dir.local swaggy-k.yml) endif $(info ################################################################) $(info Building Swaggy K application with user configurations:) $(info - OpenAPI specification URI: ${SPEC_URI}) $(info - Application name: ${APP_NAME}) $(info - Application version: ${APP_VERSION}) $(info - Application base directory: ${APP_BASE_DIR}) ################################################################ # Base targets # CI target to be executed by CI/CD tool ci: clean deps init-spec generate test-typescript test-python test-ruby doc # Ensure stage directory exists stage: mkdir -p stage # Remove all generated API clients code clean: rm -rf stage/ clients/*/generated # Retrieve the OpenAPI Generator Docker image and npm modules deps: docker pull mcr.microsoft.com/openapi/kiota:$(KIOTA_VERSION) npm install -g bootprint bootprint-openapi gh-pages mocha # Initialise OpenAPI specification from either a local file path or a remote URL # This target requires the following parameters to be supplied by user # - SPEC_URI parameter # - CONTACT_NAME parameter # - CONTACT_ parameter # - CONTACT_NAME parameter init-spec: stage if test $(findstring https, $(SPEC_URI)); then \ curl $(SPEC_URI) --output $(LOCAL_SPEC_PATH); \ else \ cp $(SPEC_URI) $(LOCAL_SPEC_PATH); \ fi yq -i '.info.contact.name = "$(CONTACT_NAME)" | .info.contact.url = "$(CONTACT_URL)" | .info.contact.email = "$(CONTACT_EMAIL)"' "$(LOCAL_SPEC_PATH)" # Update Makefile to the latest version on origin's main branch update-to-latest: curl https://raw.githubusercontent.com/cliffano/swaggy-k/main/src/Makefile-swaggy-k -o Makefile # Update Makefile to the version defined in TARGET_SWAGGY_K_VERSION parameter update-to-version: curl https://raw.githubusercontent.com/cliffano/swaggy-k/v$(TARGET_SWAGGY_K_VERSION)/src/Makefile-swaggy-k -o Makefile ################################################################ # API clients generate targets # Alias for generate-all target generate: generate-all # Generate API clients for all languages, this is separate from generate-primary target in order to # reduce the build time when processing primary languages # This target requires APP_BASE_DIR parameter to be supplied by user generate-all: for lang in ${LANGS_ALL} ; do \ docker \ run \ --rm \ -v $(APP_BASE_DIR)/clients/$$lang/generated:/app/output \ -v $(APP_BASE_DIR)/$(LOCAL_SPEC_PATH):/app/openapi.yaml \ mcr.microsoft.com/openapi/kiota:$(KIOTA_VERSION) \ generate \ -c $(APP_NAME) \ --language $$lang; \ docker \ run \ --rm \ -v $(APP_BASE_DIR)/clients/$$lang/generated:/app/output \ -v $(APP_BASE_DIR)/$(LOCAL_SPEC_PATH):/app/openapi.yaml \ mcr.microsoft.com/openapi/kiota:$(KIOTA_VERSION) \ info \ --language $$lang \ | sed -z 's/.*Example\://' > clients/$$lang/generated/deps.sh; \ chmod +x clients/$$lang/generated/deps.sh; \ done # Generate API clients for primary languages only # This target requires APP_BASE_DIR parameter to be supplied by user generate-primary: for lang in ${LANGS_PRIMARY} ; do \ docker \ run \ --rm \ -v $(APP_BASE_DIR)/clients/$$lang/generated:/app/output \ -v $(APP_BASE_DIR)/$(LOCAL_SPEC_PATH):/app/openapi.yaml \ mcr.microsoft.com/openapi/kiota:$(KIOTA_VERSION) \ generate \ -c $(APP_NAME) \ --language $$lang; \ docker \ run \ --rm \ -v $(APP_BASE_DIR)/clients/$$lang/generated:/app/output \ -v $(APP_BASE_DIR)/$(LOCAL_SPEC_PATH):/app/openapi.yaml \ mcr.microsoft.com/openapi/kiota:$(KIOTA_VERSION) \ info \ --language $$lang \ | sed -z 's/.*Example\://' > clients/$$lang/generated/deps.sh; \ chmod +x clients/$$lang/generated/deps.sh; \ cd clients/$$lang/generated/ && \ zip ../../../stage/$$lang-$(APP_NAME)-$(APP_VERSION).zip -r ./ done ################################################################ # API clients testing targets for primary languages test-typescript: mkdir -p stage/typescript/ && \ unzip stage/typescript-$(APP_NAME)-$(APP_VERSION).zip -d stage/typescript/ cd stage/typescript/ && \ ./deps.sh test-python: mkdir -p stage/python/ && \ unzip stage/python-$(APP_NAME)-$(APP_VERSION).zip -d stage/python/ cd stage/python/ && \ ./deps.sh test-ruby: mkdir -p stage/ruby/ && \ unzip stage/ruby-$(APP_NAME)-$(APP_VERSION).zip -d stage/ruby/ cd stage/ruby/ && \ ./deps.sh ################################################################ # API clients package publishing targets for primary languages publish-typescript: cd clients/typescript/generated/ && \ npm publish publish-python: cd clients/python/generated/ && \ twine upload dist/* publish-ruby: cd clients/ruby/generated/ && \ gem push `ls *.gem` ################################################################ # Documentation targets # Alias for doc-latest target doc: doc-latest # Generate API documentation locally as the latest version doc-latest: bootprint openapi $(LOCAL_SPEC_PATH) doc/api/latest/ # Generate API documentation locally as the application's version # This target requires APP_VERSION parameter to be supplied by user doc-version: bootprint openapi $(LOCAL_SPEC_PATH) doc/api/$(APP_VERSION)/ # Publish documentation via GitHub Pages doc-publish: CACHE_DIR=/tmp gh-pages --dist doc/ ################################################################ .PHONY: all test ci stage clean deps init-spec generate generate-all generate-primary test-typescript test-python test-ruby publish-typescript publish-python publish-ruby doc doc-latest doc-version doc-publish