예제 #1
0
        public static void Execute(InvokeOptions invokeOptions, CompilerOptions compilerOptions)
        {
            CompilerResult result = null;
            if (TryCompile(compilerOptions, out result))
                result.CompiledAssembly = LoadAssembly(result.PathToAssembly);

            Invoke(result.CompiledAssembly, invokeOptions);
        }
예제 #2
0
        private static CompilerResult CompileInternal(CompilerOptions compileParams)
        {
            Prepare(compileParams);

            var provider = new CSharpCodeProvider();

            var parameters = new CompilerParameters(compileParams.ReferencedAssemblyPaths, compileParams.OutputAssemblyPath, true);
            parameters.GenerateInMemory = false;
            parameters.GenerateExecutable = false;

            if (!string.IsNullOrEmpty(compileParams.SignKeyPath))
                parameters.CompilerOptions = string.Format("/keyFile:\"{0}\"", compileParams.SignKeyPath);

            var compilationResults = provider.CompileAssemblyFromFile(parameters, compileParams.FilePath);

            if (compilationResults.Errors != null && compilationResults.Errors.Count > 0)
            {
                var builder = new StringBuilder();
                var compilerErrors = compilationResults.Errors.OfType<CompilerError>().Where(x => !x.IsWarning);

                foreach (var error in compilerErrors)
                {
                    builder.AppendLine("An error occured during the compilation process.");
                    builder.AppendFormat("Error {0}: {1}. Line: {2} Column: {3}.", error.ErrorNumber, error.ErrorText, error.Line, error.Column).AppendLine();
                    builder.AppendFormat("File: {0}", error.FileName).AppendLine();
                }

                throw new CompilationException(builder.ToString(),
                    compilationResults.Errors, compilationResults.Output);
            }

            return new CompilerResult()
            {
                LastCompileTime = DateTime.UtcNow,
                PathToAssembly = compilationResults.PathToAssembly
            };
        }
예제 #3
0
        private static bool TryCompile(CompilerOptions compileParams, out CompilerResult result)
        {
            bool compilationOccured = false;
            if (compileCache.TryGetValue(compileParams.FilePath, out result))
            {
                var lastWriteTime = File.GetLastAccessTimeUtc(compileParams.FilePath);
                if (lastWriteTime > result.LastCompileTime)
                {
                    lock (compileCache)
                    {
                        if (lastWriteTime > compileCache[compileParams.FilePath].LastCompileTime)
                        {
                            result = CompileInternal(compileParams);
                            compileCache[compileParams.FilePath] = result;
                            compilationOccured = true;
                        }
                    }
                }
            }
            else
            {
                lock (compileCache)
                {
                    result = CompileInternal(compileParams);
                    compileCache[compileParams.FilePath] = result;
                    compilationOccured = true;
                }
            }

            return compilationOccured;
        }
예제 #4
0
        private static void Prepare(CompilerOptions options)
        {
            if (string.IsNullOrEmpty(options.OutputAssemblyName))
                options.OutputAssemblyName = Guid.NewGuid().ToString();

            if (string.IsNullOrEmpty(options.OutputDir))
                options.OutputDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var folderName = "artefacts" + counter++;
            var folderPath = Path.Combine(options.OutputDir, folderName);

            if (!Directory.Exists(folderPath))
                Directory.CreateDirectory(folderPath);

            var outputAssemblyPath = Path.Combine(folderPath, options.OutputAssemblyName);
            if (!outputAssemblyPath.EndsWith(".dll"))
                outputAssemblyPath = string.Concat(outputAssemblyPath, ".dll");

            options.OutputAssemblyPath = outputAssemblyPath;
        }