private void _ReadFrameworkAssemblies(XElement element, ScriptSequence scriptSequence) { scriptSequence.FrameworkAssemblies = new HashSet<string>( element .Element("FrameworkAssemblies") .Elements("Assembly") .Select(x => x.Attribute("name").Value)); }
private void _ReadSourceFiles(XElement element, ScriptSequence scriptSequence) { string sourcePath = Path.Combine(scriptSequence.SourceDirectory, scriptSequence.Name); scriptSequence.SourceFiles.Clear(); IEnumerable<string> fileNames = element .Element("SourceFiles") .Elements("SourceFile") .Select(x => x.Attribute("name").Value); foreach(string fileName in fileNames) { string filePath = Path.Combine(sourcePath, fileName); scriptSequence.SourceFiles.Add(SourceFile.Load(filePath)); } }
private XElement _WriteSourceFiles(ScriptSequence sequence) { // Make sure source directory exists. string sourcePath = Path.Combine(sequence.SourceDirectory, sequence.Name); Helper.EnsureDirectory(sourcePath); // Write the source files and their references. return new XElement("SourceFiles", sequence.SourceFiles.Select(x => { x.Save(sourcePath); return new XElement("SourceFile", new XAttribute("name", x.Name)); })); }
protected ScriptSequence(string language, ScriptSequence original) { Length = Forever; //SourceFiles = new List<SourceFile>(original.SourceFiles); //SourceFiles = new SourceFileCollection(); //SourceFiles.Files.AddRange(original.SourceFiles.Files); _sourceFiles = new SourceFileCollection(); AddSourceFiles(original.GetAllSourceFiles()); FrameworkAssemblies = new HashSet <string>(original.FrameworkAssemblies); ExternalAssemblies = new HashSet <string>(original.ExternalAssemblies); Language = language; ClassName = original.ClassName; }
protected ScriptSequence(string language, ScriptSequence original) { Length = Forever; //SourceFiles = new List<SourceFile>(original.SourceFiles); //SourceFiles = new SourceFileCollection(); //SourceFiles.Files.AddRange(original.SourceFiles.Files); _sourceFiles = new SourceFileCollection(); AddSourceFiles(original.GetAllSourceFiles()); FrameworkAssemblies = new HashSet<string>(original.FrameworkAssemblies); ExternalAssemblies = new HashSet<string>(original.ExternalAssemblies); Language = language; ClassName = original.ClassName; }
public IUserScriptHost GenerateScript(ScriptSequence sequence) { List<string> files = new List<string>(); Assembly assembly; string nameSpace = UserScriptNamespace; try { // Emit the T4 template to be compiled into the assembly. string fileName = Path.GetTempFileName(); string fileContents = sequence.Language.FrameworkGenerator.Generate(nameSpace, sequence.ClassName); File.WriteAllText(fileName, fileContents); files.Add(fileName); // Add the user's source files. foreach(SourceFile sourceFile in sequence.SourceFiles) { fileName = Path.Combine(Path.GetTempPath(), sourceFile.Name); File.WriteAllText(fileName, sourceFile.Contents); files.Add(fileName); } // Compile the sources. assembly = _Compile(files.ToArray(), _GetReferencedAssemblies(sequence), sequence.Language); } finally { // Delete the temp files. foreach(string tempFile in files) { File.Delete(tempFile); } } if(assembly != null) { // Get the generated type. Type type = assembly.GetType(string.Format("{0}.{1}", nameSpace, sequence.ClassName)); if(type != null) { // Create and return an instance. return (IUserScriptHost)Activator.CreateInstance(type); } } return null; }
private void _ReadLanguage(XElement element, ScriptSequence scriptSequence) { scriptSequence.Language = element.Element("Language").Value; }
private void _ReadClassName(XElement element, ScriptSequence scriptSequence) { scriptSequence.ClassName = element.Element("ClassName").Value; }
public IUserScriptHost GenerateScript(ScriptSequence sequence) { List<string> files = new List<string>(); Assembly assembly; string nameSpace = UserScriptNamespace; try { // Emit the T4 template to be compiled into the assembly. string fileName = Path.GetTempFileName(); string fileContents = sequence.Language.FrameworkGenerator.Generate(nameSpace, sequence.ClassName); File.WriteAllText(fileName, fileContents); files.Add(fileName); // Add the user's source files. foreach (SourceFile sourceFile in sequence.SourceFiles) { fileName = Path.Combine(Path.GetTempPath(), sourceFile.Name); File.WriteAllText(fileName, sourceFile.Contents); files.Add(fileName); } var assemblyReferences = _GetReferencedAssemblies(sequence); HashSet<string> uniqueAssemblyReferences = new HashSet<string>(assemblyReferences); // Remove .NET framework and GAC assemblies. uniqueAssemblyReferences.RemoveWhere( x => x.StartsWith(Environment.GetFolderPath(Environment.SpecialFolder.Windows), StringComparison.OrdinalIgnoreCase)); //if (createProject) CreateWorkingProject(files.ToArray(), uniqueAssemblyReferences, sequence); // Compile the sources. assembly = _Compile(files.ToArray(), uniqueAssemblyReferences, sequence); } finally { // Delete the temp files. foreach (string tempFile in files) { File.Delete(tempFile); } } if (assembly != null) { // Get the generated type. Type type = assembly.GetType(string.Format("{0}.{1}", nameSpace, sequence.ClassName)); if (type != null) { // Create and return an instance. return (IUserScriptHost)Activator.CreateInstance(type); } } return null; }
private IEnumerable<string> _GetReferencedAssemblies(ScriptSequence sequence) { List<string> assemblyReferences = new List<string>(); assemblyReferences.AddRange(sequence.FrameworkAssemblies); assemblyReferences.AddRange(sequence.ExternalAssemblies); assemblyReferences.AddRange(_GetEffectParameterAssemblies()); assemblyReferences.AddRange(_standardReferences); assemblyReferences.Add(VixenSystem.AssemblyFileName); return assemblyReferences; }
/// <returns>The file name of the compiled assembly.</returns> private Assembly _Compile(string[] files, HashSet<string> uniqueAssemblyReferences, ScriptSequence sequence) { IScriptModuleInstance language = sequence.Language; // Assembly references come in two flavors: // 1. Framework assemblies -- need only the file name. // 2. Other assemblies -- need the qualified file name. _errors.Clear(); ICompilerParameters compilerParameters = new ScriptCompilerParameters { GenerateInMemory = true, //IncludeDebugInformation = true }; compilerParameters.ReferencedAssemblies.AddRange(uniqueAssemblyReferences.ToArray()); ICompilerResults results = language.CodeProvider.CompileAssemblyFromFile(compilerParameters, files); // Get any errors. foreach (ICompilerError error in results.Errors) { _errors.Add(string.Format("{0} [{1}]: {2}", Path.GetFileName(error.FileName), error.Line, error.ErrorText)); } return results.HasErrors ? null : results.CompiledAssembly; }
private void CreateWorkingProject(string[] files, HashSet<string> assemblyReferences, ScriptSequence sequence) { new ScriptProjectGenerator(files, assemblyReferences, sequence).Generate(); }
private XElement _WriteLanguage(ScriptSequence sequence) { return new XElement("Language", sequence.Language); }
private XElement _WriteFrameworkAssemblies(ScriptSequence sequence) { return new XElement("FrameworkAssemblies", sequence.FrameworkAssemblies.Select(x => new XElement("Assembly", new XAttribute("name", x)))); }
private XElement _WriteExternalAssemblies(ScriptSequence sequence) { return new XElement("ExternalAssemblies", sequence.ExternalAssemblies.Select(x => new XElement("Assembly", new XAttribute("name", x)))); }
private XElement _WriteClassName(ScriptSequence sequence) { return new XElement("ClassName", sequence.ClassName); }