Skip to content

[Feature] Package loader for amber #668

@b1ek

Description

@b1ek

we would obviously need a package manager at some point.

i'd like to propose a system for loading packages on compiler level

with these package directories (i will refer to this as $PKGDIR from now on):

  1. ./amber_modules
  2. ~/.local/amber_modules
  3. /usr/lib/amber_modules
  4. if AMBER_PATH is set, special logic applies

when importing a path that starts with a letter, compiler should import it from $PKGDIR/package_name/main.ab, with this logic (pseudocode):

// pseudo code
const package_directories = [
    "./amber_modules",
    "~/.local/amber_modules",
    "/usr/bin/amber_modules"
];

if (AMBER_PATH.is_set()) {
    const paths = AMBER_PATH.split(':');
    for (path of paths) {
        path = path.join('amber_modules');
        if (path.is_dir()) {
            # append from the top
            package_directories.push_front(path)
        }
    }
}

function resolve_import(path: string): string? {
    if !path.startsWithLetter() {
        return resolve_local_import(path)
    }
    if path.split("/").length == 1 {
        path += "/main.ab";
    }
    if !path.endsWith(".ab") {
        path += ".ab";
    }

    for (pkgdir of package_directories) {
        if file_exists(path.join(pkgdir)) {
            return path.join(pkgdir);
        }
        if exists(path.join(pkgdir) + "/main.ab") {
            return path.join(pkgdir) + "/main.ab";
        }
    }
    
    return null;
}

resolve_import("package_name"); // import "package_name" -> "$PKGDIR/main.ab"
resolve_import("package_name/subpkg"); // import "package_name" -> "$PKGDIR/subpkg.ab" | "$PKGDIR/subpkg/main.ab"
resolve_import("package_name/subpkg.ab"); // import "package_name" -> "$PKGDIR/subpkg.ab" | "$PKGDIR/subpkg/main.ab"

for importing files in the working directory, users should use import "./local_file"

implementing this will allow for creating a package manager for amber, that would be maintained independently from compiler

this will also mean treating stdlib as a package, rather than as a hardcoded language feature

AMBER_PATH logic

AMBER_PATH is an environment variable that behaves like PATH: it contains paths separated by :. for each path, amber must check if path/amber_modules exists and if it does, add it to potential package directories with high priority.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions