forked from oelmekki/pgrebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependency_resolver.go
142 lines (119 loc) · 3.19 KB
/
dependency_resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"io/ioutil"
"regexp"
"fmt"
)
/*
* Parse files to find their dependencies requirements, and return them
* sorted accordingly.
*/
func ResolveDependencies( files []string, base string ) ( sortedFiles []string, err error ) {
resolver := DependencyResolver{ initialFiles: files, Base: base }
return resolver.Resolve()
}
type DependencyResolver struct {
Base string
initialFiles []string
sortedFiles []string
pendingFiles []SourceFile
}
/*
* Actual resolve looping
*/
func ( resolver *DependencyResolver ) Resolve() ( sortedFiles []string, err error ) {
for _, file := range resolver.initialFiles {
source := SourceFile{ path: file }
err = source.ParseDependencies( resolver.Base )
if err != nil { return }
if source.Resolved( resolver.sortedFiles ) {
resolver.sortedFiles = append( resolver.sortedFiles, source.path )
resolver.RemovePending( source )
resolver.ProcessPendings()
} else {
resolver.pendingFiles = append( resolver.pendingFiles, source )
}
}
if len( resolver.pendingFiles ) > 0 {
for i := 0 ; i < len( resolver.pendingFiles ) ; i++ {
resolver.ProcessPendings()
if len( resolver.pendingFiles ) == 0 {
break
}
}
}
if len( resolver.pendingFiles ) > 0 {
err = fmt.Errorf( "Can't resolve dependencies in %s. Circular dependencies?", resolver.Base )
} else {
sortedFiles = resolver.sortedFiles
}
return
}
/*
* Check if previously unresolved dependencies now are
*/
func ( resolver *DependencyResolver ) ProcessPendings() {
for _, source := range resolver.pendingFiles {
if source.Resolved( resolver.sortedFiles ) {
resolver.sortedFiles = append( resolver.sortedFiles, source.path )
resolver.RemovePending( source )
}
}
}
/*
* Remove a resolved source file from pending files
*/
func ( resolver *DependencyResolver ) RemovePending( source SourceFile ) {
newPendings := make( []SourceFile, 0 )
for _, pending := range resolver.pendingFiles {
if pending.path != source.path {
newPendings = append( newPendings, pending )
}
}
resolver.pendingFiles = newPendings
}
type SourceFile struct {
path string
dependencies []string
}
/*
* Read dependencies from source file
*/
func ( source *SourceFile ) ParseDependencies( base string ) ( err error ) {
source.dependencies = make( []string, 0 )
file, err := ioutil.ReadFile( source.path )
dependencyFinder := regexp.MustCompile( `--\s+require\s+['"](.*)['"]` )
dependencies := dependencyFinder.FindAllStringSubmatch( string( file ), -1 )
for _, submatches := range dependencies {
if len( submatches ) > 1 {
dependency := base + "/" + submatches[1]
alreadyExists := false
for _, existing := range source.dependencies {
if existing == dependency {
alreadyExists = true
}
}
if ! alreadyExists {
source.dependencies = append( source.dependencies, dependency )
}
}
}
return
}
/*
* Check if all dependencies of current file are resolved
*/
func ( source *SourceFile ) Resolved( readyFiles []string ) bool {
for _, file := range source.dependencies {
resolved := false
for _, readyFile := range readyFiles {
if readyFile == file {
resolved = true
}
}
if ! resolved {
return false
}
}
return true
}