diff --git a/manager.go b/manager.go index 85931ac..b857200 100644 --- a/manager.go +++ b/manager.go @@ -38,16 +38,32 @@ func (game *Game) Clean() error { return cachePathError } - var deployedListPath = filepath.Join(cachePath, "backup", "deployed.txt") + var deployedListPath = filepath.Join(cachePath, "deployed.txt") - if backupFile, openError := os.Open(deployedListPath); !(os.IsNotExist(openError)) { - defer backupFile.Close() + if deployedListFile, openError := os.Open(deployedListPath); !(os.IsNotExist(openError)) { + { + defer deployedListFile.Close() - var scanner = bufio.NewScanner(backupFile) + var deployedListScanner = bufio.NewScanner(deployedListFile) - for scanner.Scan() { - var + for deployedListScanner.Scan() { + var deployedPath = filepath.Join(game.Path, deployedListScanner.Text()) + + if removeError := os.Remove(deployedPath); removeError != nil { + return removeError + } + + var deployedDirPath = filepath.Dir(deployedPath) + + if remainingDirEntries, readDirError := os.ReadDir(deployedDirPath); (readDirError == nil) && (len(remainingDirEntries) == 0) { + if removeError := os.Remove(deployedDirPath); removeError != nil { + return removeError + } + } + } } + + os.Truncate(deployedListPath, 0) } return nil @@ -70,95 +86,103 @@ func (game *Game) ConfigPath() (string, error) { } func (game *Game) Deploy() error { + if cleanError := game.Clean(); cleanError != nil { + return cleanError + } + var cachePath, cachePathError = game.CachePath() if cachePathError != nil { return cachePathError } - var configPath, configPathError = game.ConfigPath() + var deployedListFile, deployedListCreateError = os.Create( + filepath.Join(cachePath, "deployed.txt")) - if configPathError != nil { - return configPathError + if deployedListCreateError != nil { + return deployedListCreateError } - var backupPath = filepath.Join(cachePath, "backup") - var deployedListPath = filepath.Join(backupPath, "deployed.txt") + defer deployedListFile.Close() - if backupFile, openError := os.Open(deployedListPath); !(os.IsNotExist(openError)) { - defer backupFile.Close() + { + var configPath, configPathError = game.ConfigPath() - var scanner = bufio.NewScanner(backupFile) - - for scanner.Scan() { - var + if configPathError != nil { + return configPathError } - } - var modsPath = filepath.Join(configPath, "mods") + var deployedListWriter = bufio.NewWriter(deployedListFile) + var modsPath = filepath.Join(configPath, "mods") + var restorePath = filepath.Join(cachePath, "restore") - for i := range game.ModOrder { - var mod = game.ModOrder[i] + for i := range game.ModOrder { + var mod = game.ModOrder[i] - if mod.IsEnabled { - var modPath = filepath.Join(modsPath, mod.Name) + if mod.IsEnabled { + var modPath = filepath.Join(modsPath, mod.Name) - if walkError := filepath.WalkDir(modPath, func( - path string, dirEntry fs.DirEntry, dirError error) error { + if walkError := filepath.WalkDir(modPath, func( + path string, dirEntry fs.DirEntry, dirError error) error { - if dirError != nil { - return dirError + if dirError != nil { + return dirError + } + + var fileMode = dirEntry.Type() + + if !(fileMode.IsDir()) { + var localPath, relativeError = filepath.Rel(modPath, path) + + if relativeError != nil { + return relativeError + } + + var linkPath = filepath.Join(game.Path, localPath) + + if pathError := os.MkdirAll(filepath.Dir(linkPath), + fileMode.Perm()); pathError != nil { + + return pathError + } + + if _, statError := os.Stat(linkPath); !(os.IsNotExist(statError)) { + var sourceFile, sourceOpenError = os.Open(linkPath) + + if sourceOpenError != nil { + return sourceOpenError + } + + defer sourceFile.Close() + + var targetFile, targetOpenError = os.Create(filepath.Join(restorePath, localPath)) + + if targetOpenError != nil { + return targetOpenError + } + + defer targetFile.Close() + + var _, copyError = io.Copy(targetFile, sourceFile) + + if copyError != nil { + return copyError + } + } + + if linkError := os.Link(path, linkPath); linkError != nil { + return linkError + } + + if _, writeError := deployedListWriter.WriteString(linkPath); writeError != nil { + return writeError + } + } + + return nil + }); walkError != nil { + return walkError } - - var fileMode = dirEntry.Type() - - if !(fileMode.IsDir()) { - var localPath, relativeError = filepath.Rel(modPath, path) - - if relativeError != nil { - return relativeError - } - - var linkPath = filepath.Join(game.Path, localPath) - - if pathError := os.MkdirAll(filepath.Dir(linkPath), - fileMode.Perm()); pathError != nil { - - return pathError - } - - if _, statError := os.Stat(linkPath); !(os.IsNotExist(statError)) { - var sourceFile, sourceOpenError = os.Open(linkPath) - - if sourceOpenError != nil { - return sourceOpenError - } - - defer sourceFile.Close() - - var targetFile, targetOpenError = os.Create(backupPath) - - if targetOpenError != nil { - return targetOpenError - } - - defer targetFile.Close() - - var _, copyError = io.Copy(targetFile, sourceFile) - - if copyError != nil { - return copyError - } - } - - if linkError := os.Link(path, linkPath); linkError != nil { - return linkError - } - } - - return nil - }); walkError != nil { - return walkError } } }