|
| 1 | +/// This class is used for interacting with a lock file for a given module |
| 2 | +/// Covers functionality for both creating a lock file and installing from a lock file |
| 3 | +Class %IPM.General.LockFile Extends (%RegisteredObject, %JSON.Adaptor) |
| 4 | +{ |
| 5 | + |
| 6 | +/// Name of the JSON lock file |
| 7 | +Parameter LockFileName As String = "module-lock.json"; |
| 8 | + |
| 9 | +/// Name of the module the lock file is for |
| 10 | +Property ModuleName As %IPM.DataType.ModuleName(%JSONFIELDNAME = "name") [ Required ]; |
| 11 | + |
| 12 | +/// Version string of the module |
| 13 | +Property VersionString As %IPM.DataType.VersionString(%JSONFIELDNAME = "version") [ Required ]; |
| 14 | + |
| 15 | +/// Repository where the base module used to create the lock file comes from |
| 16 | +Property Repository As %IPM.DataType.RepoName(%JSONFIELDNAME = "repository"); |
| 17 | + |
| 18 | +/// Version of the lock file schema used in creating the lock file |
| 19 | +Property LockFileVersion As %String(%JSONFIELDNAME = "lockFileVersion", MAXLEN = "") [ InitialExpression = "1" ]; |
| 20 | + |
| 21 | +/// Array of repository definitions from where at least one of the base module or dependency comes from |
| 22 | +Property Repositories As array Of %IPM.Repo.Definition(%JSONFIELDNAME = "repositories"); |
| 23 | + |
| 24 | +/// Array of dependency modules required by the module. Ordered by least dependent to most dependent |
| 25 | +Property Dependencies As array Of %IPM.General.LockFile.Dependency(%JSONFIELDNAME = "dependencies"); |
| 26 | + |
| 27 | +/// When given the name of a module, creates the lock file for it and saves it to module.Root_..#LockFileName |
| 28 | +/// flatDependencyList is the output from ##class(%IPM.Utils.Module).GetFlatDependencyListFromInvertedDependencyGraph() |
| 29 | +ClassMethod CreateLockFileForModule( |
| 30 | + module As %IPM.Storage.Module, |
| 31 | + ByRef flatDependencyList, |
| 32 | + ByRef params) |
| 33 | +{ |
| 34 | + set verbose = $get(params("Verbose"), 0) |
| 35 | + if (verbose) { |
| 36 | + write !, "Creating lock file for module "_module.DisplayName |
| 37 | + } |
| 38 | + |
| 39 | + // Create lock file object and set values for the base module |
| 40 | + set lockFile = ##class(%IPM.General.LockFile).%New() |
| 41 | + set lockFile.ModuleName = module.Name |
| 42 | + set lockFile.VersionString = module.VersionString |
| 43 | + set lockFile.Repository = module.Repository |
| 44 | + |
| 45 | + // Iterate over module dependency list |
| 46 | + // For each module: |
| 47 | + // 1. Set values for module name, version, repository (name), and direct dependencies |
| 48 | + // 2. Add repository information to the list (if the module is from a new repo) |
| 49 | + set moduleName = "" |
| 50 | + for i = 1:1:flatDependencyList.Count() { |
| 51 | + set moduleName = flatDependencyList.GetAt(i).Name |
| 52 | + set mod = ##class(%IPM.Storage.Module).NameOpen(moduleName, 0, .sc) |
| 53 | + $$$ThrowOnError(sc) |
| 54 | + |
| 55 | + if (verbose) { |
| 56 | + write !, "Adding "_mod.DisplayName_" to the lock file" |
| 57 | + } |
| 58 | + |
| 59 | + // Add the dependency to the lock file |
| 60 | + set dependencyVal = ##class(%IPM.General.LockFile.Dependency).%New() |
| 61 | + set dependencyVal.VersionString = mod.VersionString |
| 62 | + set dependencyVal.Repository = mod.Repository |
| 63 | + set depKey = "" |
| 64 | + for { |
| 65 | + set depMod = mod.Dependencies.GetNext(.depKey) |
| 66 | + quit:depKey="" |
| 67 | + $$$ThrowOnError(dependencyVal.Dependencies.SetAt(depMod.VersionString, depMod.Name)) |
| 68 | + } |
| 69 | + $$$ThrowOnError(lockFile.Dependencies.SetAt(dependencyVal, mod.Name)) |
| 70 | + |
| 71 | + // Add the dependency's repository to the lock file |
| 72 | + do AddRepositoryToLockFile(.lockFile, mod.Repository, verbose) |
| 73 | + } |
| 74 | + // Add repository for base module if not already added by a dependency |
| 75 | + // Skip undefined repositories as that means the module was installed via the zpm "load" command |
| 76 | + if (module.Repository '= "") { |
| 77 | + do AddRepositoryToLockFile(.lockFile, module.Repository, verbose) |
| 78 | + } |
| 79 | + |
| 80 | + $$$ThrowOnError(lockFile.%JSONExportToStream(.lockFileJSON, "LockFileMapping")) |
| 81 | + |
| 82 | + set lockFilePath = module.Root_..#LockFileName |
| 83 | + if (verbose) { |
| 84 | + write !, "Saving lock file for "_module.Name_" to: "_lockFilePath |
| 85 | + } |
| 86 | + set file = ##class(%Stream.FileCharacter).%New() |
| 87 | + $$$ThrowOnError(file.LinkToFile(lockFilePath)) |
| 88 | + $$$ThrowOnError(file.CopyFrom(lockFileJSON)) |
| 89 | + $$$ThrowOnError(file.%Save()) |
| 90 | +} |
| 91 | + |
| 92 | +/// Adds a module's repository to the lock file |
| 93 | +ClassMethod AddRepositoryToLockFile( |
| 94 | + ByRef lockFile As %IPM.General.LockFile, |
| 95 | + repositoryName As %String, |
| 96 | + verbose As %Boolean = 0) [ Internal ] |
| 97 | +{ |
| 98 | + set repo = ..GetRepo(repositoryName) |
| 99 | + if 'lockFile.Repositories.IsDefined(repo.Name) { |
| 100 | + if (verbose) { |
| 101 | + write !, "Adding new repository to the lock file: "_repo.Name |
| 102 | + } |
| 103 | + $$$ThrowOnError(lockFile.Repositories.SetAt(repo, repo.Name)) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/// Returns a repo based on its name |
| 108 | +ClassMethod GetRepo(repoName As %String) As %IPM.Repo.Definition [ Internal ] |
| 109 | +{ |
| 110 | + if ##class(%IPM.Repo.Definition).ServerDefinitionKeyExists(repoName, .id) { |
| 111 | + set repo = ##class(%IPM.Repo.Definition).%OpenId(id, , .sc) |
| 112 | + $$$ThrowOnError(sc) |
| 113 | + return repo |
| 114 | + } else { |
| 115 | + $$$ThrowStatus($$$ERROR($$$GeneralError,$$$FormatText("Tried getting repo ""%1"" but none found with that name", repoName))) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +} |
0 commit comments