Debian Codegraph @

modules/module.go

raw
1 // Copyright 2019 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 modules provides a client that can be used to manage Hugo Components,
15 // what's referred to as Hugo Modules. Hugo Modules is built on top of Go Modules,
16 // but also supports vendoring and components stored directly in the themes dir.
17 package modules
18
19 import (
20 "net/url"
21 "time"
22
23 "github.com/gohugoio/hugo/config"
24 )
25
26 var _ Module = (*moduleAdapter)(nil)
27
28 type Module interface {
29 // Optional config read from the configFilename above.
30 Cfg() config.Provider
31
32 // The decoded module config and mounts.
33 Config() Config
34
35 // Optional configuration filenames (e.g. "/themes/mytheme/config.json").
36 // This will be added to the special configuration watch list when in
37 // server mode.
38 ConfigFilenames() []string
39
40 // Directory holding files for this module.
41 Dir() string
42
43 // Returns whether this is a Go Module.
44 IsGoMod() bool
45
46 // Any directory remappings.
47 Mounts() []Mount
48
49 // In the dependency tree, this is the first module that defines this module
50 // as a dependency.
51 Owner() Module
52
53 // Returns the path to this module.
54 // This will either be the module path, e.g. "github.com/gohugoio/myshortcodes",
55 // or the path below your /theme folder, e.g. "mytheme".
56 Path() string
57
58 // For direct dependencies, this will be Path + "@" + VersionQuery.
59 // For managed dependencies, this will be the same as Path.
60 PathVersionQuery(escapeQuery bool) string
61
62 // Replaced by this module.
63 Replace() Module
64
65 // Returns whether Dir points below the _vendor dir.
66 Vendor() bool
67
68 // The module version.
69 Version() string
70
71 // The version query requested in the import.
72 VersionQuery() string
73
74 // Checksum for path, version
75 Sum() string
76
77 // Time version was created.
78 Time() time.Time
79
80 // Whether this module's dir is a watch candidate.
81 Watch() bool
82
83 // Origin returns the module's origin info if available (VCS, URL, Hash, etc.).
84 Origin() ModuleOrigin
85 }
86
87 // ModuleOrigin contains origin info for a Go module.
88 type ModuleOrigin struct {
89 VCS string // version control system, e.g. "git"
90 URL string // repository URL, e.g. "https://github.com/bep/hugo-testing-git-versions"
91 Subdir string // subdirectory within the repo where the module lives, e.g. "site"
92 Hash string // commit hash
93 Ref string // e.g. "refs/tags/v3.0.1"
94 }
95
96 func (o ModuleOrigin) IsZero() bool {
97 return o.URL == ""
98 }
99
100 type Modules []Module
101
102 type moduleAdapter struct {
103 path string
104 dir string
105 version string
106 versionQuery string
107 vendor bool
108 projectMod bool
109 owner Module
110
111 mounts []Mount
112
113 configFilenames []string
114 cfg config.Provider
115 config Config
116
117 // Set if a Go module.
118 gomod *goModule
119 }
120
121 func (m *moduleAdapter) Cfg() config.Provider {
122 return m.cfg
123 }
124
125 func (m *moduleAdapter) Config() Config {
126 return m.config
127 }
128
129 func (m *moduleAdapter) ConfigFilenames() []string {
130 return m.configFilenames
131 }
132
133 func (m *moduleAdapter) Dir() string {
134 // This may point to the _vendor dir.
135 if !m.IsGoMod() || m.dir != "" {
136 return m.dir
137 }
138 return m.gomod.Dir
139 }
140
141 func (m *moduleAdapter) IsGoMod() bool {
142 return m.gomod != nil
143 }
144
145 func (m *moduleAdapter) Mounts() []Mount {
146 return m.mounts
147 }
148
149 func (m *moduleAdapter) Owner() Module {
150 return m.owner
151 }
152
153 func (m *moduleAdapter) Path() string {
154 if !m.IsGoMod() || m.path != "" {
155 return m.path
156 }
157 return m.gomod.Path
158 }
159
160 func (m *moduleAdapter) PathVersionQuery(escapeQuery bool) string {
161 // We added version as a config option in Hugo v0.150.0, so
162 // to make this backward compatible, we only add the version
163 // if it was explicitly requested.
164 pathBase := m.Path()
165 if m.versionQuery == "" || !m.IsGoMod() {
166 return pathBase
167 }
168
169 q := m.versionQuery
170 if escapeQuery {
171 q = url.QueryEscape(q)
172 }
173
174 return pathBase + "@" + q
175 }
176
177 func (m *moduleAdapter) Replace() Module {
178 if m.IsGoMod() && !m.Vendor() && m.gomod.Replace != nil {
179 return &moduleAdapter{
180 gomod: m.gomod.Replace,
181 owner: m.owner,
182 }
183 }
184 return nil
185 }
186
187 func (m *moduleAdapter) Vendor() bool {
188 return m.vendor
189 }
190
191 func (m *moduleAdapter) Version() string {
192 if !m.IsGoMod() || m.version != "" {
193 return m.version
194 }
195 return m.gomod.Version
196 }
197
198 func (m *moduleAdapter) VersionQuery() string {
199 return m.versionQuery
200 }
201
202 func (m *moduleAdapter) Sum() string {
203 if !m.IsGoMod() {
204 return ""
205 }
206
207 return m.gomod.Sum
208 }
209
210 func (m *moduleAdapter) Time() time.Time {
211 if !m.IsGoMod() || m.gomod.Time == nil {
212 return time.Time{}
213 }
214
215 return *m.gomod.Time
216 }
217
218 func (m *moduleAdapter) Watch() bool {
219 if m.Owner() == nil {
220 // Main project
221 return true
222 }
223
224 if !m.IsGoMod() {
225 // Module inside /themes
226 return true
227 }
228
229 if m.Replace() != nil {
230 // Version is not set when replaced by a local folder.
231 return m.Replace().Version() == ""
232 }
233
234 // Any module set up in a workspace file will have Indirect set to false.
235 // That leaves modules inside the read-only module cache.
236 return !m.gomod.Indirect
237 }
238
239 func (m *moduleAdapter) Origin() ModuleOrigin {
240 if !m.IsGoMod() || m.gomod.Origin == nil {
241 return ModuleOrigin{}
242 }
243 return ModuleOrigin{
244 VCS: m.gomod.Origin.VCS,
245 URL: m.gomod.Origin.URL,
246 Subdir: m.gomod.Origin.Subdir,
247 Hash: m.gomod.Origin.Hash,
248 Ref: m.gomod.Origin.Ref,
249 }
250 }