示例#1
0
        public override Assembly Compile(string path, string outPath, bool cache)
        {
            Assembly asm = null;

            try
            {
                // Get from cache?
                if (this.ExistsAndUpToDate(path, outPath) && cache)
                {
                    return(Assembly.LoadFrom(outPath));
                }

                // Precompile script to a temp file
                var precompiled = this.PreCompile(File.ReadAllText(path));
                var tmp         = Path.GetTempFileName() + Path.GetExtension(path);
                File.WriteAllText(tmp, precompiled);

#if DEBUG
                var debug = true;
#else
                var debug = false;
#endif

                // Compile
                // Mono needs the settings to not treat harmless warnings as
                // errors (like a missing await in an async Task) and to not
                // spam us with warnings.
                CSScript.GlobalSettings.UseAlternativeCompiler = Path.Combine(Directory.GetCurrentDirectory(), "lib/CSSCodeProvider.dll");
                asm = CSScript.LoadWithConfig(tmp, null, debug, CSScript.GlobalSettings, "");

                this.SaveAssembly(asm, outPath);
            }
            catch (csscript.CompilerException ex)
            {
                var errors = ex.Data["Errors"] as System.CodeDom.Compiler.CompilerErrorCollection;
                var newExs = new CompilerErrorsException();

                foreach (System.CodeDom.Compiler.CompilerError err in errors)
                {
                    var newEx = new CompilerError(path, err.Line, err.Column, err.ErrorText, err.IsWarning);
                    newExs.Errors.Add(newEx);
                }

                throw newExs;
            }
            catch (UnauthorizedAccessException)
            {
                // Thrown if file can't be copied. Happens if script was
                // initially loaded from cache.
                // TODO: Also thrown if CS-Script can't create the file,
                //   ie under Linux, if /tmp/CSSCRIPT isn't writeable.
                //   Handle that somehow?
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
            }

            return(asm);
        }
示例#2
0
		public override Assembly Compile(string path, string outPath, bool cache)
		{
			Assembly asm = null;
			try
			{
				if (this.ExistsAndUpToDate(path, outPath) && cache)
					return Assembly.LoadFrom(outPath);

				// Precompile script to a temp file
				var precompiled = this.PreCompile(File.ReadAllText(path));
				var tmp = Path.GetTempFileName();
				File.WriteAllText(tmp, precompiled);

				// Compile
				var compiler = new Boo.Lang.Compiler.BooCompiler();
				compiler.Parameters.AddAssembly(typeof(Log).Assembly);
				compiler.Parameters.AddAssembly(Assembly.GetEntryAssembly());
				compiler.Parameters.Input.Add(new FileInput(tmp));
				compiler.Parameters.OutputAssembly = outPath;
				compiler.Parameters.Pipeline = new CompileToFile();

#if DEBUG
				compiler.Parameters.Debug = true;
#else
				compiler.Parameters.Debug = false;
#endif

				var context = compiler.Run();
				if (context.GeneratedAssembly == null)
				{
					var errors = context.Errors;
					var newExs = new CompilerErrorsException();

					foreach (var err in errors)
					{
						var newEx = new CompilerError(path, err.LexicalInfo.Line, err.LexicalInfo.Column, err.Message, false);
						newExs.Errors.Add(newEx);
					}

					throw newExs;
				}

				asm = context.GeneratedAssembly;
			}
			catch (CompilerErrorsException)
			{
				throw;
			}
			catch (UnauthorizedAccessException)
			{
				// Thrown if file can't be copied. Happens if script was
				// initially loaded from cache.
			}
			catch (Exception ex)
			{
				Log.Exception(ex);
			}

			return asm;
		}
示例#3
0
		public override Assembly Compile(string path, string outPath, bool cache)
		{
			Assembly asm = null;
			try
			{
				// Get from cache?
				if (this.ExistsAndUpToDate(path, outPath) && cache)
					return Assembly.LoadFrom(outPath);

				// Precompile script to a temp file
				var precompiled = this.PreCompile(File.ReadAllText(path));
				var tmp = Path.GetTempFileName() + Path.GetExtension(path);
				File.WriteAllText(tmp, precompiled);

#if DEBUG
				var debug = true;
#else
				var debug = false;
#endif

				// Compile
				// Mono needs the settings to not treat harmless warnings as
				// errors (like a missing await in an async Task) and to not
				// spam us with warnings.
				CSScript.GlobalSettings.UseAlternativeCompiler = Path.Combine(Directory.GetCurrentDirectory(), "lib/CSSCodeProvider.dll");
				asm = CSScript.LoadWithConfig(tmp, null, debug, CSScript.GlobalSettings, "");

				this.SaveAssembly(asm, outPath);
			}
			catch (csscript.CompilerException ex)
			{
				var errors = ex.Data["Errors"] as System.CodeDom.Compiler.CompilerErrorCollection;
				var newExs = new CompilerErrorsException();

				foreach (System.CodeDom.Compiler.CompilerError err in errors)
				{
					var newEx = new CompilerError(path, err.Line, err.Column, err.ErrorText, err.IsWarning);
					newExs.Errors.Add(newEx);
				}

				throw newExs;
			}
			catch (UnauthorizedAccessException)
			{
				// Thrown if file can't be copied. Happens if script was
				// initially loaded from cache.
				// TODO: Also thrown if CS-Script can't create the file,
				//   ie under Linux, if /tmp/CSSCRIPT isn't writeable.
				//   Handle that somehow?
			}
			catch (Exception ex)
			{
				Log.Exception(ex);
			}

			return asm;
		}
示例#4
0
        public override Assembly Compile(string path, string outPath, bool cache)
        {
            Assembly asm = null;

            try
            {
                if (this.ExistsAndUpToDate(path, outPath) && cache)
                {
                    return(Assembly.LoadFrom(outPath));
                }

                // Precompile script to a temp file
                var precompiled = this.PreCompile(File.ReadAllText(path));
                var tmp         = Path.GetTempFileName();
                File.WriteAllText(tmp, precompiled);

                // Compile
                var compiler = new Boo.Lang.Compiler.BooCompiler();
                compiler.Parameters.AddAssembly(typeof(Log).Assembly);
                compiler.Parameters.AddAssembly(Assembly.GetEntryAssembly());
                compiler.Parameters.Input.Add(new FileInput(tmp));
                compiler.Parameters.OutputAssembly = outPath;
                compiler.Parameters.Pipeline       = new CompileToFile();

#if DEBUG
                compiler.Parameters.Debug = true;
#else
                compiler.Parameters.Debug = false;
#endif

                var context = compiler.Run();
                if (context.GeneratedAssembly == null)
                {
                    var errors = context.Errors;
                    var newExs = new CompilerErrorsException();

                    foreach (var err in errors)
                    {
                        var newEx = new CompilerError(path, err.LexicalInfo.Line, err.LexicalInfo.Column, err.Message, false);
                        newExs.Errors.Add(newEx);
                    }

                    throw newExs;
                }

                asm = context.GeneratedAssembly;
            }
            catch (CompilerErrorsException)
            {
                throw;
            }
            catch (UnauthorizedAccessException)
            {
                // Thrown if file can't be copied. Happens if script was
                // initially loaded from cache.
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
            }

            return(asm);
        }