Skip to content

Commit

Permalink
feat: sort the dev and runtime packages in bsf.hcl
Browse files Browse the repository at this point in the history
Signed-off-by: hanshal101 <[email protected]>
  • Loading branch information
hanshal101 authored and dr-housemd committed Nov 19, 2024
1 parent a7d83f3 commit 1db6eaa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
28 changes: 18 additions & 10 deletions cmd/init/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"regexp"
"sort"
"strings"

"github.com/buildsafedev/bsf/pkg/hcl2nix"
Expand Down Expand Up @@ -64,8 +65,8 @@ func generateEmptyConf(imageName string, addCommonDeps bool, commonDepsType stri
}
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: devDeps,
Runtime: commonRTDeps,
Development: sortPackages(devDeps),
Runtime: sortPackages(commonRTDeps),
},
OCIArtifact: []hcl2nix.OCIArtifact{
{
Expand Down Expand Up @@ -109,8 +110,8 @@ func genRustCargoConf() (hcl2nix.Config, error) {
rustDevDeps := append(commonDevDeps, rustDeps...)
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: rustDevDeps,
Runtime: commonRTDeps,
Development: sortPackages(rustDevDeps),
Runtime: sortPackages(commonRTDeps),
},
RustApp: &hcl2nix.RustApp{
WorkspaceSrc: "./.",
Expand All @@ -126,8 +127,8 @@ func genPythonPoetryConf() hcl2nix.Config {
poetryDevDeps := append(commonDevDeps, pythonDeps...)
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: poetryDevDeps,
Runtime: commonRTDeps,
Development: sortPackages(poetryDevDeps),
Runtime: sortPackages(commonRTDeps),
},
PoetryApp: &hcl2nix.PoetryApp{
ProjectDir: "./.",
Expand Down Expand Up @@ -157,9 +158,9 @@ func genGoModuleConf(pd *langdetect.ProjectDetails) hcl2nix.Config {
goDevDeps := append(commonDevDeps, goDeps...)
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: goDevDeps,
Development: sortPackages(goDevDeps),
// todo: maybe we should dynamically inject the latest version of such runtime packages(cacert)?
Runtime: commonRTDeps,
Runtime: sortPackages(commonRTDeps),
},
GoModule: &hcl2nix.GoModule{
Name: name,
Expand Down Expand Up @@ -188,12 +189,19 @@ func genJsNpmConf() (hcl2nix.Config, error) {
nodeDevDeps := append(commonDevDeps, jsNpmDeps...)
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: nodeDevDeps,
Runtime: commonRTDeps,
Development: sortPackages(nodeDevDeps),
Runtime: sortPackages(commonRTDeps),
},
JsNpmApp: &hcl2nix.JsNpmApp{
PackageName: name,
PackageRoot: "./.",
},
}, nil
}

func sortPackages(packages []string) []string {
sort.Slice(packages, func(i, j int) bool {
return strings.Split(packages[i], "@")[0] < strings.Split(packages[j], "@")[0]
})
return packages
}
9 changes: 9 additions & 0 deletions pkg/strings/set.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package strings

import (
"sort"
"strings"
)

type parse func(string) string

// SliceToSet converts a slice of strings to a set of strings
Expand Down Expand Up @@ -42,6 +47,10 @@ func PreferNewSliceElements(existing []string, new []string, parseFunc parse) []
finalEle = append(finalEle, v)
}

sort.Slice(finalEle, func(i, j int) bool {
return strings.Split(finalEle[i], "@")[0] < strings.Split(finalEle[j], "@")[0]
})

return finalEle
}

Expand Down

0 comments on commit 1db6eaa

Please sign in to comment.