Skip to content

Commit

Permalink
builder: Print warning if external repository is modified or missing
Browse files Browse the repository at this point in the history
This adds functionality described in this issue:
#524

Warning is only shown when the build failed
  • Loading branch information
m-gorecki committed Sep 5, 2023
1 parent c719fde commit 75f1460
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
24 changes: 24 additions & 0 deletions newt/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Builder struct {
buildName string
linkElf string
injectedSettings map[string]string
modifiedExtRepos []string
}

func NewBuilder(
Expand Down Expand Up @@ -652,6 +653,8 @@ func (b *Builder) Build() error {
}
entries = append(entries, subEntries...)

b.modifiedExtRepos = append(b.modifiedExtRepos, bpkg.getModifiedReposNames()...)

if len(subEntries) > 0 {
bpkgCompilerMap[bpkg] = subEntries[0].Compiler
}
Expand Down Expand Up @@ -893,3 +896,24 @@ func (b *Builder) CleanArtifacts() {
os.Remove(p)
}
}

func Contains(elements []string, val string) bool {
for _, s := range elements {
if val == s {
return true
}
}
return false
}

func (b *Builder) AppendModifiedRepos(modifiedRepos []string) {
for _, repo := range modifiedRepos {
if !Contains(b.modifiedExtRepos, repo) {
b.modifiedExtRepos = append(b.modifiedExtRepos, repo)
}
}
}

func (b *Builder) GetModifiedRepos() []string {
return b.modifiedExtRepos
}
46 changes: 46 additions & 0 deletions newt/builder/buildpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
package builder

import (
"mynewt.apache.org/newt/newt/downloader"
"mynewt.apache.org/newt/newt/repo"
"os"
"path/filepath"
"regexp"
"strings"

"mynewt.apache.org/newt/newt/newtutil"
"mynewt.apache.org/newt/newt/pkg"
Expand Down Expand Up @@ -341,3 +344,46 @@ func (bpkg *BuildPackage) privateIncludeDirs(b *Builder) []string {

return incls
}

func (bpkg *BuildPackage) getModifiedReposNames() []string {
var modifiedRepos []string

settings := bpkg.rpkg.Lpkg.PkgY.AllSettings()
for settingName, setting := range settings {
if strings.HasPrefix(settingName, "repository") {
var version string

dl := downloader.NewGitDownloader()
rName := strings.TrimPrefix(settingName, "repository.")
r, _ := repo.NewRepo(rName, dl)

if util.NodeNotExist(r.Path()) {
modifiedRepos = append(modifiedRepos, r.Name())
continue
}

currentHash, _ := dl.HashFor(r.Path(), "HEAD")

aSetting, ok := setting.(map[interface{}]interface{})
if ok {
for field, value := range aSetting {
if field == "vers" {
aValue, ok := value.(string)
if ok {
version = strings.TrimSuffix(aValue, "-commit")
}
}
}
}

expectedHash, _ := dl.HashFor(r.Path(), version)
dirtyState, _ := r.DirtyState()

if currentHash != expectedHash || dirtyState != "" {
modifiedRepos = append(modifiedRepos, r.Name())
}
}
}

return modifiedRepos
}
5 changes: 5 additions & 0 deletions newt/cli/build_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ func buildRunCmd(cmd *cobra.Command, args []string, printShellCmds bool, execute
}

if err := b.Build(); err != nil {
if b.AppBuilder.GetModifiedRepos() != nil {
util.ErrorMessage(util.VERBOSITY_DEFAULT,
"Warning: Following external repos are modified or missing, which might be causing build errors:\n%v\n",
b.AppBuilder.GetModifiedRepos())
}
NewtUsage(nil, err)
}

Expand Down

0 comments on commit 75f1460

Please sign in to comment.