Skip to content

Commit

Permalink
Fix bug in exclude and add argument for exclude-schema (#8)
Browse files Browse the repository at this point in the history
* Exclude use EqualFold instead of Contains. 
* Argument for exclude-schema
* Update readme
* Add workflows (Jenkins -> Github Action) and deploy to GHCR
* Fix bug in exclude logic (handle more than one exclude)

---------

Co-authored-by: Nils-Odd Solberg <33060326+nilsodd@users.noreply.github.com>
  • Loading branch information
trondsevre and nilsodd authored Jun 28, 2023
1 parent ba28d61 commit a1a427f
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 26 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Release

on:
release:
types: [published]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:

build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

steps:
- uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@v2.2.0
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
flavor: |
latest=false
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=raw,value=latest
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: VERSION=${{ github.event.release.tag_name }}
22 changes: 22 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: build
on:
push:

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.13.x'

- name: Install dependencies
run: go get .

- name: Build
run: go build -a -v
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@ Generates `GraphQL` schemas.

## Usage

### Exclude

To exclude classes, relations, and attributes, use the --exclude flag. It performs a case-insensitive match.

Example:
```
--exclude Fravar --exclude OTUngdom
```

To exclude from the schema, use the --exclude-schema flag.

Example:
```
--exclude-schema OTUngdom
```

## Install

Expand All @@ -19,12 +33,12 @@ Mount the directory where you want the generated source code to be written as `/

Linux / MacOS:
```bash
docker run -v $(pwd):/src fint/graphql-cli:latest <ARGS>
docker run -v $(pwd):/src ghcr.io/fintlabs/fint-graphql-cli:latest <ARGS>
```

Windows PowerShell:
```ps1
docker run -v ${pwd}:/src fint/graphql-cli:latest <ARGS>
docker run -v ${pwd}:/src ghcr.io/fintlabs/fint-graphql-cli:latest <ARGS>
```

### Source
Expand Down
4 changes: 4 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ var Commands = []cli.Command{
Name: "exclude, x",
Usage: "Classes to exclude from the generated schema and classes.",
},
cli.StringSliceFlag{
Name: "exclude-schema, xs",
Usage: "Classes to exclude from the generated schema and classes.",
},
},
},
{
Expand Down
70 changes: 46 additions & 24 deletions generate/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,34 @@ func CmdGenerate(c *cli.Context) {
classes = classesOrig
} else {
for _, class := range classesOrig {
for _, s := range exclude {
if !strings.Contains(class.Name, s) {
var rel []types.Association
for _, r := range class.Relations {
if !strings.Contains(r.Target, s) {
rel = append(rel, r)
} else {
fmt.Printf("Excluding %s.%s from %s.%s\n", r.TargetPackage, r.Target, class.Package, class.Name)
}
if !isStringInList(class.Name, exclude) {
var rel []types.Association
for _, r := range class.Relations {
if !isStringInList(r.Target, exclude) {
rel = append(rel, r)
} else {
fmt.Printf("Excluding %s.%s from %s.%s\n", r.TargetPackage, r.Target, class.Package, class.Name)
}
class.Relations = rel
var att []types.Attribute
for _, a := range class.Attributes {
if !strings.Contains(a.Type, s) {
att = append(att, a)
} else {
fmt.Printf("Excluding %s (%s) from %s.%s\n", a.Name, a.Type, class.Package, class.Name)
}
}
class.Relations = rel
var att []types.Attribute
for _, a := range class.Attributes {
if !isStringInList(a.Type, exclude) {
att = append(att, a)
} else {
fmt.Printf("Excluding %s (%s) from %s.%s\n", a.Name, a.Type, class.Package, class.Name)
}
class.Attributes = att
classes = append(classes, class)
} else {
fmt.Printf("Excluding class %s.%s\n", class.Package, class.Name)
}
class.Attributes = att
classes = append(classes, class)
} else {
fmt.Printf("Excluding class %s.%s\n", class.Package, class.Name)
}
}
}

setupGraphQlSchemaDirStructure()
generateGraphQlSchema(classes)
generateGraphQlSchema(classes, c)
generateGraphQlQueryResolver(classes)
generateGraphQlService(classes)
generateGraphQlResolver(classes)
Expand Down Expand Up @@ -107,14 +105,14 @@ func writeResolver(pkg string, className string, content []byte) error {
return writeFile(path, fmt.Sprintf("%sResolver.java", className), []byte(content))
}

func generateGraphQlSchema(classes []*types.Class) {
func generateGraphQlSchema(classes []*types.Class, cli *cli.Context) {

fmt.Println("Generating GraphQL Schema")

var roots []*types.Class

for _, c := range classes {
if !c.Abstract && includePackage(c.Package) {
if !c.Abstract && includePackage(c.Package) && !excludeFromSchema(cli, c.Name) {
fmt.Printf(" > Creating schema: %s.graphqls\n", c.Name)
schema := GetGraphQlSchema(c)
err := writeSchema(c.Package, c.Name, []byte(schema))
Expand Down Expand Up @@ -211,3 +209,27 @@ func setupGraphQlSchemaDirStructure() {
func includePackage(p string) bool {
return strings.Contains(p, "administrasjon") || strings.Contains(p, "utdanning") || strings.Contains(p, "felles")
}

func excludeFromSchema(c *cli.Context, p string) bool {
excludeSchema := c.StringSlice("exclude-schema")

if len(excludeSchema) == 0 {
return false
}

for _, s := range excludeSchema {
if strings.EqualFold(p, s) {
return true
}
}
return false
}

func isStringInList(s string, list []string) bool {
for _, v := range list {
if strings.EqualFold(s, v) {
return true
}
}
return false
}

0 comments on commit a1a427f

Please sign in to comment.