Skip to content

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

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

Merged
merged 12 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
37 changes: 37 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"strings"

"github.com/BurntSushi/toml"
"github.com/dominikbraun/graph"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/otiai10/copy"
Expand Down Expand Up @@ -852,6 +853,42 @@ func (c *KpmClient) DownloadFromGit(dep *pkg.Git, localPath string) (string, err
return localPath, err
}

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

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

// Normalize line endings for Windows systems
modFileContent := strings.ReplaceAll(string(modFileBytes), "\r\n", "\n")

// Parse the TOML content
var modFileData map[string]interface{}
if err := toml.Unmarshal([]byte(modFileContent), &modFileData); err != nil {
return nil, err
}

// Extract dependency information
dependencies := make(map[string][]string)
if deps, ok := modFileData["dependencies"].(map[string]interface{}); ok {
for dep, versions := range deps {
if v, ok := versions.([]interface{}); ok {
for _, ver := range v {
if version, ok := ver.(string); ok {
dependencies[dep] = append(dependencies[dep], 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
}