v/compiler/modules.v

53 lines
1.3 KiB
V
Raw Normal View History

2019-07-21 17:53:35 +02:00
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module main
2019-09-01 21:51:16 +02:00
import os
2019-08-04 00:03:52 +02:00
// add a module and its deps (module speficic dag method)
pub fn(graph mut DepGraph) from_import_tables(import_tables []FileImportTable) {
2019-07-21 17:53:35 +02:00
for fit in import_tables {
mut deps := []string
for _, m in fit.imports {
deps << m
}
graph.add(fit.module_name, deps)
}
}
// get ordered imports (module speficic dag method)
pub fn(graph &DepGraph) imports() []string {
2019-07-21 17:53:35 +02:00
mut mods := []string
for node in graph.nodes {
if node.name == 'main' {
continue
}
mods << node.name
}
return mods
}
2019-09-01 21:51:16 +02:00
// 'strings' => 'VROOT/vlib/strings'
// 'installed_mod' => '~/.vmodules/installed_mod'
// 'local_mod' => '/path/to/current/dir/local_mod'
2019-08-04 00:03:52 +02:00
fn (v &V) find_module_path(mod string) string {
mod_path := v.module_path(mod)
2019-09-01 21:51:16 +02:00
// First check for local modules in the same directory
2019-08-04 00:03:52 +02:00
mut import_path := os.getwd() + '/$mod_path'
2019-09-01 21:51:16 +02:00
// Now search in vlib/
2019-08-04 00:03:52 +02:00
if !os.dir_exists(import_path) {
import_path = '$v.lang_dir/vlib/$mod_path'
2019-09-01 21:51:16 +02:00
}
//println('ip=$import_path')
// Finally try modules installed with vpm (~/.vmodules)
2019-08-04 00:03:52 +02:00
if !os.dir_exists(import_path) {
import_path = '$ModPath/$mod_path'
if !os.dir_exists(import_path){
2019-08-29 02:30:17 +02:00
cerror('module "$mod" not found')
2019-09-01 21:51:16 +02:00
}
2019-08-04 00:03:52 +02:00
}
2019-09-01 21:51:16 +02:00
return import_path
}