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

added new react templates, github CI and terraform generators #72

Merged
merged 6 commits into from
Nov 10, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ main-packr.go
packrd
/commit0
.history/
tmp/
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ The best way then to use this is to add an alias, then you can use the CLI as if
## Usage

1) To create a project run `commit0 create [PROJECT_NAME]`
2) A folder will be created and within that update the `commit0.yml` and then run `commit0 generate`
2) A folder will be created and within that update the `commit0.yml` and then run `commit0 generate -c <commit0.yml>`
3) You will see that there is now an idl folder created.
4) Within the idl folder modify the the protobuf services generated with your desired methods
5) Go up to the parent directory and re run `commit0 generate -l=[LANGUAGE OF CHOICE]`
5) Go up to the parent directory and re run `commit0 generate -c <commit0.yml>`
6) You will now see a `server` folder navigate to your service folder within that directory and implement the methods generated for it
7) Once you have tested your implementation and are happy with it return to the idl repo push that directory up to git
8) Return to the parent directory and check the depency file, for go it will be the go.mod file remove the lines that point it to your local directory, this will now point it to the version on git that was pushed up previously
Expand Down Expand Up @@ -66,13 +66,41 @@ this will create a commit0 executable in your working direcory. To install insta
make install-go
```

Compile a new `commit0` binary in the working directory
```
make build
```

Now you can either add your project directory to your path or just execute it directly
```
mkdir tmp
cd tmp
../commit0 create test-app
cd test-app
../../commit0 generate -c commit0.yml
```

### Architecture
The project is built with GoLang and requires Docker
- /cmd - the CLI command entry points
- /internal/generate
- /internal/config
- /internal/templator - the templating service

Example Flow:
The application starts at `cmd/generate.go`
1. loads all the templates from packr
- TODO: eventually this should be loaded remotely throug a dependency management system
2. loads the config from the commit0.yml config file
3. based on the configs, run the appropriate generators
- templator is passed in to the Generate function for dependency injection
4. each generator (`react/generate.go`, `ci/generate.go` etc) further delegates and actually executes the templating based on the configs passed in.
- `internal/templator/templator.go` is the base class and includes generic templating handling logic
- it CI is required, it'll also call a CI generator and pass in the service specific CI configs
- TOOD: CI templates have to call separate templates based on the context
- TODO: templator should be generic and not have any knowledge of the specific templating implementation (go, ci etc), move that logic upstream
5. Depending on the config (`deploy == true` for certain) it'll also run the `Execute` function and actually deploy the infrastructure

### Building locally

As the templates are embeded into the binary you will need to ensure packr2 is installed.
Expand Down
6 changes: 6 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ type aws struct {
AccountId string `yaml:"accountId"`
Region string
EKS eks
Cognito bool
Terraform terraform
}

type terraform struct {
RemoteState bool
}

type eks struct {
Expand Down
18 changes: 0 additions & 18 deletions internal/config/react.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@ type reactApp struct {
Name string
}

type reactHeader struct {
Enabled bool
}

type reactSidenavItem struct {
Path string
Label string
Icon string
}
type reactSidenav struct {
Enabled bool
Items []reactSidenavItem
}

type reactAccount struct {
Enabled bool
Required bool
Expand All @@ -31,9 +17,5 @@ type reactView struct {
type frontend struct {
Framework string
App reactApp
Account reactAccount
Header reactHeader
Sidenav reactSidenav
Views []reactView
CI CI
}
5 changes: 5 additions & 0 deletions internal/generate/ci/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (e *CIGenerationError) Error() string {
return fmt.Sprintf("Error: %s. Unable to Generate CI/CD Pipeline with config:\n%v\n", e.err, e.config)
}

// TODO shouldn't have to pass in both ciConfig, and cfg, it's redundant.
// Generate a CI configuration file based on your language and CI system
func Generate(t *templator.CITemplator, cfg *config.Commit0Config, ciConfig config.CI, basePath string, wg *sync.WaitGroup) error {

Expand All @@ -46,6 +47,10 @@ func Generate(t *templator.CITemplator, cfg *config.Commit0Config, ciConfig conf
ciConfigPath = basePath
ciFilename = ".travis.yml"
ciTemp = t.TravisCI
case "github":
ciConfigPath = fmt.Sprintf("%s/%s", basePath, ".github/workflow/")
ciFilename = "config.yml"
ciTemp = t.Github
default:
return &CIGenerationError{"Unsupported CI System", ciConfig}
}
Expand Down
9 changes: 7 additions & 2 deletions internal/generate/generate_helper.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package generate

import (
"log"
"sync"

"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/generate/golang"
"github.com/commitdev/commit0/internal/generate/kubernetes"
"github.com/commitdev/commit0/internal/generate/proto"
"github.com/commitdev/commit0/internal/generate/react"
"github.com/commitdev/commit0/internal/generate/terraform"
"github.com/commitdev/commit0/internal/templator"
"github.com/commitdev/commit0/internal/util"
"github.com/kyokomi/emoji"
"github.com/logrusorgru/aurora"
"log"
"sync"
)

func GenerateArtifactsHelper(t *templator.Templator, cfg *config.Commit0Config, pathPrefix string) {
Expand Down Expand Up @@ -47,6 +49,9 @@ func GenerateArtifactsHelper(t *templator.Templator, cfg *config.Commit0Config,
react.Generate(t, cfg, &wg, pathPrefix)
}

log.Println(aurora.Cyan(emoji.Sprintf("Generating Terraform")))
terraform.Generate(t, cfg, &wg, pathPrefix)

util.TemplateFileIfDoesNotExist(pathPrefix, "README.md", t.Readme, &wg, templator.GenericTemplateData{*cfg})

// Wait for all the templates to be generated
Expand Down
14 changes: 14 additions & 0 deletions internal/generate/terraform/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package terraform

import (
"sync"

"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/templator"
)

func Generate(t *templator.Templator, cfg *config.Commit0Config, wg *sync.WaitGroup, pathPrefix string) {
data := templator.GenericTemplateData{*cfg}

t.Terraform.TemplateFiles(data, false, wg, pathPrefix)
}
27 changes: 26 additions & 1 deletion internal/templator/templator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type CITemplator struct {
CircleCI *template.Template
TravisCI *template.Template
Jenkins *template.Template
Github *template.Template
}

// DockerTemplator contains the templates relevent to docker
Expand Down Expand Up @@ -49,6 +50,7 @@ type Templator struct {
React *DirectoryTemplator
Kubernetes *DirectoryTemplator
CI *CITemplator
Terraform *DirectoryTemplator
}

func NewTemplator(box *packr.Box) *Templator {
Expand All @@ -61,7 +63,8 @@ func NewTemplator(box *packr.Box) *Templator {
GitIgnore: NewSingleFileTemplator(box, "util/gitignore.tmpl"),
Readme: NewSingleFileTemplator(box, "util/README.tmpl"),
Docker: NewDockerFileTemplator(box),
React: NewDirectoryTemplator(box, "react"),
React: NewEJSDirectoryTemplator(box, "react"),
Terraform: NewDirectoryTemplator(box, "terraform"),
Kubernetes: NewDirectoryTemplator(box, "kubernetes"),
CI: NewCITemplator(box),
}
Expand Down Expand Up @@ -115,10 +118,15 @@ func NewCITemplator(box *packr.Box) *CITemplator {
jenkinsTemplateSource, _ := box.FindString("ci/Jenkinsfile.tmpl")
jenkinsTemplate, _ := template.New("CIConfig").Parse(jenkinsTemplateSource)

githubTemplateSource, _ := box.FindString("ci/github.tmpl")
// Github also uses double curly braces for their templates
githubTemplate, _ := template.New("CIConfig").Delims("<%=", "%>").Parse(githubTemplateSource)

return &CITemplator{
CircleCI: circleciTemplate,
TravisCI: travisciTemplate,
Jenkins: jenkinsTemplate,
Github: githubTemplate,
}
}

Expand Down Expand Up @@ -156,6 +164,23 @@ func NewDirectoryTemplator(box *packr.Box, dir string) *DirectoryTemplator {
}
}

// TODO standardize and consolidate the templating syntax, also allow for a config struct to change delimiters
// NewEJSDirectoryTemplator
func NewEJSDirectoryTemplator(box *packr.Box, dir string) *DirectoryTemplator {
templates := []*template.Template{}
for _, file := range getFileNames(box, dir) {
templateSource, _ := box.FindString(file)
template, err := template.New(file).Delims("<%=", "%>").Funcs(util.FuncMap).Parse(templateSource)
if err != nil {
panic(err)
}
templates = append(templates, template)
}
return &DirectoryTemplator{
Templates: templates,
}
}

func getFileNames(box *packr.Box, dir string) []string {
keys := []string{}
box.WalkPrefix(dir, func(path string, info file.File) error {
Expand Down
29 changes: 29 additions & 0 deletions templates/ci/github.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Build and Deploy

on: push

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v1

# https://github.com/marketplace/actions/setup-node-js-for-use-with-actions
- name: Setup Node
uses: actions/setup-node@v1
with:
node-version: '12.x'

- run: npm install

- run: npm test

- run: npm run build

- name: upload build artifacts
uses: actions/upload-artifact@v1
with:
name: build
path: build
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not important now but you should make sure you have "add newline at end of file" turned in in your editor.

24 changes: 0 additions & 24 deletions templates/commit0/commit0.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,8 @@ frontend:
framework: {{.FrontendFramework}}
ci:
system: circleci
buildImage: react/react
buildTag: 1234
buildCommand: make build
testCommand: make test
app:
name: {{.ProjectName}}
header:
enabled: true
account:
enabled: true
required: false
sidenav:
enabled: true
items:
- path: /
label: Home
icon: home
- path: /account
label: Account
icon: account_circle
views:
- path: /account
component: account
- path: /
component: home


services:
{{range .Services}}
Expand Down
6 changes: 6 additions & 0 deletions templates/react/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# https://create-react-app.dev/docs/adding-custom-environment-variables
REACT_APP_HOST=http://localhost:3000
REACT_APP_AUTH_DOMAIN=
REACT_APP_AUTH_CLIENT_ID=
REACT_APP_GRAPHQL_HOST=http://localhost:4000
REACT_APP_FILE_HOST=http://localhost:4000
52 changes: 52 additions & 0 deletions templates/react/.github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Build and Deploy

on: push

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v1

# https://github.com/marketplace/actions/setup-node-js-for-use-with-actions
- name: Setup Node
uses: actions/setup-node@v1
with:
node-version: '12.x'

- run: npm install

- run: npm run build

- name: upload build artifacts
uses: actions/upload-artifact@v1
with:
name: build
path: build

publish:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
with:
fetch-depth: 1

- name: Download build artifacts
uses: actions/download-artifact@v1
with:
name: build

# https://github.com/opspresso/action-s3-sync
- name: Publish to AWS S3
uses: opspresso/action-s3-sync@master
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
ShahNewazKhan marked this conversation as resolved.
Show resolved Hide resolved
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: "us-west-2"
FROM_PATH: "./build"
DEST_PATH: "s3://<%= .Config.Frontend.App.Name %>"
OPTIONS: "--acl public-read"
5 changes: 4 additions & 1 deletion templates/react/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
/node_modules
/.pnp
.pnp.js
/package-lock.json

# testing
/coverage
test-report.xml

# production
/build

# misc
.DS_Store
.vscode
.env.local
.env.development.local
.env.test.local
.env.production.local
.idea/
.firebase/

npm-debug.log*
yarn-debug.log*
Expand Down
7 changes: 7 additions & 0 deletions templates/react/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
.vscode
build
.expo
.next
assets
static