Skip to content

Commit

Permalink
Add config and templateDir tags (#41)
Browse files Browse the repository at this point in the history
* Add config and templateDir tags

* Remove unused print

* Add CI

* Add CI and test
  • Loading branch information
thucngyyen committed Apr 13, 2023
1 parent fb7e302 commit 64d17b3
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 3 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI

on: [push, pull_request]

jobs:
test:
# virtual environments: https://github.com/actions/virtual-environments
runs-on: ubuntu-20.04

steps:
# Caches and restores the bazelisk download directory.
- name: Cache bazelisk download
uses: actions/cache@v2
env:
cache-name: bazel-cache
with:
path: ~/.cache/bazelisk
key: ${{ runner.os }}-${{ env.cache-name }}-${{ github.ref }}
restore-keys: |
${{ runner.os }}-${{ env.cache-name }}-development
# Checks-out your repository under $GITHUB_WORKSPACE, which is the CWD for
# the rest of the steps
- uses: actions/checkout@v2

# Build
- name: Build all target
run: |
cd internal/test
bazel build //...
2 changes: 1 addition & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2019 OpenAPI-Generator-Bazel Contributors

load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
load("@rules_pkg//:pkg.bzl", "pkg_tar", "pkg_deb")

package(default_visibility = ["//visibility:public"])

Expand Down
11 changes: 10 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,14 @@
workspace(name = "openapi_tools_generator_bazel")

load("//:defs.bzl", "openapi_tools_generator_bazel_repositories")

openapi_tools_generator_bazel_repositories()

# Load rules_pkg
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_pkg",
url = "https://github.com/bazelbuild/rules_pkg/releases/download/0.3.0/rules_pkg-0.3.0.tar.gz",
sha256 = "6b5969a7acd7b60c02f816773b06fcf32fbe8ba0c7919ccdc2df4f8fb923804a",
)
load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
rules_pkg_dependencies()
19 changes: 18 additions & 1 deletion internal/openapi_generator.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,20 @@ def _new_generator_command(ctx, declared_dir, rjars):
gen_cmd += ' --type-mappings "{mappings}"'.format(
mappings = _comma_separated_pairs(ctx.attr.type_mappings),
)

gen_cmd += ' --reserved-words-mappings "{reserved_words_mappings}"'.format(
reserved_words_mappings = ",".join(ctx.attr.reserved_words_mappings),
)

if ctx.attr.config:
gen_cmd += " --config {config}".format(
config = ctx.attr.config.files.to_list()[0].path,
)
if ctx.attr.template_dir:
gen_cmd += " --template-dir {template_dir}".format(
template_dir = ctx.attr.template_dir.files.to_list()[0].path,
)

if ctx.attr.api_package:
gen_cmd += " --api-package {package}".format(
package = ctx.attr.api_package,
Expand Down Expand Up @@ -97,6 +106,12 @@ def _impl(ctx):
ctx.file.spec,
] + cjars.to_list() + rjars.to_list()

if ctx.attr.config:
inputs += ctx.attr.config.files.to_list()

if ctx.attr.template_dir:
inputs += ctx.attr.template_dir.files.to_list()

# TODO: Convert to run
ctx.actions.run_shell(
inputs = inputs,
Expand Down Expand Up @@ -157,6 +172,8 @@ _openapi_generator = rule(
".yml",
],
),
"template_dir": attr.label(allow_single_file = True),
"config": attr.label(allow_single_file = True),
"generator": attr.string(mandatory = True),
"api_package": attr.string(),
"invoker_package": attr.string(),
Expand Down
13 changes: 13 additions & 0 deletions internal/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,16 @@ openapi_generator(
],
)

openapi_generator(
name = "petstore_python_flask_with_config_tag",
generator = "python-flask",
spec = "petstore.yaml",
config = "config.yaml"
)

openapi_generator(
name = "petstore_python_flask_with_template_dir",
generator = "python-flask",
spec = "petstore.yaml",
template_dir = "python-templates"
)
10 changes: 10 additions & 0 deletions internal/test/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# openapi-generator configuration
---
generatorName: python-flask
packageName: openapi.bazel.test
enablePostProcessFile: false
globalProperties:
skipFormModel: false
additionalProperties:
generated: generated
packageRoot: openapi.bazel
3 changes: 3 additions & 0 deletions internal/test/python-templates/__init__.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @{{generated}}

GENERATOR_CHECKSUM = '{{generatorChecksum}}'

0 comments on commit 64d17b3

Please sign in to comment.