예제 #1
0
        private ScriptVariablesFixture(string rid)
        {
            var projectJson = Path.Combine(TestAssetPath, "project.json");
            var command     = new Mock <ICommand>();

            command.Setup(c => c.Execute()).Returns(new CommandResult());
            command.Setup(c => c.OnErrorLine(It.IsAny <Action <string> >())).Returns(() => command.Object);
            command.Setup(c => c.OnOutputLine(It.IsAny <Action <string> >())).Returns(() => command.Object);
            var commandFactory = new Mock <ICommandFactory>();

            commandFactory.Setup(c => c
                                 .Create(
                                     It.IsAny <string>(),
                                     It.IsAny <IEnumerable <string> >(),
                                     It.IsAny <NuGetFramework>(),
                                     It.IsAny <string>()))
            .Returns(command.Object);

            var _args = new CompilerCommandApp("dotnet compile", ".NET Compiler", "Compiler for the .NET Platform");

            _args.ConfigValue = ConfigValue;

            PreCompileScriptVariables  = new Dictionary <string, string>();
            PostCompileScriptVariables = new Dictionary <string, string>();

            var _scriptRunner = new Mock <IScriptRunner>();

            _scriptRunner.Setup(
                s =>
                s.RunScripts(It.IsAny <ProjectContext>(), It.IsAny <string>(), It.IsAny <Dictionary <string, string> >()))
            .Callback <ProjectContext, string, Dictionary <string, string> >((p, n, v) =>
            {
                if (n.Equals(ScriptNames.PreCompile))
                {
                    PreCompileScriptVariables = v;
                }

                if (n.Equals(ScriptNames.PostCompile))
                {
                    PostCompileScriptVariables = v;
                }
            });

            var managedCompiler = new ManagedCompiler(_scriptRunner.Object, commandFactory.Object);

            var rids = new List <string>();

            if (!string.IsNullOrEmpty(rid))
            {
                rids.Add(rid);
            }

            var context = ProjectContext.Create(projectJson, new NuGetFramework("dnxcore", new Version(5, 0)), rids);

            managedCompiler.Compile(context, _args);

            RuntimeOutputDir = Path.Combine(OutputPath, rid);
        }
예제 #2
0
        protected override CompilationResult RunCompile(ProjectGraphNode projectNode)
        {
            try
            {
                var managedCompiler = new ManagedCompiler(_scriptRunner, _commandFactory);

                var success = managedCompiler.Compile(projectNode.ProjectContext, _args);
                return(success ? CompilationResult.Success : CompilationResult.Failure);
            }
            finally
            {
                StampProjectWithSDKVersion(projectNode.ProjectContext);
                _incrementalManager.CacheIncrementalState(projectNode);
            }
        }
        /// <summary>
        /// Attempts to compile the current source code and load the assembly into memory.
        /// </summary>
        /// <param name="buildEngine">An <see cref="IBuildEngine"/> to use give to the compiler task so that messages can be logged.</param>
        /// <param name="taskInfo">A <see cref="TaskInfo"/> object containing details about the task.</param>
        /// <param name="assembly">The <see cref="Assembly"/> if the source code be compiled and loaded, otherwise <code>null</code>.</param>
        /// <returns><code>true</code> if the source code could be compiled and loaded, otherwise <code>null</code>.</returns>
        private bool TryCompileInMemoryAssembly(IBuildEngine buildEngine, TaskInfo taskInfo, out Assembly assembly)
        {
            // First attempt to get a compiled assembly from the cache
            //
            if (CompiledAssemblyCache.TryGetValue(taskInfo, out assembly))
            {
                return(true);
            }

            // The source code cannot actually be compiled "in memory" so instead the source code is written to disk in
            // the temp folder as well as the assembly.  After compilation, the source code and assembly are deleted.
            //
            string sourceCodePath = Path.GetTempFileName();
            string assemblyPath   = Path.GetTempFileName();

            // Delete the code file unless compilation failed or the environment variable MSBUILDLOGCODETASKFACTORYOUTPUT
            // is set (which allows for debugging problems)
            //
            bool deleteSourceCodeFile = Environment.GetEnvironmentVariable("MSBUILDLOGCODETASKFACTORYOUTPUT") == null;

            try
            {
                // Create the code
                //
                File.WriteAllText(sourceCodePath, taskInfo.SourceCode);

                // Execute the compiler.  We re-use the existing build task by hosting it and giving it our IBuildEngine instance for logging
                //
                ManagedCompiler managedCompiler = null;

                // User specified values are translated using a dictionary of known aliases and checking if the user specified
                // a valid code language is already done
                //
                if (taskInfo.CodeLanguage.Equals("CS"))
                {
                    managedCompiler = new Csc
                    {
                        NoStandardLib = true,
                    };

                    string toolExe = Environment.GetEnvironmentVariable("CscToolExe");

                    if (!String.IsNullOrEmpty(toolExe))
                    {
                        managedCompiler.ToolExe = toolExe;
                    }
                }
                else if (taskInfo.CodeLanguage.Equals("VB"))
                {
                    managedCompiler = new Vbc
                    {
                        //NoStandardLib = true,
                        //RootNamespace = "InlineCode",
                        //OptionCompare = "Binary",
                        //OptionExplicit = true,
                        //OptionInfer = true,
                        //OptionStrict = false,
                        //Verbosity = "Verbose",
                    };

                    string toolExe = Environment.GetEnvironmentVariable("VbcToolExe");

                    if (!String.IsNullOrEmpty(toolExe))
                    {
                        managedCompiler.ToolExe = toolExe;
                    }
                }

                if (!TryResolveAssemblyReferences(_log, taskInfo, out ITaskItem[] references))