Skip to content

Commit

Permalink
Merge pull request #477 from nevalang/third_party_path
Browse files Browse the repository at this point in the history
Third party path
  • Loading branch information
emil14 authored Feb 28, 2024
2 parents 980de53 + f8ad32b commit ba0982d
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 79 deletions.
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
"version": "0.2.0",
"configurations": [
// CLI
// get
{
"name": "neva get github.com/nevalang/x",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/cli",
"cwd": "${workspaceFolder}/examples",
"args": ["get", "github.com/nevalang/x"]
},
// run
{
"name": "Neva CLI 00",
"type": "go",
Expand Down
35 changes: 28 additions & 7 deletions cmd/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
)

func newCliApp( //nolint:funlen
wd string,
b builder.Builder,
workdir string,
bldr builder.Builder,
goc compiler.Compiler,
nativec compiler.Compiler,
wasmc compiler.Compiler,
Expand All @@ -39,6 +39,27 @@ func newCliApp( //nolint:funlen
return nil
},
},
{
Name: "get",
Usage: "Add dependency to current module",
Args: true,
ArgsUsage: "Provide path to the module",
Action: func(cCtx *cli.Context) error {
installedPath, err := bldr.Get(
workdir,
cCtx.Args().Get(0),
cCtx.Args().Get(1),
)
if err != nil {
return err
}
fmt.Printf(
"%s installed to %s\n", cCtx.Args().Get(0),
installedPath,
)
return nil
},
},
{
Name: "run",
Usage: "Run neva program from source code in interpreter mode",
Expand All @@ -56,10 +77,10 @@ func newCliApp( //nolint:funlen
if err != nil {
return err
}
intr := interpreter.New(b, goc, debug)
intr := interpreter.New(bldr, goc, debug)
if err := intr.Interpret(
context.Background(),
wd,
workdir,
dirFromArg,
); err != nil {
return err
Expand Down Expand Up @@ -96,15 +117,15 @@ func newCliApp( //nolint:funlen
switch target {
case "go":
return goc.Compile(
wd, dirFromArg, wd,
workdir, dirFromArg, workdir,
)
case "wasm":
return wasmc.Compile(
wd, dirFromArg, wd,
workdir, dirFromArg, workdir,
)
default:
return nativec.Compile(
wd, dirFromArg, wd,
workdir, dirFromArg, workdir,
)
}
},
Expand Down
5 changes: 1 addition & 4 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ func main() { //nolint:funlen
prsr := parser.New(false)

// pkg manager
pkgMngr := builder.New(
"/Users/emil/projects/neva/thirdparty",
prsr,
)
pkgMngr := builder.MustNew(prsr)

// compiler frontend
desugarer := desugarer.Desugarer{}
Expand Down
5 changes: 1 addition & 4 deletions cmd/lsp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ func main() {
terminator := typesystem.Terminator{}
checker := typesystem.MustNewSubtypeChecker(terminator)
resolver := typesystem.MustNewResolver(typesystem.Validator{}, checker, terminator)
builder := builder.New(
"/Users/emil/projects/neva/third_party/",
p,
)
builder := builder.MustNew(p)

indexer := indexer.New(
builder,
Expand Down
17 changes: 0 additions & 17 deletions examples/3_interfaces/logger.neva.tmp

This file was deleted.

13 changes: 7 additions & 6 deletions examples/3_interfaces/main.neva
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
interface IPrinter<T>(data T) (sig T)
import {
nevalang/main/x
}

// TODO create issue/discussion about DI sugar
// if component only have one DI dep then allow
// omitting of the dep node name. So parent component
// know nothing about it's sub-component internals
interface IPrinter<T>(data T) (sig T)

component {
Main(start any) (stop any) {
nodes {
subNode SecondComponent { depNode Printer<any> }
subNode SecondComponent {
depNode x.Printer<any>
}
}
net {
:start -> subNode:msg
Expand Down
10 changes: 5 additions & 5 deletions examples/neva.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This directory contains working examples from Nevalang tutorial.
# They not only serve documentation purposes, they are also
# e2e tests that you can manually run to ensure that Nevalang works.

neva: 0.6.0
neva: 0.6.0
deps:
nevalang/x:
path: github.com/nevalang/x
version: 0.0.6
5 changes: 5 additions & 0 deletions examplesneva.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
neva: 0.6.0
deps:
github.com/nevalang/x:
path: github.com/nevalang/x
version: ""
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
Expand Down Expand Up @@ -68,6 +67,8 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A=
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
Expand Down Expand Up @@ -166,6 +167,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
36 changes: 33 additions & 3 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/nevalang/neva/internal/compiler"
"github.com/nevalang/neva/pkg"
Expand Down Expand Up @@ -38,7 +39,7 @@ func (p Builder) Build( //nolint:funlen
Version: pkg.Version,
}

// TODO use embedded fs for stdlib
// load stdlib module from embedded fs
stdMod, err := p.LoadModuleByPath(ctx, std.FS)
if err != nil {
return compiler.RawBuild{}, &compiler.Error{
Expand All @@ -60,7 +61,7 @@ func (p Builder) Build( //nolint:funlen
continue
}

depPath, err := p.downloadDep(depModRef)
depPath, _, err := p.downloadDep(depModRef)
if err != nil {
return compiler.RawBuild{}, &compiler.Error{
Err: fmt.Errorf("download dep: %w", err),
Expand Down Expand Up @@ -91,9 +92,38 @@ func (p Builder) Build( //nolint:funlen
}, nil
}

func New(thirdParty string, parser ManifestParser) Builder {
func MustNew(parser ManifestParser) Builder {
b, err := New(parser)
if err != nil {
panic(err)
}
return b
}

func New(parser ManifestParser) (Builder, error) {
thirdParty, err := getThirdPartyPath()
if err != nil {
return Builder{}, err
}

return Builder{
thirdPartyPath: thirdParty,
manifestParser: parser,
}, nil
}

func getThirdPartyPath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}

path := filepath.Join(home, "neva", "deps")

err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return "", err
}

return path, nil
}
44 changes: 44 additions & 0 deletions internal/builder/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package builder

import (
"errors"
"fmt"
"os"

src "github.com/nevalang/neva/pkg/sourcecode"
)

func (b Builder) Get(workdir, path, version string) (string, error) {
ref := src.ModuleRef{
Path: path,
Version: version,
}

downloadPath, actualVersion, err := b.downloadDep(ref)
if err != nil {
return "", err
}

manifest, err := b.retrieveManifest(os.DirFS(workdir))
if err != nil {
return "", fmt.Errorf("Retrieve manifest: %w", err)
}

existing, ok := manifest.Deps[path]
if ok && existing.Version != actualVersion {
return "", errors.New(
"Several versions of the same dependency not yet supported.",
)
}

manifest.Deps[path] = src.ModuleRef{
Path: path,
Version: actualVersion,
}

if err := b.writeManifest(manifest, workdir); err != nil {
return "", err
}

return downloadPath, nil
}
Loading

0 comments on commit ba0982d

Please sign in to comment.