-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into report-when-dependency-graph-contains-cycle
Signed-off-by: Akash Kumar <meakash7902@gmail.com>
- Loading branch information
Showing
3 changed files
with
158 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
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,71 @@ | ||
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]map[string]string) DependencyGraph { | ||
graph := make(DependencyGraph) | ||
for dependency, details := range dependencies { | ||
// Construct full module path including version or other attributes | ||
fullPath := dependency | ||
if version, ok := details["version"]; ok { | ||
fullPath += "@" + version | ||
} else if gitURL, ok := details["git"]; ok { | ||
fullPath += "@" + gitURL | ||
if tag, ok := details["tag"]; ok { | ||
fullPath += "#" + tag | ||
} else if commit, ok := details["commit"]; ok { | ||
fullPath += "@" + commit | ||
} | ||
} else if path, ok := details["path"]; ok { | ||
fullPath += "@" + path | ||
} | ||
graph[fullPath] = make([]string, 0) | ||
} | ||
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 | ||
} |