Skip to content

Instantly share code, notes, and snippets.

@TrainerGuy22
Created June 25, 2014 01:49
Show Gist options
  • Select an option

  • Save TrainerGuy22/3ef836d9a50bcc208b53 to your computer and use it in GitHub Desktop.

Select an option

Save TrainerGuy22/3ef836d9a50bcc208b53 to your computer and use it in GitHub Desktop.
library dartboard._plugins;
import 'dart:io';
import 'dart:collection';
import 'dart:async';
import 'package:archive/archive.dart';
import 'package:yaml/yaml.dart';
part 'protocols/github.dart';
_deletePlugin(String pluginName) {
// TODO Hook into config system, delete.
}
Plugin _getPluginForPath(String path) {
var yaml = loadYaml((new File('./plugins/' + path + '/bullseye.json')).readAsStringSync());
Plugin plugin = new Plugin(yaml['name'], yaml['directoryName'], yaml['info'], version: yaml['version'], author: yaml['author']);
if(yaml['mainFile'] != null) plugin.mainFile = yaml['mainFile'];
return plugin;
}
void _initPlugin(Plugin plugin) {
// TODO Start plugin.
}
class Plugin {
final String name;
final String directoryName;
final String info;
final String version;
final String author;
String mainFile;
Plugin(this.name, this.directoryName, this.info, {this.version, this.author, this.mainFile : 'bin/start.dart'});
}
class Prefix {
final String prefix;
final Function getPath;
final Function create;
Prefix(this.prefix, String this.getPath(String), Future this.create(Prefix, String));
}
class PluginManager {
Map<String, Prefix> _prefixes = new HashMap<String, Prefix>();
PluginManager();
addProtocol(Prefix prefix) {
_prefixes[prefix.prefix] = prefix;
}
Prefix getPrefix(String pluginName) {
return _prefixes[pluginName.split(':')[0]];
}
Future initPlugins(List<String> plugins) {
Completer completer = new Completer();
List<Future> futures = new List<Future>();
for(String pluginName in plugins) {
if(pluginName.contains(':')) {
Prefix prefix = this.getPrefix(pluginName);
if(prefix != null) {
if(new File('./plugins/' + prefix.getPath(pluginName)).existsSync()) {
Plugin plugin = _getPluginForPath(prefix.getPath(pluginName));
_initPlugin(plugin);
} else {
// Just in case we add something like HTTP, where you use : after you declare the protocol. Like Github branches, etc.
Future future = _prefixes[prefix].create(prefix, pluginName.split(':').sublist(1).join(':'));
future.then((done) {
// enable plugin
Plugin plugin = _getPluginForPath(prefix.getPath(pluginName));
_initPlugin(plugin);
})
..catchError((Error err) {
print('Plugin ' + pluginName + ' failed to download. Removing from the configuration file.');
_deletePlugin(pluginName);
print(Error.safeToString(err));
});
futures.add(future);
}
} else {
print('Plugin ' + pluginName + ' failed to download, due to a non-existant prefix. Removing from the configuration file.');
_deletePlugin(pluginName);
}
} else {
// no protocol, check file system as a fallback.
if(new File('./plugins/' + pluginName + "/bullseye.yaml").existsSync()) {
Plugin plugin = _getPluginForPath(pluginName);
_initPlugin(plugin);
} else {
print('Local plugin ' + pluginName + ' failed to load. Removing from the configuration file.');
_deletePlugin(pluginName);
}
}
}
Future.wait(futures).then((futures) => completer.complete());
return completer.future;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment