| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package skeletons |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"embed" |
| 19 |
"errors" |
| 20 |
"io/fs" |
| 21 |
"path/filepath" |
| 22 |
"strings" |
| 23 |
|
| 24 |
"github.com/gohugoio/hugo/helpers" |
| 25 |
"github.com/gohugoio/hugo/parser" |
| 26 |
"github.com/gohugoio/hugo/parser/metadecoders" |
| 27 |
"github.com/spf13/afero" |
| 28 |
) |
| 29 |
|
| 30 |
//go:embed all:project/* |
| 31 |
var projectFs embed.FS |
| 32 |
|
| 33 |
//go:embed all:theme/* |
| 34 |
var themeFs embed.FS |
| 35 |
|
| 36 |
// CreateTheme creates a theme skeleton. |
| 37 |
func CreateTheme(createpath string, sourceFs afero.Fs, format string) error { |
| 38 |
if exists, _ := helpers.Exists(createpath, sourceFs); exists { |
| 39 |
return errors.New(createpath + " already exists") |
| 40 |
} |
| 41 |
|
| 42 |
format = strings.ToLower(format) |
| 43 |
|
| 44 |
projectConfig := map[string]any{ |
| 45 |
"baseURL": "https://example.org/", |
| 46 |
"locale": "en-US", |
| 47 |
"title": "My New Hugo Project", |
| 48 |
"menus": map[string]any{ |
| 49 |
"main": []any{ |
| 50 |
map[string]any{ |
| 51 |
"name": "Home", |
| 52 |
"pageRef": "/", |
| 53 |
"weight": 10, |
| 54 |
}, |
| 55 |
map[string]any{ |
| 56 |
"name": "Posts", |
| 57 |
"pageRef": "/posts", |
| 58 |
"weight": 20, |
| 59 |
}, |
| 60 |
map[string]any{ |
| 61 |
"name": "Tags", |
| 62 |
"pageRef": "/tags", |
| 63 |
"weight": 30, |
| 64 |
}, |
| 65 |
}, |
| 66 |
}, |
| 67 |
"module": map[string]any{ |
| 68 |
"hugoVersion": map[string]any{ |
| 69 |
"extended": false, |
| 70 |
"min": "0.146.0", |
| 71 |
}, |
| 72 |
}, |
| 73 |
} |
| 74 |
|
| 75 |
err := createProjectConfig(sourceFs, createpath, projectConfig, format) |
| 76 |
if err != nil { |
| 77 |
return err |
| 78 |
} |
| 79 |
|
| 80 |
defaultArchetype := map[string]any{ |
| 81 |
"title": "{{ replace .File.ContentBaseName \"-\" \" \" | title }}", |
| 82 |
"date": "{{ .Date }}", |
| 83 |
"draft": true, |
| 84 |
} |
| 85 |
|
| 86 |
err = createDefaultArchetype(sourceFs, createpath, defaultArchetype, format) |
| 87 |
if err != nil { |
| 88 |
return err |
| 89 |
} |
| 90 |
|
| 91 |
return copyFiles(createpath, sourceFs, themeFs) |
| 92 |
} |
| 93 |
|
| 94 |
// CreateProject creates a project skeleton. |
| 95 |
func CreateProject(createpath string, sourceFs afero.Fs, force bool, format string) error { |
| 96 |
format = strings.ToLower(format) |
| 97 |
if exists, _ := helpers.Exists(createpath, sourceFs); exists { |
| 98 |
if isDir, _ := helpers.IsDir(createpath, sourceFs); !isDir { |
| 99 |
return errors.New(createpath + " already exists but not a directory") |
| 100 |
} |
| 101 |
|
| 102 |
isEmpty, _ := helpers.IsEmpty(createpath, sourceFs) |
| 103 |
|
| 104 |
switch { |
| 105 |
case !isEmpty && !force: |
| 106 |
return errors.New(createpath + " already exists and is not empty. See --force.") |
| 107 |
case !isEmpty && force: |
| 108 |
var all []string |
| 109 |
fs.WalkDir(projectFs, ".", func(path string, d fs.DirEntry, err error) error { |
| 110 |
if d.IsDir() && path != "." { |
| 111 |
all = append(all, path) |
| 112 |
} |
| 113 |
return nil |
| 114 |
}) |
| 115 |
all = append(all, filepath.Join(createpath, "hugo."+format)) |
| 116 |
for _, path := range all { |
| 117 |
if exists, _ := helpers.Exists(path, sourceFs); exists { |
| 118 |
return errors.New(path + " already exists") |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
projectConfig := map[string]any{ |
| 125 |
"baseURL": "https://example.org/", |
| 126 |
"locale": "en-us", |
| 127 |
"title": "My New Hugo Project", |
| 128 |
} |
| 129 |
|
| 130 |
err := createProjectConfig(sourceFs, createpath, projectConfig, format) |
| 131 |
if err != nil { |
| 132 |
return err |
| 133 |
} |
| 134 |
|
| 135 |
defaultArchetype := map[string]any{ |
| 136 |
"title": "{{ replace .File.ContentBaseName \"-\" \" \" | title }}", |
| 137 |
"date": "{{ .Date }}", |
| 138 |
"draft": true, |
| 139 |
} |
| 140 |
|
| 141 |
err = createDefaultArchetype(sourceFs, createpath, defaultArchetype, format) |
| 142 |
if err != nil { |
| 143 |
return err |
| 144 |
} |
| 145 |
|
| 146 |
return copyFiles(createpath, sourceFs, projectFs) |
| 147 |
} |
| 148 |
|
| 149 |
func copyFiles(createpath string, sourceFs afero.Fs, skeleton embed.FS) error { |
| 150 |
return fs.WalkDir(skeleton, ".", func(path string, d fs.DirEntry, err error) error { |
| 151 |
_, slug, _ := strings.Cut(path, "/") |
| 152 |
if d.IsDir() { |
| 153 |
return sourceFs.MkdirAll(filepath.Join(createpath, slug), 0o777) |
| 154 |
} else { |
| 155 |
if filepath.Base(path) != ".gitkeep" { |
| 156 |
data, _ := fs.ReadFile(skeleton, path) |
| 157 |
return helpers.WriteToDisk(filepath.Join(createpath, slug), bytes.NewReader(data), sourceFs) |
| 158 |
} |
| 159 |
return nil |
| 160 |
} |
| 161 |
}) |
| 162 |
} |
| 163 |
|
| 164 |
func createProjectConfig(fs afero.Fs, createpath string, in map[string]any, format string) (err error) { |
| 165 |
var buf bytes.Buffer |
| 166 |
err = parser.InterfaceToConfig(in, metadecoders.FormatFromString(format), &buf) |
| 167 |
if err != nil { |
| 168 |
return err |
| 169 |
} |
| 170 |
|
| 171 |
return helpers.WriteToDisk(filepath.Join(createpath, "hugo."+format), &buf, fs) |
| 172 |
} |
| 173 |
|
| 174 |
func createDefaultArchetype(fs afero.Fs, createpath string, in map[string]any, format string) (err error) { |
| 175 |
var buf bytes.Buffer |
| 176 |
err = parser.InterfaceToFrontMatter(in, metadecoders.FormatFromString(format), &buf) |
| 177 |
if err != nil { |
| 178 |
return err |
| 179 |
} |
| 180 |
|
| 181 |
return helpers.WriteToDisk(filepath.Join(createpath, "archetypes", "default.md"), &buf, fs) |
| 182 |
} |