Skip to content

Commit 626e532

Browse files
committed
fix #7: exclude double new lines
1 parent f10bfb4 commit 626e532

File tree

3 files changed

+188
-6
lines changed

3 files changed

+188
-6
lines changed

internal/cmd/makefile/entity.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"path/filepath"
99
"strings"
1010
"sync"
11+
12+
"go.octolab.org/safe"
13+
"go.octolab.org/unsafe"
1114
)
1215

1316
const (
@@ -49,22 +52,26 @@ func (makefile Makefile) AppendTo(output io.Writer) error {
4952
if err != nil {
5053
return err
5154
}
52-
defer func() { _ = file.Close() }()
55+
defer safe.Close(file, unsafe.Ignore)
5356

5457
scanner := bufio.NewScanner(file)
5558
scanner.Split(bufio.ScanLines)
5659
for scanner.Scan() {
5760
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 {
5966
if _, err := fmt.Fprintln(output, text); err != nil {
6067
return err
6168
}
6269
continue
6370
}
64-
if strings.HasPrefix(text, sincludeDirective) {
71+
if isSafeInclude {
6572
name := strings.TrimSpace(strings.TrimPrefix(text, sincludeDirective))
6673
if err := Makefile(name).AppendTo(output); err == nil {
67-
_, _ = fmt.Fprintln(output)
74+
unsafe.DoSilent(fmt.Fprintln(output))
6875
}
6976
continue
7077
}
@@ -91,8 +98,8 @@ func (makefile Makefile) CompileTo(dir string) error {
9198
if err != nil {
9299
return err
93100
}
94-
defer func() { _ = output.Close() }()
95-
return makefile.AppendTo(output)
101+
defer safe.Close(output, unsafe.Ignore)
102+
return makefile.AppendTo(DeduplicateNewLines(output))
96103
}
97104

98105
type Makefiles []Makefile

internal/cmd/makefile/pkg.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package makefile
2+
3+
import "io"
4+
5+
type Writer func([]byte) (int, error)
6+
7+
func (w Writer) Write(input []byte) (int, error) { return w(input) }
8+
9+
func DeduplicateNewLines(stream io.Writer) Writer {
10+
var counter int
11+
return func(input []byte) (int, error) {
12+
filtered := input[:0]
13+
for _, r := range input {
14+
if counter > 1 && r == '\n' {
15+
continue
16+
}
17+
if r == '\n' {
18+
counter++
19+
} else {
20+
counter = 0
21+
}
22+
filtered = append(filtered, r)
23+
}
24+
25+
_, err := stream.Write(filtered)
26+
return len(input), err
27+
}
28+
}

0 commit comments

Comments
 (0)