1
1
package ratio
2
2
3
- // original: https://github.com/hhatto/gocloc/blob/b2dad3847df87ab84c56bb8d27c91ca041e69c16 /language.go
3
+ // original: https://github.com/hhatto/gocloc/blob/7b24285f3e4368e0b3df5cd16b0969f3c9be03cb /language.go
4
4
import (
5
5
"bufio"
6
6
"bytes"
7
- "log"
8
7
"os"
9
8
"path/filepath"
10
9
"regexp"
@@ -27,9 +26,9 @@ var shebang2ext = map[string]string{
27
26
"escript" : "erl" ,
28
27
}
29
28
30
- func shebang (line string ) (shebangLang string , ok bool ) {
29
+ func getShebang (line string ) (shebangLang string , ok bool ) {
31
30
ret := reShebangEnv .FindAllStringSubmatch (line , - 1 )
32
- if len ( ret ) != 0 && len (ret [0 ]) == 3 {
31
+ if ret != nil && len (ret [0 ]) == 3 {
33
32
shebangLang = ret [0 ][2 ]
34
33
if sl , ok := shebang2ext [shebangLang ]; ok {
35
34
return sl , ok
@@ -38,7 +37,7 @@ func shebang(line string) (shebangLang string, ok bool) {
38
37
}
39
38
40
39
ret = reShebangLang .FindAllStringSubmatch (line , - 1 )
41
- if len ( ret ) != 0 && len (ret [0 ]) >= 2 {
40
+ if ret != nil && len (ret [0 ]) >= 2 {
42
41
shebangLang = ret [0 ][1 ]
43
42
if sl , ok := shebang2ext [shebangLang ]; ok {
44
43
return sl , ok
@@ -49,18 +48,47 @@ func shebang(line string) (shebangLang string, ok bool) {
49
48
return "" , false
50
49
}
51
50
52
- func fileType (path string ) (ext string , ok bool ) {
51
+ func getFileTypeByShebang (path string ) (shebangLang string , ok bool ) {
52
+ f , err := os .Open (path )
53
+ if err != nil {
54
+ return // ignore error
55
+ }
56
+ defer f .Close ()
57
+
58
+ reader := bufio .NewReader (f )
59
+ line , err := reader .ReadBytes ('\n' )
60
+ if err != nil {
61
+ return
62
+ }
63
+ line = bytes .TrimLeftFunc (line , unicode .IsSpace )
64
+
65
+ if len (line ) > 2 && line [0 ] == '#' && line [1 ] == '!' {
66
+ return getShebang (string (line ))
67
+ }
68
+ return
69
+ }
70
+
71
+ func getFileType (path string ) (ext string , ok bool ) {
53
72
ext = filepath .Ext (path )
54
73
base := filepath .Base (path )
55
74
56
75
switch ext {
57
76
case ".m" , ".v" , ".fs" , ".r" , ".ts" :
58
- content , err := os .ReadFile (filepath .Clean (path ))
77
+ content , err := os .ReadFile (path )
78
+ if err != nil {
79
+ return "" , false
80
+ }
81
+ lang := enry .GetLanguage (path , content )
82
+ return lang , true
83
+ case ".mo" :
84
+ content , err := os .ReadFile (path )
59
85
if err != nil {
60
86
return "" , false
61
87
}
62
88
lang := enry .GetLanguage (path , content )
63
- log .Printf ("path=%v, lang=%v\n " , path , lang )
89
+ if lang != "" {
90
+ return "Motoko" , true
91
+ }
64
92
return lang , true
65
93
}
66
94
@@ -88,7 +116,7 @@ func fileType(path string) (ext string, ok bool) {
88
116
return "" , false
89
117
}
90
118
91
- shebangLang , ok := fileTypeByShebang (path )
119
+ shebangLang , ok := getFileTypeByShebang (path )
92
120
if ok {
93
121
return shebangLang , true
94
122
}
@@ -98,25 +126,3 @@ func fileType(path string) (ext string, ok bool) {
98
126
}
99
127
return ext , ok
100
128
}
101
-
102
- func fileTypeByShebang (path string ) (shebangLang string , ok bool ) {
103
- f , err := os .Open (filepath .Clean (path ))
104
- if err != nil {
105
- return // ignore error
106
- }
107
- reader := bufio .NewReader (f )
108
- line , err := reader .ReadBytes ('\n' )
109
- if err != nil {
110
- _ = f .Close () //nostyle:handlerrors
111
- return
112
- }
113
- line = bytes .TrimLeftFunc (line , unicode .IsSpace )
114
-
115
- if len (line ) > 2 && line [0 ] == '#' && line [1 ] == '!' {
116
- _ = f .Close () //nostyle:handlerrors
117
- return shebang (string (line ))
118
- }
119
- _ = f .Close () //nostyle:handlerrors
120
-
121
- return
122
- }
0 commit comments