diff --git a/api/download.go b/api/download.go index 19792b6..a190dcc 100644 --- a/api/download.go +++ b/api/download.go @@ -11,6 +11,23 @@ import ( "strings" ) +// retrieves the Source directory of a given Source +func GetSourcePath(source Source, moduleName string) string { + if source.Type == "git" { + var dest string + if source.Destination != "" { + repoName := strings.Split(source.URL, "/") + dest = filepath.Join(moduleName, strings.ReplaceAll(repoName[len(repoName)-1], ".git", "")) + } else { + dest = filepath.Join(moduleName, source.Destination) + } + return dest + } else if source.Type == "tar" { + return filepath.Join(moduleName, source.Destination) + } + return "" +} + // DownloadSource downloads a source to the downloads directory // according to its type (git, tar, ...) func DownloadSource(downloadPath string, source Source, moduleName string) error { @@ -23,7 +40,7 @@ func DownloadSource(downloadPath string, source Source, moduleName string) error if err != nil { return err } - return checksumValidation(source, filepath.Join(downloadPath, moduleName)) + return checksumValidation(source, filepath.Join(downloadPath, GetSourcePath(source, moduleName), moduleName+".tar")) } else { return fmt.Errorf("unsupported source type %s", source.Type) } @@ -32,16 +49,17 @@ func DownloadSource(downloadPath string, source Source, moduleName string) error // DownloadGitSource downloads a git source to the downloads directory // and checks out the commit or tag func DownloadGitSource(downloadPath string, source Source, moduleName string) error { - fmt.Printf("Source is git: %s\n", source.URL) - - dest := filepath.Join(downloadPath, moduleName) + fmt.Printf("Downloading git source: %s\n", source.URL) if source.Commit == "" && source.Tag == "" && source.Branch == "" { return fmt.Errorf("missing source commit, tag or branch") } + dest := filepath.Join(downloadPath, GetSourcePath(source, moduleName)) + os.MkdirAll(dest, 0777) + if source.Tag != "" { - fmt.Printf("Using a tag: %s\n", source.Tag) + fmt.Printf("Using tag %s\n", source.Tag) cmd := exec.Command( "git", @@ -55,10 +73,10 @@ func DownloadGitSource(downloadPath string, source Source, moduleName string) er return err } } else { - fmt.Printf("Using a commit: %s\n", source.Commit) + fmt.Printf("Using commit %s\n", source.Commit) if source.Branch == "" { - return fmt.Errorf("missing source branch, needed to checkout commit") + return fmt.Errorf("missing source branch") } fmt.Printf("Cloning repository: %s\n", source.URL) @@ -119,7 +137,8 @@ func DownloadGitSource(downloadPath string, source Source, moduleName string) er func DownloadTarSource(downloadPath string, source Source, moduleName string) error { fmt.Printf("Source is tar: %s\n", source.URL) //Create the destination path - dest := filepath.Join(downloadPath, moduleName) + dest := filepath.Join(downloadPath, GetSourcePath(source, moduleName)) + os.MkdirAll(dest, 0777) //Download the resource res, err := http.Get(source.URL) if err != nil { @@ -128,7 +147,7 @@ func DownloadTarSource(downloadPath string, source Source, moduleName string) er defer res.Body.Close() //Create the destination tar file - file, err := os.Create(dest) + file, err := os.Create(filepath.Join(dest, moduleName+".tar")) if err != nil { return err } @@ -165,22 +184,24 @@ func MoveSource(downloadPath string, sourcesPath string, source Source, moduleNa fmt.Printf("Moving source: %s\n", moduleName) if source.Type == "git" { + dest := GetSourcePath(source, moduleName) return os.Rename( - filepath.Join(downloadPath, moduleName), - filepath.Join(sourcesPath, moduleName), + filepath.Join(downloadPath, dest), + filepath.Join(sourcesPath, dest), ) } else if source.Type == "tar" { + os.MkdirAll(filepath.Join(sourcesPath, GetSourcePath(source, moduleName)), 0777) cmd := exec.Command( "tar", - "-xf", filepath.Join(downloadPath, moduleName), - "-C", sourcesPath, + "-xf", filepath.Join(downloadPath, GetSourcePath(source, moduleName), moduleName+".tar"), + "-C", filepath.Join(sourcesPath, GetSourcePath(source, moduleName)), ) err := cmd.Run() if err != nil { return err } - return os.Remove(filepath.Join(downloadPath, moduleName)) + return os.Remove(filepath.Join(downloadPath, GetSourcePath(source, moduleName), moduleName+".tar")) } else { return fmt.Errorf("unsupported source type %s", source.Type) } diff --git a/api/structs.go b/api/structs.go index 3c95aaf..db1b0a3 100644 --- a/api/structs.go +++ b/api/structs.go @@ -1,14 +1,15 @@ package api type Source struct { - URL string `json:"url"` - Checksum string `json:"checksum"` - Type string `json:"type"` - Commit string `json:"commit"` - Tag string `json:"tag"` - Branch string `json:"branch"` - Packages []string `json:"packages"` - Paths []string `json:"paths"` + URL string `json:"url"` + Checksum string `json:"checksum"` + Type string `json:"type"` + Destination string `json:"destination"` + Commit string `json:"commit"` + Tag string `json:"tag"` + Branch string `json:"branch"` + Packages []string `json:"packages"` + Paths []string `json:"paths"` } type Recipe struct { diff --git a/core/apt.go b/core/apt.go index 864f74f..587aeae 100644 --- a/core/apt.go +++ b/core/apt.go @@ -92,3 +92,4 @@ func BuildAptModule(moduleInterface interface{}, recipe *api.Recipe) (string, er return "", errors.New("no packages or paths specified") } + diff --git a/core/cmake.go b/core/cmake.go index 2eb1732..e676ff1 100644 --- a/core/cmake.go +++ b/core/cmake.go @@ -2,6 +2,7 @@ package core import ( "fmt" + "path/filepath" "github.com/mitchellh/mapstructure" @@ -43,7 +44,7 @@ func BuildCMakeModule(moduleInterface interface{}, recipe *api.Recipe) (string, cmd := fmt.Sprintf( "cd /sources/%s && mkdir -p build && cd build && cmake ..%s && make", - module.Name, + filepath.Join(recipe.SourcesPath, api.GetSourcePath(module.Source, module.Name)), buildFlags, ) diff --git a/core/dpkg-buildpackage.go b/core/dpkg-buildpackage.go index ce4ef07..6f22f18 100644 --- a/core/dpkg-buildpackage.go +++ b/core/dpkg-buildpackage.go @@ -2,6 +2,7 @@ package core import ( "fmt" + "path/filepath" "github.com/mitchellh/mapstructure" "github.com/vanilla-os/vib/api" @@ -33,7 +34,7 @@ func BuildDpkgBuildPkgModule(moduleInterface interface{}, recipe *api.Recipe) (s cmd := fmt.Sprintf( "cd /sources/%s && dpkg-buildpackage -d -us -uc -b", - module.Name, + filepath.Join(api.GetSourcePath(module.Source, module.Name)), ) for _, path := range module.Source.Paths { diff --git a/core/dpkg.go b/core/dpkg.go index 24122f1..79918a3 100644 --- a/core/dpkg.go +++ b/core/dpkg.go @@ -30,7 +30,7 @@ func BuildDpkgModule(moduleInterface interface{}, recipe *api.Recipe) (string, e } cmd := "" for _, path := range module.Source.Paths { - cmd += fmt.Sprintf(" dpkg -i /sources/%s && apt install -f && ", path) + cmd += fmt.Sprintf(" dpkg -i /sources/%s/%s && apt install -f && ", module.Name, path) } cmd += " && apt clean" diff --git a/core/go.go b/core/go.go index 493eb50..6645134 100644 --- a/core/go.go +++ b/core/go.go @@ -50,7 +50,7 @@ func BuildGoModule(moduleInterface interface{}, recipe *api.Recipe) (string, err cmd := fmt.Sprintf( "cd /sources/%s && go build%s -o %s", - module.Name, + api.GetSourcePath(module.Source, module.Name), buildFlags, buildVars["GO_OUTPUT_BIN"], ) diff --git a/core/make.go b/core/make.go index 15587d3..59bca94 100644 --- a/core/make.go +++ b/core/make.go @@ -27,5 +27,5 @@ func BuildMakeModule(moduleInterface interface{}, recipe *api.Recipe) (string, e return "", err } - return "cd /sources/" + module.Name + " && make && make install", nil + return "cd /sources/" + api.GetSourcePath(module.Source, module.Name) + " && make && make install", nil } diff --git a/core/meson.go b/core/meson.go index 6558b74..8fb5078 100644 --- a/core/meson.go +++ b/core/meson.go @@ -34,7 +34,7 @@ func BuildMesonModule(moduleInterface interface{}, recipe *api.Recipe) (string, tmpDir := fmt.Sprintf("/tmp/%s-%s", module.Source.Checksum, module.Name) cmd := fmt.Sprintf( "cd /sources/%s && meson %s && ninja -C %s && ninja -C %s install", - module.Name, + api.GetSourcePath(module.Source, module.Name), tmpDir, tmpDir, tmpDir,