feat: rework how modules are loaded

This commit is contained in:
rvcas 2022-11-10 10:27:03 -05:00 committed by Lucas
parent 7b5763edeb
commit c9da049712
9 changed files with 22 additions and 28 deletions

View File

@ -104,26 +104,15 @@ impl Project {
} }
fn read_source_files(&mut self) -> Result<(), Error> { fn read_source_files(&mut self) -> Result<(), Error> {
let lib = self.root.join(format!("src/{}", self.config.name)); let lib = self.root.join("lib");
let scripts = self.root.join("src/scripts"); let scripts = self.root.join("validators");
self.read_root_lib_file()?;
self.aiken_files(&scripts, ModuleKind::Script)?; self.aiken_files(&scripts, ModuleKind::Script)?;
self.aiken_files(&lib, ModuleKind::Lib)?; self.aiken_files(&lib, ModuleKind::Lib)?;
Ok(()) Ok(())
} }
fn read_root_lib_file(&mut self) -> Result<(), Error> {
let root_lib_path = self.root.join(format!("src/{}.ak", self.config.name));
if root_lib_path.exists() {
self.add_module(root_lib_path, &self.root.join("src"), ModuleKind::Lib)?;
}
Ok(())
}
fn parse_sources(&mut self) -> Result<ParsedModules, Error> { fn parse_sources(&mut self) -> Result<ParsedModules, Error> {
let mut errors = Vec::new(); let mut errors = Vec::new();
let mut parsed_modules = HashMap::with_capacity(self.sources.len()); let mut parsed_modules = HashMap::with_capacity(self.sources.len());
@ -458,7 +447,7 @@ impl Project {
} }
fn add_module(&mut self, path: PathBuf, dir: &Path, kind: ModuleKind) -> Result<(), Error> { fn add_module(&mut self, path: PathBuf, dir: &Path, kind: ModuleKind) -> Result<(), Error> {
let name = self.module_name(dir, &path, kind); let name = self.module_name(dir, &path);
let code = fs::read_to_string(&path).map_err(|error| Error::FileIo { let code = fs::read_to_string(&path).map_err(|error| Error::FileIo {
path: path.clone(), path: path.clone(),
error, error,
@ -474,12 +463,7 @@ impl Project {
Ok(()) Ok(())
} }
fn module_name( fn module_name(&self, package_path: &Path, full_module_path: &Path) -> String {
&self,
package_path: &Path,
full_module_path: &Path,
kind: ModuleKind,
) -> String {
// ../../{config.name}/module.ak // ../../{config.name}/module.ak
// module.ak // module.ak
@ -498,14 +482,7 @@ impl Project {
.to_string(); .to_string();
// normalise windows paths // normalise windows paths
let name = name.replace('\\', "/"); name.replace('\\', "/")
// project_name/module
if kind.is_lib() && name != self.config.name {
format!("{}/{}", self.config.name, name)
} else {
name
}
} }
} }

View File

@ -0,0 +1,6 @@
name = "aiken_std"
version = "0.1.0"
licences = ["Apache-2.0"]
description = "Aiken contracts"
[dependencies]

View File

@ -0,0 +1,8 @@
pub type ScriptContext(purpose) {
tx_info: TxInfo,
script_purpose: purpose,
}
pub type TxInfo {
idk: Int,
}

View File

@ -0,0 +1,3 @@
pub fn spend() -> Bool {
True
}