-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from Peefy/mod-graph-cmd
feat: add kcl mod graph command
- Loading branch information
Showing
4 changed files
with
169 additions
and
66 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
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,99 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/dominikbraun/graph" | ||
"github.com/spf13/cobra" | ||
"kcl-lang.io/kpm/pkg/client" | ||
"kcl-lang.io/kpm/pkg/env" | ||
pkg "kcl-lang.io/kpm/pkg/package" | ||
"kcl-lang.io/kpm/pkg/reporter" | ||
) | ||
|
||
const ( | ||
modGraphDesc = `This command prints the module dependency graph. | ||
Each module is identified as a string of the form path@version. | ||
` | ||
modGraphExample = ` # Print the current module dependency graph. | ||
kcl mod graph` | ||
) | ||
|
||
// NewModGraphCmd returns the mod graph command. | ||
func NewModGraphCmd(cli *client.KpmClient) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "graph", | ||
Short: "prints dependencies", | ||
Long: modGraphDesc, | ||
Example: modGraphExample, | ||
RunE: func(_ *cobra.Command, args []string) error { | ||
return ModGraph(cli, args) | ||
}, | ||
SilenceUsage: true, | ||
} | ||
return cmd | ||
} | ||
|
||
func ModGraph(cli *client.KpmClient, args []string) error { | ||
// acquire the lock of the package cache. | ||
err := cli.AcquirePackageCacheLock() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer func() { | ||
// release the lock of the package cache after the function returns. | ||
releaseErr := cli.ReleasePackageCacheLock() | ||
if releaseErr != nil && err == nil { | ||
err = releaseErr | ||
} | ||
}() | ||
|
||
pwd, err := os.Getwd() | ||
|
||
if err != nil { | ||
return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.") | ||
} | ||
|
||
globalPkgPath, err := env.GetAbsPkgPath() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
kclPkg, err := pkg.LoadKclPkg(pwd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = kclPkg.ValidateKpmHome(globalPkgPath) | ||
if err != (*reporter.KpmEvent)(nil) { | ||
return err | ||
} | ||
|
||
depGraph, err := cli.GetDependencyGraph(kclPkg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
adjMap, err := depGraph.AdjacencyMap() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Print the dependency graph to stdout. | ||
root := fmt.Sprintf("%s@%s", kclPkg.GetPkgName(), kclPkg.GetPkgVersion()) | ||
err = graph.BFS(depGraph, root, func(source string) bool { | ||
for target := range adjMap[source] { | ||
reporter.ReportMsgTo( | ||
fmt.Sprint(source, " ", target), | ||
cli.GetLogWriter(), | ||
) | ||
} | ||
return false | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
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