@@ -17,12 +17,15 @@ import (
1717 "github.com/zcong1993/leetcode-tool/pkg/leetcode"
1818)
1919
20+ type TplFile struct {
21+ Name string
22+ FileName string
23+ TplStr string
24+ }
25+
2026type LanguageConfig struct {
21- LeetcodeLang string
22- CodeTplStr string
23- TestCodeTplStr string
24- CodeFileName string
25- TestFileName string
27+ LeetcodeLang string
28+ TplFiles []TplFile
2629}
2730
2831const (
@@ -33,25 +36,20 @@ const (
3336var (
3437 languageConfigs = map [string ]LanguageConfig {
3538 "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 }},
4141 },
4242 "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 }},
4845 },
4946 "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" , "" }},
5553 },
5654 }
5755)
@@ -107,9 +105,6 @@ func Run(n string, lang string) {
107105 folderName := prefix + number
108106 fp := filepath .Join (folder , folderName )
109107 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" )
113108 metaf := & MetaWithFolder {
114109 * meta ,
115110 folderName ,
@@ -119,20 +114,23 @@ func Run(n string, lang string) {
119114 metaf .Meta .Content = strings .ReplaceAll (metaf .Meta .Content , "↵" , "" )
120115 metaf .Meta .Code = gjson .Get (metaf .CodeSnippets , fmt .Sprintf ("#(lang=%s).code" , config .LeetcodeLang )).String ()
121116
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" )
132118 if ! fileExists (problemFp ) {
133119 bf := mustExecuteTemplate ("problem" , problemStr , metaf )
134120 ioutil .WriteFile (problemFp , bf , 0644 )
135121 }
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+ }
136134 fmt .Printf ("Done: %s\n " , fp )
137135}
138136
@@ -197,3 +195,21 @@ var (
197195it('solve_{{ .Index }} should pass', () => {})
198196`
199197)
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