-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
86ab407
commit 09579dc
Showing
1 changed file
with
118 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package dump | ||
|
||
import ( | ||
"encoding/csv" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/urfave/cli/v2" | ||
"github.com/williamfzc/srctx/parser" | ||
"os" | ||
"slices" | ||
"strconv" | ||
) | ||
|
||
var flags = []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "src", | ||
Value: ".", | ||
Usage: "project path", | ||
}, | ||
&cli.StringFlag{ | ||
Name: "csv", | ||
Value: "output.csv", | ||
Usage: "output csv file", | ||
}, | ||
} | ||
|
||
type Options struct { | ||
Src string `json:"src"` | ||
Csv string `json:"csv"` | ||
} | ||
|
||
func AddDumpCmd(app *cli.App) { | ||
dumpCmd := &cli.Command{ | ||
Name: "dump", | ||
Usage: "dump file relations", | ||
Flags: flags, | ||
Action: func(cCtx *cli.Context) error { | ||
src := cCtx.String("src") | ||
csvPath := cCtx.String("csv") | ||
|
||
sourceContext, err := parser.FromGolangSrc(src) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
files := sourceContext.Files() | ||
slices.Sort(files) | ||
log.Infof("files in lsif: %d", len(files)) | ||
|
||
fileCount := len(files) | ||
relationMatrix := make([][]int, fileCount) | ||
for i := range relationMatrix { | ||
relationMatrix[i] = make([]int, fileCount) | ||
} | ||
|
||
fileIndexMap := make(map[string]int) | ||
for idx, file := range files { | ||
fileIndexMap[file] = idx | ||
} | ||
|
||
for _, file := range files { | ||
defs, err := sourceContext.DefsByFileName(file) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, def := range defs { | ||
refs, err := sourceContext.RefsFromDefId(def.Id()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, ref := range refs { | ||
refFile := sourceContext.FileName(ref.FileId) | ||
if refFile == file { | ||
continue | ||
} | ||
if refFile != "" { | ||
relationMatrix[fileIndexMap[file]][fileIndexMap[refFile]]++ | ||
} | ||
} | ||
} | ||
} | ||
|
||
csvFile, err := os.Create(csvPath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer csvFile.Close() | ||
|
||
writer := csv.NewWriter(csvFile) | ||
defer writer.Flush() | ||
|
||
header := append([]string{""}, files...) | ||
if err := writer.Write(header); err != nil { | ||
panic(err) | ||
} | ||
|
||
for i, row := range relationMatrix { | ||
csvRow := make([]string, len(row)+1) | ||
csvRow[0] = files[i] | ||
for j, val := range row { | ||
if val == 0 { | ||
csvRow[j+1] = "" | ||
} else { | ||
csvRow[j+1] = strconv.Itoa(val) | ||
} | ||
} | ||
if err := writer.Write(csvRow); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
log.Infof("CSV file generated successfully.") | ||
return nil | ||
}, | ||
} | ||
app.Commands = append(app.Commands, dumpCmd) | ||
} |