-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathlockfile.das
More file actions
122 lines (104 loc) · 3.2 KB
/
Copy pathlockfile.das
File metadata and controls
122 lines (104 loc) · 3.2 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
options gen2
options rtti
options indenting = 4
options no_global_variables = false
options persistent_heap
module lockfile shared public
require daslib/fio
require strings
require daslib/json
require daslib/json_boost
struct PackageEntry {
name : string
source : string
version : string // requested version (e.g. "1.0" from install foo@1.0)
tag : string // resolved git tag (e.g. "v1.0" after resolve())
branch : string // resolved git branch (if branch-based)
root : bool = true
local : bool = false
global : bool = false // installed in das_root/modules/ (shared across projects)
}
struct LockFile {
sdk_version : string
packages : array<PackageEntry>
}
let LOCK_FILE_NAME = "daspkg.lock"
let GLOBAL_LOCK_FILE_NAME = ".daspkg_global.lock"
def lock_file_path(project_root : string) : string {
return "{project_root}/{LOCK_FILE_NAME}"
}
def global_lock_file_path() : string {
return "{get_das_root()}/modules/{GLOBAL_LOCK_FILE_NAME}"
}
// find index of package by name, -1 if not found
def find_package(lf : LockFile; name : string) : int {
for (i in range(length(lf.packages))) {
if (lf.packages[i].name == name) return i
}
return -1
}
def has_package(lf : LockFile; name : string) : bool {
return find_package(lf, name) >= 0
}
def get_package(lf : LockFile; name : string) : PackageEntry {
let idx = find_package(lf, name)
if (idx >= 0) return lf.packages[idx]
return PackageEntry()
}
// add or replace a package entry (appends if new, replaces in-place if existing)
def set_package(var lf : LockFile; entry : PackageEntry) {
let idx = find_package(lf, entry.name)
if (idx >= 0) {
lf.packages[idx] = entry
} else {
lf.packages |> push_clone(entry)
}
}
def remove_package(var lf : LockFile; name : string) {
let idx = find_package(lf, name)
if (idx >= 0) {
lf.packages |> erase(idx)
}
}
def read_lock_file_at(path : string; var lf : LockFile) : bool {
var ok = false
fopen(path, "rb") $(f) {
if (f == null) return
let text = fread(f)
var error : string
var parsed = read_json(text, error)
if (parsed == null) {
to_log(LOG_ERROR, "Error reading {path}: {error}\n")
return
}
lf = from_JV(parsed, type<LockFile>)
ok = true
}
return ok
}
def write_lock_file_at(path : string; lf : LockFile) : bool {
let text = sprint_json(lf, true)
var ok = false
fopen(path, "wb") $(f) {
if (f == null) {
to_log(LOG_ERROR, "Error: cannot write {path}\n")
return
}
fwrite(f, text)
fwrite(f, "\n")
ok = true
}
return ok
}
def read_lock_file(project_root : string; var lf : LockFile) : bool {
return read_lock_file_at(lock_file_path(project_root), lf)
}
def write_lock_file(project_root : string; lf : LockFile) : bool {
return write_lock_file_at(lock_file_path(project_root), lf)
}
def read_global_lock_file(var lf : LockFile) : bool {
return read_lock_file_at(global_lock_file_path(), lf)
}
def write_global_lock_file(lf : LockFile) : bool {
return write_lock_file_at(global_lock_file_path(), lf)
}