Skip to content

Commit

Permalink
Merge pull request #281 from coveooss/bugs/DT-7519-do_not_generate_fi…
Browse files Browse the repository at this point in the history
…les_if_no_change

Optimize file generation and overloading
  • Loading branch information
jocgir authored Dec 3, 2024
2 parents bca4536 + 7a2e5a1 commit 3f6b7dd
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 16 deletions.
89 changes: 86 additions & 3 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,114 +10,183 @@
"adipiscing",
"afterwhile",
"aindent",
"alecthomas",
"aliqua",
"amet",
"andybalholm",
"anmitsu",
"araneae",
"armon",
"asinh",
"assertw",
"atanh",
"awrap",
"bclear",
"behaviour",
"bmatcuk",
"bofa",
"bwawxf",
"bxor",
"camelcase",
"cascadia",
"cbrt",
"circl",
"cloudflare",
"colorln",
"consectetur",
"copysign",
"copystructure",
"cosinus",
"coveo",
"coveooss",
"criticalf",
"cukek",
"cutset",
"cyphar",
"dario",
"davecgh",
"debugf",
"defval",
"difflib",
"diffmatchpatch",
"dijt",
"divf",
"dnoqohhmyyi",
"dolore",
"dolrw",
"dont",
"doublestar",
"drhodes",
"eiusmod",
"elazarl",
"elit",
"emirpasic",
"endexpr",
"envar",
"envars",
"erfc",
"expm",
"extensionfiles",
"fatih",
"flink",
"forgeround",
"frankban",
"frombits",
"funcs",
"gcfg",
"genny",
"github",
"gliderlabs",
"goerrors",
"gomega",
"gomod",
"gopkg",
"goproxy",
"goquery",
"gotemplate",
"HCLVALUE",
"goutils",
"groupcache",
"hashicorp",
"hbfnv",
"hclvalue",
"hexa",
"htpasswd",
"huandu",
"hugo",
"hykdgkcw",
"hypot",
"iavg",
"ibhfb",
"ihcl",
"ilogb",
"imct",
"incididunt",
"infof",
"infoln",
"interpretated",
"isatty",
"jbenet",
"kebabcase",
"kenobi",
"kevinburke",
"keymap",
"keypair",
"kijsi",
"kindis",
"kindof",
"knownhosts",
"kqfl",
"labore",
"ldexp",
"ldflags",
"lenc",
"lgamma",
"logb",
"logrus",
"lqez",
"lshift",
"markdownlint",
"mattn",
"maxf",
"Metaclass",
"mczn",
"mergo",
"metaclass",
"minf",
"missingkey",
"mitchellh",
"modf",
"mulf",
"multilines",
"multilogger",
"munerum",
"mvgo",
"nadipiscing",
"naliqua",
"namet",
"nconsectetur",
"ndks",
"ndolore",
"neiusmod",
"nelit",
"nextafter",
"ngjh",
"nincididunt",
"nindent",
"nlabore",
"noticef",
"npqgf",
"nsed",
"ntempor",
"objx",
"odre",
"onsi",
"otherkey",
"pickv",
"pjbgf",
"pmezard",
"println",
"puerkito",
"pwsh",
"qmdlor",
"quicktest",
"reflectwalk",
"reutils",
"rogpeppe",
"rounddown",
"rshift",
"secode",
"securejoin",
"semicolumn",
"sergi",
"shlex",
"shopspring",
"signbit",
"sincos",
"sindent",
"sirupsen",
"sjoin",
"skeema",
"slxvdp",
"snakecase",
"splitn",
"sprintln",
Expand All @@ -127,6 +196,7 @@
"stripansi",
"striptcolor",
"subf",
"subcomponent",
"subselection",
"substr",
"swapcase",
Expand All @@ -137,17 +207,30 @@
"tracef",
"traiecta",
"trimall",
"trunc",
"tsed",
"typeis",
"tztisl",
"unmanaged",
"unmatch",
"unrk",
"unsplit",
"untitle",
"urlquery",
"uuid",
"uuidv",
"varias",
"warnf",
"warningf",
"winio",
"worktree",
"warnf"
"xanzy",
"xerrors",
"xhit",
"xstrings",
"yncou",
"yuin",
"zlds",
"zscifu"
]
}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test-all:

# IMPORTANT:
# call `make doc` to generate the doc rendering used to test gotemplate
# Be sure to validate the rendered files before commiting your code
# Be sure to validate the rendered files before committing your code
.PHONY: doc
doc:
./render-doc
Expand Down
21 changes: 21 additions & 0 deletions collections/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"unicode"

"github.com/coveooss/multilogger"
"github.com/pmezard/go-difflib/difflib"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
Expand All @@ -34,6 +35,26 @@ func (s String) ContainsRune(r rune) bool { return strings.ContainsRune(string(s
// If substr is an empty string, Count returns 1 + the number of Unicode code points in s.
func (s String) Count(substr string) int { return strings.Count(string(s), substr) }

// Diff compares the current String with another string and returns the differences
// in a unified diff format. The context lines are set to 3.
//
// Parameters:
//
// compare - the string to compare with the current String.
//
// Returns:
//
// A new String containing the unified diff of the two strings.
func (s String) Diff(compare string) String {
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(s.String()),
B: difflib.SplitLines(compare),
Context: 3,
}
text, _ := difflib.GetUnifiedDiffString(diff)
return String(text)
}

// EqualFold reports whether s and t, interpreted as UTF-8 strings,
// are equal under Unicode case-folding.
func (s String) EqualFold(t string) bool { return strings.EqualFold(string(s), t) }
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/go-errors/errors v1.5.1
github.com/go-git/go-git/v5 v5.12.0
github.com/hashicorp/hcl v1.0.0
github.com/pmezard/go-difflib v1.0.0
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.10.0
Expand Down Expand Up @@ -48,7 +49,6 @@ require (
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/skeema/knownhosts v1.3.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion render-doc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ rsync -av docs_tests/ $DOC_FOLDER -r
@{name} := $re.ReplaceAllString($category.Name, `${name}`)
@-info("Generating documentation for", $name)
@-save(joinPath(folder, lower($name))+".md", $content)
@--end foreach' -Vfolder=$DOC_FOLDER/functions_reference
@--end foreach' -V folder=$DOC_FOLDER/functions_reference
printf -- '---\nbookFlatSection: true\nweight: 4\n---' > $DOC_FOLDER/functions_reference/_index.md

./gotemplate --no-extension '
Expand Down
15 changes: 7 additions & 8 deletions template/template_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (t *Template) processTemplate(template, sourceFolder, targetFolder string,
}

if t.options[OutputStdout] {
err = t.printResult(template, resultFile, result)
err = t.printResult(template, resultFile, result, changed)
if err != nil {
errors.Print(err)
}
Expand All @@ -109,14 +109,13 @@ func (t *Template) processTemplate(template, sourceFolder, targetFolder string,
}

mode := must(os.Stat(template)).(os.FileInfo).Mode()
if !isTemplate && !t.options[Overwrite] {
newName := template + ".original"
InternalLog.Infof("%s => %s", utils.Relative(t.folder, template), utils.Relative(t.folder, newName))
must(os.Rename(template, template+".original"))
}

if sourceFolder != targetFolder {
must(os.MkdirAll(filepath.Dir(resultFile), 0777))
} else if !isTemplate && !t.options[Overwrite] {
newName := template + ".original"
InternalLog.Infof("%s => %s", utils.Relative(t.folder, template), utils.Relative(t.folder, newName))
must(os.Rename(template, template+".original"))
}
InternalLog.Infoln("Writing file", utils.Relative(t.folder, resultFile))

Expand Down Expand Up @@ -225,6 +224,7 @@ func (t *Template) processContentInternal(originalContent, source string, origin
if !(strictMode) {
InternalLog.Errorf("Ignored gotemplate error in %s (file left unchanged):\n%s", color.CyanString(th.Filename), err.Error())
result, err = th.Source, nil
changed = false
}
}
}()
Expand Down Expand Up @@ -257,8 +257,6 @@ func (t *Template) processContentInternal(originalContent, source string, origin
}
result = revertReplacements(t.substitute(out.String()))

changed = result != originalContent

if topCall && !t.options[AcceptNoValue] {
s := String(result)
// Detect possible <no value> or <nil> that could have been generated
Expand All @@ -279,5 +277,6 @@ func (t *Template) processContentInternal(originalContent, source string, origin
}
result = s.Str()
}
changed = result != originalContent
return
}
4 changes: 2 additions & 2 deletions template/template_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (t *Template) ProcessTemplates(sourceFolder, targetFolder string, templates
return t.ProcessTemplatesWithHandler(sourceFolder, targetFolder, nil, templates...)
}

func (t *Template) printResult(source, target, result string) (err error) {
func (t *Template) printResult(source, target, result string, changed bool) (err error) {
if utils.IsTerraformFile(target) {
base := filepath.Base(target)
tempFolder := must(os.CreateTemp(t.tempFolder, base)).(string)
Expand All @@ -64,7 +64,7 @@ func (t *Template) printResult(source, target, result string) (err error) {
result = string(bytes)
}

if !t.isTemplate(source) && !t.options[Overwrite] {
if changed && !t.isTemplate(source) && !t.options[Overwrite] {
source += ".original"
}

Expand Down

0 comments on commit 3f6b7dd

Please sign in to comment.