2019-07-21 18:53:35 +03: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.
|
|
|
|
|
2019-10-13 16:37:43 +03:00
|
|
|
module compiler
|
2019-07-21 18:53:35 +03:00
|
|
|
|
2019-09-01 22:51:16 +03:00
|
|
|
import os
|
2019-08-04 01:03:52 +03:00
|
|
|
|
2019-10-04 15:48:09 +03:00
|
|
|
const (
|
2019-10-05 07:10:28 +03:00
|
|
|
v_modules_path = os.home_dir() + '.vmodules'
|
2019-10-04 15:48:09 +03:00
|
|
|
)
|
|
|
|
|
2019-09-03 19:11:21 +03:00
|
|
|
// add a module and its deps (module speficic dag method)
|
2019-09-23 13:42:20 +03:00
|
|
|
pub fn(graph mut DepGraph) from_import_tables(import_tables map[string]FileImportTable) {
|
|
|
|
for _, fit in import_tables {
|
2019-07-21 18:53:35 +03:00
|
|
|
mut deps := []string
|
|
|
|
for _, m in fit.imports {
|
|
|
|
deps << m
|
|
|
|
}
|
|
|
|
graph.add(fit.module_name, deps)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:11:21 +03:00
|
|
|
// get ordered imports (module speficic dag method)
|
|
|
|
pub fn(graph &DepGraph) imports() []string {
|
2019-07-21 18:53:35 +03:00
|
|
|
mut mods := []string
|
|
|
|
for node in graph.nodes {
|
|
|
|
mods << node.name
|
|
|
|
}
|
|
|
|
return mods
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:48:09 +03:00
|
|
|
fn (v &V) module_path(mod string) string {
|
|
|
|
// submodule support
|
|
|
|
if mod.contains('.') {
|
2019-10-12 22:31:05 +03:00
|
|
|
return mod.replace('.', os.path_separator)
|
2019-10-05 07:10:28 +03:00
|
|
|
// return mod.replace('.', '/')
|
2019-10-04 15:48:09 +03:00
|
|
|
}
|
|
|
|
return mod
|
|
|
|
}
|
|
|
|
|
2019-09-01 22:51:16 +03:00
|
|
|
// 'strings' => 'VROOT/vlib/strings'
|
|
|
|
// 'installed_mod' => '~/.vmodules/installed_mod'
|
|
|
|
// 'local_mod' => '/path/to/current/dir/local_mod'
|
2019-10-08 02:32:12 +03:00
|
|
|
fn (v &V) find_module_path(mod string) ?string {
|
2019-08-04 01:03:52 +03:00
|
|
|
mod_path := v.module_path(mod)
|
2019-09-01 22:51:16 +03:00
|
|
|
// First check for local modules in the same directory
|
2019-10-12 22:31:05 +03:00
|
|
|
mut import_path := os.getwd() + '${os.path_separator}$mod_path'
|
2019-09-01 22:51:16 +03:00
|
|
|
// Now search in vlib/
|
2019-10-13 14:34:29 +03:00
|
|
|
if mod == 'compiler' || !os.dir_exists(import_path) {
|
2019-10-12 22:31:05 +03:00
|
|
|
import_path = '$v.lang_dir${os.path_separator}vlib${os.path_separator}$mod_path'
|
2019-09-01 22:51:16 +03:00
|
|
|
}
|
|
|
|
//println('ip=$import_path')
|
|
|
|
// Finally try modules installed with vpm (~/.vmodules)
|
2019-08-04 01:03:52 +03:00
|
|
|
if !os.dir_exists(import_path) {
|
2019-10-12 22:31:05 +03:00
|
|
|
import_path = '$v_modules_path${os.path_separator}$mod_path'
|
2019-08-04 01:03:52 +03:00
|
|
|
if !os.dir_exists(import_path){
|
2019-10-08 02:32:12 +03:00
|
|
|
return error('module "$mod" not found')
|
2019-09-01 22:51:16 +03:00
|
|
|
}
|
2019-08-04 01:03:52 +03:00
|
|
|
}
|
2019-09-01 22:51:16 +03:00
|
|
|
return import_path
|
|
|
|
}
|
2019-10-12 01:17:37 +03:00
|
|
|
|
2019-10-13 03:05:11 +03:00
|
|
|
[inline] fn mod_gen_name(mod string) string {
|
2019-10-12 01:17:37 +03:00
|
|
|
return mod.replace('.', '_dot_')
|
|
|
|
}
|
|
|
|
|
2019-10-13 03:05:11 +03:00
|
|
|
[inline] fn mod_gen_name_rev(mod string) string {
|
2019-10-12 01:17:37 +03:00
|
|
|
return mod.replace('_dot_', '.')
|
2019-10-12 22:31:05 +03:00
|
|
|
}
|