aboutsummaryrefslogtreecommitdiffhomepage
path: root/create/skeletons/skeletons.go
blob: 802b15fb09f4d04331bb01488178243711611f56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// 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 skeletons

import (
	"bytes"
	"embed"
	"errors"
	"io/fs"
	"path/filepath"
	"strings"

	"github.com/gohugoio/hugo/helpers"
	"github.com/gohugoio/hugo/parser"
	"github.com/gohugoio/hugo/parser/metadecoders"
	"github.com/spf13/afero"
)

//go:embed all:site/*
var siteFs embed.FS

//go:embed all:theme/*
var themeFs embed.FS

// CreateTheme creates a theme skeleton.
func CreateTheme(createpath string, sourceFs afero.Fs) error {
	if exists, _ := helpers.Exists(createpath, sourceFs); exists {
		return errors.New(createpath + " already exists")
	}
	return copyFiles(createpath, sourceFs, themeFs)
}

// CreateSite creates a site skeleton.
func CreateSite(createpath string, sourceFs afero.Fs, force bool, format string) error {
	format = strings.ToLower(format)
	if exists, _ := helpers.Exists(createpath, sourceFs); exists {
		if isDir, _ := helpers.IsDir(createpath, sourceFs); !isDir {
			return errors.New(createpath + " already exists but not a directory")
		}

		isEmpty, _ := helpers.IsEmpty(createpath, sourceFs)

		switch {
		case !isEmpty && !force:
			return errors.New(createpath + " already exists and is not empty. See --force.")
		case !isEmpty && force:
			var all []string
			fs.WalkDir(siteFs, ".", func(path string, d fs.DirEntry, err error) error {
				if d.IsDir() && path != "." {
					all = append(all, path)
				}
				return nil
			})
			all = append(all, filepath.Join(createpath, "hugo."+format))
			for _, path := range all {
				if exists, _ := helpers.Exists(path, sourceFs); exists {
					return errors.New(path + " already exists")
				}
			}
		}
	}

	err := newSiteCreateConfig(sourceFs, createpath, format)
	if err != nil {
		return err
	}

	err = newSiteCreateArchetype(sourceFs, createpath, format)
	if err != nil {
		return err
	}

	return copyFiles(createpath, sourceFs, siteFs)
}

func copyFiles(createpath string, sourceFs afero.Fs, skeleton embed.FS) error {
	return fs.WalkDir(skeleton, ".", func(path string, d fs.DirEntry, err error) error {
		_, slug, _ := strings.Cut(path, "/")
		if d.IsDir() {
			return sourceFs.MkdirAll(filepath.Join(createpath, slug), 0o777)
		} else {
			if filepath.Base(path) != ".gitkeep" {
				data, _ := fs.ReadFile(skeleton, path)
				return helpers.WriteToDisk(filepath.Join(createpath, slug), bytes.NewReader(data), sourceFs)
			}
			return nil
		}
	})
}

func newSiteCreateConfig(fs afero.Fs, createpath string, format string) (err error) {
	in := map[string]string{
		"baseURL":      "https://example.org/",
		"title":        "My New Hugo Site",
		"languageCode": "en-us",
	}

	var buf bytes.Buffer
	err = parser.InterfaceToConfig(in, metadecoders.FormatFromString(format), &buf)
	if err != nil {
		return err
	}

	return helpers.WriteToDisk(filepath.Join(createpath, "hugo."+format), &buf, fs)
}

func newSiteCreateArchetype(fs afero.Fs, createpath string, format string) (err error) {
	in := map[string]any{
		"title": "{{ replace .File.ContentBaseName \"-\" \" \" | title }}",
		"date":  "{{ .Date }}",
		"draft": true,
	}

	var buf bytes.Buffer
	err = parser.InterfaceToConfig(in, metadecoders.FormatFromString(format), &buf)
	if err != nil {
		return err
	}

	return helpers.WriteToDisk(filepath.Join(createpath, "archetypes", "default.md"), &buf, fs)
}