Skip to content

Commit

Permalink
scaffolder: assets moved
Browse files Browse the repository at this point in the history
the assets directory needs to be accessible from the main module. The
embed directive cannot embed files outside of the directory where
the source file exists. This is why we propose a new structure.

This will allow go installing the user named summon executable:

go install github.com/davidovich/summon-example-assets/summon@latest
  • Loading branch information
davidovich committed Nov 5, 2021
1 parent a45dfde commit c259051
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 18 deletions.
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [How it Works](#how-it-works)
- [Configuration](#configuration)
- [Data repository](#data-repository)
- [Migration from versions prior to v0.13.0](#migration-from-versions-prior-to-v0130)
- [Summon config File](#summon-config-file)
- [Build](#build)
- [Install](#install)
Expand Down Expand Up @@ -70,8 +71,11 @@ coding is necessary in the assert repo.

Summon also provides a boostrapping feature in the scaffold command.

Summon builds upon the new go 1.16 [embed.FS](https://pkg.go.dev/embed) feature used to pack assets in a
go binary. You then install this at destination using standard `go install`.
> New in v0.13.0
Summon builds upon the new go 1.16 [embed.FS](https://pkg.go.dev/embed) feature
used to pack assets in a go binary. You then install this at destination using
standard `go install`.

When you invoke this binary with a contained asset path, the invoked files are
placed locally and the summoned file path is returned so it can be consumed by
Expand Down Expand Up @@ -99,22 +103,31 @@ You will then have something resembling this structure:
.
├── Makefile
├── README.md
├── assets
│ └── summon.config.yaml
├── go.mod
└── summon.go
├── go.sum
└── summon
├── assets
│ └── summon.config.yaml
└── summon.go
```

There is an example setup at https://github.com/davidovich/summon-example-assets.

You just need to populate the `assets` directory with your own data.
You just need to populate the `summon/assets` directory with your own data.

The `summon.go` file of the main module is the entry point to the summon
The `summon/summon.go` file of the main module is the entry point to the summon
library, and creates the main command executable.

#### Migration from versions prior to v0.13.0

The v0.13.0 version uses the embed.FS and the `//go:embed assets/*` directive.
Prior versions used to reference the `assets/` dir at the root of the repo.
This means that the data being embeded must now be a sibling of the source
file containing `package main`.

### Summon config File

The `assets/summon.config.yaml` is an (optional) configuration file to
The `summon/assets/summon.config.yaml` is an (optional) configuration file to
customize summon. You can define:

- aliases
Expand Down
10 changes: 6 additions & 4 deletions internal/scaffold/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
func TestCreateScaffold(t *testing.T) {
assert := assert.New(t)

content := "module example.com/my/assets\n\nrequire github.com/davidovich/summon latest\n"

testCases := []struct {
desc string
args []string
Expand All @@ -26,13 +28,13 @@ func TestCreateScaffold(t *testing.T) {
desc: "happy path",
args: []string{".", "example.com/my/assets", "summon"},
file: "go.mod",
content: "module example.com/my/assets\n",
content: content,
},
{
desc: "output dir",
args: []string{"subdir", "example.com/my/assets", "summon"},
file: "subdir/go.mod",
content: "module example.com/my/assets\n",
content: content,
},
{
desc: "non empty dir error",
Expand All @@ -51,13 +53,13 @@ func TestCreateScaffold(t *testing.T) {
f.Close()
},
file: "summon/go.mod",
content: "module example.com/my/assets\n",
content: content,
},
{
desc: "named exe",
args: []string{".", "example.com/my/assets", "my-assets"},
file: "go.mod",
content: "module example.com/my/assets\n",
content: content,
},
{
desc: "README rendered contents",
Expand Down
4 changes: 2 additions & 2 deletions internal/scaffold/templates/scaffold/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
SUMMONER_NAME := {{ .SummonerName }}

ASSETS := $(shell find assets)
ASSETS := $(shell find $(SUMMONER_NAME)/assets)

all: bin/$(SUMMONER_NAME)

bin/$(SUMMONER_NAME): summon.go $(ASSETS)
bin/$(SUMMONER_NAME): $(SUMMONER_NAME)/$(SUMMONER_NAME).go $(ASSETS)
go build -o $@ $<

.PHONY: clean
Expand Down
6 changes: 4 additions & 2 deletions internal/scaffold/templates/scaffold/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ Summon `some-asset` like so:
{{ .SummonerName }} some-asset
```

By default, summon will instantiate the asset in the `.summoned/` directory and return its path. This can be overriden in the `asssets/summon.config.yaml` file or by using the `-o` flag.
By default, summon will instantiate the asset in the `.summoned/` directory and
return its path. This can be overriden in the `{{ .SummonerName }}/asssets/summon.config.yaml` file
or by using the `-o` flag.

Get more help with `{{ .SummonerName }} -h`.

Updating assets
---------------

1) Make modifications (additions, removals) in the `assets/` dir
1) Make modifications (additions, removals) in the `{{ .SummonerName }}/assets/` dir
2) Invoke `make`
3) Commit changes
4) Tag with a semantic version (prefix with `v`)
Expand Down
2 changes: 2 additions & 0 deletions internal/scaffold/templates/scaffold/{{.go}}.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module {{ .ModName }}

require github.com/davidovich/summon latest
8 changes: 7 additions & 1 deletion scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (

"github.com/davidovich/summon/internal/scaffold"
"github.com/davidovich/summon/pkg/command"
"github.com/davidovich/summon/pkg/summon"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -81,13 +82,18 @@ func newMainCmd() *cobra.Command {
gitcmd := execCmd(git, "-C", dest, "init")
gitcmd.Stdout = os.Stdout
_ = gitcmd.Run()

gocmd := execCmd("go", "mod", "tidy")
gocmd.Dir = dest
gocmd.Stdout = os.Stdout
_ = gocmd.Run()
}
return err
},
}

initCmd.Flags().StringVarP(&dest, "out", "o", ".", "destination directory")
initCmd.Flags().StringVarP(&summonName, "name", "n", "summon", "summon executable name")
initCmd.Flags().StringVarP(&summonName, "name", "n", summon.Name, "summon executable name")
initCmd.Flags().BoolVarP(&force, "force", "f", false, "force overwrite")

rootCmd.AddCommand(initCmd)
Expand Down
2 changes: 1 addition & 1 deletion scaffold/scaffold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestScaffolder(t *testing.T) {
desc: "happy path",
args: []string{"init", "example.com/my/assets"},
file: "go.mod",
content: "module example.com/my/assets\n",
content: "module example.com/my/assets\n\nrequire github.com/davidovich/summon latest\n",
},
{
desc: "non empty dir error",
Expand Down

0 comments on commit c259051

Please sign in to comment.