github.com/gohugoio/hugo@v0.88.1/common/hugo/hugo.go (about) 1 // Copyright 2018 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 hugo 15 16 import ( 17 "fmt" 18 "html/template" 19 "os" 20 "path/filepath" 21 "runtime/debug" 22 "sort" 23 "strings" 24 25 "github.com/gohugoio/hugo/hugofs/files" 26 27 "github.com/spf13/afero" 28 29 "github.com/gohugoio/hugo/config" 30 "github.com/gohugoio/hugo/hugofs" 31 ) 32 33 const ( 34 EnvironmentDevelopment = "development" 35 EnvironmentProduction = "production" 36 ) 37 38 var ( 39 // commitHash contains the current Git revision. 40 // Use mage to build to make sure this gets set. 41 commitHash string 42 43 // buildDate contains the date of the current build. 44 buildDate string 45 46 // vendorInfo contains vendor notes about the current build. 47 vendorInfo string 48 ) 49 50 // Info contains information about the current Hugo environment 51 type Info struct { 52 CommitHash string 53 BuildDate string 54 55 // The build environment. 56 // Defaults are "production" (hugo) and "development" (hugo server). 57 // This can also be set by the user. 58 // It can be any string, but it will be all lower case. 59 Environment string 60 } 61 62 // Version returns the current version as a comparable version string. 63 func (i Info) Version() VersionString { 64 return CurrentVersion.Version() 65 } 66 67 // Generator a Hugo meta generator HTML tag. 68 func (i Info) Generator() template.HTML { 69 return template.HTML(fmt.Sprintf(`<meta name="generator" content="Hugo %s" />`, CurrentVersion.String())) 70 } 71 72 func (i Info) IsProduction() bool { 73 return i.Environment == EnvironmentProduction 74 } 75 76 func (i Info) IsExtended() bool { 77 return IsExtended 78 } 79 80 // NewInfo creates a new Hugo Info object. 81 func NewInfo(environment string) Info { 82 if environment == "" { 83 environment = EnvironmentProduction 84 } 85 return Info{ 86 CommitHash: commitHash, 87 BuildDate: buildDate, 88 Environment: environment, 89 } 90 } 91 92 func GetExecEnviron(workDir string, cfg config.Provider, fs afero.Fs) []string { 93 env := os.Environ() 94 nodepath := filepath.Join(workDir, "node_modules") 95 if np := os.Getenv("NODE_PATH"); np != "" { 96 nodepath = workDir + string(os.PathListSeparator) + np 97 } 98 config.SetEnvVars(&env, "NODE_PATH", nodepath) 99 config.SetEnvVars(&env, "PWD", workDir) 100 config.SetEnvVars(&env, "HUGO_ENVIRONMENT", cfg.GetString("environment")) 101 fis, err := afero.ReadDir(fs, files.FolderJSConfig) 102 if err == nil { 103 for _, fi := range fis { 104 key := fmt.Sprintf("HUGO_FILE_%s", strings.ReplaceAll(strings.ToUpper(fi.Name()), ".", "_")) 105 value := fi.(hugofs.FileMetaInfo).Meta().Filename 106 config.SetEnvVars(&env, key, value) 107 } 108 } 109 110 return env 111 } 112 113 // GetDependencyList returns a sorted dependency list on the format package="version". 114 // It includes both Go dependencies and (a manually maintained) list of C(++) dependencies. 115 func GetDependencyList() []string { 116 var deps []string 117 118 formatDep := func(path, version string) string { 119 return fmt.Sprintf("%s=%q", path, version) 120 } 121 122 if IsExtended { 123 deps = append( 124 deps, 125 // TODO(bep) consider adding a DepsNonGo() method to these upstream projects. 126 formatDep("github.com/sass/libsass", "3.6.5"), 127 formatDep("github.com/webmproject/libwebp", "v1.2.0"), 128 ) 129 } 130 131 bi, ok := debug.ReadBuildInfo() 132 if !ok { 133 return deps 134 } 135 136 for _, dep := range bi.Deps { 137 deps = append(deps, formatDep(dep.Path, dep.Version)) 138 } 139 140 sort.Strings(deps) 141 142 return deps 143 } 144 145 // IsRunningAsTest reports whether we are running as a test. 146 func IsRunningAsTest() bool { 147 for _, arg := range os.Args { 148 if strings.HasPrefix(arg, "-test") { 149 return true 150 } 151 } 152 return false 153 }