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
30 changes: 30 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"encoding/json"
"fmt"
"io"
"io/ioutil"

Check failure on line 7 in pkg/client/client.go

View workflow job for this annotation

GitHub Actions / Lint checks

SA1019: "io/ioutil" has been deprecated since Go 1.16: As of Go 1.16, the same functionality is now provided by package io or package os, and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
Peefy marked this conversation as resolved.
Show resolved Hide resolved
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -823,6 +824,35 @@
return localPath, err
}

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

// Parse kcl.mod file
modFileBytes, err := ioutil.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
46 changes: 46 additions & 0 deletions pkg/client/dependency_graph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package client

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

// 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