aboutsummaryrefslogtreecommitdiff
path: root/src/core/locker.nim
blob: d52b55d377c0157aec4090753c6c0bbf9777644f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import std / [
  os,
  json,
  hashes,
  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,
  chosenTag: string,
  configPath: string
) =
  ## Updates the lockfile
  var lock: JsonNode = loadLock()
  let
    config: string = readConfigFile(configPath)
    jsonObject: JsonNode = %*{
      "source": { "url": sourceURL },
      "tag": chosenTag,
      "config": config
      # I should probably add a sha256 thingy
    }
  lock[packageName] = jsonObject
  writeLock(lock)
  echo "Updated {packageName}".fmt
  

proc needRebuildLock*(
  packageName: string,
  sourceURL: string,
  chosenTag: string, 
  configPath: string
): bool =
  ## Checks whether the following have changed:\n
  ## - source (repo url)
  ## - tag (new release, manually updated, etc)
  result = false
  let lockFile = loadLock()
  if not lockFile.hasKey(packageName):
    echo "{packageName} added to queue"
    result = 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"
      result = true
      
    if package.hasKey("source") and 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"
        result = true

    if not package.hasKey("tag"):
      result = true
    else:
      if package["tag"].getStr != chosenTag:
        echo "Release tag has changed from {package["tag"]} -> {chosenTag}".fmt
        echo "{packageName} added to queue"
        result = true
    if not package.hasKey("config"):
      echo "Configuration file not found for package {packageName}".fmt
      echo "{packageName} added to queue"
      result = true
    else:
      let config: string = readConfigFile(configPath)
      if package["config"].getStr != config:
        echo "Configuration file changed for package {packageName}".fmt
        echo "{packageName} added to queue"
        result = true





    
Directive (EU) 2019/790, Article 4(3); all rights regarding Text and Data Mining (TDM) are reserved.