Exemple #1
0
        public static int ProcessCSXFile(string path)
        {
            var engin     = Program.DI.GetService <Engine>();
            var filePath2 = Path.Combine(Environment.CurrentDirectory, path);

            if (File.Exists(filePath2))
            {
                var templateContent = File.ReadAllText(filePath2);

                if (TryFindProjectFile(filePath2, out string projectFile) == false)
                {
                    throw new ProjectNotFoundException("Current work directory is not in a project directory");
                }
                var host = Program.DI.GetService <ITextTemplatingEngineHost>();
                // Console.WriteLine("proj>>"+projectFile);
                // Resolve metadata
                IMetadataResolveable resolver     = Program.DI.GetService <IMetadataResolveable>();
                ProjectMetadata      projMetadata = resolver.ReadProject(projectFile);
                //Console.WriteLine("proj.meta>>"+projMetadata.OutputPath);

                var scriptCode = File.ReadAllText(filePath2);
                var result     = engin.ProcessCSXTemplate(templateContent, filePath2,
                                                          resolver, projMetadata);

                // var outputPath = Path.Combine(
                //     Path.GetDirectoryName(filePath),
                //     $"{Path.GetFileNameWithoutExtension(filePath)}{host.FileExtension}");
                // File.WriteAllText(outputPath, result, host.Encoding);
            }

            return(0);
        }
Exemple #2
0
        public string ProcessCSXTemplate(string content, string filePath,
                                         IMetadataResolveable resolver, ProjectMetadata projmeta)
        {
            var references = new List <MetadataReference>();

            // project references
            references.AddRange(resolver.ResolveMetadataReference());
            // assembly instruction
            output = projmeta.OutputPath;
            var opt =
                ScriptOptions.Default

                .WithReferences(_host.StandardAssemblyReferences)
                .WithMetadataResolver(ScriptMetadataResolver.Default.WithSearchPaths(RuntimeEnvironment.GetRuntimeDirectory()))
                //   .AddImports(_host.StandardImports) //no standard imports
                .WithFilePath(filePath);

            var refFiltd = references.Where((item, index) =>
                                            !_host.StandardAssemblyReferences.Any(
                                                x => item.Display.Contains(x + ".dll"))
                                            //dont load dlls that re already included in standard
                                            ).ToList();

            // foreach (var item in rr)
            // { Console.WriteLine("ref:" + item.Display); }
            // opt = opt.WithReferences(rr).AddImports(_host.StandardImports); //system object not defined
            foreach (var dep in refFiltd)
            {
                // Logger.Debug("Adding reference to a runtime dependency => " + runtimeDependency);
                opt = opt.AddReferences(MetadataReference.CreateFromFile(dep.Display));
            }
            var loader = new InteractiveAssemblyLoader();

            try
            {
                CSharpScript.EvaluateAsync(content, options: opt, loader)
                .ContinueWith(s => s.Result).Wait();
            }
            catch (CompilationErrorException ex)
            {
                if (ex.Message.Contains(")"))
                {
                    var m = ex.Message.Split(")");
                    ttConsole.WriteError(m[0] + ")");
                    ttConsole.WriteError(m[1]);
                }
                else
                {
                    ttConsole.WriteError(ex.Message);
                }
                ttConsole.WriteNormal("");
                ttConsole.WriteError(ex.StackTrace);
            }
            return("");
        }
 public RoslynCompilationService(ITextTemplatingEngineHost host, IMetadataResolveable resolver)
 {
     _host     = host;
     _resolver = resolver;
 }