forked from limetext/lime
/
fix.go
70 lines (59 loc) · 1.32 KB
/
fix.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
// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package main
import (
"bytes"
"go/format"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"time"
)
var year = strconv.FormatInt(int64(time.Now().Year()), 10)
var licenseheader = []byte(`// Copyright ` + year + ` The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
`)
func patch(path string, fi os.FileInfo, err error) error {
if fi.IsDir() {
switch filepath.Base(path) {
case "testdata", "build2", "3rdparty":
return filepath.SkipDir
}
return nil
}
switch filepath.Ext(path) {
case ".go", ".c", ".cpp":
default:
return nil
}
changed := false
cmp, err := ioutil.ReadFile(path)
if err != nil {
return err
}
lhn := append(licenseheader, '\n')
if !bytes.Equal([]byte("// Copyright"), cmp[:12]) {
cmp = append(lhn, cmp...)
log.Println("Added license to", path)
changed = true
}
fmt, err := format.Source(cmp)
if err != nil {
return err
}
if !bytes.Equal(cmp, fmt) {
log.Println("Formatted", path)
changed = true
}
if changed {
return ioutil.WriteFile(path, fmt, fi.Mode().Perm())
}
return nil
}
func main() {
log.Println("done: ", filepath.Walk("./", patch))
}