protected void SaveCommand(CommandInfo item) { string ext = item.Language ?? ".py"; if (!runtime.TryGetEngineByFileExtension(ext, out var engine)) { engine = runtime.GetEngine(item.Language); ext = engine.Setup.FileExtensions.First(); } if (!ext.StartsWith(".")) { ext = '.' + ext; } string path = Path.Combine(directory, Path.GetFileName(item.Name) + ext); using (var writer = File.CreateText(path)) { var xmlSettings = new XmlWriterSettings() { Indent = true, OmitXmlDeclaration = true }; using (XmlWriter xml = XmlWriter.Create(new CommentWriter(writer), xmlSettings)) { xml.WriteStartElement("command"); xml.WriteElementString("description", item.Description); foreach (var p in item.Parameters) { xml.WriteStartElement("param"); xml.WriteAttributeString("name", p.Name); xml.WriteAttributeString("type", p.Type); xml.WriteString(p.Description); xml.WriteEndElement(); } xml.WriteEndElement(); xml.Flush(); } writer.WriteLine(); writer.WriteLine(); using (var reader = new StringReader(item.ScriptCode.TrimEnd())) { string line; while (null != (line = reader.ReadLine())) { writer.WriteLine(line); } } } }
public void LoadCommands() { Logger.Info("Loading commands."); Dictionary <string, ScriptCommand> newCommands = new Dictionary <string, ScriptCommand>(StringComparer.InvariantCultureIgnoreCase); foreach (CommandInfo info in CommandManager.GetItems()) { if (newCommands.ContainsKey(info.Name)) { Logger.Warn("Duplicated command name '{0}' detected. Only the first will be loaded.", info.Name); continue; } try { if (!runtime.TryGetEngineByFileExtension(info.Language, out var engine)) { engine = runtime.GetEngine(info.Language); } ScriptSource source = engine.CreateScriptSourceFromString(info.ScriptCode, Microsoft.Scripting.SourceCodeKind.Statements); CompiledCode code = source.Compile(); ScriptScope scope = engine.CreateScope(); scope.SetVariable("plugins", Plugins); foreach (var param in info.Parameters) { scope.SetVariable(param.Name, null); } if (!string.IsNullOrEmpty(info.ScriptFile)) { engine.ExecuteFile(info.ScriptFile, scope); } newCommands.Add(info.Name, new ScriptCommand(code, scope)); Logger.Debug("Added command '{0}'.", info.Name); } catch (Exception x) { Logger.Error(x, "Error loading command '{0}'.", info.Name); } } commands = newCommands; }
/// <summary> /// 指定したディレクトリ以下のスクリプトを読み込む /// </summary> /// <param name="rootDir"></param> /// <param name="scriptExecutionCallback"></param> private void LoadScriptsFromDirectory(String rootDir, ScriptExecutionCallback scriptExecutionCallback) { if (Directory.Exists(rootDir)) { foreach (var path in Directory.GetFiles(rootDir, "*.*", SearchOption.AllDirectories)) { ScriptEngine engine; if (_scriptRuntime.TryGetEngineByFileExtension(Path.GetExtension(path), out engine)) { try { String expression = File.ReadAllText(path, Encoding.UTF8); ScriptScope scriptScope = PrepareScriptScopeByPath(path); engine.Execute(expression, scriptScope); scriptExecutionCallback(path, null); } catch (Exception ex) { scriptExecutionCallback(path, ex); } } } } }