Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: skipping files based on regex and fixing parsing of args #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ The command line version is more powerful as you can add flags and a path, see t
- You might need to provide permissions, this program is not a virus. On Unix, run `chmod +x renpy-graphviz*`.
- `renpy-graphviz.png` should appear, **enjoy**!

Use the boolean flags only in `-flag=value` format, not in `-flag value ` format, e.g. use `./renpy-graphviz -open=false`.

### Go library

```cmd
Expand Down
25 changes: 19 additions & 6 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"pkg.amethysts.studio/renpy-graphviz/parser"
)

// isFlag checks if a string is a flag (starts with a dash)
func isFlag(arg string) bool {
return len(arg) > 0 && arg == "-"
}

// PlugCLI handles the Command Line Interface
func PlugCLI() (string, parser.RenpyGraphOptions) {

Expand All @@ -31,6 +36,7 @@ func PlugCLI() (string, parser.RenpyGraphOptions) {
var hideScreens bool
var hideNestedScreens bool
var fullDebug bool
var skipFilesRegex string

// TOML
getDefaultConfig()
Expand All @@ -45,6 +51,7 @@ func PlugCLI() (string, parser.RenpyGraphOptions) {
hideNestedScreens = !config.Get("config.nested-screens").(bool)
silent = config.Get("config.silent").(bool)
fullDebug = config.Get("config.debug").(bool)
skipFilesRegex = config.Get("config.skip-files").(string)
}

// CLI overrides TOML
Expand All @@ -55,19 +62,25 @@ func PlugCLI() (string, parser.RenpyGraphOptions) {
flag.BoolVar(&hideScreens, "hide-screens", hideScreens, "Hide screens")
flag.BoolVar(&hideNestedScreens, "hide-nested", hideNestedScreens, "Hide nested screens")
flag.BoolVar(&fullDebug, "debug", fullDebug, "Debug")
flag.StringVar(&skipFilesRegex, "skip-files", skipFilesRegex, "Regex pattern for excluding files")

flag.Parse()

path := "."
if len(flag.Args()) > 0 {
path = flag.Args()[0]
// Manually handle the non-flag argument
var path string
if len(os.Args) > 1 && !isFlag(os.Args[1]) {
path = os.Args[1]
os.Args = append(os.Args[:1], os.Args[2:]...) // Remove the non-flag argument from os.Args
} else {
path = "."
}
flag.Parse()
return path, parser.RenpyGraphOptions{
ShowEdgesLabels: !hideEdgesLabels,
ShowAtoms: showAtoms,
Silent: silent,
OpenFile: openFile,
ShowScreens: !hideScreens,
ShowNestedScreens: !hideNestedScreens,
FullDebug: fullDebug}
FullDebug: fullDebug,
SkipFilesRegex: skipFilesRegex,
}
}
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ silent = false # default: false
# Debug mode
debug = false # default: false

# Regex for skipping files
skip-files = "" # default: ""

### You can edit ↑ above ↑

### HOW TO USE THE PROGRAM? ###
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func main() {
path, options := PlugCLI()

content := parser.GetRenpyContent(path)
content := parser.GetRenpyContent(path, options)

graph, err := parser.Graph(content, options)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion parser/examples_graphs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ digraph {
r := strings.NewReplacer(" ", "", "\t", "", "\n", "")
for _, tc := range testCases {
t.Run(tc.pathToScript, func(t *testing.T) {
renpyLines := GetRenpyContent(tc.pathToScript)
renpyLines := GetRenpyContent(tc.pathToScript, tc.options)
graphResult, err := Graph(renpyLines, tc.options)
require.NoError(t, err)

Expand Down
14 changes: 11 additions & 3 deletions parser/filehandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

// GetRenpyContent opens all renpy files and transform them into a string list
// 1 line of script = 1 list element
func GetRenpyContent(rootPath string) []string {
files, err := walkMatch(rootPath, "*.rpy")
func GetRenpyContent(rootPath string, options RenpyGraphOptions) []string {
files, err := walkMatch(rootPath, "*.rpy", options.SkipFilesRegex)
if err != nil {
log.Fatalf("failed to find root folder: %s", err)
}
Expand Down Expand Up @@ -59,7 +59,7 @@ func GetRenpyContent(rootPath string) []string {
return fileTextLines
}

func walkMatch(root, pattern string) ([]string, error) {
func walkMatch(root, pattern string, skipPattern string) ([]string, error) {
var matches []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand All @@ -68,6 +68,14 @@ func walkMatch(root, pattern string) ([]string, error) {
if info.IsDir() {
return nil
}
if filepath.Base(path) == "test.rpy" {
println("foudn test rpy")
}
if matched, err := filepath.Match(skipPattern, filepath.Base(path)); err != nil {
return err
} else if matched {
return nil
}
if matched, err := filepath.Match(pattern, filepath.Base(path)); err != nil {
return err
} else if matched {
Expand Down
2 changes: 1 addition & 1 deletion parser/filehandling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestGetRenpyContent(t *testing.T) {
result := GetRenpyContent("../testCases/simple")
result := GetRenpyContent("../testCases/simple", RenpyGraphOptions{})

expected := strings.Split(`label start:
"Is my VN simple ?"
Expand Down
15 changes: 8 additions & 7 deletions parser/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ type RenpyGraph struct {

// RenpyGraphOptions are options that can be set to make a more customizable graph
type RenpyGraphOptions struct {
ShowEdgesLabels bool // Show Labels on Edges? Can be unreadable but there is more information
ShowAtoms bool // Show lonely nodes ? Might be useful but useless most of the time - and it avoids writing IGNORE tag everywhere
ShowScreens bool // Show screens ?
ShowNestedScreens bool // Show nested screens (`use` keyword)
Silent bool // Display .dot graph in the stdout
OpenFile bool // Open the image in the default image viewer or not ?
FullDebug bool // Show all lines and updates
ShowEdgesLabels bool // Show Labels on Edges? Can be unreadable but there is more information
ShowAtoms bool // Show lonely nodes ? Might be useful but useless most of the time - and it avoids writing IGNORE tag everywhere
ShowScreens bool // Show screens ?
ShowNestedScreens bool // Show nested screens (`use` keyword)
Silent bool // Display .dot graph in the stdout
OpenFile bool // Open the image in the default image viewer or not ?
FullDebug bool // Show all lines and updates
SkipFilesRegex string // Skip all files matching this regex
}

// NewGraph creates an empty graph
Expand Down
4 changes: 2 additions & 2 deletions parser/perf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (

func BenchmarkGetContent(b *testing.B) {
for i := 0; i < b.N; i++ {
GetRenpyContent("../testCases/complex")
GetRenpyContent("../testCases/complex", RenpyGraphOptions{})
}
}

var graph RenpyGraph

func BenchmarkGraph(b *testing.B) {
var g RenpyGraph
renpyLines := GetRenpyContent("../testCases/complex")
renpyLines := GetRenpyContent("../testCases/complex", RenpyGraphOptions{})
for i := 0; i < b.N; i++ {
g, _ = Graph(renpyLines, RenpyGraphOptions{})
}
Expand Down