Skip to content

Commit

Permalink
Merge pull request #86 from nix-community/symlink-indent-dehadouken
Browse files Browse the repository at this point in the history
Symlink indent dehadouken
  • Loading branch information
adisbladis committed Sep 2, 2022
2 parents b57059c + 9b360fe commit 4620c77
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 54 deletions.
108 changes: 57 additions & 51 deletions builder/symlink/symlink.go
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand All @@ -16,40 +15,44 @@ type Package struct {
ReplacedPath string `json:"replaced,omitempty"`
}

func main() {
sources := make(map[string]string)
pkgs := make(map[string]*Package)

{
b, err := ioutil.ReadFile(os.Getenv("sourcesPath"))
if err != nil {
panic(err)
}
type Sources map[string]string

err = json.Unmarshal(b, &sources)
if err != nil {
panic(err)
}
func populateStruct(path string, data interface{}) {
pathVal := os.Getenv(path)
if len(path) == 0 {
panic(fmt.Sprintf("env var '%s' was unset", path))
}
path = pathVal

{
b, err := ioutil.ReadFile(os.Getenv("jsonPath"))
if err != nil {
panic(err)
}
b, err := os.ReadFile(path)
if err != nil {
panic(err)
}

err = json.Unmarshal(b, &pkgs)
if err != nil {
panic(err)
}
err = json.Unmarshal(b, &data)
if err != nil {
panic(err)
}
}

func main() {
sources := make(Sources)
pkgs := make(map[string]*Package)

populateStruct("sourcesPath", &sources)

populateStruct("jsonPath", &pkgs)

keys := make([]string, 0, len(pkgs))
for key := range pkgs {
keys = append(keys, key)
}
sort.Strings(keys)

makeSymlinks(keys, sources)
}

func makeSymlinks(keys []string, sources Sources) {
// Iterate, in reverse order
for i := len(keys) - 1; i >= 0; i-- {
key := keys[i]
Expand All @@ -58,46 +61,49 @@ func main() {
paths := []string{key}

for _, path := range paths {

vendorDir := filepath.Join("vendor", filepath.Dir(path))
if err := os.MkdirAll(vendorDir, 0755); err != nil {
if err := os.MkdirAll(vendorDir, 0o755); err != nil {
panic(err)
}

if _, err := os.Stat(filepath.Join("vendor", path)); err == nil {
files, err := ioutil.ReadDir(src)
if err != nil {
panic(err)
}

for _, f := range files {
innerSrc := filepath.Join(src, f.Name())
dst := filepath.Join("vendor", path, f.Name())
if err := os.Symlink(innerSrc, dst); err != nil {
// assume it's an existing directory, try to link the directory content instead.
// TODO should we do this recursively
files, err := ioutil.ReadDir(innerSrc)
if err != nil {
panic(err)
}
for _, f := range files {
if err := os.Symlink(filepath.Join(innerSrc, f.Name()), filepath.Join(dst, f.Name())); err != nil {
fmt.Println("ignore symlink error", filepath.Join(innerSrc, f.Name()), filepath.Join(dst, f.Name()))
}
}
}
}

vendorPath := filepath.Join("vendor", path)
if _, err := os.Stat(vendorPath); err == nil {
populateVendorPath(vendorPath, src)
continue
}

// If the file doesn't already exist, just create a simple symlink
err := os.Symlink(src, filepath.Join("vendor", path))
err := os.Symlink(src, vendorPath)
if err != nil {
panic(err)
}

}
}
}

func populateVendorPath(vendorPath string, src string) {
files, err := os.ReadDir(src)
if err != nil {
panic(err)
}

for _, f := range files {
innerSrc := filepath.Join(src, f.Name())
dst := filepath.Join(vendorPath, f.Name())
if err := os.Symlink(innerSrc, dst); err != nil {
// assume it's an existing directory, try to link the directory content instead.
// TODO should we do this recursively?
files, err := os.ReadDir(innerSrc)
if err != nil {
panic(err)
}
for _, f := range files {
srcFile := filepath.Join(innerSrc, f.Name())
dstFile := filepath.Join(dst, f.Name())
if err := os.Symlink(srcFile, dstFile); err != nil {
fmt.Println("ignoring symlink error", srcFile, dstFile)
}
}
}
}
}
5 changes: 2 additions & 3 deletions internal/generate/generate.go
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -16,9 +15,9 @@ import (
"sync"

"github.com/nix-community/go-nix/pkg/nar"
log "github.com/sirupsen/logrus"
"github.com/nix-community/gomod2nix/internal/lib"
schema "github.com/nix-community/gomod2nix/internal/schema"
log "github.com/sirupsen/logrus"
"golang.org/x/mod/modfile"
)

Expand All @@ -45,7 +44,7 @@ func common(directory string) ([]*goModDownload, map[string]string, error) {
}).Info("Parsing go.mod")

// Read go.mod
data, err := ioutil.ReadFile(goModPath)
data, err := os.ReadFile(goModPath)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 4620c77

Please sign in to comment.