1
1
package resolution
2
2
3
3
import (
4
- "os"
5
4
"path"
6
5
"path/filepath"
7
6
"strings"
8
7
9
8
"github.com/vknabel/lithia/ast"
9
+ "github.com/vknabel/lithia/world"
10
10
)
11
11
12
12
type ModuleResolver struct {
@@ -20,7 +20,7 @@ type ModuleResolver struct {
20
20
lithiaSourceGlob string
21
21
}
22
22
23
- func DefaultModuleResolver (importRoots ... string ) ModuleResolver {
23
+ func NewDefaultModuleResolver (importRoots ... string ) ModuleResolver {
24
24
return ModuleResolver {
25
25
externalImportRoots : defaultImportRoots (importRoots ... ),
26
26
defaultPackageName : "root" ,
@@ -54,13 +54,13 @@ type PackageManifest struct {
54
54
55
55
func defaultImportRoots (importRoots ... string ) []string {
56
56
roots := importRoots
57
- if path , ok := os .LookupEnv ("LITHIA_LOCALS" ); ok {
57
+ if path , ok := world . Current . Env .LookupEnv ("LITHIA_LOCALS" ); ok {
58
58
roots = append (roots , path )
59
59
}
60
- if path , ok := os .LookupEnv ("LITHIA_PACKAGES" ); ok {
60
+ if path , ok := world . Current . Env .LookupEnv ("LITHIA_PACKAGES" ); ok {
61
61
roots = append (roots , path )
62
62
}
63
- if path , ok := os .LookupEnv ("LITHIA_STDLIB" ); ok {
63
+ if path , ok := world . Current . Env .LookupEnv ("LITHIA_STDLIB" ); ok {
64
64
roots = append (roots , path )
65
65
} else {
66
66
roots = append (roots , "/usr/local/opt/lithia/stdlib" )
@@ -81,7 +81,7 @@ func (mr *ModuleResolver) ResolvePackageForReferenceFile(referenceFile string) R
81
81
referenceFile = removingFilePrefix (referenceFile )
82
82
for _ , candidates := range mr .manifestSearchPaths {
83
83
manifestPath := filepath .Join (path .Dir (referenceFile ), candidates , mr .manifestName )
84
- if _ , err := os .Stat (manifestPath ); err == nil {
84
+ if _ , err := world . Current . FS .Stat (manifestPath ); err == nil {
85
85
packagePath := path .Dir (manifestPath )
86
86
return ResolvedPackage {
87
87
Name : mr .defaultPackageName ,
@@ -92,7 +92,7 @@ func (mr *ModuleResolver) ResolvePackageForReferenceFile(referenceFile string) R
92
92
}
93
93
}
94
94
}
95
- dir , err := os .Getwd ()
95
+ dir , err := world . Current . FS .Getwd ()
96
96
if err != nil {
97
97
dir = path .Dir (referenceFile )
98
98
}
@@ -108,6 +108,11 @@ func (mr *ModuleResolver) ResolvePackageAndModuleForReferenceFile(referenceFile
108
108
}
109
109
moduleFilepath := filepath .Dir (relativeFile )
110
110
moduleParts := strings .Split (moduleFilepath , string (filepath .Separator ))
111
+ for i := len (moduleParts ) - 1 ; i >= 0 ; i -- {
112
+ if moduleParts [i ] == "." {
113
+ moduleParts = append (moduleParts [:i ], moduleParts [i + 1 :]... )
114
+ }
115
+ }
111
116
resolvedModule , err := mr .resolveModuleWithinPackage (pkg , moduleParts )
112
117
if err != nil {
113
118
return mr .CreateSingleFileModule (pkg , referenceFile )
@@ -121,9 +126,11 @@ func (mr *ModuleResolver) AddRootImportPath(path string) {
121
126
122
127
func (mr * ModuleResolver ) CreateSingleFileModule (pkg ResolvedPackage , file string ) ResolvedModule {
123
128
file = removingFilePrefix (file )
129
+ trimmed := strings .TrimSuffix (filepath .Base (file ), ".lithia" )
130
+ uniform := strings .ReplaceAll (trimmed , "." , "_" )
124
131
return ResolvedModule {
125
132
packageRef : & pkg ,
126
- relativeName : ast .ModuleName (strings . ReplaceAll ( strings . TrimSuffix ( filepath . Base ( file ), ".lithia" ), "." , "_" ) ),
133
+ relativeName : ast .ModuleName (uniform ),
127
134
Path : file ,
128
135
Files : []string {file },
129
136
}
@@ -143,10 +150,10 @@ func (mr *ModuleResolver) ResolveModuleFromPackage(pkg ResolvedPackage, moduleNa
143
150
searchPaths := append ([]string {pkg .Path }, mr .externalImportRoots ... )
144
151
for _ , searchPath := range searchPaths {
145
152
packagePath := path .Join (searchPath , packageName )
146
- if info , err := os .Stat (packagePath ); err == nil && info .IsDir () {
153
+ if info , err := world . Current . FS .Stat (packagePath ); err == nil && info .IsDir () {
147
154
var match ResolvedPackage
148
155
manifestPath := path .Join (packagePath , mr .manifestName )
149
- if _ , err := os .Stat (manifestPath ); err == nil && ! info .IsDir () {
156
+ if _ , err := world . Current . FS .Stat (manifestPath ); err == nil && ! info .IsDir () {
150
157
match = ResolvedPackage {
151
158
Name : packageName ,
152
159
Path : packagePath ,
@@ -165,15 +172,15 @@ func (mr *ModuleResolver) ResolveModuleFromPackage(pkg ResolvedPackage, moduleNa
165
172
166
173
func (mr * ModuleResolver ) resolveModuleWithinPackage (pkg ResolvedPackage , moduleParts []string ) (ResolvedModule , error ) {
167
174
if len (moduleParts ) == 0 {
168
- files , err := filepath .Glob (path .Join (pkg .Path , mr .defaultSrcDir , mr .lithiaSourceGlob ))
175
+ files , err := world . Current . FS .Glob (path .Join (pkg .Path , mr .defaultSrcDir , mr .lithiaSourceGlob ))
169
176
if len (files ) > 0 {
170
177
return ResolvedModule {
171
178
packageRef : & pkg ,
172
179
Path : path .Join (pkg .Path , mr .defaultSrcDir ),
173
180
Files : files ,
174
181
}, err
175
182
}
176
- files , err = filepath .Glob (path .Join (pkg .Path , mr .lithiaSourceGlob ))
183
+ files , err = world . Current . FS .Glob (path .Join (pkg .Path , mr .lithiaSourceGlob ))
177
184
return ResolvedModule {
178
185
packageRef : & pkg ,
179
186
Path : pkg .Path ,
@@ -182,7 +189,7 @@ func (mr *ModuleResolver) resolveModuleWithinPackage(pkg ResolvedPackage, module
182
189
}
183
190
pathElems := append ([]string {pkg .Path }, moduleParts ... )
184
191
modulePath := path .Join (pathElems ... )
185
- files , err := filepath .Glob (path .Join (modulePath , mr .lithiaSourceGlob ))
192
+ files , err := world . Current . FS .Glob (path .Join (modulePath , mr .lithiaSourceGlob ))
186
193
return ResolvedModule {
187
194
packageRef : & pkg ,
188
195
relativeName : ast .ModuleName (strings .Join (moduleParts , "." )),
0 commit comments