示例#1
0
        public static Type Compile(ShadowClass shadowClass)
        {
            if (Directory.Exists(Constants.TempDirectory) == false)
            {
                Directory.CreateDirectory(Constants.TempDirectory);
            }

            var filname = Path.GetRandomFileName();
            var path    = Path.Combine(Constants.TempDirectory, filname);

            var result = shadowClass.Compile(path);

            if (result.Success)
            {
                return(Assembly.LoadFrom(path).GetTypes().FirstOrDefault());
            }

            var errors = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error);

            foreach (var error in errors)
            {
                var message = error.GetMessage();

                message = message.Replace("__Typewriter.", string.Empty);
                message = message.Replace("__Code.", string.Empty);
                message = message.Replace("publicstatic", string.Empty);

                Log.Warn("Template error: {0} {1}", error.Id, message);
            }

            throw new Exception("Failed to compile template.");
        }
示例#2
0
        public static Type Compile(ProjectItem projectItem, ShadowClass shadowClass)
        {
            if (Directory.Exists(Constants.TempDirectory) == false)
            {
                Directory.CreateDirectory(Constants.TempDirectory);
            }

            var filname = Path.GetRandomFileName();
            var path    = Path.Combine(Constants.TempDirectory, filname);

            var result = shadowClass.Compile(path);

            ErrorList.Clear();

            var errors = result.Diagnostics.Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error || diagnostic.Severity == DiagnosticSeverity.Warning);

            var hasErrors = false;

            foreach (var error in errors)
            {
                var message = error.GetMessage();

                message = message.Replace("__Typewriter.", string.Empty);
                //message = message.Replace("__Code.", string.Empty);
                message = message.Replace("publicstatic", string.Empty);

                Log.Warn("Template error: {0} {1}", error.Id, message);

                if (error.Severity == DiagnosticSeverity.Error || error.IsWarningAsError)
                {
                    ErrorList.AddError(projectItem, message);
                    hasErrors = true;
                }
                else
                {
                    ErrorList.AddWarning(projectItem, message);
                }
            }

            if (hasErrors)
            {
                ErrorList.Show();
            }

            if (result.Success)
            {
                var assembly = Assembly.LoadFrom(path);
                var type     = assembly.GetType("__Typewriter.Template");

                return(type);
            }

            throw new Exception("Failed to compile template.");
        }
示例#3
0
        public static Type Compile(ShadowClass shadowClass)
        {
            if (Directory.Exists(Constants.TempDirectory) == false)
            {
                Directory.CreateDirectory(Constants.TempDirectory);
            }

            Assembly asm;
            var      cachePath = Path.Combine(Constants.TempDirectory, Sha256(shadowClass.Source) + ".bin");

            if (!File.Exists(cachePath))
            {
                foreach (Assembly assembly in shadowClass.ReferencedAssemblies)
                {
                    if (assembly.GlobalAssemblyCache)
                    {
                        continue;
                    }

                    var asmSourcePath = assembly.Location;
                    var asmDestPath   = Path.Combine(Constants.TempDirectory, Path.GetFileName(asmSourcePath));
                    try
                    {
                        //File may be in use
                        File.Copy(asmSourcePath, asmDestPath, true);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"Warn: {e}");
                        //Log.Warn(e.ToString());
                    }
                }

                var filname = Path.GetRandomFileName();
                var path    = Path.Combine(Constants.TempDirectory, filname);

                var result = shadowClass.Compile(path);

                //ErrorList.Clear();

                var errors = result.Diagnostics.Where(diagnostic =>
                                                      diagnostic.Severity == DiagnosticSeverity.Error ||
                                                      diagnostic.Severity == DiagnosticSeverity.Warning);

                foreach (var error in errors)
                {
                    var message = error.GetMessage();

                    message = message.Replace("__Typewriter.", string.Empty);
                    //message = message.Replace("__Code.", string.Empty);
                    message = message.Replace("publicstatic", string.Empty);

                    //Log.Warn("Template error: {0} {1}", error.Id, message);
                    Console.WriteLine("Template error: {0} {1}", error.Id, message);

                    if (error.Severity == DiagnosticSeverity.Error || error.IsWarningAsError)
                    {
                        Console.WriteLine("Error: {0}", message);
                        //ErrorList.AddError(projectItem, message);
                    }
                    else
                    {
                        //ErrorList.AddWarning(projectItem, message);
                        Console.WriteLine($"Warn: {message}");
                    }
                }

//            if (hasErrors)
//                ErrorList.Show();

                if (!result.Success)
                {
                    throw new InvalidOperationException("Template compilation errors.");
                }
                asm = Assembly.LoadFrom(path);
                File.Move(path, cachePath);
            }
            else
            {
                asm = Assembly.Load(File.ReadAllBytes(cachePath));
            }
            var type = asm.GetType("__Typewriter.Template");

            return(type);
        }
示例#4
0
        public static Type Compile(ProjectItem projectItem, ShadowClass shadowClass)
        {
            if (Directory.Exists(Constants.TempDirectory) == false)
            {
                Directory.CreateDirectory(Constants.TempDirectory);
            }

            foreach (Assembly assembly in shadowClass.ReferencedAssemblies)
            {
                if (assembly.GlobalAssemblyCache)
                {
                    continue;
                }

                var asmSourcePath = assembly.Location;
                var asmDestPath   = Path.Combine(Constants.TempDirectory, Path.GetFileName(asmSourcePath));

                var          sourceAssemblerInfo = AssemblyName.GetAssemblyName(asmSourcePath);
                AssemblyName destAssemblerInfo   = null;

                // Ignores the Assembler if versions are equals.
                if (File.Exists(asmDestPath))
                {
                    destAssemblerInfo = AssemblyName.GetAssemblyName(asmDestPath);
                    if (sourceAssemblerInfo.Version.CompareTo(destAssemblerInfo.Version) == 0)
                    {
                        continue;
                    }
                }

                try
                {
                    //File may be in use
                    File.Copy(asmSourcePath, asmDestPath, true);
                }
                catch (Exception e)
                {
                    Log.Warn($"{e.ToString()} \r\n  Source Version {sourceAssemblerInfo.Version}, Target Verison  {destAssemblerInfo?.Version.ToString() ?? "None"} ");
                }
            }

            var filname = Path.GetRandomFileName();
            var path    = Path.Combine(Constants.TempDirectory, filname);

            var result = shadowClass.Compile(path);

            ErrorList.Clear();

            var errors = result.Diagnostics.Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error || diagnostic.Severity == DiagnosticSeverity.Warning);

            var hasErrors = false;

            foreach (var error in errors)
            {
                var message = error.GetMessage();

                message = message.Replace("__Typewriter.", string.Empty);
                //message = message.Replace("__Code.", string.Empty);
                message = message.Replace("publicstatic", string.Empty);

                Log.Warn("Template error: {0} {1}", error.Id, message);

                if (error.Severity == DiagnosticSeverity.Error || error.IsWarningAsError)
                {
                    ErrorList.AddError(projectItem, message);
                    hasErrors = true;
                }
                else
                {
                    ErrorList.AddWarning(projectItem, message);
                }
            }

            if (hasErrors)
            {
                ErrorList.Show();
            }

            if (result.Success)
            {
                var assembly = Assembly.LoadFrom(path);
                var type     = assembly.GetType("__Typewriter.Template");

                return(type);
            }

            throw new Exception("Failed to compile template.");
        }