Initial commit of pluto-lang plp fork

master
sloum 1 year ago
parent dcb006eb2f
commit 3c8886105f
  1. 19
      README.md
  2. 8
      go.mod
  3. 82
      go.sum
  4. 39
      main.go
  5. 36
      operators/generate.go
  6. 20
      operators/install.go
  7. 23
      operators/list.go
  8. 9
      operators/remove.go
  9. 5
      operators/update.go
  10. 77
      operators/util.go

@ -1,22 +1,21 @@
# plp
# slp
[Pl](#)uto [P](#)ackage Manager.
SLope Package manager.
An easy way to install, remove, and update Pluto packages.
An easy way to install, remove, and update slope packages.
```
plp +package # installs a package
plp -package # removes a package
plp ^package # updates a package
slp +package # installs a package
slp -package # removes a package
slp ^package # updates a package
```
Operations can be chained (e.g. `plp +foo -bar`.)
Operations can be chained (e.g. `slp +foo -bar`.)
You can list all packages available for installation:
```
plp list
slp list
```
Packages can be registered in the repository `pluto-language/packages` -
just submit a PR.
Packages can be registered in the repository at [https://git.rawtext.club/slope-lang/packages](slope-lang/packages) by submitting a PR.

@ -0,0 +1,8 @@
module git.rawtext.club/slope-lang/slp
go 1.16
require (
github.com/fatih/color v1.12.0
gopkg.in/src-d/go-git.v4 v4.13.1
)

@ -0,0 +1,82 @@
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc=
github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY=
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4=
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smtodRU+gha3+BeqJ69lRk=
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190729092621-ff9f1409240a/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/src-d/go-billy.v4 v4.3.2 h1:0SQA1pRztfTFx2miS8sA97XvooFeNOmvUenF4o0EcVg=
gopkg.in/src-d/go-billy.v4 v4.3.2/go.mod h1:nDjArDMp+XMs1aFAESLRjfGSgfvoYN0hDfzEk0GjC98=
gopkg.in/src-d/go-git-fixtures.v3 v3.5.0 h1:ivZFOIltbce2Mo8IjzUHAFoq/IylO9WHhNOAJK+LsJg=
gopkg.in/src-d/go-git-fixtures.v3 v3.5.0/go.mod h1:dLBcvytrw/TYZsNTWCnkNF2DSIlzWYqTe3rJR56Ac7g=
gopkg.in/src-d/go-git.v4 v4.13.1 h1:SRtFyV8Kxc0UP7aCHcijOMQGPxHSmMOPrzulQWolkYE=
gopkg.in/src-d/go-git.v4 v4.13.1/go.mod h1:nx5NYcxdKxq5fpltdHnPa2Exj4Sx0EclMWZQbYDu2z8=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=

@ -5,23 +5,21 @@ import (
"os"
"strings"
"github.com/fatih/color"
"github.com/Zac-Garby/plp/args"
"github.com/Zac-Garby/plp/operators"
"git.rawtext.club/slope-lang/slp/args"
"git.rawtext.club/slope-lang/slp/operators"
)
func main() {
arg := os.Args
if len(arg) <= 1 {
fmt.Println(` < plp - Pluto Package Manager >
fmt.Println(` < slp - slope package manager >
plp +<package> installs a package
plp -<package> removes a package
plp ^<package> updates a package
slp +<package> installs a package
slp -<package> removes a package
slp ^<package> updates a package
plp list lists all available packages
plp gen creates a new package
slp list lists all available packages
slp gen creates a new package
`)
return
@ -64,7 +62,7 @@ func main() {
for _, op := range ops {
var err error
fmt.Printf(" - ")
fmt.Print(" - ")
switch op.Type {
case args.INSTALL:
@ -81,8 +79,7 @@ func main() {
updates = append(updates, op.Package)
}
fmt.Print(" ")
color.Green("done")
fmt.Print(" \033[32mdone\033[0m")
if err != nil {
fmt.Println(err)
@ -92,22 +89,16 @@ func main() {
fmt.Println()
var (
green = color.New(color.FgGreen, color.Bold)
cyan = color.New(color.FgCyan, color.Bold)
red = color.New(color.FgRed, color.Bold)
)
printStat(green, "INSTALLED", installs)
printStat(red, "REMOVED", removes)
printStat(cyan, "UPDATED", updates)
printStat("\033[1;32m", "INSTALLED", installs)
printStat("\033[1;31m", "REMOVED", removes)
printStat("\033[1;36m", "UPDATED", updates)
}
func printStat(colour *color.Color, prefix string, pkgs []string) {
func printStat(color, prefix string, pkgs []string) {
count := len(pkgs)
if count > 0 {
colour.Printf("%10s ", prefix)
fmt.Printf("%s%10s\033[0m", color, prefix)
if count > 1 {
fmt.Printf("%d packages", count)

@ -15,7 +15,6 @@ import (
type genOpts struct {
title string
display string
description string
author string
version string
@ -29,7 +28,7 @@ func Generate() error {
return err
}
pwd, err := filepath.Abs(".")
pwd, err := os.Getwd()
if err != nil {
return err
}
@ -54,14 +53,13 @@ func Generate() error {
return err
}
color.Cyan("\nPackage '%s' has been created!\n", opts.title)
fmt.Printf("\nPackage %q has been created!\n", opts.title)
return nil
}
func promptOptions() (*genOpts, error) {
reader := bufio.NewReader(os.Stdin)
colour := color.New(color.Bold)
// A title is entirely composed of lowercase letters, digits, dashes, and underscores
title, err := promptOption(
@ -69,30 +67,17 @@ func promptOptions() (*genOpts, error) {
`^[a-z\d-_]+$`,
"the title can only contain lowercase letters, digits, dashes, and underscores",
reader,
colour,
)
if err != nil {
return nil, err
}
display, err := promptOption(
"display name",
`^.+$`,
"the display name must be at least 1 character",
reader,
colour,
)
if err != nil {
return nil, err
}
description, err := promptOption(
"description",
`^.+$`,
"the description must be at least 1 character",
reader,
colour,
)
if err != nil {
return nil, err
@ -103,7 +88,6 @@ func promptOptions() (*genOpts, error) {
`^.+$`,
"the author's name must be at least 1 character",
reader,
colour,
)
if err != nil {
return nil, err
@ -111,10 +95,9 @@ func promptOptions() (*genOpts, error) {
version, err := promptOption(
"version",
`^\d+\.\d+\.\d+$`,
"the version must be three integers separated by dots",
`.*`,
"",
reader,
colour,
)
if err != nil {
return nil, err
@ -122,21 +105,20 @@ func promptOptions() (*genOpts, error) {
return &genOpts{
title: title,
display: display,
description: description,
author: author,
version: version,
}, nil
}
func promptOption(msg, pattern, errMsg string, reader *bufio.Reader, colour *color.Color) (string, error) {
func promptOption(msg, pattern, errMsg string, reader *bufio.Reader) (string, error) {
var (
text string
err error
)
for {
colour.Printf("%15s: ", msg)
fmt.Printf("\033[1m%15s:\033[0m ", msg)
text, err = reader.ReadString('\n')
if err != nil {
@ -151,13 +133,12 @@ func promptOption(msg, pattern, errMsg string, reader *bufio.Reader, colour *col
}
if matches {
goto done
break
}
color.Red(" invalid input: %s", errMsg)
fmt.Printf("\033[31m invalid input:\033[0m %s\n", errMsg)
}
done:
return text, nil
}
@ -216,7 +197,6 @@ func applyOptionsToTemplate(dir string, opts *genOpts) error {
func applyOptions(str string, opts *genOpts) string {
replacements := map[string]string{
"%title": opts.title,
"%display": opts.display,
"%description": opts.description,
"%author": opts.author,
"%version": opts.version,

@ -1,17 +1,14 @@
package operators
import (
"errors"
"fmt"
"path/filepath"
"gopkg.in/src-d/go-git.v4"
)
// Install downloads and installs a
// package named 'pkg'
func Install(pkg string) error {
downloadDirectory()
downloadTheDirectory()
packages, err := getPackages()
if err != nil {
@ -20,22 +17,29 @@ func Install(pkg string) error {
repo, ok := packages[pkg]
if !ok {
msg := fmt.Sprintf("package '%s' not found in $PLUTO/libraries/packages.json", pkg)
return errors.New(msg)
return fmt.Errorf("package %q not found in package list", pkg)
}
root, err := getRoot()
modDir, err := getModBaseDir()
if err != nil {
return err
}
_, err = git.PlainClone(filepath.Join(root, "libraries", pkg), false, &git.CloneOptions{
_, err = git.PlainClone(filepath.Join(modDir, pkg), false, &git.CloneOptions{
URL: repo,
})
if err != nil {
return err
}
_, err := os.Stat(filepath.Join(modDir, pkg, "main.slo"))
if err != nil && os.IsNotExist(err) {
err = Remove(pkg)
if err != nil {
return fmt.Errorf("package %q was not a valid slope package, an attempt to remove the files failed: %s", pkg, repo)
}
return fmt.Errorf("package %q does not contain a valid 'main.slo' file, the failed package has been removed", pkg)
}
return nil
}

@ -4,8 +4,6 @@ import (
"fmt"
"sort"
"strings"
"github.com/fatih/color"
)
type pkg struct {
@ -14,14 +12,23 @@ type pkg struct {
type pkgs []pkg
func (p pkgs) Len() int { return len(p) }
func (p pkgs) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p pkgs) Less(i, j int) bool { return p[i].title < p[j].title }
func (p pkgs) Len() int {
return len(p)
}
func (p pkgs) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p pkgs) Less(i, j int) bool {
return p[i].title < p[j].title
}
// List lists all available packages
// from packages.json
func List() error {
downloadDirectory()
downloadTheDirectory()
packages, err := getPackages()
if err != nil {
@ -33,8 +40,7 @@ func List() error {
headers := fmt.Sprintf("\n%-20s %-"+fmt.Sprintf("%d", longest)+"s\n", "TITLE", "REPO")
headers = strings.Repeat(" ", len(headers)-2) + headers
headerColour := color.New(color.FgCyan, color.Underline, color.Bold)
headerColour.Printf(headers)
fmt.Printf("\033[1;36;4m%s\033[0m", headers)
sorted := sortPackages(packages)
@ -76,3 +82,4 @@ func sortPackages(packages map[string]string) pkgs {
sort.Sort(pks)
return pks
}

@ -1,7 +1,6 @@
package operators
import (
"errors"
"fmt"
"os"
"path/filepath"
@ -9,16 +8,16 @@ import (
// Remove removes an installed package
func Remove(pkg string) error {
root, err := getRoot()
modDir, err := getModBaseDir()
if err != nil {
return err
}
path := filepath.Join(root, "libraries", pkg)
path := filepath.Join(modDir, pkg)
if _, err := os.Stat(path); err != nil {
msg := fmt.Sprintf("package '%s' not found at '%s'", pkg, path)
return errors.New(msg)
msg := fmt.Sprintf()
return fmt.Errorf("package '%s' not found", pkg)
}
err = os.RemoveAll(path)

@ -1,5 +1,10 @@
package operators
// TODO make this take an optional commit hash
// and/or branch name. Instead of reinstalling,
// is pulling better? It leaves open a repo url
// change as an issue...
// Update removes then installs a
// package, to ensure it is up to date
func Update(pkg string) error {

@ -2,7 +2,6 @@ package operators
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
@ -12,41 +11,73 @@ import (
"path/filepath"
)
func getRoot() (string, error) {
var root string
const (
packageListURL string = "https://git.rawtext.club/slope-lang/packages/raw/branch/master/packages.json"
)
if r, exists := os.LookupEnv("PLUTO"); exists {
root = r
} else {
usr, err := user.Current()
if err != nil {
return "", err
func ExpandedAbsFilepath(p string) string {
if strings.HasPrefix(p, "~") {
if p == "~" || strings.HasPrefix(p, "~/") {
homedir, _ := os.UserHomeDir()
if len(p) <= 2 {
p = homedir
} else if len(p) > 2 {
p = filepath.Join(homedir, p[2:])
}
} else {
i := strings.IndexRune(p, '/')
var u string
var remainder string
if i < 0 {
u = p[1:]
remainder = ""
} else {
u = p[1:i]
remainder = p[i:]
}
usr, err := user.Lookup(u)
if err != nil {
p = filepath.Join("/home", u, remainder)
} else {
p = filepath.Join(usr.HomeDir, remainder)
}
}
root = filepath.Join(usr.HomeDir, "pluto")
} else if !strings.HasPrefix(p, "/") {
wd, _ := os.Getwd()
p = filepath.Join(wd, p)
}
return root, nil
path, _ := filepath.Abs(p)
return path
}
func downloadDirectory() error {
var (
url = "https://raw.githubusercontent.com/pluto-language/packages/master/packages.json"
root, err = getRoot()
)
func getModBaseDir() string {
p := os.Getenv("SLOPE_MOD_PATH")
if p == "" {
x := os.Getenv("XDG_DATA_HOME")
if x == "" {
return ExpandedAbsFilepath("~/.local/share/slope/modules/")
}
return filepath.Join(x, "slope", "modules")
}
return p
}
func downloadTheDirectory() error {
modDir, err := getModBaseDir()
if err != nil {
return err
}
out, err := os.Create(filepath.Join(root, "libraries", "packages.json"))
out, err := os.Create(filepath.Join(modDir, "packages.json"))
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get(url)
resp, err := http.Get(packageListURL)
if err != nil {
return err
}
@ -62,12 +93,12 @@ func downloadDirectory() error {
}
func getPackages() (map[string]string, error) {
root, err := getRoot()
modDir, err := getModBaseDir()
if err != nil {
return nil, err
}
data, err := ioutil.ReadFile(filepath.Join(root, "libraries", "packages.json"))
data, err := ioutil.ReadFile(filepath.Join(modDir, "packages.json"))
if err != nil {
return nil, err
}
@ -85,8 +116,7 @@ func getPackages() (map[string]string, error) {
repo, ok := v.(string)
if !ok {
msg := fmt.Sprintf("packages.json: invalid repository for %s. submit an issue to get it fixed", pkg)
return nil, errors.New(msg)
return nil, fmt.Errorf("packages.json: invalid repository for %s. submit an issue to get it fixed", pkg)
}
packages[pkg] = repo
@ -94,3 +124,4 @@ func getPackages() (map[string]string, error) {
return packages, nil
}

Loading…
Cancel
Save