Skip to content

Commit

Permalink
feat[close #45]: Add source destination option and split up sources b…
Browse files Browse the repository at this point in the history
…etter
  • Loading branch information
axtloss committed Apr 24, 2024
1 parent 07710d6 commit f11e895
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 28 deletions.
49 changes: 35 additions & 14 deletions api/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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",
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
17 changes: 9 additions & 8 deletions api/structs.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
1 change: 1 addition & 0 deletions core/apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,4 @@ func BuildAptModule(moduleInterface interface{}, recipe *api.Recipe) (string, er

return "", errors.New("no packages or paths specified")
}

3 changes: 2 additions & 1 deletion core/cmake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"fmt"
"path/filepath"

"github.com/mitchellh/mapstructure"

Expand Down Expand Up @@ -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,
)

Expand Down
3 changes: 2 additions & 1 deletion core/dpkg-buildpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"fmt"
"path/filepath"

"github.com/mitchellh/mapstructure"
"github.com/vanilla-os/vib/api"
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/dpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion core/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
Expand Down
2 changes: 1 addition & 1 deletion core/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion core/meson.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f11e895

Please sign in to comment.