Skip to content

Commit

Permalink
Add Tailwind to advance options. (#233)
Browse files Browse the repository at this point in the history
* Add Tailwind to advance options
  • Loading branch information
PrajvalBadiger authored Jun 12, 2024
1 parent 64f74a8 commit 200939b
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 12 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ You can now use the `--advanced` flag when running the `create` command to get a
- [HTMX](https://htmx.org/) support using [Templ](https://templ.guide/)
- CI/CD workflow setup using [Github Actions](https://docs.github.com/en/actions)
- [Websocket](https://pkg.go.dev/nhooyr.io/websocket) sets up a websocket endpoint
- [Tailwind](https://tailwindcss.com/) Css framework

Note: selecting tailwind option automatically selects htmx.

<a id="blueprint-ui"></a>

Expand Down Expand Up @@ -165,9 +168,14 @@ For the websocket:
go-blueprint create --advanced --feature websocket
```

For Tailwind:
```bash
go-blueprint create --advanced --feature tailwind
```

Or all features at once:
```bash
go-blueprint create --name my-project --framework chi --driver mysql --advanced --feature htmx --feature githubaction --feature websocket
go-blueprint create --name my-project --framework chi --driver mysql --advanced --feature htmx --feature githubaction --feature websocket --feature tailwind
```

<p align="center">
Expand Down
6 changes: 6 additions & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ var createCmd = &cobra.Command{
fmt.Println(endingMsgStyle.Render("\nNext steps:"))
fmt.Println(endingMsgStyle.Render(fmt.Sprintf("• cd into the newly created project with: `cd %s`\n", project.ProjectName)))

if options.Advanced.Choices["Tailwind"] {
options.Advanced.Choices["Htmx"] = true
fmt.Println(endingMsgStyle.Render("• Install the tailwind standalone cli if you haven't already, grab the executable for your platform from the latest release on GitHub\n"))
fmt.Println(endingMsgStyle.Render("• More info about the Tailwind CLI: https://tailwindcss.com/blog/standalone-cli\n"))
}

if options.Advanced.Choices["Htmx"] {
fmt.Println(endingMsgStyle.Render("• Install the templ cli if you haven't already by running `go install github.com/a-h/templ/cmd/templ@latest`\n"))
fmt.Println(endingMsgStyle.Render("• Generate templ function files by running `templ generate`\n"))
Expand Down
3 changes: 2 additions & 1 deletion cmd/flags/advancedFeatures.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ const (
Htmx string = "htmx"
GoProjectWorkflow string = "githubaction"
Websocket string = "websocket"
Tailwind string = "tailwind"
)

var AllowedAdvancedFeatures = []string{string(Htmx), string(GoProjectWorkflow), string(Websocket)}
var AllowedAdvancedFeatures = []string{string(Htmx), string(GoProjectWorkflow), string(Websocket), string(Tailwind)}

func (f AdvancedFeatures) String() string {
return strings.Join(f, ",")
Expand Down
59 changes: 52 additions & 7 deletions cmd/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (p *Project) CreateMainFile() error {
// check if AbsolutePath exists
if _, err := os.Stat(p.AbsolutePath); os.IsNotExist(err) {
// create directory
if err := os.Mkdir(p.AbsolutePath, 0754); err != nil {
if err := os.Mkdir(p.AbsolutePath, 0o754); err != nil {
log.Printf("Could not create directory: %v", err)
return err
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func (p *Project) CreateMainFile() error {
// Create a new directory with the project name
projectPath := filepath.Join(p.AbsolutePath, p.ProjectName)
if _, err := os.Stat(projectPath); os.IsNotExist(err) {
err := os.MkdirAll(projectPath, 0751)
err := os.MkdirAll(projectPath, 0o751)
if err != nil {
log.Printf("Error creating root project directory %v\n", err)
return err
Expand Down Expand Up @@ -300,7 +300,6 @@ func (p *Project) CreateMainFile() error {

// Create correct docker compose for the selected driver
if p.DBDriver != "none" {

if p.DBDriver != "sqlite" {
p.createDockerMap()
p.Docker = p.DBDriver
Expand Down Expand Up @@ -394,6 +393,52 @@ func (p *Project) CreateMainFile() error {
return err
}

if p.AdvancedOptions[string(flags.Tailwind)] {
// select htmx option automatically since tailwind is selected
p.AdvancedOptions[string(flags.Htmx)] = true

tailwindConfigFile, err := os.Create(fmt.Sprintf("%s/tailwind.config.js", projectPath))
if err != nil {
cobra.CheckErr(err)
}
defer tailwindConfigFile.Close()

tailwindConfigTemplate := advanced.TailwindConfigTemplate()
err = os.WriteFile(fmt.Sprintf("%s/tailwind.config.js", projectPath), tailwindConfigTemplate, 0o644)
if err != nil {
return err
}

err = os.MkdirAll(fmt.Sprintf("%s/%s/assets/css", projectPath, cmdWebPath), 0o755)
if err != nil {
cobra.CheckErr(err)
}

inputCssFile, err := os.Create(fmt.Sprintf("%s/%s/assets/css/input.css", projectPath, cmdWebPath))
if err != nil {
cobra.CheckErr(err)
}
defer inputCssFile.Close()

inputCssTemplate := advanced.InputCssTemplate()
err = os.WriteFile(fmt.Sprintf("%s/%s/assets/css/input.css", projectPath, cmdWebPath), inputCssTemplate, 0o644)
if err != nil {
return err
}

outputCssFile, err := os.Create(fmt.Sprintf("%s/%s/assets/css/output.css", projectPath, cmdWebPath))
if err != nil {
cobra.CheckErr(err)
}
defer outputCssFile.Close()

outputCssTemplate := advanced.OutputCssTemplate()
err = os.WriteFile(fmt.Sprintf("%s/%s/assets/css/output.css", projectPath, cmdWebPath), outputCssTemplate, 0o644)
if err != nil {
return err
}
}

if p.AdvancedOptions[string(flags.Htmx)] {
// create folders and hello world file
err = p.CreatePath(cmdWebPath, projectPath)
Expand All @@ -407,7 +452,7 @@ func (p *Project) CreateMainFile() error {
}
defer helloTemplFile.Close()

//inject hello.templ template
// inject hello.templ template
helloTemplTemplate := template.Must(template.New("hellotempl").Parse((string(advanced.HelloTemplTemplate()))))
err = helloTemplTemplate.Execute(helloTemplFile, p)
if err != nil {
Expand All @@ -426,7 +471,7 @@ func (p *Project) CreateMainFile() error {
return err
}

err = os.MkdirAll(fmt.Sprintf("%s/%s/assets/js", projectPath, cmdWebPath), 0755)
err = os.MkdirAll(fmt.Sprintf("%s/%s/assets/js", projectPath, cmdWebPath), 0o755)
if err != nil {
cobra.CheckErr(err)
}
Expand All @@ -438,7 +483,7 @@ func (p *Project) CreateMainFile() error {
defer htmxMinJsFile.Close()

htmxMinJsTemplate := advanced.HtmxJSTemplate()
err = os.WriteFile(fmt.Sprintf("%s/%s/assets/js/htmx.min.js", projectPath, cmdWebPath), htmxMinJsTemplate, 0644)
err = os.WriteFile(fmt.Sprintf("%s/%s/assets/js/htmx.min.js", projectPath, cmdWebPath), htmxMinJsTemplate, 0o644)
if err != nil {
return err
}
Expand Down Expand Up @@ -622,7 +667,7 @@ func (p *Project) CreateMainFile() error {
func (p *Project) CreatePath(pathToCreate string, projectPath string) error {
path := filepath.Join(projectPath, pathToCreate)
if _, err := os.Stat(path); os.IsNotExist(err) {
err := os.MkdirAll(path, 0751)
err := os.MkdirAll(path, 0o751)
if err != nil {
log.Printf("Error creating directory %v\n", err)
return err
Expand Down
5 changes: 5 additions & 0 deletions cmd/steps/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func InitSteps(projectType flags.Framework, databaseType flags.Database) *Steps
Title: "Websocket endpoint",
Desc: "Add a websocket endpoint",
},
{
Flag: "Tailwind",
Title: "TailwindCSS",
Desc: "A utility-first CSS framework (selecting this will automatically add HTMX)",
},
},
},
},
Expand Down
1 change: 1 addition & 0 deletions cmd/template/advanced/files/htmx/base.templ.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ templ Base() {
<head>
<meta charset="utf-8"/>
<title>Go Blueprint Hello</title>
<link href="assets/css/output.css" rel="stylesheet"/>
<script src="assets/js/htmx.min.js"></script>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion cmd/template/advanced/files/htmx/hello.templ.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package web
templ HelloForm() {
@Base() {
<form hx-post="/hello" method="POST" hx-target="#hello-container">
<input id="name" name="name" type="text"/>
<input {{if .AdvancedOptions.tailwind}}class="border" {{end}}id="name" name="name" type="text"/>
<button type="submit">Submit</button>
</form>
<div id="hello-container"></div>
Expand Down
3 changes: 3 additions & 0 deletions cmd/template/advanced/files/tailwind/input.css.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
Empty file.
11 changes: 11 additions & 0 deletions cmd/template/advanced/files/tailwind/tailwind.config.js.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./cmd/web/**/*.html", "./cmd/web/**/*.templ",
],
theme: {
extend: {},
},
plugins: [],
}

23 changes: 21 additions & 2 deletions cmd/template/advanced/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ var helloTemplTemplate []byte
//go:embed files/htmx/base.templ.tmpl
var baseTemplTemplate []byte

//go:embed files/tailwind/tailwind.config.js.tmpl
var tailwindConfigTemplate []byte

//go:embed files/tailwind/input.css.tmpl
var inputCssTemplate []byte

//go:embed files/tailwind/output.css.tmpl
var outputCssTemplate []byte

//go:embed files/htmx/htmx.min.js.tmpl
var htmxMinJsTemplate []byte

Expand Down Expand Up @@ -55,7 +64,6 @@ var fiberHtmxTemplImports []byte
//go:embed files/websocket/imports/fiber.tmpl
var fiberWebsocketTemplImports []byte


func EchoHtmxTemplRoutesTemplate() []byte {
return echoHtmxTemplRoutes
}
Expand Down Expand Up @@ -96,6 +104,18 @@ func BaseTemplTemplate() []byte {
return baseTemplTemplate
}

func TailwindConfigTemplate() []byte {
return tailwindConfigTemplate
}

func InputCssTemplate() []byte {
return inputCssTemplate
}

func OutputCssTemplate() []byte {
return outputCssTemplate
}

func HtmxJSTemplate() []byte {
return htmxMinJsTemplate
}
Expand Down Expand Up @@ -123,4 +143,3 @@ func FiberHtmxTemplImportsTemplate() []byte {
func FiberWebsocketTemplImportsTemplate() []byte {
return fiberWebsocketTemplImports
}

1 change: 1 addition & 0 deletions cmd/template/framework/files/makefile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ all: build
build:
@echo "Building..."
{{if .AdvancedOptions.htmx}}@templ generate{{end}}
{{if .AdvancedOptions.tailwind}}@tailwindcss -i cmd/web/assets/css/input.css -o cmd/web/assets/css/output.css{{end}}
@go build -o main cmd/api/main.go

# Run the application
Expand Down

0 comments on commit 200939b

Please sign in to comment.