Skip to content

Commit

Permalink
fix v0.0.0 bug, consolidate more utils, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
verdverm committed May 31, 2020
1 parent 0d42781 commit 7a05eb6
Show file tree
Hide file tree
Showing 40 changed files with 271 additions and 917 deletions.
2 changes: 2 additions & 0 deletions cue.sums
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/hofstadter-io/hofmod-cli v0.5.5 h1:U39psc0wrKEEdYiNZ5+9b+50PyxFLCeQ7b
github.com/hofstadter-io/hofmod-cli v0.5.5/cue.mods h1:8TVLRMLOvfcVRZu6NqDr4VFmvuw/9Ca7YpCMo1fTRmU=
github.com/hofstadter-io/hofmod-cli v0.5.6 h1:o64aCgzhH62fZvpa2uK+bENIfXrEcMHfXCHuMJTxoFw=
github.com/hofstadter-io/hofmod-cli v0.5.6/cue.mods h1:8TVLRMLOvfcVRZu6NqDr4VFmvuw/9Ca7YpCMo1fTRmU=
github.com/hofstadter-io/hofmod-cli v0.5.9 h1:8j7ZfA/NmmLP85tIMLzYLslLUZqUNCYrdm7US3pQpxM=
github.com/hofstadter-io/hofmod-cli v0.5.9/cue.mods h1:8TVLRMLOvfcVRZu6NqDr4VFmvuw/9Ca7YpCMo1fTRmU=
github.com/hofstadter-io/hofmod-client v0.0.0 h1:CjwsIJVLSPca2EHIVwiMrLCko4BGtH6QE5muWkpcNtU=
github.com/hofstadter-io/hofmod-client v0.0.0/cue.mods h1:U7nb/fqOz92SgTVIgDb4DWMhF5Fp+bLmWxRTQc5+c3Y=
github.com/hofstadter-io/hofmod-config v0.0.0 h1:QIfhuP79XdMR94NVSxZtpqIrPOyvpO9ps6ChT2hUPIs=
Expand Down
98 changes: 77 additions & 21 deletions lib/mod/cache/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"strings"

googithub "github.com/google/go-github/v30/github"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"

"github.com/hofstadter-io/hof/lib/mod/repos/github"
"github.com/hofstadter-io/hof/lib/mod/util"
"github.com/hofstadter-io/hof/lib/yagu"
"github.com/hofstadter-io/hof/lib/yagu/repos/github"
)

func Fetch(lang, mod, ver string) (err error) {
Expand Down Expand Up @@ -52,7 +53,69 @@ func fetch(lang, mod, ver string) error {
}
}

func fetchGitHub(lang, owner, repo, tag string) error {
func fetchGitHub(lang, owner, repo, tag string) (err error) {
FS := memfs.New()

if tag == "v0.0.0" {
err = fetchGitHubBranch(FS, lang, owner, repo, "")
} else {
err = fetchGitHubTag(FS, lang, owner, repo, tag)
}
if err != nil {
return fmt.Errorf("While fetching from github\n%w\n", err)
}

fmt.Println("filelist:")
files, err := yagu.BillyGetFilelist(FS)
if err != nil {
return fmt.Errorf("While getting filelist\n%w\n", err)
}

for _, f := range files {
fmt.Println(" -", f.Name())
}

fmt.Println("Writing...", )
err = Write(lang, "github.com", owner, repo, tag, FS)
if err != nil {
return fmt.Errorf("While writing to cache\n%w\n", err)
}

return nil
}
func fetchGitHubBranch(FS billy.Filesystem, lang, owner, repo, branch string) error {
client, err := github.NewClient()
if err != nil {
return err
}

// TODO find and set default branch
if branch == "" {
branch = "master"
r, err := github.GetRepo(client, owner, repo)
if err != nil {
return err
}

fmt.Printf("%#+v\n", *r)
}

fmt.Println("Fetch github branch", lang, owner, repo, branch)

zReader, err := github.FetchBranchZip(client, branch)
if err != nil {
return fmt.Errorf("While fetching branch zipfile\n%w\n", err)
}

err = yagu.BillyLoadFromZip(zReader, FS, true)
if err != nil {
return fmt.Errorf("While reading branch zipfile\n%w\n", err)
}

return nil
}
func fetchGitHubTag(FS billy.Filesystem, lang, owner, repo, tag string) error {
fmt.Println("Fetch github tag", lang, owner, repo, tag)
client, err := github.NewClient()
if err != nil {
return err
Expand All @@ -68,29 +131,22 @@ func fetchGitHub(lang, owner, repo, tag string) error {
for _, t := range tags {
if tag != "" && tag == *t.Name {
T = t
fmt.Printf("FOUND ")
fmt.Printf("FOUND %v\n", *t)
}
// fmt.Println(*t.Name, *t.Commit.SHA)
}

if T != nil {
zReader, err := github.FetchTagZip(client, T)
if err != nil {
return fmt.Errorf("While fetching zipfile\n%w\n", err)
}
FS := memfs.New()

err = util.BillyLoadFromZip(zReader, FS, true)
if err != nil {
return fmt.Errorf("While reading zipfile\n%w\n", err)
}

// fmt.Println("GOT HERE 1")
if T == nil {
return fmt.Errorf("Did not find tag %q for 'https://github.com/%s/%s' @%s", tag, owner, repo, tag)
}
zReader, err := github.FetchTagZip(client, T)
if err != nil {
return fmt.Errorf("While fetching tag zipfile\n%w\n", err)
}

err = Write(lang, "github.com", owner, repo, tag, FS)
if err != nil {
return fmt.Errorf("While writing to cache\n%w\n", err)
}
err = yagu.BillyLoadFromZip(zReader, FS, true)
if err != nil {
return fmt.Errorf("While reading tag zipfile\n%w\n", err)
}

return nil
Expand Down
17 changes: 12 additions & 5 deletions lib/mod/cache/local.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package cache

import (
"os"
"path/filepath"

"github.com/hofstadter-io/hof/lib/mod/util"
)

var (
LocalCacheBaseDir = filepath.Join(util.UserHomeDir(), ".mvs")
)
var LocalCacheBaseDir = ".hof/mods"

func init() {
d, err := os.UserConfigDir()
if err != nil {
return
}

// save to hof dir for cache across projects
LocalCacheBaseDir = filepath.Join(d, "hof/mods")
}

func SetBaseDir(basedir string) {
LocalCacheBaseDir = basedir
Expand Down
5 changes: 2 additions & 3 deletions lib/mod/cache/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"path/filepath"

"github.com/go-git/go-billy/v5"
"github.com/hofstadter-io/yagu"

"github.com/hofstadter-io/hof/lib/mod/util"
"github.com/hofstadter-io/hof/lib/yagu"
)

func Outdir(lang, remote, owner, repo, tag string) string {
Expand All @@ -27,5 +26,5 @@ func Write(lang, remote, owner, repo, tag string, FS billy.Filesystem) error {
if err != nil {
return err
}
return util.BillyWriteDirToOS(outdir, "/", FS)
return yagu.BillyWriteDirToOS(outdir, "/", FS)
}
8 changes: 1 addition & 7 deletions lib/mod/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ import (
)

func envSetup(env *testscript.Env) error {

env.Vars = append(env.Vars, "HOF_TELEMETRY_DISABLED=1")

return nil
}

func TestMod(t *testing.T) {

func TestModTests(t *testing.T) {
yagu.Mkdir(".workdir/tests")

testscript.Run(t, testscript.Params{
Setup: envSetup,
Dir: "testdata",
Expand All @@ -27,9 +23,7 @@ func TestMod(t *testing.T) {
}

func TestModBugs(t *testing.T) {

yagu.Mkdir(".workdir/bugs")

testscript.Run(t, testscript.Params{
Setup: envSetup,
Dir: "testdata/bugs",
Expand Down
10 changes: 5 additions & 5 deletions lib/mod/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/hofstadter-io/hof/lib/mod/cache"
"github.com/hofstadter-io/hof/lib/mod/parse/sumfile"
"github.com/hofstadter-io/hof/lib/mod/repos/github"
"github.com/hofstadter-io/hof/lib/mod/util"
"github.com/hofstadter-io/hof/lib/yagu"
"github.com/hofstadter-io/hof/lib/yagu/repos/github"
)

func Hack(lang string, args []string) error {
Expand Down Expand Up @@ -49,7 +49,7 @@ func Hack(lang string, args []string) error {
}
FS := memfs.New()

err = util.BillyLoadFromZip(zReader, FS, true)
err = yagu.BillyLoadFromZip(zReader, FS, true)
if err != nil {
return fmt.Errorf("While reading zipfile\n%w\n", err)
}
Expand All @@ -63,12 +63,12 @@ func Hack(lang string, args []string) error {

// fmt.Println("GOT HERE 2")

dirhash, err := util.BillyCalcHash(FS)
dirhash, err := yagu.BillyCalcHash(FS)
if err != nil {
return fmt.Errorf("While calculating dir hash\n%w\n", err)
}

modhash, err := util.BillyCalcFileHash("cue.mods", FS)
modhash, err := yagu.BillyCalcFileHash("cue.mods", FS)
if err != nil {
return fmt.Errorf("While calculating mod hash\n%w\n", err)
}
Expand Down
20 changes: 12 additions & 8 deletions lib/mod/langs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"gopkg.in/yaml.v3"

"cuelang.org/go/cue"

"github.com/hofstadter-io/hof/lib/mod/langs"
"github.com/hofstadter-io/hof/lib/mod/modder"
"github.com/hofstadter-io/hof/lib/mod/util"
)

// FROM the USER's HOME dir
const GLOBAL_MVS_CONFIG = ".mvs/config.cue"
const GLOBAL_MVS_CONFIG = "hof/.mvsconfig.cue"
const LOCAL_MVS_CONFIG = ".mvsconfig.cue"

var (
Expand Down Expand Up @@ -107,7 +107,8 @@ func LangInfo(lang string) (string, error) {
func InitLangs() {
var err error

cueSpec, err := util.CueRuntime.Compile("spec.cue", langs.ModderSpec)
rt := cue.Runtime{}
cueSpec, err := rt.Compile("spec.cue", langs.ModderSpec)
if err != nil {
panic(err)
}
Expand All @@ -117,7 +118,8 @@ func InitLangs() {
}
for lang, cueString := range langs.DefaultModdersCue {
var mdrMap map[string]*modder.Modder
cueLang, err := util.CueRuntime.Compile(lang, cueString)

cueLang, err := rt.Compile(lang, cueString)
if err != nil {
panic(err)
}
Expand All @@ -138,7 +140,10 @@ func InitLangs() {
langs.DefaultModders[lang] = mdrMap[lang]
}

homedir := util.UserHomeDir()
homedir, err := os.UserConfigDir()
if err != nil {
fmt.Println(err)
}

// Global Language Modder Config
err = initFromFile(path.Join(homedir, GLOBAL_MVS_CONFIG))
Expand All @@ -151,7 +156,6 @@ func InitLangs() {
if err != nil {
fmt.Println(err)
}

}

func initFromFile(filepath string) error {
Expand All @@ -168,7 +172,8 @@ func initFromFile(filepath string) error {
var mdrMap map[string]*modder.Modder

// Compile the config into cue
i, err := util.CueRuntime.Compile(filepath, string(bytes))
rt := cue.Runtime{}
i, err := rt.Compile(filepath, string(bytes))
if err != nil {
return err
}
Expand All @@ -193,7 +198,6 @@ func initFromFile(filepath string) error {
fmt.Printf("trying to customize unknown language %s\n", lang)
}
}
// util.PrintCueInstance(iMerged)

err = iMerged.Value().Decode(&mdrMap)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions lib/mod/modder/modder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"github.com/go-git/go-billy/v5"

"cuelang.org/go/cue"
"github.com/hofstadter-io/hof/lib/mod/util"

"github.com/hofstadter-io/hof/lib/yagu"
)

// This modder is for more complex, yet configurable module processing.
Expand Down Expand Up @@ -80,7 +81,7 @@ type Modder struct {

func NewFromFile(lang, filepath string, FS billy.Filesystem) (*Modder, error) {

bytes, err := util.BillyReadAll(filepath, FS)
bytes, err := yagu.BillyReadAll(filepath, FS)
if err != nil {
if _, ok := err.(*os.PathError); !ok && err.Error() != "file does not exist" && err.Error() != "no such file or directory" {
return nil, err
Expand All @@ -91,7 +92,8 @@ func NewFromFile(lang, filepath string, FS billy.Filesystem) (*Modder, error) {

var mdrMap map[string]*Modder

i, err := util.CueRuntime.Compile(filepath, string(bytes))
rt := cue.Runtime{}
i, err := rt.Compile(filepath, string(bytes))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 7a05eb6

Please sign in to comment.