diff options
author | Bjørn Erik Pedersen <[email protected]> | 2023-10-26 09:38:13 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2023-10-26 20:41:19 +0200 |
commit | 71fd79a3f44101ef515349a24527e6c0b566f368 (patch) | |
tree | 4bd1763b0b09a5767ee5501b6df298fd12efc6d2 /common | |
parent | c4a530f104f2b0d1dd82211b77b603a07db7c181 (diff) | |
download | hugo-71fd79a3f44101ef515349a24527e6c0b566f368.tar.gz hugo-71fd79a3f44101ef515349a24527e6c0b566f368.zip |
Revise the deprecation logging
This introduces a more automatic way of increasing the log levels for deprecation log statements based on the version it was deprecated.
The thresholds are a little arbitrary, but
* We log INFO for 6 releases
* We log WARN for another 6 releases
* THen ERROR (failing the build)
This should give theme authors plenty of time to catch up without having the log filled with warnings.
Diffstat (limited to 'common')
-rw-r--r-- | common/hugo/hugo.go | 56 | ||||
-rw-r--r-- | common/hugo/hugo_test.go | 15 | ||||
-rw-r--r-- | common/hugo/version.go | 9 |
3 files changed, 70 insertions, 10 deletions
diff --git a/common/hugo/hugo.go b/common/hugo/hugo.go index 1138cf2ac..f6eb72d8f 100644 --- a/common/hugo/hugo.go +++ b/common/hugo/hugo.go @@ -22,14 +22,15 @@ import ( "sort" "strings" "sync" + "time" godartsassv1 "github.com/bep/godartsass" + "github.com/bep/logg" "github.com/mitchellh/mapstructure" - "time" - "github.com/bep/godartsass/v2" "github.com/gohugoio/hugo/common/hexec" + "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/hugofs/files" "github.com/spf13/afero" @@ -183,8 +184,10 @@ type buildInfo struct { *debug.BuildInfo } -var bInfo *buildInfo -var bInfoInit sync.Once +var ( + bInfo *buildInfo + bInfoInit sync.Once +) func getBuildInfo() *buildInfo { bInfoInit.Do(func() { @@ -211,7 +214,6 @@ func getBuildInfo() *buildInfo { bInfo.GoArch = s.Value } } - }) return bInfo @@ -255,7 +257,7 @@ func GetDependencyListNonGo() []string { } if dartSass := dartSassVersion(); dartSass.ProtocolVersion != "" { - var dartSassPath = "github.com/sass/dart-sass-embedded" + dartSassPath := "github.com/sass/dart-sass-embedded" if IsDartSassV2() { dartSassPath = "github.com/sass/dart-sass" } @@ -346,3 +348,45 @@ var ( func IsDartSassV2() bool { return !strings.Contains(DartSassBinaryName, "embedded") } + +// Deprecate informs about a deprecation starting at the given version. +// +// A deprecation typically needs a simple change in the template, but doing so will make the template incompatible with older versions. +// Theme maintainers generally want +// 1. No warnings or errors in the console when building a Hugo site. +// 2. Their theme to work for at least the last few Hugo versions. +func Deprecate(item, alternative string, version string) { + level := deprecationLogLevelFromVersion(version) + DeprecateLevel(item, alternative, version, level) +} + +// DeprecateLevel informs about a deprecation logging at the given level. +func DeprecateLevel(item, alternative, version string, level logg.Level) { + var msg string + if level == logg.LevelError { + msg = fmt.Sprintf("%s was deprecated in Hugo %s and will be removed in Hugo %s. %s", item, version, CurrentVersion.Next().ReleaseVersion(), alternative) + } else { + msg = fmt.Sprintf("%s was deprecated in Hugo %s and will be removed in a future release. %s", item, version, alternative) + } + + loggers.Log().Logger().WithLevel(level).Logf(msg) +} + +// We ususally do about one minor version a month. +// We want people to run at least the current and previous version without any warnings. +// We want people who don't update Hugo that often to see the warnings and errors before we remove the feature. +func deprecationLogLevelFromVersion(ver string) logg.Level { + from := MustParseVersion(ver) + to := CurrentVersion + minorDiff := to.Minor - from.Minor + switch { + case minorDiff >= 12: + // Start failing the build after about a year. + return logg.LevelError + case minorDiff >= 6: + // Start printing warnings after about six months. + return logg.LevelWarn + default: + return logg.LevelInfo + } +} diff --git a/common/hugo/hugo_test.go b/common/hugo/hugo_test.go index 5b4a6f9ad..e252dffbe 100644 --- a/common/hugo/hugo_test.go +++ b/common/hugo/hugo_test.go @@ -17,6 +17,7 @@ import ( "fmt" "testing" + "github.com/bep/logg" qt "github.com/frankban/quicktest" ) @@ -49,6 +50,20 @@ func TestHugoInfo(t *testing.T) { c.Assert(devHugoInfo.IsServer(), qt.Equals, true) } +func TestDeprecationLogLevelFromVersion(t *testing.T) { + c := qt.New(t) + + c.Assert(deprecationLogLevelFromVersion("0.55.0"), qt.Equals, logg.LevelError) + ver := CurrentVersion + c.Assert(deprecationLogLevelFromVersion(ver.String()), qt.Equals, logg.LevelInfo) + ver.Minor -= 1 + c.Assert(deprecationLogLevelFromVersion(ver.String()), qt.Equals, logg.LevelInfo) + ver.Minor -= 6 + c.Assert(deprecationLogLevelFromVersion(ver.String()), qt.Equals, logg.LevelWarn) + ver.Minor -= 6 + c.Assert(deprecationLogLevelFromVersion(ver.String()), qt.Equals, logg.LevelError) +} + type testConfig struct { environment string running bool diff --git a/common/hugo/version.go b/common/hugo/version.go index 3bb6472e2..6cabfdbb9 100644 --- a/common/hugo/version.go +++ b/common/hugo/version.go @@ -67,8 +67,11 @@ func (h VersionString) String() string { // Compare implements the compare.Comparer interface. func (h VersionString) Compare(other any) int { - v := MustParseVersion(h.String()) - return compareVersions(v, other) + return compareVersions(h.Version(), other) +} + +func (h VersionString) Version() Version { + return MustParseVersion(h.String()) } // Eq implements the compare.Eqer interface. @@ -264,7 +267,6 @@ func compareFloatWithVersion(v1 float64, v2 Version) int { if v1maj > v2.Major { return 1 - } if v1maj < v2.Major { @@ -276,7 +278,6 @@ func compareFloatWithVersion(v1 float64, v2 Version) int { } return -1 - } func GoMinorVersion() int { |