diff options
Diffstat (limited to '')
| -rw-r--r-- | src/old.core/locker.nim | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/old.core/locker.nim b/src/old.core/locker.nim new file mode 100644 index 0000000..9b86d82 --- /dev/null +++ b/src/old.core/locker.nim @@ -0,0 +1,110 @@ +import std / [ + os, + json, + strformat +] + +import ./manager + +proc lockPath*(): string = + let + homeDir: string = getHomeDir() + lockDir: string = homeDir / ".local" / "tesserae" + ensureDir(lockDir) + result = lockDir / "tesserae.lock" + +proc loadLock*(): JsonNode = + let lockFile: string = lockPath() + result = %*{} + if fileExists(lockFile): + try: + result = parseFile(lockFile) + except JsonParsingError: + echo "JsonParsingError. Treating the lockfile as non existant" + result = %*{} + +proc writeLock*(jsonObject: JsonNode) = + let lockFile: string = lockPath() + writeFile(lockFile, jsonObject.pretty()) + +proc updateLock*( + packageName: string, + sourceURL: string, + sourceDir: string, + chosenTag: string, + configPath: string +) = + ## Updates the lockfile + var lock: JsonNode = loadLock() + let jsonObject: JsonNode = %*{ + "source": { "url": sourceURL }, + "tag": chosenTag, + "sha256": computeSHA256(sourceDir, chosenTag), + "config": readConfigFile(configPath) + # I should probably add a sha256 thingy + } + lock[packageName] = jsonObject + writeLock(lock) + echo "Updated {packageName}".fmt + +proc needRebuildLock*( + packageName: string, + sourceURL: string, + sourceDir: string, + chosenTag: string, + configPath: string +): bool = + ## Checks whether the following have changed:\n + ## - source (repo url) + ## - sha256 + ## - tag (new release, manually updated, etc) + ## - config file of package + result = false + let lockFile = loadLock() + + if not lockFile.hasKey(packageName): + echo "{packageName} added to queue".fmt + return true + + else: + let package = lockFile[packageName] + if not package.hasKey("source"): + echo "Could not find source for package {packageName}".fmt + echo "{packageName} added to queue".fmt + return true + if package["source"].hasKey("url"): + let packageSource: string = package["source"]["url"].getStr + if packageSource != sourceURL: + echo "Source URL for package {packageName} has changed".fmt + echo "{packageName} added to queue".fmt + return true + + + if not package.hasKey("sha256"): + echo "sha256 not found for package {packageName}".fmt + echo "{packageName} added to queue".fmt + return true + if package["sha256"].getStr != computeSHA256(sourceDir, chosenTag): + echo "sha256 mismatch for package {packageName}".fmt + echo "{packageName} added to queue".fmt + return true + + + if not package.hasKey("tag"): + echo "Could not find release tag for package {packageName}".fmt + echo "{packageName} added to queue".fmt + return true + if package["tag"].getStr != chosenTag: + echo "Release tag has changed from {package["tag"]} -> {chosenTag}".fmt + echo "{packageName} added to queue".fmt + return true + + + if not package.hasKey("config"): + echo "Configuration file not found for package {packageName}".fmt + echo "{packageName} added to queue" + return true + if package["config"].getStr != readConfigFile(configPath): + echo "Configuration file changed for package {packageName}".fmt + echo "{packageName} added to queue".fmt + return true |
