@@ -17,12 +17,15 @@ import (
17
17
"github.com./zcong1993/leetcode-tool/pkg/leetcode"
18
18
)
19
19
20
+ type TplFile struct {
21
+ Name string
22
+ FileName string
23
+ TplStr string
24
+ }
25
+
20
26
type LanguageConfig struct {
21
- LeetcodeLang string
22
- CodeTplStr string
23
- TestCodeTplStr string
24
- CodeFileName string
25
- TestFileName string
27
+ LeetcodeLang string
28
+ TplFiles []TplFile
26
29
}
27
30
28
31
const (
@@ -33,25 +36,20 @@ const (
33
36
var (
34
37
languageConfigs = map [string ]LanguageConfig {
35
38
"go" : {
36
- CodeTplStr : codeStrGo ,
37
- TestCodeTplStr : testCodeStrGo ,
38
- LeetcodeLang : "Go" ,
39
- CodeFileName : "solve_%s.go" ,
40
- TestFileName : "solve_%s_test.go" ,
39
+ LeetcodeLang : "Go" ,
40
+ TplFiles : []TplFile {{"code" , "solve_%s.go" , codeStrGo }, {"test" , "solve_%s_test.go" , testCodeStrGo }},
41
41
},
42
42
"ts" : {
43
- CodeTplStr : codeStrTs ,
44
- TestCodeTplStr : testCodeStrTs ,
45
- LeetcodeLang : "TypeScript" ,
46
- CodeFileName : "solve_%s.ts" ,
47
- TestFileName : "solve_%s.test.ts" ,
43
+ LeetcodeLang : "TypeScript" ,
44
+ TplFiles : []TplFile {{"code" , "solve_%s.ts" , codeStrTs }, {"test" , "solve_%s.test.ts" , testCodeStrTs }},
48
45
},
49
46
"js" : {
50
- CodeTplStr : codeStrJs ,
51
- TestCodeTplStr : testCodeStrJs ,
52
- LeetcodeLang : "JavaScript" ,
53
- CodeFileName : "solve_%s.js" ,
54
- TestFileName : "solve_%s.test.js" ,
47
+ LeetcodeLang : "JavaScript" ,
48
+ TplFiles : []TplFile {{"code" , "solve_%s.js" , codeStrJs }, {"test" , "solve_%s.test.js" , testCodeStrJs }},
49
+ },
50
+ "py3" : {
51
+ LeetcodeLang : "Python3" ,
52
+ TplFiles : []TplFile {{"code" , "solve_%s.py" , codeStrPy3 }, {"test" , "test_%s.py" , testCodeStrPy3 }, {"__init__" , "__init__.py" , "" }},
55
53
},
56
54
}
57
55
)
@@ -107,9 +105,6 @@ func Run(n string, lang string) {
107
105
folderName := prefix + number
108
106
fp := filepath .Join (folder , folderName )
109
107
os .MkdirAll (fp , 0755 )
110
- codeFp := filepath .Join (fp , fmt .Sprintf (config .CodeFileName , number ))
111
- codeTestFp := filepath .Join (fp , fmt .Sprintf (config .TestFileName , number ))
112
- problemFp := filepath .Join (fp , "problem.md" )
113
108
metaf := & MetaWithFolder {
114
109
* meta ,
115
110
folderName ,
@@ -119,20 +114,23 @@ func Run(n string, lang string) {
119
114
metaf .Meta .Content = strings .ReplaceAll (metaf .Meta .Content , "↵" , "" )
120
115
metaf .Meta .Code = gjson .Get (metaf .CodeSnippets , fmt .Sprintf ("#(lang=%s).code" , config .LeetcodeLang )).String ()
121
116
122
- if ! fileExists (codeFp ) {
123
- bf := mustExecuteTemplate ("code" , config .CodeTplStr , metaf )
124
- ioutil .WriteFile (codeFp , bf , 0644 )
125
- }
126
-
127
- if ! fileExists (codeTestFp ) {
128
- bf := mustExecuteTemplate ("test" , config .TestCodeTplStr , metaf )
129
- ioutil .WriteFile (codeTestFp , bf , 0644 )
130
- }
131
-
117
+ problemFp := filepath .Join (fp , "problem.md" )
132
118
if ! fileExists (problemFp ) {
133
119
bf := mustExecuteTemplate ("problem" , problemStr , metaf )
134
120
ioutil .WriteFile (problemFp , bf , 0644 )
135
121
}
122
+
123
+ for _ , tpl := range config .TplFiles {
124
+ fileName := tpl .FileName
125
+ if strings .Count (tpl .FileName , "%s" ) > 0 {
126
+ fileName = fmt .Sprintf (tpl .FileName , number )
127
+ }
128
+ fp := filepath .Join (fp , fileName )
129
+ if ! fileExists (fp ) {
130
+ bf := mustExecuteTemplate (tpl .Name , tpl .TplStr , metaf )
131
+ ioutil .WriteFile (fp , bf , 0644 )
132
+ }
133
+ }
136
134
fmt .Printf ("Done: %s\n " , fp )
137
135
}
138
136
@@ -197,3 +195,21 @@ var (
197
195
it('solve_{{ .Index }} should pass', () => {})
198
196
`
199
197
)
198
+
199
+ var (
200
+ codeStrPy3 = `'''
201
+ @index {{ .Index }}
202
+ @title {{ .Title }}
203
+ @difficulty {{ .Difficulty }}
204
+ @tags {{ .TagStr }}
205
+ @draft false
206
+ @link {{ .Link }}
207
+ @frontendId {{ .FrontendId }}
208
+ '''
209
+
210
+ {{ .Code }}
211
+ `
212
+ testCodeStrPy3 = `def test_solve():
213
+ pass
214
+ `
215
+ )
0 commit comments