Skip to content

Commit

Permalink
feat: Make it easier to configure local includes
Browse files Browse the repository at this point in the history
  • Loading branch information
axtloss committed Nov 15, 2024
1 parent 929bc8a commit 442c3f3
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 22 deletions.
55 changes: 49 additions & 6 deletions api/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,40 @@ func GetSourcePath(source Source, moduleName string) string {
file := strings.Split(url[len(url)-1], "?")[0]
fileParts := strings.Split(file, ".")
return filepath.Join(moduleName, strings.Join(fileParts[:len(fileParts)-1], "."))
case "local":
toplevelDir := strings.Split(source.URL, "/")
return filepath.Join(moduleName, toplevelDir[len(toplevelDir)-1])
}

return ""
}

// Download the source based on its type and validate its checksum
func DownloadSource(downloadPath string, source Source, moduleName string) error {
func DownloadSource(recipe *Recipe, source Source, moduleName string) error {
fmt.Printf("Downloading source: %s\n", source.URL)

switch source.Type {
case "git":
return DownloadGitSource(downloadPath, source, moduleName)
return DownloadGitSource(recipe.DownloadsPath, source, moduleName)
case "tar":
err := DownloadTarSource(downloadPath, source, moduleName)
err := DownloadTarSource(recipe.DownloadsPath, source, moduleName)
if err != nil {
return err
}
return checksumValidation(source, filepath.Join(downloadPath, GetSourcePath(source, moduleName), moduleName+".tar"))
return checksumValidation(source, filepath.Join(recipe.DownloadsPath, GetSourcePath(source, moduleName), moduleName+".tar"))
case "file":
err := DownloadFileSource(downloadPath, source, moduleName)
err := DownloadFileSource(recipe.DownloadsPath, source, moduleName)
if err != nil {
return err
}

extension := filepath.Ext(source.URL)
filename := fmt.Sprintf("%s%s", moduleName, extension)
destinationPath := filepath.Join(downloadPath, GetSourcePath(source, moduleName), filename)
destinationPath := filepath.Join(recipe.DownloadsPath, GetSourcePath(source, moduleName), filename)

return checksumValidation(source, destinationPath)
case "local":
return DownloadLocalSource(recipe.SourcesPath, source, moduleName)
default:
return fmt.Errorf("unsupported source type %s", source.Type)
}
Expand Down Expand Up @@ -175,6 +180,42 @@ func DownloadTarSource(downloadPath string, source Source, moduleName string) er
return nil
}

// Copies a local source for use during the build, skips the Download directory and copies directly into the source path
func DownloadLocalSource(sourcesPath string, source Source, moduleName string) error {
fmt.Printf("Source is local: %s\n", source.URL)
dest := filepath.Join(sourcesPath, GetSourcePath(source, moduleName))
os.MkdirAll(dest, 0o777)
fileInfo, err := os.Stat(source.URL)
if err != nil {
return err
}
if fileInfo.IsDir() {
fmt.Println("ROOTDIR:: ", source.URL)
root := os.DirFS(source.URL)
return os.CopyFS(dest, root)
} else {
fileName := strings.Split(source.URL, "/")
out, err := os.Create(filepath.Join(dest, fileName[len(fileName)-1]))
if err != nil {
return err
}
defer out.Close()

in, err := os.Open(source.URL)
if err != nil {
return err
}
defer in.Close()

_, err = io.Copy(out, in)
if err != nil {
return err
}
return nil
}

}

// Move downloaded sources from the download path to the sources path
func MoveSources(downloadPath string, sourcesPath string, sources []Source, moduleName string) error {
fmt.Println("Moving sources for " + moduleName)
Expand Down Expand Up @@ -224,6 +265,8 @@ func MoveSource(downloadPath string, sourcesPath string, source Source, moduleNa
}

return os.Remove(filepath.Join(downloadPath, GetSourcePath(source, moduleName), moduleName+".tar"))
case "local":
return nil
default:
return fmt.Errorf("unsupported source type %s", source.Type)
}
Expand Down
1 change: 1 addition & 0 deletions api/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Recipe struct {
ParentPath string
DownloadsPath string
SourcesPath string
IncludesPath string
PluginPath string
Containerfile string
Finalize []interface{}
Expand Down
8 changes: 1 addition & 7 deletions core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,11 @@ func BuildContainerfile(recipe *api.Recipe) error {
}

// INCLUDES.CONTAINER
_, err = containerfile.WriteString("ADD includes.container /\n")
_, err = containerfile.WriteString(fmt.Sprintf("ADD %s /\n", recipe.IncludesPath))
if err != nil {
return err
}

// SOURCES
/*_, err = containerfile.WriteString("ADD sources /sources\n")
if err != nil {
return err
}*/

for _, cmd := range cmds {
err = ChangeWorkingDirectory(cmd.Workdir, containerfile)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions core/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ func LoadRecipe(path string) (*api.Recipe, error) {
// directory. For example, if you want to include a file in
// /etc/nginx/nginx.conf you must place it in includes/etc/nginx/nginx.conf
// so it will be copied to the correct location in the container
includesContainerPath := filepath.Join(filepath.Dir(recipePath), "includes.container")
_, err = os.Stat(includesContainerPath)
if len(strings.TrimSpace(recipe.IncludesPath)) == 0 {
recipe.IncludesPath = filepath.Join("includes.container")
}
_, err = os.Stat(recipe.IncludesPath)
if os.IsNotExist(err) {
err := os.MkdirAll(includesContainerPath, 0755)
err := os.MkdirAll(recipe.IncludesPath, 0755)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion core/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func BuildShellModule(moduleInterface interface{}, recipe *api.Recipe) (string,

for _, source := range module.Sources {
if strings.TrimSpace(source.Type) != "" {
err := api.DownloadSource(recipe.DownloadsPath, source, module.Name)
err := api.DownloadSource(recipe, source, module.Name)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/cmake.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func BuildModule(moduleInterface *C.char, recipeInterface *C.char) *C.char {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}

err = api.DownloadSource(recipe.DownloadsPath, module.Source, module.Name)
err = api.DownloadSource(recipe, module.Source, module.Name)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/dpkg-buildpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func BuildModule(moduleInterface *C.char, recipeInterface *C.char) *C.char {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}

err = api.DownloadSource(recipe.DownloadsPath, module.Source, module.Name)
err = api.DownloadSource(recipe, module.Source, module.Name)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func BuildModule(moduleInterface *C.char, recipeInterface *C.char) *C.char {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}

err = api.DownloadSource(recipe.DownloadsPath, module.Source, module.Name)
err = api.DownloadSource(recipe, module.Source, module.Name)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/make.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func BuildModule(moduleInterface *C.char, recipeInterface *C.char) *C.char {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}

err = api.DownloadSource(recipe.DownloadsPath, module.Source, module.Name)
err = api.DownloadSource(recipe, module.Source, module.Name)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/meson.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func BuildModule(moduleInterface *C.char, recipeInterface *C.char) *C.char {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}

err = api.DownloadSource(recipe.DownloadsPath, module.Source, module.Name)
err = api.DownloadSource(recipe, module.Source, module.Name)
if err != nil {
return C.CString(fmt.Sprintf("ERROR: %s", err.Error()))
}
Expand Down

0 comments on commit 442c3f3

Please sign in to comment.