-
Notifications
You must be signed in to change notification settings - Fork 4
/
header.go
80 lines (73 loc) · 2.61 KB
/
header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package file
import (
"strings"
"projectforge.dev/projectforge/app/util"
)
const (
HeaderContent = "Content managed by Project Forge, see [projectforge.md] for details."
headerCommentPound = "# "
headerCommentSlashes = "// "
headerCommentXML = "<!-- "
headerCommentXMLEnd = " -->"
)
func ContainsHeader(s string) bool {
return strings.Contains(s, HeaderContent) || strings.Contains(s, "$PF_IGNORE$")
}
func contentWithHeader(filename string, t Type, c string, linebreak string, pkg string, logger util.Logger) string {
if strings.Contains(c, IgnorePattern) {
return c
}
switch t.Key {
case TypeBatch.Key:
return secondLine(c, "rem "+HeaderContent, linebreak)
case
TypeCodeowners.Key, TypeDocker.Key, TypeDockerIgnore.Key, TypeEnv.Key, TypeGraphQL.Key, TypeHCL.Key,
TypeMakefile.Key, TypeProperties.Key, TypePython.Key, TypeYAML.Key:
return headerCommentPound + HeaderContent + linebreak + c
case TypeConf.Key, TypeEditorConfig.Key, TypeESLint.Key, TypeGitIgnore.Key:
return c
case TypeIcons.Key, TypeIgnore.Key, TypeJSON.Key, TypePList.Key, TypeProtobuf.Key, TypeSVG.Key:
return c
case TypeCSS.Key:
return "/* " + HeaderContent + " */" + linebreak + c
case TypeGo.Key:
goLine := headerCommentSlashes + HeaderContent
if pkg != "" {
goLine = headerCommentSlashes + "Package " + pkg + " - " + HeaderContent
}
if strings.HasPrefix(c, "//go:build") {
return secondLine(c, linebreak+goLine, linebreak)
}
if strings.Contains(c, "}}}//go:build") {
tok := "{{{ end }}}"
idx := strings.Index(c, tok)
if idx == -1 {
return c
}
return c[0:idx+len(tok)] + linebreak + goLine + linebreak + c[idx+len(tok):]
}
return goLine + linebreak + c
case TypeGoMod.Key, TypeGradle.Key, TypeJavaScript.Key, TypeKotlin.Key, TypeSwift.Key, TypeTypeScript.Key:
return headerCommentSlashes + HeaderContent + linebreak + c
case TypeHTML.Key:
return headerCommentXML + HeaderContent + headerCommentXMLEnd + linebreak + c
case TypeMarkdown.Key:
return "<!--- " + HeaderContent + headerCommentXMLEnd + linebreak + c
case TypeSQL.Key:
return "-- " + HeaderContent + linebreak + c
case TypeShell.Key:
return secondLine(c, headerCommentPound+HeaderContent, linebreak)
case TypeEntitlements.Key, TypeXML.Key:
return secondLine(c, headerCommentXML+HeaderContent+headerCommentXMLEnd, linebreak)
default:
logger.Warnf("unhandled header for file [%s], of type [%s]", filename, t.Key)
return c
}
}
func secondLine(content string, rplc string, linebreak string) string {
idx := strings.Index(content, linebreak)
if idx == -1 {
return content
}
return content[0:idx] + linebreak + rplc + content[idx:]
}