-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yugj
committed
Dec 4, 2023
1 parent
43405ff
commit 58f3783
Showing
9 changed files
with
216 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
# sql to java change log | ||
|
||
## 2023-12-02 | ||
* fix windows os .Author null | ||
* support organizing directories by package path | ||
* support some built-in functions | ||
|
||
## 2023-11-25 | ||
* support generate java file from templates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package gen | ||
|
||
import ( | ||
"log" | ||
"os" | ||
) | ||
|
||
func createDirIfNeed(basePath string, name string) { | ||
|
||
var path = basePath + "/" + name | ||
autoCreateDir(path) | ||
} | ||
|
||
func autoCreateDir(path string) { | ||
|
||
if !existsPath(path) { | ||
_ = os.MkdirAll(path, os.ModePerm) | ||
} | ||
} | ||
|
||
func existsPath(path string) bool { | ||
_, err := os.Stat(path) | ||
if err != nil { | ||
if os.IsExist(err) { | ||
return true | ||
} | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func openFile(destFile string) *os.File { | ||
file, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE, 0644) | ||
if err != nil { | ||
log.Println("Open file err =", err) | ||
panic(err) | ||
} | ||
//defer file.Close() | ||
return file | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package gen | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func outputFormat(baseDir string) { | ||
|
||
var outputDir = baseDir + "/output" | ||
var outputFormatDir = baseDir + "/output_format" | ||
|
||
filePackageMap, err := getJavaPackagePath(outputDir) | ||
if err != nil { | ||
return | ||
} | ||
|
||
for javaFile, packagePath := range filePackageMap { | ||
println(javaFile + "->" + packagePath) | ||
dest := outputFormatDir + "/" + packagePath | ||
fileCopy(javaFile, dest) | ||
} | ||
} | ||
|
||
// java file path -> packagePath | ||
func getJavaPackagePath(outputDir string) (map[string]string, error) { | ||
|
||
var result = make(map[string]string) | ||
outputDirFiles, _ := ioutil.ReadDir(outputDir) | ||
|
||
for _, subDir := range outputDirFiles { | ||
abstSubDir := outputDir + "/" + subDir.Name() | ||
|
||
if !subDir.IsDir() { | ||
continue | ||
} | ||
|
||
subDirFiles, _ := ioutil.ReadDir(abstSubDir) | ||
for _, file := range subDirFiles { | ||
if strings.HasSuffix(file.Name(), ".java") { | ||
javaPath := abstSubDir + "/" + file.Name() | ||
|
||
packageName := getPackageName(javaPath) | ||
|
||
if len(packageName) == 0 { | ||
continue | ||
} | ||
|
||
result[javaPath] = packageName2Path(packageName) | ||
} | ||
} | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func packageName2Path(packageName string) string { | ||
return strings.ReplaceAll(packageName, ".", "/") | ||
} | ||
|
||
func getPackageName(javaFile string) string { | ||
|
||
packageLine := readFirstLine(javaFile) | ||
if len(packageLine) == 0 { | ||
return "" | ||
} | ||
|
||
packageLine = strings.TrimPrefix(packageLine, "package") | ||
packageLine = strings.TrimSuffix(packageLine, ";") | ||
packageLine = strings.TrimSpace(packageLine) | ||
|
||
return packageLine | ||
} | ||
|
||
func readFirstLine(javaFile string) string { | ||
|
||
file, err := os.Open(javaFile) | ||
if err != nil { | ||
fmt.Println("Failed to open file:", err) | ||
return "" | ||
} | ||
|
||
defer file.Close() | ||
scanner := bufio.NewScanner(file) | ||
if scanner.Scan() { | ||
firstLine := scanner.Text() | ||
return firstLine | ||
} else { | ||
return "" | ||
} | ||
} | ||
|
||
func fileCopy(sourceFile string, dir string) { | ||
|
||
if !existsPath(dir) { | ||
autoCreateDir(dir) | ||
} | ||
|
||
_, fileName := filepath.Split(sourceFile) | ||
|
||
input, _ := ioutil.ReadFile(sourceFile) | ||
|
||
destFile := dir + "/" + fileName | ||
_ = ioutil.WriteFile(destFile, input, 0644) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters