Skip to content

Commit

Permalink
Add css.TailwindCSS
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jun 24, 2024
1 parent c94ea56 commit 366f857
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 21 deletions.
76 changes: 59 additions & 17 deletions common/hexec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"

"github.com/cli/safeexec"
"github.com/gohugoio/hugo/config"
Expand Down Expand Up @@ -84,7 +86,7 @@ var WithEnviron = func(env []string) func(c *commandeer) {
}

// New creates a new Exec using the provided security config.
func New(cfg security.Config) *Exec {
func New(cfg security.Config, workingDir string) *Exec {
var baseEnviron []string
for _, v := range os.Environ() {
k, _ := config.SplitEnvVar(v)
Expand All @@ -95,6 +97,7 @@ func New(cfg security.Config) *Exec {

return &Exec{
sc: cfg,
workingDir: workingDir,
baseEnviron: baseEnviron,
}
}
Expand All @@ -119,15 +122,23 @@ func SafeCommand(name string, arg ...string) (*exec.Cmd, error) {

// Exec enforces a security policy for commands run via os/exec.
type Exec struct {
sc security.Config
sc security.Config
workingDir string

// os.Environ filtered by the Exec.OsEnviron whitelist filter.
baseEnviron []string

npxInit sync.Once
npxAvailable bool
}

func (e *Exec) New(name string, arg ...any) (Runner, error) {
return e.new(name, "", arg...)
}

// New will fail if name is not allowed according to the configured security policy.
// Else a configured Runner will be returned ready to be Run.
func (e *Exec) New(name string, arg ...any) (Runner, error) {
func (e *Exec) new(name string, fullyQualifiedName string, arg ...any) (Runner, error) {
if err := e.sc.CheckAllowedExec(name); err != nil {
return nil, err
}
Expand All @@ -136,27 +147,51 @@ func (e *Exec) New(name string, arg ...any) (Runner, error) {
copy(env, e.baseEnviron)

cm := &commandeer{
name: name,
env: env,
name: name,
fullyQualifiedName: fullyQualifiedName,
env: env,
}

return cm.command(arg...)
}

// Npx will try to run npx, and if that fails, it will
// try to run the binary directly.
// Npx will in order:
// 1. Try fo find the binary in the WORKINGDIR/node_modules/.bin directory.
// 2. If not found, and npx is available, run npx --no-install <name> <args>.
// 3. Fall back to the PATH.
func (e *Exec) Npx(name string, arg ...any) (Runner, error) {
r, err := e.npx(name, arg...)
// npx is slow, so first try the common case.
nodeBinFilename := filepath.Join(e.workingDir, nodeModulesBinPath, name)
_, err := safeexec.LookPath(nodeBinFilename)
if err == nil {
return r, nil
return e.new(name, nodeBinFilename, arg...)
}
e.checkNpx()
if e.npxAvailable {
r, err := e.npx(name, arg...)
if err == nil {
return r, nil
}
}
return e.New(name, arg...)
}

const (
npxNoInstall = "--no-install"
npxBinary = "npx"
nodeModulesBinPath = "node_modules/.bin"
)

func (e *Exec) checkNpx() {
e.npxInit.Do(func() {
e.npxAvailable = InPath(npxBinary)
})
}

// npx is a convenience method to create a Runner running npx --no-install <name> <args.
func (e *Exec) npx(name string, arg ...any) (Runner, error) {
arg = append(arg[:0], append([]any{"--no-install", name}, arg[0:]...)...)
return e.New("npx", arg...)
arg = append(arg[:0], append([]any{npxNoInstall, name}, arg[0:]...)...)
return e.New(npxBinary, arg...)
}

// Sec returns the security policies this Exec is configured with.
Expand Down Expand Up @@ -209,8 +244,9 @@ type commandeer struct {
dir string
ctx context.Context

name string
env []string
name string
fullyQualifiedName string
env []string
}

func (c *commandeer) command(arg ...any) (*cmdWrapper, error) {
Expand All @@ -230,10 +266,16 @@ func (c *commandeer) command(arg ...any) (*cmdWrapper, error) {
}
}

bin, err := safeexec.LookPath(c.name)
if err != nil {
return nil, &NotFoundError{
name: c.name,
var bin string
if c.fullyQualifiedName != "" {
bin = c.fullyQualifiedName
} else {
var err error
bin, err = safeexec.LookPath(c.name)
if err != nil {
return nil, &NotFoundError{
name: c.name,
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion config/allconfig/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ func (l *configLoader) loadModules(configs *Configs) (modules.ModulesConfig, *mo
ignoreVendor, _ = hglob.GetGlob(hglob.NormalizePath(s))
}

ex := hexec.New(conf.Security)
ex := hexec.New(conf.Security, workingDir)

hook := func(m *modules.ModulesConfig) error {
for _, tc := range m.AllModules {
Expand Down
1 change: 1 addition & 0 deletions config/security/securityConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var DefaultConfig = Config{
"^go$", // for Go Modules
"^npx$", // used by all Node tools (Babel, PostCSS).
"^postcss$",
"^tailwindcss$",
),
// These have been tested to work with Hugo's external programs
// on Windows, Linux and MacOS.
Expand Down
2 changes: 1 addition & 1 deletion deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (d *Deps) Init() error {
}

if d.ExecHelper == nil {
d.ExecHelper = hexec.New(d.Conf.GetConfigSection("security").(security.Config))
d.ExecHelper = hexec.New(d.Conf.GetConfigSection("security").(security.Config), d.Conf.WorkingDir())
}

if d.MemCache == nil {
Expand Down
2 changes: 1 addition & 1 deletion hugolib/integrationtest_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ func (s *IntegrationTestBuilder) initBuilder() error {
sc := security.DefaultConfig
sc.Exec.Allow, err = security.NewWhitelist("npm")
s.Assert(err, qt.IsNil)
ex := hexec.New(sc)
ex := hexec.New(sc, s.Cfg.WorkingDir)
command, err := ex.New("npm", "install")
s.Assert(err, qt.IsNil)
s.Assert(command.Run(), qt.IsNil)
Expand Down
5 changes: 5 additions & 0 deletions resources/resource_transformers/postcss/postcss.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@ func (imp *importResolver) shouldImport(s string) bool {
return false
}

// TODO1
if strings.Contains(s, "tailwindcss") {
return false
}

return shouldImportRe.MatchString(s)
}

Expand Down
141 changes: 141 additions & 0 deletions resources/resource_transformers/postcss/tailwindcss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package postcss

import (
"bytes"
"fmt"
"io"

"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/hexec"
"github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/resources"
"github.com/gohugoio/hugo/resources/internal"
"github.com/gohugoio/hugo/resources/resource"
)

// NewTailwindCSSClient creates a new TailwindCSSClient with the given specification.
func NewTailwindCSSClient(rs *resources.Spec) *TailwindCSSClient {
return &TailwindCSSClient{rs: rs}
}

// Client is the client used to do TailwindCSS transformations.
type TailwindCSSClient struct {
rs *resources.Spec
}

// Process transforms the given Resource with the PostCSS processor.
func (c *TailwindCSSClient) Process(res resources.ResourceTransformer, options map[string]any) (resource.Resource, error) {
return res.Transform(&tailwindcssTransformation{rs: c.rs, optionsm: options})
}

type tailwindcssTransformation struct {
optionsm map[string]any
rs *resources.Spec
}

func (t *tailwindcssTransformation) Key() internal.ResourceTransformationKey {
return internal.NewResourceTransformationKey("tailwindcss", t.optionsm)
}

type TailwindCSSOptions struct{}

func debug(what ...any) {
fmt.Println(what...) // TODO1
}

func (t *tailwindcssTransformation) Transform(ctx *resources.ResourceTransformationCtx) error {
const binaryName = "tailwindcss"

debug(binaryName)

infol := t.rs.Logger.InfoCommand(binaryName)
infow := loggers.LevelLoggerToWriter(infol)

ex := t.rs.ExecHelper

// The tailwindcss CLI does not support streaming, so we need to write to temporary files.
// tempDir := os.TempDir()
// defer os.RemoveAll(tempDir)

// outFilename := filepath.Join(tempDir, "hugo-tailwind-out.css")
workingDir := t.rs.Cfg.BaseConfig().WorkingDir

var cmdArgs []any = []any{
"--input=-", // Read from stdin.
//"--output", outFilename,
//"--cwd", workingDir,
"--optimize", // TDOO1 opts also: minimize?
}

// TODO1
// npm i tailwindcss @tailwindcss/cli
// npm i tailwindcss@next @tailwindcss/cli@next
// npx tailwindcss -h

var errBuf bytes.Buffer

stderr := io.MultiWriter(infow, &errBuf)
cmdArgs = append(cmdArgs, hexec.WithStderr(stderr))
cmdArgs = append(cmdArgs, hexec.WithStdout(ctx.To))
cmdArgs = append(cmdArgs, hexec.WithEnviron(hugo.GetExecEnviron(workingDir, t.rs.Cfg, t.rs.BaseFs.Assets.Fs)))

cmd, err := ex.Npx(binaryName, cmdArgs...)
if err != nil {
if hexec.IsNotFound(err) {
// This may be on a CI server etc. Will fall back to pre-built assets.
return &herrors.FeatureNotAvailableError{Cause: err}
}
return err
}

stdin, err := cmd.StdinPipe()
if err != nil {
return err
}

src := ctx.From

imp := newImportResolver(
ctx.From,
ctx.InPath,
Options{}, // TODO1
t.rs.Assets.Fs, t.rs.Logger, ctx.DependencyManager,
)

// TODO1 option {
src, err = imp.resolve()
if err != nil {
return err
}

go func() {
defer stdin.Close()
io.Copy(stdin, src)
}()

err = cmd.Run()
if err != nil {
if hexec.IsNotFound(err) {
return &herrors.FeatureNotAvailableError{
Cause: err,
}
}
return imp.toFileError(errBuf.String())
}

return nil
}
18 changes: 17 additions & 1 deletion tpl/css/css.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Namespace struct {
d *deps.Deps
scssClientLibSass *scss.Client
postcssClient *postcss.Client
tailwindcssClient *postcss.TailwindCSSClient
babelClient *babel.Client

// The Dart Client requires a os/exec process, so only
Expand Down Expand Up @@ -63,7 +64,21 @@ func (ns *Namespace) PostCSS(args ...any) (resource.Resource, error) {
return ns.postcssClient.Process(r, m)
}

// Sass processes the given Resource with Sass.
// TailwindCSS processes the given Resource with tailwindcss.
func (ns *Namespace) TailwindCSS(args ...any) (resource.Resource, error) {
if len(args) > 2 {
return nil, errors.New("must not provide more arguments than resource object and options")
}

r, m, err := resourcehelpers.ResolveArgs(args)
if err != nil {
return nil, err
}

return ns.tailwindcssClient.Process(r, m)
}

// Sass processes the given Resource with SASS.
func (ns *Namespace) Sass(args ...any) (resource.Resource, error) {
if len(args) > 2 {
return nil, errors.New("must not provide more arguments than resource object and options")
Expand Down Expand Up @@ -145,6 +160,7 @@ func init() {
d: d,
scssClientLibSass: scssClient,
postcssClient: postcss.New(d.ResourceSpec),
tailwindcssClient: postcss.NewTailwindCSSClient(d.ResourceSpec),
babelClient: babel.New(d.ResourceSpec),
}

Expand Down

0 comments on commit 366f857

Please sign in to comment.