Skip to content

Commit

Permalink
feature: allowing to specify the version of tailwind to be used.
Browse files Browse the repository at this point in the history
As requested in one of our issues this change allows to specify the
version of TailwindCSS to download.
  • Loading branch information
paganotoni committed Jul 19, 2024
1 parent e9a1e60 commit 79d1f8b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 28 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# Tailo

Tailo is a Go wrapper for the common operations with the TailwindCSS binary. It is intended to automate the process of installing the TailwindCSS binary, running it, and cleaning up the generated files.
Tailo is a Go wrapper for the common operations with the TailwindCSS binary. It is intended to automate the process of installing the TailwindCSS binary, running it, and cleaning up the generated files.

## Setup Command
The setup command is an easy way to invoke tailo from your application without creating a dependency on this package.

```sh
go run github.com/paganotoni/tailo/cmd/setup@latest // Using Latest Tailwind

// Or Using a specific version
go run github.com/paganotoni/tailo/cmd/setup@latest -version=v3.4.6
```

## Build Command
Like the Setup command the Build Command allows to specify the version of Tailwind to be used.

```sh
go run ./cmd/build -version=v3.4.6
```
18 changes: 8 additions & 10 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ import (
// CSS file and generate compiled CSS it expects to find
// the options in the config file.
func Build(options ...Option) {
err := Setup()
if err != nil {
// Applying passed options
for _, option := range options {
option()
}

if err := Setup(); err != nil {
fmt.Println("Error running the setup:", err.Error())
os.Exit(1)

return
}

// Applying passed options
for _, option := range options {
option()
}

if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
err := Setup()
if err != nil {
Expand All @@ -40,8 +39,7 @@ func Build(options ...Option) {

fmt.Println("[tailo] Running:", cmd.String())

err = cmd.Run()
if err != nil {
panic(err)
if err := cmd.Run(); err != nil {
fmt.Println("[tailo] Error running tailwindcss:", err)
}
}
12 changes: 7 additions & 5 deletions cmd/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ import (
)

var (
input string
output string
config string
binary string
input string
output string
config string
binary string
version string
)

func init() {
flag.StringVar(&input, "input", "internal/assets/application.css", "The CSS input file path.")
flag.StringVar(&output, "output", "public/application.css", "The CSS output file path.")
flag.StringVar(&config, "config", "tailwind.config.js", "The TailwindCSS configuration file path.")
flag.StringVar(&binary, "binary", "bin/tailwindcss", "The TailwindCSS CLI binary path.")
flag.StringVar(&version, "version", "", "The TailwindCSS version to use, defaults to empty which means latest.")
}

func main() {
flag.Parse()

tailo.Setup()
tailo.Build(
tailo.UseVersion(version),
tailo.UseInputPath(input),
tailo.UseOutputPath(output),
tailo.UseConfigPath(config),
Expand Down
25 changes: 22 additions & 3 deletions cmd/setup/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
package main

import "github.com/paganotoni/tailo"
import (
"flag"

"github.com/paganotoni/tailo"
)

var (
input string
output string
config string
binary string
version string
)

func init() {
flag.StringVar(&version, "version", "", "The TailwindCSS version to use, defaults to empty which means latest.")
}

func main() {
//TODO: allow flags?
tailo.Setup()
flag.Parse()

tailo.Setup(
tailo.UseVersion(version),
)
}
6 changes: 6 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ func UseBinaryPath(path string) Option {
binaryPath = path
}
}

func UseVersion(v string) Option {
return func() {
version = v
}
}
35 changes: 26 additions & 9 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,30 @@ import (
)

var (
version string
binaries = map[string]string{
"darwin_amd64": "tailwindcss-macos-x64",
"darwin_arm64": "tailwindcss-macos-arm64",
"linux_amd64": "tailwindcss-linux-x64",
"linux_arm64": "tailwindcss-linux-arm64",
"linux_arm": "tailwindcss-linux-armv7",
"windows_amd64": "tailwindcss-windows-x64",
"windows_arm64": "tailwindcss-windows-arm64",
"darwin_amd64": "macos-x64",
"darwin_arm64": "macos-arm64",
"linux_amd64": "linux-x64",
"linux_arm64": "linux-arm64",
"linux_arm": "linux-armv7",
"windows_amd64": "windows-x64",
"windows_arm64": "windows-arm64",
}

latestURL = "https://github.com/tailwindlabs/tailwindcss/releases/latest/download/tailwindcss-%s"
versionedURL = "https://github.com/tailwindlabs/tailwindcss/releases/download/%s/tailwindcss-%s"
)

// Setup downloads the Tailwind CSS CLI binary for the
// given operating system and architecture. It makes the
// binary executable and places it in the bin/ directory.
func Setup() error {
func Setup(options ...Option) error {
// Running options
for _, option := range options {
option()
}

if _, err := os.Stat("bin/tailwindcss"); err == nil {
fmt.Println("Tailwind CSS CLI binary already exists.")

Expand All @@ -39,7 +48,15 @@ func Setup() error {
return fmt.Errorf("Unsupported operating system and architecture: %s", currentArch)
}

url := fmt.Sprintf("https://github.com/tailwindlabs/tailwindcss/releases/latest/download/%v", binary)
url := latestURL
args := []any{binary}

if version != "" {
url = versionedURL
args = []any{version, binary}
}

url = fmt.Sprintf(url, args...)
fmt.Println("Downloading from:", url)

resp, err := http.Get(url)
Expand Down

0 comments on commit 79d1f8b

Please sign in to comment.