Skip to content

Commit

Permalink
feat: add default generator (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
twelvelabs authored Oct 30, 2024
1 parent 7c95306 commit 71f7890
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/stamp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func NewApp(meta *AppMeta) (*App, error) {
return nil, fmt.Errorf("startup error: %w", err)
}
store := NewStore(storePath)
err = store.Init()
if err != nil {
return nil, err
}

app := &App{
Config: config,
Expand Down
5 changes: 5 additions & 0 deletions internal/stamp/generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Default Generator

This is the default generator that comes pre-installed with stamp. It generates new generators.

![yo dog](https://i.imgflip.com/98dgrp.jpg)
1 change: 1 addition & 0 deletions internal/stamp/generator/_src/_src/greeting.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{`{{ .Message }}`}} 👋
18 changes: 18 additions & 0 deletions internal/stamp/generator/_src/generator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# {{ "" }}yaml-language-server: $schema=https://raw.githubusercontent.com/twelvelabs/stamp/refs/heads/main/docs/stamp.schema.json
---
name: "{{ .GeneratorName }}"
description: "{{ .GeneratorName }} description."

values:
- key: Name
default: '{{`{{ env "USER" }}`}}'

- key: Message
default: "Hello, {{`{{ .Name }}`}}"

tasks:
- type: create
src:
path: greeting.txt
dst:
path: greeting.txt
24 changes: 24 additions & 0 deletions internal/stamp/generator/generator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/twelvelabs/stamp/refs/heads/main/docs/stamp.schema.json
---
name: generator
description: |
Generator for creating new generators.
values:
- key: GeneratorName
default: "{{ base .DstPath }}"

tasks:
- type: create
src:
path: _src
dst:
path: _src

- type: create
src:
path: generator.yaml
content_type: text
dst:
path: generator.yaml
content_type: text
19 changes: 19 additions & 0 deletions internal/stamp/store.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package stamp

import (
"embed"
"os"
"path/filepath"

"github.com/twelvelabs/stamp/internal/fsutil"
"github.com/twelvelabs/stamp/internal/pkg"
)

// Note: the `all:` prefix is required so that the
// dirs starting with `_` are included.
//
//go:embed all:generator
var defaultGen embed.FS

type Store struct {
*pkg.Store
}
Expand Down Expand Up @@ -32,6 +43,14 @@ func (s *Store) AsGenerators(packages []*pkg.Package, err error) ([]*Generator,
return NewGenerators(s, packages)
}

func (s *Store) Init() error {
defaultGenPath := filepath.Join(s.Store.BasePath, "generator")
if fsutil.PathExists(defaultGenPath) {
return nil
}
return os.CopyFS(s.Store.BasePath, defaultGen)
}

func (s *Store) Load(name string) (*Generator, error) {
return s.AsGenerator(s.Store.Load(name))
}
Expand Down
34 changes: 34 additions & 0 deletions internal/stamp/store_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package stamp

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/twelvelabs/termite/testutil"
)

// NewTestStore returns a new store pointing to ./testdata/generators.
Expand All @@ -15,6 +17,38 @@ func NewTestStore() *Store {
return NewStore(storePath)
}

func TestStore_Init(t *testing.T) {
testutil.InTempDir(t, func(tmpDir string) {
store := NewStore(tmpDir)

genDir := filepath.Join(tmpDir, "generator")
assert.NoDirExists(t, genDir)

err := store.Init()
assert.NoError(t, err)
assert.DirExists(t, genDir)
assert.DirExists(t, filepath.Join(genDir, "_src"))
assert.FileExists(t, filepath.Join(genDir, "generator.yaml"))
})
}

func TestStore_Init_WhenAlreadyExists(t *testing.T) {
testutil.InTempDir(t, func(tmpDir string) {
store := NewStore(tmpDir)

genDir := filepath.Join(tmpDir, "generator")
_ = os.Mkdir(genDir, 0777)
_ = os.WriteFile(filepath.Join(genDir, "hello.txt"), []byte("hi"), 0600)

err := store.Init()
assert.NoError(t, err)
assert.DirExists(t, genDir)
assert.FileExists(t, filepath.Join(genDir, "hello.txt"))
assert.NoDirExists(t, filepath.Join(genDir, "_src"))
assert.NoFileExists(t, filepath.Join(genDir, "generator.yaml"))
})
}

func TestStore_LoadAll(t *testing.T) {
store := NewStore("/some/path/that/does/not/exist")
items, err := store.LoadAll()
Expand Down

0 comments on commit 71f7890

Please sign in to comment.