File: Quark.sc

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,292 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (308 lines) | stat: -rw-r--r-- 7,351 bytes parent folder | download | duplicates (3)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308

Quark {
	var <name, url, >refspec, data, <localPath, branch;
	var <changed = false, <git;

	*new { |name, refspec, url, localPath|
		var args = Quark.parseQuarkName(name, refspec, url, localPath);
		if(args.isNil, {
			Error("Quark '%' not found".format(name)).throw;
		});
		^super.new.init(*args)
	}
	*fromLocalPath { |path|
		var name, url, refspec;
		path = Quarks.quarkNameAsLocalPath(path);
		name = path.basename;
		^super.new.init(name, url, refspec, path)
	}
	*fromDirectoryEntry { |name, directoryEntry|
		var url, refspec;
		# url, refspec = directoryEntry.split($@);
		^super.new.init(name, url, refspec)
	}
	init { |argName, argUrl, argRefspec, argLocalPath|
		name = argName;
		url = argUrl;
		refspec = argRefspec;
		localPath = argLocalPath ?? {Quarks.folder +/+ name};
		if(Git.isGit(localPath), {
			git = Git(localPath);
		});
	}
	data {
		^data ?? {
			data = this.parseQuarkFile() ?? { IdentityDictionary.new }
		}
	}
	version {
		^this.data['version']
	}
	summary {
		^this.data['summary']
	}
	url {
		^url ?? {
			url = git !? { git.url }
		}
	}
	refspec {
		^refspec ?? {
			git !? { git.refspec }
		}
	}
	tags {
		^git !? { git.tags }
	}
	branch {
		^git !? { git.branch }
	}
	isDownloaded {
		^File.exists(this.localPath)
	}
	isGit {
		^git.notNil
	}
	isInstalled {
		^Quarks.pathIsInstalled(this.localPath)
	}
	isCompatible {
		var isCompatible = true;
		if(this.data.includesKey('isCompatible'), {
			{
				isCompatible = this.data['isCompatible'].value !== false
			}.try({ |error|
				("Failed to evalute quarkfile data field: isCompatible" + this).error;
				error.reportError;
			});
		});
		^isCompatible
	}
	runHook { |hook|
		hook = hook.asSymbol;
		if(this.data[hook].notNil, {
			"Run % hook".format(hook).postln;
			try {
				this.data[hook].(this.data);
			} { |e|
				"Encountered error while running hook '%' for Quark('%'). Installation or update for this Quark may not have completed correctly.".warn;
				e.throw();
			}
		});
	}

	install {
		var success = Quarks.installQuark(this);
		changed = true;
		data = nil;
		^success
	}
	update {
		if(Git.isGit(localPath), {
			data = git = refspec = nil;
			changed = true;
			this.runHook(\preUpdate);
			git = Git(localPath);
			git.pull();
			git.checkout("master");
			// force reload of data by resetting it to nil
			data = nil;
			this.runHook(\postUpdate);
			("Quark '%' updated to version: % tag: % refspec: %".format(name, this.version, this.git.tag, this.refspec)).postln;
		}, {
			("Quark" + name + "was not installed using git, cannot update.").warn;
		});
	}
	uninstall {
		this.runHook(\preUninstall);
		Quarks.uninstallQuark(this);
		this.runHook(\postUninstall);
		changed = true;
	}

	checkout {
		var rs;
		if(git.isNil, {
			git = Git(localPath);
		});
		if(this.isDownloaded.not, {
			if(this.url.isNil, {
				Error("No git url, cannot checkout quark" + this).throw;
			});
			git.clone(url);
			// get tags etc
			git.fetch();
			changed = true;
			data = nil;
		});
		if(refspec.notNil, {
			if(this.isDownloaded
				and: {git.notNil}
				and: {git.isDirty},
			{
				("% has uncommited changes, cannot checkout %".format(name, refspec ? "")).warn;
			}, {
				if(refspec == "HEAD", {
					git.pull()
				}, {
					rs = this.validateRefspec(refspec);
					if(rs.notNil, {
						git.checkout(rs)
					});
				});
			});
			changed = true;
			data = nil;
		});
	}
	validateRefspec { |refspec|
		var tag;
		if(refspec == "HEAD"
			or: {refspec.findRegexp("^[a-zA-Z0-9]{40}$").size != 0}, {
			^refspec
		});
		if(refspec.beginsWith("tags/").not, {
			tag = refspec;
			refspec = "tags/" ++ tag;
		}, {
			tag = refspec.copyToEnd(5);
		});
		if(this.tags.includesEqual(tag).not, {
			("Tag not found:" + this + tag + Char.nl + "Possible tags:" + this.tags).warn;
			^nil
		});
		^refspec
	}
	checkForUpdates {
		var tags;
		if(this.isGit, {
			tags = git.tags().sort;
			git.fetch();
			// if not already marked as changed
			if(changed.not, {
				// changed if there are new tags
				changed = git.tags().sort != tags;
			})
		});
	}

	dependencies {
		var deps = this.data['dependencies'] ?? {^[]};
		if(deps.isSequenceableCollection.not, {
			("Invalid dependencies " + this + deps).warn;
			^[]
		});
		^deps.collect({ |dep|
			var q = Quark.parseDependency(dep, this);
			if(q.isNil, {
				"% not found".format(dep).warn;
			});
			q
		}).select({ |it| it.notNil });
	}
	deepDependencies {
		// warning: everything must be checked out just to get the dependency list
		^this.prCollectDependencies(Dictionary.new).values
	}
	prCollectDependencies { |collector|
		this.dependencies.do({ |q|
			var prev = collector[q.name];
			if(prev.notNil, {
				if(prev.refspec != q.refspec, {
					("% requires % but % is already registered as a dependency".format(this, q, prev)).warn;
				});
			}, {
				q.checkout();
				collector[q.name] = q;
				q.prCollectDependencies(collector);
			});
		});
		^collector
	}
	*parseDependency { |dep, forQuark|
		// parse a dependency specifier and return a Quark
		// (1) string
		if(dep.isString, {
			^Quark.prMakeDep(*dep.split($@))
		});
		// (2) name -> version
		if(dep.isKindOf(Association), {
			^Quark.prMakeDep(dep.key.asString)
		});
		// (3) [name, version, url]
		// url is svn repo and should be ignored now
		if(dep.isSequenceableCollection, {
			^Quark.prMakeDep(dep.first)
		});
		("Cannot parse dependency:" + dep + (forQuark ? "")).error;
		^nil
	}
	*prMakeDep { |name, refspec|
		// protected make dependency
		// if not found then posts error and returns nil
		var args = Quark.parseQuarkName(name, refspec);
		if(args.isNil, {
			^nil
		});
		^super.new.init(*args)
	}
	*parseQuarkName { |name, refspec, url, localPath|
		// determine which quark the string 'name' refers to
		// name is one of: quarkname, url, localPath
		// returns: [name, url, refspec, localPath]
		var directoryEntry;
		if(name.contains("://"), {
			^[PathName(name).fileNameWithoutExtension(), name, refspec, localPath]
		});
		if(Quarks.isPath(name), {
			// if not provided then url can be determined later by the Quark
			^[name.basename, url, refspec, name]
		});
		// search Quarks folders
		(Quarks.additionalFolders ++ [Quarks.folder]).do({ |f|
			var lp = localPath ?? {f +/+ name};
			if(File.existsCaseSensitive(lp), {
				^[name, url, refspec, lp]
			});
		});
		// lookup url in directory
		directoryEntry = Quarks.findQuarkURL(name);
		if(directoryEntry.notNil, {
			directoryEntry = directoryEntry.split($@);
			^[name, url ? directoryEntry[0], refspec ? directoryEntry[1], localPath]
		});
		// not found
		^nil
	}
	parseQuarkFile {
		var qfp = this.localPath +/+ name ++ ".quark",
			result;
		if(File.exists(qfp), {
			result = thisProcess.interpreter.compileFile(qfp).value;
			if(result.isNil, {
				("Failed to parse %".format(qfp)).warn;
			});
		});
		^result
	}

	printOn { arg stream;
		var v = this.version ? this.refspec;
		stream << "Quark: " << name;
		if(v.notNil,{ stream << "[" << v << "]" });
	}
	help {
		var p = this.data['schelp'];
		// explicit pointer to a help file in quark data
		if(p.notNil, {
			^HelpBrowser.openHelpFor(p);
		});
		// old html help doc
		p = this.data['helpdoc'];
		if(p.notNil, {
			(this.localPath +/+ p).openOS;
			// ^HelpBrowser.goTo(URI.fromLocalPath(this.localPath +/+ p).asString);
		});
		HelpBrowser.openBrowsePage("Quarks>" ++ name);
	}
}