Created
October 12, 2018 15:52
-
-
Save Bios-Marcel/c5a10ac48d0a8219d32b4828c5f79e34 to your computer and use it in GitHub Desktop.
Add package-info.java to all folders containing .java files
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
package main | |
import ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"path/filepath" | |
"strings" | |
) | |
var template = `%s | |
@org.eclipse.jdt.annotation.NonNullByDefault | |
package %s; | |
` | |
//Recursively creates NonNullByDefault annotated java package info files. | |
func main() { | |
comment := flag.String("comment", "", "--comment <Comment>") | |
workspace := flag.String("workspace", "", "--workspace <Path to workspace>") | |
flag.Parse() | |
if workspace == nil || len(*workspace) == 0 { | |
fmt.Println("Workspace path hasn't been specified!") | |
os.Exit(1) | |
} else { | |
absWorkingDir, err := filepath.Abs(filepath.Dir(os.Args[0])) | |
if err != nil { | |
panic(err) | |
} | |
absWorkspaceDir := filepath.Join(absWorkingDir, *workspace) | |
createPackageInfo(absWorkspaceDir, *comment) | |
} | |
} | |
func createPackageInfo(workspace string, comment string) error { | |
return filepath.Walk(workspace, func(path string, info os.FileInfo, err error) error { | |
if err != nil { | |
panic(err) | |
} | |
if info.IsDir() { | |
if DirContainsJavaFiles(path) { | |
_, infoError := os.Stat(filepath.Join(path, "package-info.java")) | |
if os.IsNotExist(infoError) { | |
packagePath := strings.Replace(path, workspace, "", 1) | |
packagePathDots := strings.TrimPrefix(strings.Replace(packagePath, "\\", ".", -1), ".") | |
fileContent := fmt.Sprintf(template, comment, packagePathDots) | |
fmt.Println(fmt.Sprintf("Adding package-info.java to '%s'", path)) | |
ioutil.WriteFile(filepath.Join(path, "package-info.java"), []byte(fileContent), os.ModeType) | |
} else { | |
fmt.Println(fmt.Sprintf("Skipping '%s', as a package-info.java already exists.", path)) | |
//TODO NonNullByDefaultHinzufügen | |
} | |
} | |
return nil | |
} | |
return nil | |
}) | |
} | |
func DirContainsJavaFiles(dir string) bool { | |
files, err := ioutil.ReadDir(dir) | |
if err != nil { | |
return false | |
} | |
for _, fileInfo := range files { | |
if strings.HasSuffix(fileInfo.Name(), ".java") { | |
return true | |
} | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment