Skip to content

Commit

Permalink
support archived package
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Nov 1, 2023
1 parent 3b7b9c0 commit 0e67d8b
Show file tree
Hide file tree
Showing 10 changed files with 329 additions and 433 deletions.
4 changes: 1 addition & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,9 @@ func (n *Nami) PrintConfigs() {
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetHeader([]string{"Key", "Value", "Description"})
table.Append([]string{"nami.root", "~/.nami", "Nami root dir, can't be modified"})
table.Append([]string{"nami.db", "~/.nami/db", "Nami db file, can't be modified"})
table.Append([]string{"nami.bin", "~/.nami/bin", "Nami bin dir, can't be modified"})
table.Append([]string{"nami.cache", "~/.nami/cache", "Nami cache dir, can't be modified"})
table.Append([]string{"nami.tmp", "~/.nami/tmp", "Nami tmp dir, can't be modified"})
table.Append([]string{"nami.dir", "~/.nami/dir", "Nami dir dir, can't be modified"})
s, err := n.GetConfig("github.token")
if err != nil {
log.Println(err)
Expand Down
29 changes: 14 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
module github.com/txthinking/nami

go 1.19
go 1.21

require (
github.com/bitly/go-simplejson v0.5.0
github.com/d5/tengo/v2 v2.10.1
github.com/bitly/go-simplejson v0.5.1
github.com/d5/tengo/v2 v2.16.1
github.com/google/go-github v17.0.0+incompatible
github.com/olekukonko/tablewriter v0.0.5
github.com/urfave/cli/v2 v2.3.0
go.etcd.io/bbolt v1.3.5
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
github.com/urfave/cli/v2 v2.25.7
go.etcd.io/bbolt v1.3.8
golang.org/x/oauth2 v0.13.0
)

require (
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.25.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
413 changes: 38 additions & 375 deletions go.sum

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import (
"github.com/urfave/cli/v2"
)

var version = "20231101"

func main() {
app := cli.NewApp()
app.Name = "nami"
app.Version = "20230606"
app.Usage = "The easy way to download anything from anywhere"
app.Version = version
app.Usage = "A clean and tidy package manager."
app.Authors = []*cli.Author{
{
Name: "Cloud",
Expand Down Expand Up @@ -56,7 +58,7 @@ func main() {
log.Println("Check nami version", err)
return nil
}
if s > "v20230606" {
if s > ("v" + version) {
return errors.New("New version: " + s + ", please upgrade nami first: $ nami install nami")
}
return nil
Expand Down
57 changes: 54 additions & 3 deletions nami.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

type Nami struct {
HomeDir string
DenoDir string
DirDir string
CacheDir string
BinDir string
DB *bbolt.DB
Expand All @@ -46,6 +46,7 @@ type Package struct {
Name string
Version string
Files map[string]string
Dirs []string
}

func NewNami() (*Nami, error) {
Expand All @@ -57,17 +58,21 @@ func NewNami() (*Nami, error) {
if err := os.MkdirAll(bin, 0777); err != nil {
return nil, err
}
dir := filepath.Join(s, ".nami", "dir")
if err := os.MkdirAll(dir, 0777); err != nil {
return nil, err
}
db, err := bbolt.Open(filepath.Join(s, ".nami", "db"), 0644, nil)
if err != nil {
return nil, err
}
n := &Nami{
HomeDir: s,
BinDir: bin,
DirDir: dir,
CacheDir: filepath.Join(s, ".nami", "cache"),
DenoDir: filepath.Join(s, ".nami", "deno"),
CopiedDir: filepath.Join(s, ".nami", "copied"),
TmpDir: filepath.Join(s, ".nami", "tmp"),
BinDir: bin,
DB: db,
}
n.Module()
Expand Down Expand Up @@ -128,11 +133,13 @@ func (n *Nami) Install(name, script, js string) (func(), error) {
p := &Package{
Name: name,
Files: make(map[string]string),
Dirs: make([]string, 0),
}
files, err := os.ReadDir(n.CacheDir)
if err != nil {
return nil, err
}
links := make([]string, 0)
for _, file := range files {
if file.Name() == "version" {
b, err := os.ReadFile(filepath.Join(n.CacheDir, file.Name()))
Expand All @@ -142,13 +149,32 @@ func (n *Nami) Install(name, script, js string) (func(), error) {
p.Version = string(b)
continue
}
if file.Name() == "links" {
b, err := os.ReadFile(filepath.Join(n.CacheDir, file.Name()))
if err != nil {
return nil, err
}
links = strings.Split(strings.TrimSpace(string(b)), "\n")
continue
}
if file.IsDir() {
p.Dirs = append(p.Dirs, file.Name())
if err := os.RemoveAll(filepath.Join(n.DirDir, file.Name())); err != nil {
return nil, err
}
if err := os.Rename(filepath.Join(n.CacheDir, file.Name()), filepath.Join(n.DirDir, file.Name())); err != nil {
return nil, err
}
continue
}
p.Files[file.Name()] = ""
if file.Name() == "nami" || file.Name() == "nami.exe" {
if err := os.Chmod(filepath.Join(n.CacheDir, file.Name()), 0755); err != nil {
return nil, err
}
continue
}

l, err := os.ReadDir(n.CopiedDir)
if err != nil {
return nil, err
Expand All @@ -162,6 +188,7 @@ func (n *Nami) Install(name, script, js string) (func(), error) {
if got {
continue
}

r, err := os.Open(filepath.Join(n.CacheDir, file.Name()))
if err != nil {
return nil, err
Expand All @@ -176,6 +203,18 @@ func (n *Nami) Install(name, script, js string) (func(), error) {
return nil, err
}
}
for _, v := range links {
p.Files[filepath.Base(v)] = ""
if err := os.Remove(filepath.Join(n.BinDir, filepath.Base(v))); err != nil {
if !strings.Contains(err.Error(), "no such file") {
return nil, err
}
}
if err := os.Symlink(filepath.Join(n.DirDir, v), filepath.Join(n.BinDir, filepath.Base(v))); err != nil {
return nil, err
}
}

err = n.DB.Update(func(tx *bbolt.Tx) error {
b, err := json.Marshal(p)
if err != nil {
Expand Down Expand Up @@ -247,6 +286,11 @@ func (n *Nami) Remove(name string) error {
return err
}
}
for _, v := range p.Dirs {
if err := os.RemoveAll(filepath.Join(n.DirDir, v)); err != nil {
return err
}
}
err = n.DB.Update(func(tx *bbolt.Tx) error {
t, err := tx.CreateBucketIfNotExists([]byte("package"))
if err != nil {
Expand Down Expand Up @@ -294,6 +338,7 @@ func (n *Nami) Print(name string, remote bool) {
if p == nil {
p = &Package{
Files: make(map[string]string),
Dirs: make([]string, 0),
}
}
table := tablewriter.NewWriter(os.Stdout)
Expand All @@ -304,6 +349,9 @@ func (n *Nami) Print(name string, remote bool) {
for k, _ := range p.Files {
l = append(l, k)
}
for _, v := range p.Dirs {
l = append(l, v+"/")
}
table.Append([]string{"Installed Files", strings.Join(l, ", ")})
table.Render()
}
Expand All @@ -325,6 +373,9 @@ func (n *Nami) PrintAll() {
for k, _ := range p.Files {
l = append(l, k)
}
for _, v := range p.Dirs {
l = append(l, v+"/")
}
table.Append([]string{string(k), p.Version, strings.Join(l, ", ")})
return nil
})
Expand Down
54 changes: 54 additions & 0 deletions package/go.tengo
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
os := import("os")
fmt := import("fmt")
text := import("text")
nami := import("nami")
json := import("json")

b := nami.sh1("curl", "-L", "https://go.dev/dl/?mode=json")
if is_error(b) {
fmt.println(b)
os.exit(1)
}
v := json.decode(b)[0].version[2:]
err := nami.write_file(nami.cache_dir+"/version", v)
if is_error(err) {
fmt.println(err)
os.exit(1)
}

if nami.os != "linux" && nami.os != "darwin" && nami.os != "windows" {
fmt.println("This package does not support your OS or Arch now. PR welcome: https://github.com/txthinking/nami/blob/master/package")
os.exit(1)
}

if nami.os != "windows" {
err := nami.sh("curl", "-L", "--progress-bar", "https://go.dev/dl/go"+v+"."+nami.os+"-"+nami.arch+".tar.gz", "-o", "/tmp/_.tgz")
if is_error(err) {
fmt.println(err)
os.exit(1)
}
nami.sh("rm", "-rf", "/tmp/_")
nami.sh("mkdir", "/tmp/_")
nami.sh("tar", "zxvf", "/tmp/_.tgz", "-C", "/tmp/_")
nami.sh("mv", "/tmp/_/go", nami.cache_dir+"/go")
nami.write_file(nami.cache_dir+"/links", "go/bin/go\n")
nami.append_file(nami.cache_dir+"/links", "go/bin/gofmt\n")
}

if nami.os == "windows" {
if err := nami.sh("which", "7z"); is_error(err){
fmt.println("Please install 7z first: $ nami install 7z")
os.exit(1)
}
err := nami.sh("curl", "-L", "--progress-bar", "https://go.dev/dl/go"+v+"."+nami.os+"-"+nami.arch+".zip", "-o", "/tmp/_.zip")
if is_error(err) {
fmt.println(err)
os.exit(1)
}
nami.sh("rm", "-rf", "/tmp/_")
nami.sh("mkdir", "/tmp/_")
nami.sh("7z", "x", "/tmp/_.zip", "-o/tmp/_")
nami.sh("mv", "/tmp/_/go", nami.cache_dir+"/go")
nami.write_file(nami.cache_dir+"/links", "go/bin/go.exe\n")
nami.append_file(nami.cache_dir+"/links", "go/bin/gofmt.exe\n")
}
6 changes: 3 additions & 3 deletions package/jb.tengo
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ if is_error(err) {

got := false
if nami.os == "linux" && nami.arch == "amd64" {
old := ""
s := nami.sh1("sh", "-c", "cat /proc/cpuinfo | grep avx2")
if is_error(s) {
old = "-baseline"
fmt.println("Your CPU is too obsolete, so we have abandoned support for antiquities. But you can compile jb from source https://github.com/txthinking/jb")
os.exit(1)
}
got = true
err := nami.sh(`curl`, `-L`, `--progress-bar`, `https://github.com/txthinking/jb/releases/latest/download/jb_linux_amd64`+old, `-o`, text.join([nami.cache_dir, "jb"], os.path_separator))
err := nami.sh(`curl`, `-L`, `--progress-bar`, `https://github.com/txthinking/jb/releases/latest/download/jb_linux_amd64`, `-o`, text.join([nami.cache_dir, "jb"], os.path_separator))
if is_error(err) {
fmt.println(err)
os.exit(1)
Expand Down
51 changes: 51 additions & 0 deletions package/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// check os
if (!(nami.os == "darwin" && nami.arch == "arm64") && !(nami.os == "darwin" && nami.arch == "amd64") && !(nami.os == "linux" && nami.arch == "arm64") && !(nami.os == "linux" && nami.arch == "amd64") && !(nami.os == "windows" && nami.arch == "amd64")) {
echo("This package does not support your OS or Arch now. PR welcome: https://github.com/txthinking/nami/blob/master/package")
exit(1)
}

// version
var s = $1("curl -L https://nodejs.org/download/release/index.json")
var v = JSON.parse(s).find(v => v.lts).version
await writefile(`${nami.cache_dir}/version`, v)

if (nami.os != "windows") {
var s = ""
if (nami.os == "darwin" && nami.arch == "arm64") {
s = "darwin-arm64"
}
if (nami.os == "darwin" && nami.arch == "amd64") {
s = "darwin-x64"
}
if (nami.os == "linux" && nami.arch == "arm64") {
s = "linux-arm64"
}
if (nami.os == "linux" && nami.arch == "amd64") {
s = "linux-x64"
}
s = `node-${v}-${s}`
$(`curl -L --progress-bar https://nodejs.org/dist/${v}/${s}.tar.gz -o /tmp/_.tgz`)
$`rm -rf /tmp/_`
$`mkdir /tmp/_`
$`tar zxvf /tmp/_.tgz -C /tmp/_`
$(`mv /tmp/_/${s} ${nami.cache_dir}/node`)
write_file(`${nami.cache_dir}/links`, "node/bin/node\n")
append_file(`${nami.cache_dir}/links`, "node/bin/corepack\n")
append_file(`${nami.cache_dir}/links`, "node/bin/npm\n")
append_file(`${nami.cache_dir}/links`, "node/bin/npx\n")
}
if (nami.os == "windows") {
if (!which("7z")) {
throw 'need 7z, recommend $ nami install 7z';
}
var s = `node-${v}-win-x64`
$(`curl -L --progress-bar https://nodejs.org/dist/${v}/${s}.zip -o /tmp/_.zip`)
$`rm -rf /tmp/_`
$`mkdir /tmp/_`
$`7z x /tmp/_.zip -o/tmp/_`
$(`mv /tmp/_/${s} ${nami.cache_dir}/node`)
write_file(`${nami.cache_dir}/links`, "node/bin/node.exe\n")
append_file(`${nami.cache_dir}/links`, "node/bin/corepack\n")
append_file(`${nami.cache_dir}/links`, "node/bin/npm\n")
append_file(`${nami.cache_dir}/links`, "node/bin/npx\n")
}
32 changes: 32 additions & 0 deletions package/zig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// check os
if (!(nami.os == "darwin" && nami.arch == "arm64") && !(nami.os == "darwin" && nami.arch == "amd64") && !(nami.os == "linux" && nami.arch == "arm64") && !(nami.os == "linux" && nami.arch == "amd64")) {
echo("This package does not support your OS or Arch now. PR welcome: https://github.com/txthinking/nami/blob/master/package")
exit(1)
}

// version
// var s = $1("curl -L https://ziglang.org/download/index.json")
var v = "0.12.0-dev.899+027aabf49"; // force version since zig still not 1.0
await writefile(`${nami.cache_dir}/version`, v)

var s = ""
if (nami.os == "darwin" && nami.arch == "arm64") {
s = "macos-aarch64"
}
if (nami.os == "darwin" && nami.arch == "amd64") {
s = "macos-x86_64"
}
if (nami.os == "linux" && nami.arch == "arm64") {
s = "linux-aarch64"
}
if (nami.os == "linux" && nami.arch == "amd64") {
s = "linux-x86_64"
}
s = `zig-${s}-${v}`
$(`curl -L --progress-bar https://ziglang.org/builds/${s}.tar.xz -o /tmp/_.txz`)
$`rm -rf /tmp/_`
$`mkdir /tmp/_`
$`tar Jxvf /tmp/_.txz -C /tmp/_`
$(`mv /tmp/_/${s} ${nami.cache_dir}/zig`)
write_file(`${nami.cache_dir}/links`, "zig/zig\n")

Loading

0 comments on commit 0e67d8b

Please sign in to comment.