Fix breaking changes and add test for

This commit is contained in:
2025-01-11 23:51:39 +01:00
parent d5d9819233
commit 51416782e8
2 changed files with 114 additions and 15 deletions

View File

@@ -37,6 +37,9 @@ func (a *Addon) GetRemoteRelease() (body []byte, err error) {
func (a *Addon) HasBreakingChanges(lhsToc, rhsToc string) bool {
lhsLines := strings.Split(lhsToc, "\n")
rhsLines := strings.Split(rhsToc, "\n")
if len(lhsLines) != len(rhsLines) {
return true
}
for i, lhsLine := range lhsLines {
rhsLine := rhsLines[i]
lhsLine = strings.TrimSpace(lhsLine)
@@ -81,6 +84,36 @@ func (a *Addon) Update(body []byte) (hasBreakingChanges bool, err error) {
return false, fmt.Errorf("error creating zip reader: %w", err)
}
log.Printf("Found %d files", len(zipReader.File))
// Yes it is quite ugly iterating twice doing the same work
// Maybe I fix it later but definitely not now
for _, file := range zipReader.File {
if strings.HasSuffix(file.Name, ".toc") {
localToc, err := os.ReadFile(a.GetLocalTocPath())
if err != nil {
if os.IsNotExist(err) {
localToc = []byte{}
} else {
return false, fmt.Errorf("error reading local toc: %w", err)
}
}
fileHandle, err := file.Open()
if err != nil {
return false, fmt.Errorf("error opening file: %w", err)
}
fileData, err := io.ReadAll(fileHandle)
if err != nil {
return false, fmt.Errorf("error reading file: %w", err)
}
if a.HasBreakingChanges(string(localToc), string(fileData)) {
Warning.Printf("Has breaking changes")
hasBreakingChanges = true
}
}
}
for _, file := range zipReader.File {
if file.FileInfo().IsDir() {
continue
@@ -95,21 +128,6 @@ func (a *Addon) Update(body []byte) (hasBreakingChanges bool, err error) {
return false, fmt.Errorf("error reading file: %w", err)
}
if strings.HasSuffix(file.Name, ".toc") {
localToc, err := os.ReadFile(a.GetLocalTocPath())
if err != nil {
if os.IsNotExist(err) {
localToc = []byte{}
} else {
return false, fmt.Errorf("error reading local toc: %w", err)
}
}
if a.HasBreakingChanges(string(localToc), string(fileData)) {
log.Printf("Has breaking changes")
hasBreakingChanges = true
}
}
localPath := filepath.Join(settings.GamePath, "Interface", "AddOns", file.Name)
log.Printf("Updating file %s", localPath)
err = UpdateFile(localPath, fileData)
@@ -117,6 +135,7 @@ func (a *Addon) Update(body []byte) (hasBreakingChanges bool, err error) {
return false, fmt.Errorf("error updating file: %w", err)
}
}
if hasBreakingChanges {
Warning.Printf("Has breaking changes")
}