File: Quarks.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 (441 lines) | stat: -rw-r--r-- 10,856 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441

Quarks {

	classvar
		<folder,
		<>additionalFolders,
		directory,
		<>directoryUrl="https://github.com/supercollider-quarks/quarks.git",
		fetched=false,
		cache,
		regex,
		installedPaths;

	*install { |name, refspec|
		var path, quark;
		if(Quarks.isPath(name).not, {
			quark = Quark(name, refspec);
			this.installQuark(quark);
			^quark
		}, {
			// local path / ~/ ./
			path = this.asAbsolutePath(name);
			if(File.exists(path).not, {
				("Quarks-install: path does not exist" + path).error;
				^nil
			});
			if(File.type(path) != \directory, {
				("Quarks-install: path is not a directory" + path).error;
				^nil
			});
			quark = Quark.fromLocalPath(path);
			this.installQuark(quark);
			^quark
		});
	}
	*uninstall { |name|
		// by quark name or local path
		this.installed.do { |q|
			if(q.name == name, {
				this.uninstallQuark(q);
			});
		};
	}
	*uninstallQuark { |quark|
		quark.runHook(\preUninstall);
		this.unlink(quark.localPath);
		quark.runHook(\postUninstall);
		this.clearCache;
	}
	*clear {
		this.installed.do({ |quark|
			LanguageConfig.removeIncludePath(quark.localPath);
		});
		LanguageConfig.store(LanguageConfig.currentPath);
		this.clearCache;
	}
	*clearCache {
		cache = Dictionary.new;
		installedPaths = nil;
	}
	*load { |path, done|
		// install a list of quarks from a text file
		var file, line, dir,re, nameRe;
		var match, localPath, url, name, refspec, q;
		// localPath=url[@refspec]
		re = "^([^=]+)=?([^@]+)@?(.*)$";
		// quarkname[@refspec]
		nameRe = "^([^@]+)@?(.*)$";
		path = this.asAbsolutePath(path);
		dir = path.dirname;
		if(File.exists(path).not, {
			^("Quark set file does not exist:" + path).error;
		});
		this.clear();
		forkIfNeeded({
			file = File.open(path, "r");
			while({
				line = file.getLine();
				line.notNil
			}, {
				0.05.wait;
				// resolve any paths relative to the quark file
				match = line.findRegexp(re);
				if(match.size > 0, {
					localPath = match[1][1];
					name = localPath.basename;
					localPath = this.asAbsolutePath(localPath, dir);
					url = match[2][1];
					refspec = match[3];
					if(refspec.notNil, {
						refspec = refspec[1];
					});
					q = Quark(name, refspec, url, localPath);
					q.install();
				}, {
					match = line.findRegexp(nameRe);
					if(match.size > 0, {
						name = match[1][1];
						refspec = match[2];
						if(refspec.notNil, {
							refspec = refspec[1];
						});
						if(Quarks.isPath(name), {
							this.install(this.asAbsolutePath(name, dir), refspec);
						}, {
							this.install(name, refspec);
						});
					}, {
						"Unreadable line: %".format(line).error;
					});
				});
			});
			this.clearCache();
			done.value();
		}, clock: AppClock);
	}
	*save { |path|
		// save currently installed quarks to a text file
		// localPath=url[@refspec]
		var file, dir, lines;
		path = this.asAbsolutePath(path);
		dir = path.dirname;
		lines = this.installed.collect({ |quark|
			var localPath, url="", refspec;
			localPath = this.asRelativePath(quark.localPath, dir);
			if(Git.isGit(quark.localPath), {
				url = quark.url;
				if(Git(quark.localPath).isDirty, {
					("Working copy is dirty" + quark.localPath).warn;
				}, {
					refspec = quark.refspec;
				});
			});
			if(refspec.notNil, {
				"%=%@%".format(localPath, url, refspec)
			}, {
				"%=%".format(localPath, url)
			});
		});
		file = File.open(path, "w");
		lines = lines.join(Char.nl);
		file.write(lines);
		file.close();
		^lines
	}
	*update { |name|
		// by quark name or by supplying a local path
		// resolving / ~/ ./
		// is it a git
		var quark, localPath;
		if(name.isNil, {
			("Missing required argument: quark name").throw;
		});
		localPath = this.quarkNameAsLocalPath(name);
		if(Git.isGit(localPath), {
			// Quark.update will run preUpdate and postUpdate hooks
			Quark.fromLocalPath(localPath).update();
		}, {
			("Quark" + name + "was not installed using git, cannot update.").warn;
		});
	}
	*installed {
		^LanguageConfig.includePaths
			.collect(Quark.fromLocalPath(_))
	}
	*installedPaths {
		^installedPaths ?? {
			installedPaths = LanguageConfig.includePaths.collect({ |apath|
				apath.withoutTrailingSlash
			});
		}
	}
	*isInstalled { |name|
		^this.installedPaths.any({ |path|
			path.endsWith(name)
		})
	}
	*pathIsInstalled { |path|
		^this.installedPaths.includesEqual(path.withoutTrailingSlash)
	}
	*openFolder {
		this.folder.openOS;
	}
	*gui {
		^QuarksGui.new
	}

	*installQuark { |quark|
		var
			deps,
			incompatible = { arg name;
				(quark.name
					+ "reports an incompatibility with this SuperCollider version"
					+ "or with other already installed quarks."
				).postln;
				false
			},
			prev = this.installed.detect({ |q| q.name == quark.name });

		if(prev.notNil and: {prev.localPath != quark.localPath}, {
			("A version of % is already installed at %".format(quark, prev.localPath)).error;
			^false
		});

		"Installing %".format(quark.name).postln;

		quark.checkout();
		if(quark.isCompatible().not, {
			^incompatible.value(quark.name);
		});
		quark.dependencies.do { |dep|
			var ok = dep.install();
			if(ok.not, {
				("Failed to install" + quark.name).error;
				^false
			});
		};
		quark.runHook(\preInstall);
		this.link(quark.localPath);
		quark.runHook(\postInstall);
		(quark.name + "installed").postln;
		this.clearCache();
		^true
	}

	*link { |path|
		path = path.withoutTrailingSlash;
		if(LanguageConfig.includePaths.includesEqual(path).not, {
			path.debug("Adding path");
			LanguageConfig.addIncludePath(path);
			LanguageConfig.store(LanguageConfig.currentPath);
			installedPaths = installedPaths.add(path.withoutTrailingSlash);
			^true
		});
		^false
	}
	*unlink { |path|
		path = path.withoutTrailingSlash;
		if(LanguageConfig.includePaths.includesEqual(path), {
			path.debug("Removing path");
			LanguageConfig.removeIncludePath(path);
			LanguageConfig.store(LanguageConfig.currentPath);
			installedPaths.remove(path.withoutTrailingSlash);
			^true
		});
		^false
	}

	*initClass {
		folder = Platform.userAppSupportDir +/+ "downloaded-quarks";
		additionalFolders = additionalFolders ? [];
		if(File.exists(folder).not, {
			folder.mkdir();
		});
		this.clearCache;
		if(thisProcess.platform.name !== 'windows', {
			regex = (
				isPath: "^[~\\.]?/",
				isAbsolutePath: "^/"
			);
		}, {
			regex = (
				isPath: "\\\\|/",
				isAbsolutePath: "^[A-Za-z]:(?:\\\\|/)",
				isURL: "://"
			);
		});
	}
	*at { |name|
		var q;
		^cache[name] ?? {
			q = Quark(name);
			cache.put(name, q);
			q
		}
	}
	*findQuarkURL { |name|
		^this.directory[name]
	}
	*directory {
		// just urls
		if(directory.isNil, {
			this.fetchDirectory(false);
		});
		^directory
	}
	*addFolder { |path|
		// in addition to the default downloaded-quarks
		// add folders that contain quarks to offer on the menu for installation
		// these may be private quarks, cloned working copies or folders where you
		// have manually downloaded quarks
		additionalFolders = additionalFolders.add(path.standardizePath);
	}
	*all {
		// all known quarks
		var all = Dictionary.new, f;
		// those in index
		this.directory.keysValuesDo({ |name, quarkURL|
			var q = Quark.fromDirectoryEntry(name, quarkURL);
			all[q.localPath] = q;
		});

		f = { |path|
			// \directory or \not_found, but not a file
			var downloadedQuarks = "downloaded-quarks" +/+ "quarks";
			if(File.type(path) !== \regular, {
				path = path.withoutTrailingSlash;
				// ignore the directory
				if(path.endsWith(downloadedQuarks).not, {
					all[path] = Quark.fromLocalPath(path);
				});
			});
		};
		(Quarks.folder +/+ "*").pathMatch.do(f);
		additionalFolders.do({ |folder|
			(folder +/+ "*").pathMatch.do(f);
		});
		LanguageConfig.includePaths.do(f);
		^all.atAll(all.order)
	}
	*fetchDirectory { |force=true|
		// will only pull every 15 minutes unless force is true
		var dirTxtPath = Quarks.folder +/+ "quarks" +/+ "directory.txt",
			fetch = true;

		if(File.exists(dirTxtPath), {
			fetch = force // or: fetched.not
		});
		{
			if(fetch, { this.prFetchDirectory });
			this.prReadDirectoryFile(dirTxtPath);
		}.try({ arg err;
			// 'err' should, by definition, be an Exception
			// and all Exceptions should respond to 'errorString'
			// but "throw during error handling" is really really bad,
			// so, be doubly sure it won't happen
			var text = err.tryPerform(\errorString) ?? { text = err.asString };
			("Failed to read quarks directory listing: %\n%".format(if(fetch, directoryUrl, dirTxtPath), text)).error;
			if(fetch, {
				// if fetch failed, try read from cache
				if(File.exists(dirTxtPath), {
					this.prReadDirectoryFile(dirTxtPath);
				});
			}, {
				// if read from cache failed, try fetching
				if(fetch, { this.prFetchDirectory });
				this.prReadDirectoryFile(dirTxtPath);
			});
		});
	}
	*checkForUpdates { |done, quarkAction|
		forkIfNeeded({
			this.all.do { arg quark;
				if(quark.isGit, {
					quarkAction.value(quark);
					quark.checkForUpdates();
				});
				0.05.wait;
			};
			done.value();
		}, AppClock);
	}
	*prReadDirectoryFile { |dirTxtPath|
		var file;
		directory = Dictionary.new;
		file = File.open(dirTxtPath, "r");
		{
			var line, kv;
			while({
				line = file.getLine();
				line.notNil;
			}, {
				kv = line.split($=);
				directory[kv[0]] = kv[1];
			});
		}.protect({
			file.close;
		});
	}
	*prFetchDirectory {
		// clone or pull
		// unless existing non-git copy exists
		var git, localPath;
		if(fetched, { ^this });
		("Fetching" + directoryUrl).debug;
		localPath = Quarks.folder +/+ "quarks";
		git = Git(localPath);
		if(Git.isGit(localPath), {
			git.pull
		}, {
			// check for manually downloaded copy
			// that exists but is not a git repo
			if(File.exists(localPath +/+ "directory.txt").not, {
				git.clone(directoryUrl)
			})
		});
		fetched = true;
	}
	*quarkNameAsLocalPath { |name|
		^if(this.isPath(name), {
			this.asAbsolutePath(name)
		}, {
			Quark(name).localPath
		});
	}
	*isPath { |string|
		if(thisProcess.platform.name !== 'windows', {
			^string.findRegexp(regex.isPath).size != 0
		}, {
			^(string.findRegexp(regex.isPath).size != 0).and(string.findRegexp(regex.isURL).size == 0)
		});
	}
	*asAbsolutePath { |path, relativeTo|
		^if(path.findRegexp(regex.isAbsolutePath).size != 0, {
			path
		}, {
			if(path.at(0) == $~, {
				^path.standardizePath
			});
			// scroot is the cwd at startup
			if(path.beginsWith("." ++ Platform.pathSeparator), {
				^(relativeTo ?? {PathName.scroot}) +/+ path.copyToEnd(2)
			});
			(relativeTo ?? {PathName.scroot}) +/+ path
		})
	}
	*asRelativePath { |path, relativeToDir|
		var d;
		if(path.beginsWith(relativeToDir), {
			^"." ++ path.copyToEnd(relativeToDir.size)
		});
		d = Platform.userHomeDir;
		// ~/path if in home
		if(path.beginsWith(d), {
			^"~" ++ path.copyToEnd(d.size)
		});
		^path
	}
	// quarks fetch all available quark specs
	// directory.json
}