summary refs log tree commit diff stats
path: root/tools/atlas
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2023-05-26 09:24:01 +0200
committerGitHub <noreply@github.com>2023-05-26 09:24:01 +0200
commit908e9717324f83225eff66982c8c9a94f64ad29b (patch)
tree8d8fc244b5cb28ae61d10854c07d97b931aa7cbe /tools/atlas
parent609bf3d7c8bf880dea70f6e8211976f3ec1567a0 (diff)
downloadNim-908e9717324f83225eff66982c8c9a94f64ad29b.tar.gz
Atlas: misc (#21919)
* Atlas: misc

* Atlas: use the lockfile if one exists
Diffstat (limited to 'tools/atlas')
-rw-r--r--tools/atlas/atlas.md14
-rw-r--r--tools/atlas/atlas.nim45
2 files changed, 34 insertions, 25 deletions
diff --git a/tools/atlas/atlas.md b/tools/atlas/atlas.md
index c898bc00e..d0a45c866 100644
--- a/tools/atlas/atlas.md
+++ b/tools/atlas/atlas.md
@@ -18,21 +18,17 @@ Atlas uses three concepts:
 ### Workspaces
 
 Every workspace is isolated, nothing is shared between workspaces.
-A workspace is a directory that has a file `atlas.workspace` inside it. If `atlas`
-is run on a (sub-)directory that is not within a workspace, a workspace is created
-automatically for you. Atlas picks the current directory or one of its parent directories
-that has no `.git` subdirectory inside it as its workspace.
+A workspace is a directory that has a file `atlas.workspace` inside it. Use `atlas init`
+to create a workspace out of the current working directory.
 
-Thanks to this setup, it's easy to develop multiple projects at the same time.
-
-A project plus its dependencies are stored in a workspace:
+Projects plus their dependencies are stored in a workspace:
 
   $workspace / main project
+  $workspace / other project
   $workspace / _deps / dependency A
   $workspace / _deps / dependency B
 
-The deps directory can be set via `--deps:DIR` explicitly. It defaults to `_deps`.
-If you want it to be the same as the workspace use `--deps:.`.
+The deps directory can be set via `--deps:DIR` during `atlas init`.
 
 
 ### Projects
diff --git a/tools/atlas/atlas.nim b/tools/atlas/atlas.nim
index c3d942d22..d455fd676 100644
--- a/tools/atlas/atlas.nim
+++ b/tools/atlas/atlas.nim
@@ -389,23 +389,43 @@ proc findNimbleFile(c: AtlasContext; dep: Dependency): string =
           return ""
 
 proc addUniqueDep(c: var AtlasContext; work: var seq[Dependency];
-                  tokens: seq[string]) =
+                  tokens: seq[string]; lockfile: Table[string, LockFileEntry]) =
+  let pkgName = tokens[0]
   let oldErrors = c.errors
-  let url = toUrl(c, tokens[0])
+  let url = toUrl(c, pkgName)
   if oldErrors != c.errors:
-    warn c, toName(tokens[0]), "cannot resolve package name"
+    warn c, toName(pkgName), "cannot resolve package name"
   elif not c.processed.containsOrIncl(url / tokens[2]):
-    work.add Dependency(name: toName(tokens[0]), url: url, commit: tokens[2],
-                        rel: toDepRelation(tokens[1]))
+    if lockfile.contains(pkgName):
+      work.add Dependency(name: toName(pkgName),
+                          url: lockfile[pkgName].url,
+                          commit: lockfile[pkgName].commit,
+                          rel: normal)
+    else:
+      work.add Dependency(name: toName(pkgName), url: url, commit: tokens[2],
+                          rel: toDepRelation(tokens[1]))
 
 template toDestDir(p: PackageName): string = p.string
 
+proc readLockFile(filename: string): Table[string, LockFileEntry] =
+  let jsonAsStr = readFile(filename)
+  let jsonTree = parseJson(jsonAsStr)
+  let data = to(jsonTree, seq[LockFileEntry])
+  result = initTable[string, LockFileEntry]()
+  for d in items(data):
+    result[d.dir] = d
+
 proc collectDeps(c: var AtlasContext; work: var seq[Dependency];
                  dep: Dependency; nimbleFile: string): string =
   # If there is a .nimble file, return the dependency path & srcDir
   # else return "".
   assert nimbleFile != ""
   let nimbleInfo = extractRequiresInfo(c, nimbleFile)
+
+  let lockFilePath = dependencyDir(c, dep) / LockFileName
+  let lockFile = if fileExists(lockFilePath): readLockFile(lockFilePath)
+                 else: initTable[string, LockFileEntry]()
+
   for r in nimbleInfo.requires:
     var tokens: seq[string] = @[]
     for token in tokenizeRequires(r):
@@ -423,7 +443,7 @@ proc collectDeps(c: var AtlasContext; work: var seq[Dependency];
       tokens.add commit
 
     if tokens.len >= 3 and cmpIgnoreCase(tokens[0], "nim") != 0:
-      c.addUniqueDep work, tokens
+      c.addUniqueDep work, tokens, lockFile
   result = toDestDir(dep.name) / nimbleInfo.srcDir
 
 proc collectNewDeps(c: var AtlasContext; work: var seq[Dependency];
@@ -457,14 +477,6 @@ proc cloneLoop(c: var AtlasContext; work: var seq[Dependency]; startIsDep: bool)
       collectNewDeps(c, work, w, result, i == 0)
     inc i
 
-proc readLockFile(c: var AtlasContext) =
-  let jsonAsStr = readFile(c.projectDir / LockFileName)
-  let jsonTree = parseJson(jsonAsStr)
-  let data = to(jsonTree, seq[LockFileEntry])
-  c.lockFileToUse = initTable[string, LockFileEntry]()
-  for d in items(data):
-    c.lockFileToUse[d.dir] = d
-
 proc clone(c: var AtlasContext; start: string; startIsDep: bool): seq[string] =
   # non-recursive clone.
   let url = toUrl(c, start)
@@ -476,7 +488,7 @@ proc clone(c: var AtlasContext; start: string; startIsDep: bool): seq[string] =
 
   c.projectDir = c.workspace / toDestDir(work[0].name)
   if c.lockOption == useLock:
-    readLockFile c
+    c.lockFileToUse = readLockFile(c.projectDir / LockFileName)
   result = cloneLoop(c, work, startIsDep)
   if c.lockOption == genLock:
     writeFile c.projectDir / LockFileName, toJson(c.lockFileToWrite).pretty
@@ -794,7 +806,8 @@ proc main =
         break
     if nimbleFile.len == 0:
       error "could not find a .nimble file"
-    installDependencies(c, nimbleFile)
+    else:
+      installDependencies(c, nimbleFile)
   of "refresh":
     noArgs()
     updatePackages(c)