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

Graph Construction : kcl mod graph to show the dependency graph of KCL package #265

Merged
merged 12 commits into from
Feb 20, 2024
29 changes: 29 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,35 @@ func (c *KpmClient) DownloadFromGit(dep *pkg.Git, localPath string) (string, err
return localPath, err
}

// Parse kcl.mod file and extract dependencies
func (c *KpmClient) ParseKclModFile(kclPkg *pkg.KclPkg) (map[string][]string, error) {
// Get path to kcl.mod file
modFilePath := kclPkg.ModFile.GetModFilePath()

// Parse kcl.mod file
modFileBytes, err := os.ReadFile(modFilePath)
if err != nil {
return nil, err
}

// Initialize map to store dependencies
dependencies := make(map[string][]string)

// Parse each line in the mod file
lines := strings.Split(string(modFileBytes), "\n")
for _, line := range lines {
// Extract dependency name and version
parts := strings.Fields(line)
if len(parts) >= 2 {
dependency := parts[0]
version := parts[1]
dependencies[dependency] = append(dependencies[dependency], version)
}
}

return dependencies, nil
}

// DownloadFromOci will download the dependency from the oci repository.
func (c *KpmClient) DownloadFromOci(dep *pkg.Oci, localPath string) (string, error) {
ociClient, err := oci.NewOciClient(dep.Reg, dep.Repo, &c.settings)
Expand Down
57 changes: 57 additions & 0 deletions pkg/client/dependency_graph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package client

import pkg "kcl-lang.io/kpm/pkg/package"

// Construct dependency graph
type DependencyGraph map[string][]string

// Function to construct dependency graph by parsing kcl.mod file
func ConstructDependencyGraphFromModFile(kpmClient *KpmClient, kclPkg *pkg.KclPkg) (DependencyGraph, error) {
dependencies, err := kpmClient.ParseKclModFile(kclPkg)
if err != nil {
return nil, err
}
return ConstructDependencyGraph(dependencies), nil
}

// Function to construct dependency graph from dependency map
func ConstructDependencyGraph(dependencies map[string][]string) DependencyGraph {
graph := make(DependencyGraph)
for dependency, versions := range dependencies {
graph[dependency] = versions
}
return graph
}

// Traverse dependency graph using depth-first search (DFS)
func DFS(graph DependencyGraph, dependency string, visited map[string]bool, result []string) []string {
// Mark current dependency as visited
visited[dependency] = true

// Add current dependency to result
result = append(result, dependency)

// Recursively traverse dependencies
for _, dep := range graph[dependency] {
if !visited[dep] {
result = DFS(graph, dep, visited, result)
}
}

return result
}

// Output dependencies in the same format as go mod graph
func OutputDependencies(graph DependencyGraph) []string {
result := make([]string, 0)
visited := make(map[string]bool)

// Traverse each dependency using DFS
for dependency := range graph {
if !visited[dependency] {
result = DFS(graph, dependency, visited, result)
}
}

return result
}
Loading