module.go - hugo - [fork] hugo port for 9front
HTML git clone https://git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
module.go (4020B)
---
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 "time"
21
22 "github.com/gohugoio/hugo/config"
23 )
24
25 var _ Module = (*moduleAdapter)(nil)
26
27 type Module interface {
28 // Optional config read from the configFilename above.
29 Cfg() config.Provider
30
31 // The decoded module config and mounts.
32 Config() Config
33
34 // Optional configuration filenames (e.g. "/themes/mytheme/config.json").
35 // This will be added to the special configuration watch list when in
36 // server mode.
37 ConfigFilenames() []string
38
39 // Directory holding files for this module.
40 Dir() string
41
42 // Returns whether this is a Go Module.
43 IsGoMod() bool
44
45 // Any directory remappings.
46 Mounts() []Mount
47
48 // In the dependency tree, this is the first module that defines this module
49 // as a dependency.
50 Owner() Module
51
52 // Returns the path to this module.
53 // This will either be the module path, e.g. "github.com/gohugoio/myshortcodes",
54 // or the path below your /theme folder, e.g. "mytheme".
55 Path() string
56
57 // Replaced by this module.
58 Replace() Module
59
60 // Returns whether Dir points below the _vendor dir.
61 Vendor() bool
62
63 // The module version.
64 Version() string
65
66 // Time version was created.
67 Time() time.Time
68
69 // Whether this module's dir is a watch candidate.
70 Watch() bool
71 }
72
73 type Modules []Module
74
75 type moduleAdapter struct {
76 path string
77 dir string
78 version string
79 vendor bool
80 projectMod bool
81 owner Module
82
83 mounts []Mount
84
85 configFilenames []string
86 cfg config.Provider
87 config Config
88
89 // Set if a Go module.
90 gomod *goModule
91 }
92
93 func (m *moduleAdapter) Cfg() config.Provider {
94 return m.cfg
95 }
96
97 func (m *moduleAdapter) Config() Config {
98 return m.config
99 }
100
101 func (m *moduleAdapter) ConfigFilenames() []string {
102 return m.configFilenames
103 }
104
105 func (m *moduleAdapter) Dir() string {
106 // This may point to the _vendor dir.
107 if !m.IsGoMod() || m.dir != "" {
108 return m.dir
109 }
110 return m.gomod.Dir
111 }
112
113 func (m *moduleAdapter) IsGoMod() bool {
114 return m.gomod != nil
115 }
116
117 func (m *moduleAdapter) Mounts() []Mount {
118 return m.mounts
119 }
120
121 func (m *moduleAdapter) Owner() Module {
122 return m.owner
123 }
124
125 func (m *moduleAdapter) Path() string {
126 if !m.IsGoMod() || m.path != "" {
127 return m.path
128 }
129 return m.gomod.Path
130 }
131
132 func (m *moduleAdapter) Replace() Module {
133 if m.IsGoMod() && !m.Vendor() && m.gomod.Replace != nil {
134 return &moduleAdapter{
135 gomod: m.gomod.Replace,
136 owner: m.owner,
137 }
138 }
139 return nil
140 }
141
142 func (m *moduleAdapter) Vendor() bool {
143 return m.vendor
144 }
145
146 func (m *moduleAdapter) Version() string {
147 if !m.IsGoMod() || m.version != "" {
148 return m.version
149 }
150 return m.gomod.Version
151 }
152
153 func (m *moduleAdapter) Time() time.Time {
154 if !m.IsGoMod() || m.gomod.Time == nil {
155 return time.Time{}
156 }
157
158 return *m.gomod.Time
159 }
160
161 func (m *moduleAdapter) Watch() bool {
162 if m.Owner() == nil {
163 // Main project
164 return true
165 }
166
167 if !m.IsGoMod() {
168 // Module inside /themes
169 return true
170 }
171
172 if m.Replace() != nil {
173 // Version is not set when replaced by a local folder.
174 return m.Replace().Version() == ""
175 }
176
177 // Any module set up in a workspace file will have Indirect set to false.
178 // That leaves modules inside the read-only module cache.
179 return !m.gomod.Indirect
180 }