|
| 1 | +package lang |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/j178/leetgo/config" |
| 7 | + "github.com/j178/leetgo/leetcode" |
| 8 | + "github.com/j178/leetgo/utils" |
| 9 | +) |
| 10 | + |
| 11 | +type java struct { |
| 12 | + baseLang |
| 13 | +} |
| 14 | + |
| 15 | +func (j java) HasInitialized(outDir string) (bool, error) { |
| 16 | + return false, nil |
| 17 | +} |
| 18 | + |
| 19 | +func (j java) Initialize(outDir string) error { |
| 20 | + return nil |
| 21 | +} |
| 22 | + |
| 23 | +func (j java) RunLocalTest(q *leetcode.QuestionData, outDir string, targetCase string) (bool, error) { |
| 24 | + genResult, err := j.GeneratePaths(q) |
| 25 | + if err != nil { |
| 26 | + return false, fmt.Errorf("generate paths failed: %w", err) |
| 27 | + } |
| 28 | + genResult.SetOutDir(outDir) |
| 29 | + |
| 30 | + testFile := genResult.GetFile(TestFile).GetPath() |
| 31 | + if !utils.IsExist(testFile) { |
| 32 | + return false, fmt.Errorf("file %s not found", utils.RelToCwd(testFile)) |
| 33 | + } |
| 34 | + |
| 35 | + execDir, err := getTempBinDir(q, j) |
| 36 | + if err != nil { |
| 37 | + return false, fmt.Errorf("get temp bin dir failed: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + args := []string{"javac", "-d", execDir, testFile} |
| 41 | + err = buildTest(q, genResult, args) |
| 42 | + if err != nil { |
| 43 | + return false, fmt.Errorf("build failed: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + args = []string{"java", "--class-path", execDir, "Main"} |
| 47 | + return runTest(q, genResult, args, targetCase) |
| 48 | +} |
| 49 | + |
| 50 | +func (j java) generateNormalTestCode(q *leetcode.QuestionData) (string, error) { |
| 51 | + return "", nil |
| 52 | +} |
| 53 | + |
| 54 | +func (j java) generateSystemDesignTestCode(q *leetcode.QuestionData) (string, error) { |
| 55 | + return "", nil |
| 56 | +} |
| 57 | + |
| 58 | +func (j java) generateTestContent(q *leetcode.QuestionData) (string, error) { |
| 59 | + if q.MetaData.SystemDesign { |
| 60 | + return j.generateSystemDesignTestCode(q) |
| 61 | + } |
| 62 | + return j.generateNormalTestCode(q) |
| 63 | +} |
| 64 | + |
| 65 | +func (j java) generateCodeFile( |
| 66 | + q *leetcode.QuestionData, |
| 67 | + filename string, |
| 68 | + blocks []config.Block, |
| 69 | + modifiers []ModifierFunc, |
| 70 | + separateDescriptionFile bool, |
| 71 | +) ( |
| 72 | + FileOutput, |
| 73 | + error, |
| 74 | +) { |
| 75 | + codeHeader := "" |
| 76 | + testContent, err := j.generateTestContent(q) |
| 77 | + if err != nil { |
| 78 | + return FileOutput{}, err |
| 79 | + } |
| 80 | + blocks = append( |
| 81 | + []config.Block{ |
| 82 | + { |
| 83 | + Name: beforeBeforeMarker, |
| 84 | + Template: codeHeader, |
| 85 | + }, |
| 86 | + { |
| 87 | + Name: afterAfterMarker, |
| 88 | + Template: testContent, |
| 89 | + }, |
| 90 | + }, |
| 91 | + blocks..., |
| 92 | + ) |
| 93 | + content, err := j.generateCodeContent( |
| 94 | + q, |
| 95 | + blocks, |
| 96 | + modifiers, |
| 97 | + separateDescriptionFile, |
| 98 | + ) |
| 99 | + if err != nil { |
| 100 | + return FileOutput{}, err |
| 101 | + } |
| 102 | + return FileOutput{ |
| 103 | + Filename: filename, |
| 104 | + Content: content, |
| 105 | + Type: CodeFile | TestFile, |
| 106 | + }, nil |
| 107 | +} |
| 108 | + |
| 109 | +func (j java) GeneratePaths(q *leetcode.QuestionData) (*GenerateResult, error) { |
| 110 | + filenameTmpl := getFilenameTemplate(q, j) |
| 111 | + baseFilename, err := q.GetFormattedFilename(j.slug, filenameTmpl) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + genResult := &GenerateResult{ |
| 116 | + SubDir: baseFilename, |
| 117 | + Question: q, |
| 118 | + Lang: j, |
| 119 | + } |
| 120 | + genResult.AddFile( |
| 121 | + FileOutput{ |
| 122 | + Filename: "solution.java", |
| 123 | + Type: CodeFile | TestFile, |
| 124 | + }, |
| 125 | + ) |
| 126 | + genResult.AddFile( |
| 127 | + FileOutput{ |
| 128 | + Filename: "testcases.txt", |
| 129 | + Type: TestCasesFile, |
| 130 | + }, |
| 131 | + ) |
| 132 | + if separateDescriptionFile(j) { |
| 133 | + genResult.AddFile( |
| 134 | + FileOutput{ |
| 135 | + Filename: "question.md", |
| 136 | + Type: DocFile, |
| 137 | + }, |
| 138 | + ) |
| 139 | + } |
| 140 | + return genResult, nil |
| 141 | +} |
| 142 | + |
| 143 | +func (j java) Generate(q *leetcode.QuestionData) (*GenerateResult, error) { |
| 144 | + filenameTmpl := getFilenameTemplate(q, j) |
| 145 | + baseFilename, err := q.GetFormattedFilename(j.slug, filenameTmpl) |
| 146 | + if err != nil { |
| 147 | + return nil, err |
| 148 | + } |
| 149 | + genResult := &GenerateResult{ |
| 150 | + Question: q, |
| 151 | + Lang: j, |
| 152 | + SubDir: baseFilename, |
| 153 | + } |
| 154 | + |
| 155 | + separateDescriptionFile := separateDescriptionFile(j) |
| 156 | + blocks := getBlocks(j) |
| 157 | + modifiers, err := getModifiers(j, builtinModifiers) |
| 158 | + if err != nil { |
| 159 | + return nil, err |
| 160 | + } |
| 161 | + codeFile, err := j.generateCodeFile(q, "solution.java", blocks, modifiers, separateDescriptionFile) |
| 162 | + if err != nil { |
| 163 | + return nil, err |
| 164 | + } |
| 165 | + testcaseFile, err := j.generateTestCasesFile(q, "testcases.txt") |
| 166 | + if err != nil { |
| 167 | + return nil, err |
| 168 | + } |
| 169 | + genResult.AddFile(codeFile) |
| 170 | + genResult.AddFile(testcaseFile) |
| 171 | + |
| 172 | + if separateDescriptionFile { |
| 173 | + docFile, err := j.generateDescriptionFile(q, "question.md") |
| 174 | + if err != nil { |
| 175 | + return nil, err |
| 176 | + } |
| 177 | + genResult.AddFile(docFile) |
| 178 | + } |
| 179 | + |
| 180 | + return genResult, nil |
| 181 | +} |
0 commit comments