Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: add auto-gen for codegen scripts #47

Merged
merged 2 commits into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions pkg/generator/codegen_tmpls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package generator

const boilerplateTmpl = `/*
Copyright YEAR The {{.ProjectName}} Authors

Commercial software license.
*/
`

const updateGeneratedTmpl = `#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

DOCKER_REPO_ROOT="/go/src/{{.RepoPath}}"
IMAGE=${IMAGE:-"gcr.io/coreos-k8s-scale-testing/codegen"}

docker run --rm \
-v "$PWD":"$DOCKER_REPO_ROOT" \
-w "$DOCKER_REPO_ROOT" \
"$IMAGE" \
"/go/src/k8s.io/code-generator/generate-groups.sh" \
"all" \
"{{.RepoPath}}/pkg/generated" \
"{{.RepoPath}}/pkg/apis" \
"{{.APIDirName}}:{{.Version}}" \
--go-header-file "./hack/codegen/boilerplate.go.txt" \
$@
.
`
48 changes: 48 additions & 0 deletions pkg/generator/gen_codegen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package generator

import (
"io"
"text/template"
)

// Boilerplate contains all the customized data needed to generate codegen/boilerplate.go.txt
// for a new operator when pairing with boilerplateTmpl template.
type Boilerplate struct {
ProjectName string
}

// UpdateGenerated contains all the customized data needed to generate codegen/update-generated.sh
// for a new operator when pairing with updateGeneratedTmpl template.
type UpdateGenerated struct {
RepoPath string
APIDirName string
Version string
}

func renderBoilerplateFile(w io.Writer, projectName string) error {
t := template.New("codegen/boilerplate.go.txt")
t, err := t.Parse(boilerplateTmpl)
if err != nil {
return err
}

b := Boilerplate{
ProjectName: projectName,
}
return t.Execute(w, b)
}

func renderUpdateGeneratedFile(w io.Writer, repo, apiDirName, version string) error {
t := template.New("codegen/boilerplate.go.txt")
t, err := t.Parse(updateGeneratedTmpl)
if err != nil {
return err
}

b := UpdateGenerated{
RepoPath: repo,
APIDirName: apiDirName,
Version: version,
}
return t.Execute(w, b)
}