8
8
"path/filepath"
9
9
"strings"
10
10
"sync"
11
+
12
+ "go.octolab.org/safe"
13
+ "go.octolab.org/unsafe"
11
14
)
12
15
13
16
const (
@@ -49,22 +52,26 @@ func (makefile Makefile) AppendTo(output io.Writer) error {
49
52
if err != nil {
50
53
return err
51
54
}
52
- defer func () { _ = file .Close () }( )
55
+ defer safe .Close (file , unsafe . Ignore )
53
56
54
57
scanner := bufio .NewScanner (file )
55
58
scanner .Split (bufio .ScanLines )
56
59
for scanner .Scan () {
57
60
text := scanner .Text ()
58
- if ! strings .HasPrefix (text , includeDirective ) && ! strings .HasPrefix (text , sincludeDirective ) {
61
+
62
+ isInclude := strings .HasPrefix (text , includeDirective )
63
+ isSafeInclude := strings .HasPrefix (text , sincludeDirective )
64
+
65
+ if ! isInclude && ! isSafeInclude {
59
66
if _ , err := fmt .Fprintln (output , text ); err != nil {
60
67
return err
61
68
}
62
69
continue
63
70
}
64
- if strings . HasPrefix ( text , sincludeDirective ) {
71
+ if isSafeInclude {
65
72
name := strings .TrimSpace (strings .TrimPrefix (text , sincludeDirective ))
66
73
if err := Makefile (name ).AppendTo (output ); err == nil {
67
- _ , _ = fmt .Fprintln (output )
74
+ unsafe . DoSilent ( fmt .Fprintln (output ) )
68
75
}
69
76
continue
70
77
}
@@ -91,8 +98,8 @@ func (makefile Makefile) CompileTo(dir string) error {
91
98
if err != nil {
92
99
return err
93
100
}
94
- defer func () { _ = output .Close () }( )
95
- return makefile .AppendTo (output )
101
+ defer safe .Close (output , unsafe . Ignore )
102
+ return makefile .AppendTo (DeduplicateNewLines ( output ) )
96
103
}
97
104
98
105
type Makefiles []Makefile
0 commit comments