Exemplo n.º 1
0
 public void TestConstructor1()
 {
     BuildRequestData config1 = new BuildRequestData("file", new Dictionary <string, string>(), "toolsVersion", new string[0], null);
 }
Exemplo n.º 2
0
        public void TestConstructor2NegativeConfigId()
        {
            BuildRequestData config1 = new BuildRequestData("file", new Dictionary <string, string>(), "toolsVersion", new string[0], null);

            new BuildRequestConfiguration(-1, config1, "2.0");
        }
Exemplo n.º 3
0
        public void BuildCmdlet()
        {
            var currentDirectory = Environment.CurrentDirectory.Replace("\\", "/");

            rootDirectoryPath = Regex.Replace(currentDirectory, "bin/Debug/*.*", "rootDirectory.txt");

            if (!string.IsNullOrEmpty(this.RootDirectory))
            {
                File.WriteAllBytes(rootDirectoryPath, new byte[0]);
                File.WriteAllBytes(rootDirectoryPath, Encoding.ASCII.GetBytes(RootDirectory));
            }
            var outputHeaderRow = new List <string>();
            var outputItemRow   = new List <string>();
            var repos           = this.GetRepositories();

            if (repos != null && repos.Any())
            {
                FileLogger logger      = new FileLogger();
                string     logFilePath = string.Empty;
                foreach (var repo in repos)
                {
                    try
                    {
                        logFilePath = repo.Remove(repo.LastIndexOf("\\") + 1) + "build.log";

                        logger.Parameters = @"logFile=" + logFilePath;

                        ProjectCollection pc = new ProjectCollection();

                        Dictionary <string, string> GlobalProperty = new Dictionary <string, string>();
                        GlobalProperty.Add("Configuration", "Debug");
                        GlobalProperty.Add("Platform", "Any CPU");

                        BuildRequestData buildRequest = new BuildRequestData(repo, GlobalProperty, null, new string[] { "Build" }, null);

                        //register file logger using BuildParameters
                        BuildParameters bp = new BuildParameters(pc);
                        bp.Loggers = new List <Microsoft.Build.Framework.ILogger> {
                            logger
                        }.AsEnumerable();

                        //build solution
                        BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, buildRequest);

                        //Unregister all loggers to close the log file
                        pc.UnregisterAllLoggers();

                        //read lines from log file having project build output details
                        string[] solutionBuildOutputs = File.ReadAllLines(logFilePath);

                        //write the result of solution build to html report
                        outputHeaderRow.Add(string.Format("{0};Build Result", repo));

                        //split the contents of logger file to retrieve project build details
                        string[] splitter       = { "__________________________________________________" };
                        string   loggerOutput   = File.ReadAllText(logFilePath);
                        string[] projectResults = loggerOutput.Split(splitter, StringSplitOptions.None);
                        foreach (string projectBuildDetails in projectResults)
                        {
                            if (projectBuildDetails.Contains("(default targets):"))
                            {
                                if (projectBuildDetails.Contains("Done building project \""))
                                {
                                    //write the result of failed projects build to html report
                                    string[] lines = projectBuildDetails.Split("\n".ToCharArray());
                                    string   buildFailedProjectName = lines.Where(x => x.Contains("Done building project \"")).FirstOrDefault();
                                    buildFailedProjectName = buildFailedProjectName.Replace("Done building project ", string.Empty).Trim();
                                    buildFailedProjectName = buildFailedProjectName.Replace("\"", string.Empty);
                                    buildFailedProjectName = buildFailedProjectName.Replace(" -- FAILED.", string.Empty);
                                    outputItemRow.Add(buildFailedProjectName + ";FAILED");
                                }
                                else
                                {
                                    //write the result of successfully built projects to html report
                                    string[] lines = projectBuildDetails.Split("\n".ToCharArray());
                                    string   buildSuccededProjectName = lines.Where(x => x.Contains(" (default targets):")).FirstOrDefault().Replace("\" (default targets):", "");
                                    string   finalProjectName         = buildSuccededProjectName.Substring(buildSuccededProjectName.LastIndexOf("\\") + 1);
                                    outputItemRow.Add(finalProjectName + ";SUCCEEDED");
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            else
            {
                throw new Exception("No repos found");
            }
        }
        public void TestBuildWithNewConfiguration()
        {
            BuildRequestData          data   = new BuildRequestData(Path.GetFullPath("TestFile"), new Dictionary <string, string>(), "TestToolsVersion", new string[0], null);
            BuildRequestConfiguration config = new BuildRequestConfiguration(1, data, "2.0");

            _cache.AddConfiguration(config);

            // Configure the builder to spawn build requests
            MockRequestBuilder        builder          = (MockRequestBuilder)_host.GetComponent(BuildComponentType.RequestBuilder);
            BuildRequestData          data2            = new BuildRequestData(Path.GetFullPath("OtherFile"), new Dictionary <string, string>(), "TestToolsVersion", new string[0], null);
            BuildRequestConfiguration unresolvedConfig = new BuildRequestConfiguration(data2, "2.0");

            builder.NewRequests.Add(new FullyQualifiedBuildRequest[1] {
                new FullyQualifiedBuildRequest(unresolvedConfig, new string[1] {
                    "requiredTarget1"
                }, true)
            });

            // Create the initial build request
            string[] targets = new string[3] {
                "target1", "target2", "target3"
            };
            BuildRequest request = CreateNewBuildRequest(1, targets);

            // Kick it off
            VerifyEngineStatus(BuildRequestEngineStatus.Uninitialized);
            _engine.InitializeForBuild(new NodeLoggingContext(_host.LoggingService, 0, false));
            _engine.SubmitBuildRequest(request);
            Thread.Sleep(250);
            VerifyEngineStatus(BuildRequestEngineStatus.Active);

            // Wait for the request to generate the child request with the unresolved configuration
            WaitForEvent(_newConfigurationEvent, "NewConfigurationEvent");
            Assert.Equal(Path.GetFullPath("OtherFile"), _newConfiguration_Config.ProjectFullPath);
            Assert.Equal("TestToolsVersion", _newConfiguration_Config.ToolsVersion);
            Assert.True(_newConfiguration_Config.WasGeneratedByNode);
            Thread.Sleep(250);
            VerifyEngineStatus(BuildRequestEngineStatus.Waiting);

            // Resolve the configuration
            BuildRequestConfigurationResponse response = new BuildRequestConfigurationResponse(_newConfiguration_Config.ConfigurationId, 2, 0);

            _engine.ReportConfigurationResponse(response);

            // Now wait for the actual requests to be issued.
            WaitForEvent(_newRequestEvent, "NewRequestEvent");
            Assert.Equal(2, _newRequest_Request.BuildRequests[0].ConfigurationId);
            Assert.Equal(2, _newRequest_Request.BuildRequests[0].ConfigurationId);
            Assert.Equal(1, _newRequest_Request.BuildRequests[0].Targets.Count);
            Assert.Equal("requiredTarget1", _newRequest_Request.BuildRequests[0].Targets[0]);

            // Report a result to satisfy the build request
            BuildResult result = new BuildResult(_newRequest_Request.BuildRequests[0]);

            result.AddResultsForTarget("requiredTarget1", TestUtilities.GetEmptySucceedingTargetResult());
            _engine.UnblockBuildRequest(new BuildRequestUnblocker(result));

            // Continue the request
            _engine.UnblockBuildRequest(new BuildRequestUnblocker(request.GlobalRequestId));

            // Wait for the original request to complete
            WaitForEvent(_requestCompleteEvent, "RequestComplete");
            Assert.Equal(request, _requestComplete_Request);
            Assert.Equal(BuildResultCode.Success, _requestComplete_Result.OverallResult);
            Thread.Sleep(250);
            VerifyEngineStatus(BuildRequestEngineStatus.Idle);
        }
Exemplo n.º 5
0
        public bool Build(IVsProject p, BuildCallback buildCallback, BuildLogger buildLogger)
        {
            MSBuildProject project = MSBuildUtils.LoadedProject(ProjectUtil.GetProjectFullPath(p), DTEUtil.IsCppProject(p), false);

            //
            // We need to set this before we acquire the build resources otherwise Msbuild
            // will not see the changes.
            //
            bool onlyLogCriticalEvents = project.ProjectCollection.OnlyLogCriticalEvents;

            project.ProjectCollection.Loggers.Add(buildLogger);
            project.ProjectCollection.OnlyLogCriticalEvents = false;

            uint cookie;
            int  err = BuildManagerAccessor.AcquireBuildResources(VSBUILDMANAGERRESOURCE.VSBUILDMANAGERRESOURCE_DESIGNTIME |
                                                                  VSBUILDMANAGERRESOURCE.VSBUILDMANAGERRESOURCE_UITHREAD, out cookie);

            if (err != VSConstants.E_PENDING && err != VSConstants.S_OK)
            {
                ErrorHandler.ThrowOnFailure(err);
            }

            if (err == VSConstants.E_PENDING)
            {
                project.ProjectCollection.Loggers.Remove(buildLogger);
                project.ProjectCollection.OnlyLogCriticalEvents = onlyLogCriticalEvents;

                Dispatcher          = Dispatcher.CurrentDispatcher;
                BuildAvailableEvent = new ManualResetEvent(false);
                BuildAvailableEvent.SafeWaitHandle = new SafeWaitHandle(BuildManagerAccessor.DesignTimeBuildAvailable, false);

                Thread t = new Thread(() =>
                {
                    BuildAvailableEvent.WaitOne();
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        Package.Instance.BuildNextProject();
                    }));
                });
                t.Start();
                return(false);
            }
            else
            {
                try
                {
                    Dictionary <string, string> properties = new Dictionary <string, string>();
                    string platform = buildCallback.ProjectConfiguration.PlatformName;
                    properties["Platform"]      = platform.Equals("Any CPU") ? "AnyCPU" : platform;
                    properties["Configuration"] = buildCallback.ProjectConfiguration.ConfigurationName;

                    BuildRequestData buildRequest = new BuildRequestData(
                        ProjectUtil.GetProjectFullPath(p),
                        properties,
                        null,
                        new string[] { "IceBuilder_Compile" },
                        project.ProjectCollection.HostServices,
                        BuildRequestDataFlags.IgnoreExistingProjectState |
                        BuildRequestDataFlags.ReplaceExistingProjectInstance);

                    BuildSubmission submission = BuildManager.DefaultBuildManager.PendBuildRequest(buildRequest);
                    ErrorHandler.ThrowOnFailure(BuildManagerAccessor.RegisterLogger(submission.SubmissionId, buildLogger));
                    buildCallback.BeginBuild();
                    submission.ExecuteAsync((s) =>
                    {
                        Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(() =>
                        {
                            project.ProjectCollection.Loggers.Remove(buildLogger);
                            project.ProjectCollection.OnlyLogCriticalEvents = onlyLogCriticalEvents;
                            BuildManagerAccessor.ReleaseBuildResources(cookie);
                            BuildManagerAccessor.UnregisterLoggers(submission.SubmissionId);
                            buildCallback.EndBuild(submission.BuildResult.OverallResult == BuildResultCode.Success);
                        }));
                    }, null);

                    return(true);
                }
                catch (Exception)
                {
                    project.ProjectCollection.Loggers.Remove(buildLogger);
                    project.ProjectCollection.OnlyLogCriticalEvents = onlyLogCriticalEvents;
                    BuildManagerAccessor.ReleaseBuildResources(cookie);
                    throw;
                }
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Initializes a configuration from a BuildRequestData structure.  Used by the BuildManager.
 /// Figures out the correct tools version to use, falling back to the provided default if necessary.
 /// May throw InvalidProjectFileException.
 /// </summary>
 /// <param name="data">The data containing the configuration information.</param>
 /// <param name="defaultToolsVersion">The default ToolsVersion to use as a fallback</param>
 internal BuildRequestConfiguration(BuildRequestData data, string defaultToolsVersion)
     : this(0, data, defaultToolsVersion)
 {
 }
Exemplo n.º 7
0
        private static void EvaluateProject(OutputContext output, DotnetProjectServiceBuilder project)
        {
            var sw = Stopwatch.StartNew();

            // Currently we only log at debug level.
            var logger = new ConsoleLogger(
                verbosity: LoggerVerbosity.Normal,
                write: message => output.WriteDebug(message),
                colorSet: null,
                colorReset: null);

            // We need to isolate projects from each other for testing. MSBuild does not support
            // loading the same project twice in the same collection.
            var projectCollection = new ProjectCollection();

            ProjectInstance projectInstance;

            Microsoft.Build.Evaluation.Project msbuildProject;

            try
            {
                output.WriteDebugLine($"Loading project '{project.ProjectFile.FullName}'.");
                msbuildProject = Microsoft.Build.Evaluation.Project.FromFile(project.ProjectFile.FullName, new ProjectOptions()
                {
                    ProjectCollection = projectCollection,
                    GlobalProperties  = project.BuildProperties
                });
                projectInstance = msbuildProject.CreateProjectInstance();
                output.WriteDebugLine($"Loaded project '{project.ProjectFile.FullName}'.");
            }
            catch (Exception ex)
            {
                throw new CommandException($"Failed to load project: '{project.ProjectFile.FullName}'.", ex);
            }

            try
            {
                AssemblyLoadContext.Default.Resolving += ResolveAssembly;

                output.WriteDebugLine($"Restoring project '{project.ProjectFile.FullName}'.");

                // Similar to what MSBuild does for restore:
                // https://github.com/microsoft/msbuild/blob/3453beee039fb6f5ccc54ac783ebeced31fec472/src/MSBuild/XMake.cs#L1417
                //
                // We need to do restore as a separate operation
                var restoreRequest = new BuildRequestData(
                    projectInstance,
                    targetsToBuild: new[] { "Restore" },
                    hostServices: null,
                    flags: BuildRequestDataFlags.ClearCachesAfterBuild | BuildRequestDataFlags.SkipNonexistentTargets | BuildRequestDataFlags.IgnoreMissingEmptyAndInvalidImports);

                var parameters = new BuildParameters(projectCollection)
                {
                    Loggers = new[] { logger, },
                };

                // We don't really look at the result, because it's not clear we should halt totally
                // if restore fails.
                var restoreResult = BuildManager.DefaultBuildManager.Build(parameters, restoreRequest);
                output.WriteDebugLine($"Restored project '{project.ProjectFile.FullName}'.");

                msbuildProject.MarkDirty();
                projectInstance = msbuildProject.CreateProjectInstance();

                var targets = new List <string>()
                {
                    "ResolveReferences",
                    "ResolvePackageDependenciesDesignTime",
                    "PrepareResources",
                    "GetAssemblyAttributes",
                };

                var result = projectInstance.Build(
                    targets: targets.ToArray(),
                    loggers: new[] { logger, });

                // If the build fails, we're not really blocked from doing our work.
                // For now we just log the output to debug. There are errors that occur during
                // running these targets we don't really care as long as we get the data.
            }
            finally
            {
                AssemblyLoadContext.Default.Resolving -= ResolveAssembly;
            }

            // Reading a few different version properties to be more resilient.
            var version =
                projectInstance.GetProperty("AssemblyInformationalVersion")?.EvaluatedValue ??
                projectInstance.GetProperty("InformationalVersion")?.EvaluatedValue ??
                projectInstance.GetProperty("Version").EvaluatedValue;

            project.Version = version;
            output.WriteDebugLine($"Found application version: {version}");

            var targetFrameworks = projectInstance.GetPropertyValue("TargetFrameworks");

            project.TargetFrameworks = targetFrameworks.Split(';', StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty <string>();

            // Figure out if functions app.
            // If so, run app with function host.
            project.RunCommand             = projectInstance.GetPropertyValue("RunCommand");
            project.RunArguments           = projectInstance.GetPropertyValue("RunArguments");
            project.TargetPath             = projectInstance.GetPropertyValue("TargetPath");
            project.PublishDir             = projectInstance.GetPropertyValue("PublishDir");
            project.AssemblyName           = projectInstance.GetPropertyValue("AssemblyName");
            project.IntermediateOutputPath = projectInstance.GetPropertyValue("IntermediateOutputPath");

            output.WriteDebugLine($"RunCommand={project.RunCommand}");
            output.WriteDebugLine($"RunArguments={project.RunArguments}");
            output.WriteDebugLine($"TargetPath={project.TargetPath}");
            output.WriteDebugLine($"PublishDir={project.PublishDir}");
            output.WriteDebugLine($"AssemblyName={project.AssemblyName}");
            output.WriteDebugLine($"IntermediateOutputPath={project.IntermediateOutputPath}");

            // Normalize directories to their absolute paths
            project.IntermediateOutputPath = Path.Combine(project.ProjectFile.DirectoryName, NormalizePath(project.IntermediateOutputPath));
            project.TargetPath             = Path.Combine(project.ProjectFile.DirectoryName, NormalizePath(project.TargetPath));
            project.PublishDir             = Path.Combine(project.ProjectFile.DirectoryName, NormalizePath(project.PublishDir));

            var targetFramework = projectInstance.GetPropertyValue("TargetFramework");

            project.TargetFramework = targetFramework;
            output.WriteDebugLine($"Found target framework: {targetFramework}");

            // TODO: Parse the name and version manually out of the TargetFramework field if it's non-null
            project.TargetFrameworkName    = projectInstance.GetPropertyValue("_ShortFrameworkIdentifier");
            project.TargetFrameworkVersion = projectInstance.GetPropertyValue("_ShortFrameworkVersion") ?? projectInstance.GetPropertyValue("_TargetFrameworkVersionWithoutV");

            var sharedFrameworks = projectInstance.GetItems("FrameworkReference").Select(i => i.EvaluatedInclude).ToList();

            project.Frameworks.AddRange(sharedFrameworks.Select(s => new Framework(s)));
            output.WriteDebugLine($"Found shared frameworks: {string.Join(", ", sharedFrameworks)}");

            // determine container base image
            if (project.ContainerInfo != null)
            {
                project.ContainerInfo.BaseImageName = projectInstance.GetPropertyValue("ContainerBaseImage");
                project.ContainerInfo.BaseImageTag  = projectInstance.GetPropertyValue("ContainerBaseTag");
            }

            bool PropertyIsTrue(string property)
            {
                return(projectInstance.GetPropertyValue(property) is string s && !string.IsNullOrEmpty(s) && bool.Parse(s));
            }

            project.IsAspNet = project.Frameworks.Any(f => f.Name == "Microsoft.AspNetCore.App") ||
                               projectInstance.GetPropertyValue("MicrosoftNETPlatformLibrary") == "Microsoft.AspNetCore.App" ||
                               PropertyIsTrue("_AspNetCoreAppSharedFxIsEnabled") ||
                               PropertyIsTrue("UsingMicrosoftNETSdkWeb");

            output.WriteDebugLine($"IsAspNet={project.IsAspNet}");

            output.WriteDebugLine($"Evaluation Took: {sw.Elapsed.TotalMilliseconds}ms");

            // The Microsoft.Build.Locator doesn't handle the loading of other assemblies
            // that are shipped with MSBuild (ex NuGet).
            //
            // This means that the set of assemblies that need special handling depends on the targets
            // that we run :(
            //
            // This is workaround for this limitation based on the targets we need to run
            // to resolve references and versions.
            //
            // See: https://github.com/microsoft/MSBuildLocator/issues/86
            Assembly?ResolveAssembly(AssemblyLoadContext context, AssemblyName assemblyName)
            {
                if (assemblyName.Name is object)
                {
                    var msbuildDirectory = Environment.GetEnvironmentVariable("MSBuildExtensionsPath") !;
                    var assemblyFilePath = Path.Combine(msbuildDirectory, assemblyName.Name + ".dll");
                    if (File.Exists(assemblyFilePath))
                    {
                        return(context.LoadFromAssemblyPath(assemblyFilePath));
                    }
                }

                return(default);
Exemplo n.º 8
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Logging;
using Microsoft.Build.Framework;
using System.Xml;

namespace SMA17Pr2 {


	public interface ICompiler {
		bool compile();
	}

	public class WindowsCompiler : ICompiler {
		public string solnPath { get; private set; }
		public BuildResult result { get; private set; }

		public WindowsCompiler(string _solnPath) {
			solnPath = _solnPath;
			result = new BuildResult();
		}

		public bool compile() {
			var props = new Dictionary<string, string>();
			ConsoleLogger l = new ConsoleLogger();// { Verbosity = LoggerVerbosity.Detailed };
			List<ILogger> logs = new List<ILogger>();
			logs.Add(l);
			props["Configuration"] = "Release";
			BuildRequestData request = new BuildRequestData(solnPath, props, "4.0", new string[] { "Build" }, null);
			var buildParams = new BuildParameters();
			buildParams.Loggers = logs;

			result = BuildManager.DefaultBuildManager.Build(buildParams, request);
			if (result.OverallResult != BuildResultCode.Success) {
				Console.WriteLine(result.Exception);
				return false;
			}
			return true;
		}
	}

	public class UnixCompiler : ICompiler {
		public string solnPath { get; private set; }
		public UnixCompiler(string _solnPath) {
			solnPath = _solnPath;
		}
		public bool compile() {
			System.Diagnostics.Process proc = new System.Diagnostics.Process();
Exemplo n.º 9
0
        public bool Build(IEnumerable <string> ProjectsPath, long VersionId, string OutputDirectory)
        {
            var ret = false;

            try
            {
                // Properties passed to MSBUID (the ones of the .target file)
                var globalProperties = new Dictionary <string, string>();
                globalProperties.Add("CodeContractsCacheDirectory", OutputDirectory);
                globalProperties.Add("CodeContractsCacheAnalysisResults", "true");
                globalProperties.Add("CodeContractsCacheVersion", VersionId.ToString());
                globalProperties.Add("CodeContractsCacheMaxSize", Int32.MaxValue.ToString());


#if WITH_LOG
                // It does not work: The log file is empty
                var logFileName = "build." + VersionId + ".log";
                var logger      = new FileLogger();
                logger.Parameters = "logfile=" + Path.Combine(OutputDirectory, logFileName);
                logger.Verbosity  = Microsoft.Build.Framework.LoggerVerbosity.Diagnostic;
                var loggers = new FileLogger[] { logger };
                // with this version, loggers don't work (log files are created but empty), why?
#else
                var loggers = new FileLogger[0];
#endif

                using (var projectCollection = new ProjectCollection(globalProperties, loggers, ToolsetDefinitionLocations.Registry))
                {
                    // The only way to communicate to msbuild is to create an xml file, and pass it to msbuild
                    XNamespace ns        = "http://schemas.microsoft.com/developer/msbuild/2003";
                    var        xmlTarget = new XElement(ns + "Target", new XAttribute("Name", "Build"));

                    var properties = "";

                    foreach (var projectPath in ProjectsPath)
                    {
                        Contract.Assert(projectPath != null);
                        xmlTarget.Add(new XElement(ns + "MSBuild",
                                                   new XAttribute("Projects", Path.Combine(this.SourceDirectory, projectPath)),
                                                   new XAttribute("Targets", "Rebuild"), // ensures the project will be rebuilt from scratch (is it needed?)
                                                   new XAttribute("UseResultsCache", "false"),
                                                   new XAttribute("UnloadProjectsOnCompletion", "true"),
                                                   new XAttribute("ContinueOnError", "true"),
                                                   new XAttribute("StopOnFirstFailure", "false"),
                                                   new XAttribute("Properties", properties)));
                    }

                    // create the project
                    var xmlProject = new XElement(ns + "Project", xmlTarget);

                    // now read the project
                    var project = projectCollection.LoadProject(xmlProject.CreateReader());

                    // create an instance of the project
                    var projectInstance = project.CreateProjectInstance();

                    // create the parameters for the build
                    var buildParameters = new BuildParameters(projectCollection);
                    buildParameters.EnableNodeReuse = false;
                    buildParameters.ResetCaches     = true;

                    // Ask a build on this project
                    var buildRequestData = new BuildRequestData(projectInstance, new string[] { "Build" });

                    // we need to create a new BuildManager each time, otherwise it wrongly caches project files
                    var buildManager = new BuildManager();

                    // now do the build!
                    var buildResult = buildManager.Build(buildParameters, buildRequestData);

                    ret = buildResult.OverallResult == BuildResultCode.Success;

#if WITH_LOG
                    logger.Shutdown();
#endif

                    // now cleanup - it seems it does not improve
                    projectCollection.UnregisterAllLoggers();
                    projectCollection.UnloadAllProjects();
                }

                #region Version using the default BuildManager (disabled)
#if false
                // First version. It does not work, because msbuild keeps a cache of the projects, and so it compiles the new files with the old project
                // The Log works
                Microsoft.Build.Execution.BuildManager.DefaultBuildManager.ResetCaches(); // ensures MSBuild doesn't use cached version of project files (it handles very badly versions changing!), not sure it works

                XNamespace ns        = "http://schemas.microsoft.com/developer/msbuild/2003";
                var        xmlTarget = new XElement(ns + "Target", new XAttribute("Name", "Build"));

                var properties = "";
                properties += ";CodeContractsCacheDirectory=" + OutputDirectory;
                properties += ";CodeContractsCacheAnalysisResults=true";
                properties += ";CodeContractsCacheVersion=" + VersionId;

                foreach (var projectPath in ProjectsPath)
                {
                    Contract.Assume(projectPath != null);
                    xmlTarget.Add(new XElement(ns + "MSBuild",
                                               new XAttribute("Projects", Path.Combine(this.WorkingDirectory, projectPath)),
                                               new XAttribute("Targets", "Rebuild"), // ensures the project will be rebuilt from scratch (is it needed?)
                                               new XAttribute("UseResultsCache", "false"),
                                               new XAttribute("UnloadProjectsOnCompletion", "true"),
                                               new XAttribute("ContinueOnError", "true"),
                                               new XAttribute("StopOnFirstFailure", "false"),
                                               new XAttribute("Properties", properties)));
                }

                var xmlProject = new XElement(ns + "Project", xmlTarget);

                var project = new Project(xmlProject.CreateReader());

                var logFileName = "build." + VersionId + ".log";
                var logger      = new Microsoft.Build.Logging.FileLogger();
                logger.Parameters = "logfile=" + Path.Combine(OutputDirectory, logFileName);
                ret = project.Build(logger);
        #endif
                #endregion
            }
            catch
            {
                ret = false;
            }

            return(ret);
        }
Exemplo n.º 10
0
        public static void PerformNightlyBuild(ConfigFile config)
        {
            string          packagePath     = Path.Combine(config.PackageDir, config.PackageName);
            FileVersionInfo versionCore     = null;
            FileVersionInfo versionEditor   = null;
            FileVersionInfo versionLauncher = null;

            // Build the target Solution
            Console.WriteLine("================================ Build Solution ===============================");
            {
                var buildProperties = new Dictionary <string, string>()
                {
                    { "Configuration", "Release" }
                };
                var buildRequest    = new BuildRequestData(config.SolutionPath, buildProperties, null, new string[] { "Build" }, null);
                var buildParameters = new BuildParameters();
                //buildParameters.Loggers = new[] { new ConsoleLogger(LoggerVerbosity.Minimal) };
                var buildResult = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest);
                if (buildResult.OverallResult != BuildResultCode.Success)
                {
                    throw new ApplicationException("The project doesn't compile properly. Cannot proceed in this state.");
                }

                versionCore     = FileVersionInfo.GetVersionInfo(Path.Combine(config.BuildResultDir, "Duality.dll"));
                versionEditor   = FileVersionInfo.GetVersionInfo(Path.Combine(config.BuildResultDir, "DualityEditor.exe"));
                versionLauncher = FileVersionInfo.GetVersionInfo(Path.Combine(config.BuildResultDir, "DualityLauncher.exe"));

                Console.WriteLine("Build Successful");
                Console.WriteLine("  Core Version:     {0}", versionCore.FileVersion);
                Console.WriteLine("  Editor Version:   {0}", versionEditor.FileVersion);
                Console.WriteLine("  Launcher Version: {0}", versionLauncher.FileVersion);
            }
            Console.WriteLine("===============================================================================");
            Console.WriteLine();
            Console.WriteLine();

            // Perform unit testing
            Console.WriteLine("================================= Unit Testing ================================");
            {
                foreach (string nunitProjectFile in Directory.EnumerateFiles(config.UnitTestProjectDir, "*.nunit", SearchOption.TopDirectoryOnly))
                {
                    Console.Write("Testing '{0}'... ", Path.GetFileName(nunitProjectFile));

                    // Don't use /timeout, as it will execute tests from a different thread,
                    // which will break a lot of graphics-related Duality stuff!
                    string resultFile = "UnitTestResult.xml";
                    ExecuteCommand(
                        string.Format("{0} {1} /result={2}",
                                      Path.Combine(config.NUnitBinDir, "nunit-console.exe"),
                                      nunitProjectFile,
                                      resultFile),
                        verbose: false);

                    if (File.Exists(resultFile))
                    {
                        bool        unitTestFailed = false;
                        XmlDocument resultDoc      = new XmlDocument();
                        resultDoc.Load(resultFile);

                        Stack <XmlElement> elementStack = new Stack <XmlElement>();
                        elementStack.Push(resultDoc["test-results"]);
                        do
                        {
                            XmlElement   currentElement   = elementStack.Pop();
                            XmlAttribute successAttribute = currentElement.Attributes["success"];

                            if (successAttribute == null)
                            {
                                foreach (XmlElement child in currentElement.OfType <XmlElement>().Reverse())
                                {
                                    elementStack.Push(child);
                                }
                            }
                            else
                            {
                                bool success = (successAttribute == null) || XmlConvert.ToBoolean(successAttribute.Value.ToLower());
                                if (!success)
                                {
                                    unitTestFailed = true;
                                    break;
                                }
                            }
                        } while (elementStack.Count > 0);

                        if (unitTestFailed)
                        {
                            ExecuteBackgroundCommand(resultFile);
                            ExecuteBackgroundCommand(
                                string.Format("{0} {1}",
                                              Path.Combine(config.NUnitBinDir, "nunit.exe"),
                                              nunitProjectFile));
                            throw new ApplicationException(string.Format("At least one unit test has failed. See {0} for more information.", resultFile));
                        }
                        else
                        {
                            Console.WriteLine("success!");
                            File.Delete(resultFile);
                        }
                    }
                    else
                    {
                        throw new ApplicationException("Something appears to have failed during unit testing, because no result file was found.");
                    }
                }
            }
            Console.WriteLine("===============================================================================");
            Console.WriteLine();
            Console.WriteLine();

            // Build the documentation
            if (!config.NoDocs)
            {
                Console.WriteLine("================================== Build Docs =================================");
                {
                    var buildProperties = new Dictionary <string, string>()
                    {
                        { "Configuration", "Release" }
                    };
                    var buildRequest    = new BuildRequestData(config.DocSolutionPath, buildProperties, null, new string[] { "Build" }, null);
                    var buildParameters = new BuildParameters();
                    buildParameters.Loggers = new[] { new ConsoleLogger(LoggerVerbosity.Minimal) };
                    var buildResult = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest);
                    if (buildResult.OverallResult != BuildResultCode.Success)
                    {
                        throw new ApplicationException("Documentation Build Failure");
                    }
                    File.Copy(
                        Path.Combine(config.DocBuildResultDir, config.DocBuildResultFile),
                        Path.Combine(config.BuildResultDir, config.DocBuildResultFile),
                        true);
                    Console.WriteLine("Documentation Build Successful");
                }
                Console.WriteLine("===============================================================================");
                Console.WriteLine();
                Console.WriteLine();
            }
            else if (File.Exists(Path.Combine(config.DocBuildResultDir, config.DocBuildResultFile)))
            {
                Console.WriteLine("============================== Copy existing Docs =============================");
                {
                    File.Copy(
                        Path.Combine(config.DocBuildResultDir, config.DocBuildResultFile),
                        Path.Combine(config.BuildResultDir, config.DocBuildResultFile),
                        true);
                }
                Console.WriteLine("===============================================================================");
                Console.WriteLine();
                Console.WriteLine();
            }

            // Copy the results to the target directory
            Console.WriteLine("================================ Copy to Target ===============================");
            {
                Console.WriteLine("Creating target directory '{0}'", config.IntermediateTargetDir);
                if (Directory.Exists(config.IntermediateTargetDir))
                {
                    Directory.Delete(config.IntermediateTargetDir, true);
                }
                CopyDirectory(config.BuildResultDir, config.IntermediateTargetDir, true, path =>
                {
                    string fileName = Path.GetFileName(path);
                    foreach (string blackListEntry in config.FileCopyBlackList)
                    {
                        if (Regex.IsMatch(fileName, WildcardToRegex(blackListEntry), RegexOptions.IgnoreCase))
                        {
                            Console.ForegroundColor = ConsoleColor.DarkGray;
                            Console.WriteLine("Ignore {0}", path);
                            Console.ForegroundColor = ConsoleColor.Gray;
                            return(false);
                        }
                    }
                    Console.WriteLine("Copy   {0}", path);
                    return(true);
                });
                if (!string.IsNullOrEmpty(config.AdditionalFileDir) && Directory.Exists(config.AdditionalFileDir))
                {
                    CopyDirectory(config.AdditionalFileDir, config.IntermediateTargetDir, true);
                }
            }
            Console.WriteLine("===============================================================================");
            Console.WriteLine();
            Console.WriteLine();

            // Create the ZIP package
            Console.WriteLine("============================== Create ZIP Package =============================");
            {
                Console.WriteLine("Package Path: {0}", packagePath);
                if (!Directory.Exists(config.PackageDir))
                {
                    Directory.CreateDirectory(config.PackageDir);
                }

                ZipFile  package = new ZipFile();
                string[] files   = Directory.GetFiles(config.IntermediateTargetDir, "*", SearchOption.AllDirectories);
                package.AddFiles(files);
                package.Save(packagePath);
            }
            Console.WriteLine("===============================================================================");
            Console.WriteLine();
            Console.WriteLine();

            // Cleanup
            Console.WriteLine("=================================== Cleanup ===================================");
            {
                Console.WriteLine("Deleting target directory '{0}'", config.IntermediateTargetDir);
                if (Directory.Exists(config.IntermediateTargetDir))
                {
                    Directory.Delete(config.IntermediateTargetDir, true);
                }
            }
            Console.WriteLine("===============================================================================");
            Console.WriteLine();
            Console.WriteLine();

            // Build all NuGet Packages
            Console.WriteLine("============================= Build NuGet Packages ============================");
            {
                bool nugetFound      = File.Exists(config.NuGetPath);
                bool nugetSpecsFound = Directory.Exists(config.NuGetPackageSpecsDir);
                if (nugetFound && nugetSpecsFound)
                {
                    if (!Directory.Exists(config.NuGetPackageTargetDir))
                    {
                        Directory.CreateDirectory(config.NuGetPackageTargetDir);
                    }

                    Console.WriteLine("Deleting old package files in '{0}'...", config.NuGetPackageTargetDir);
                    foreach (string file in Directory.EnumerateFiles(config.NuGetPackageTargetDir, "*.nupkg", SearchOption.TopDirectoryOnly))
                    {
                        File.Delete(file);
                    }

                    Console.WriteLine("Creating packages from '{0}'...", config.NuGetPackageSpecsDir);
                    foreach (string file in Directory.EnumerateFiles(config.NuGetPackageSpecsDir, "*.nuspec", SearchOption.AllDirectories))
                    {
                        string fileAbs = Path.GetFullPath(file);
                        ExecuteCommand(Path.GetFullPath(config.NuGetPath) + " pack " + fileAbs, config.NuGetPackageTargetDir, false);
                    }
                }
                else if (!nugetFound)
                {
                    throw new ApplicationException(string.Format("Can't find NuGet command line tool '{0}'.", config.NuGetPath));
                }
                else if (!nugetSpecsFound)
                {
                    throw new ApplicationException(string.Format("Can't find NuGet package specs directory '{0}'.", config.NuGetPackageSpecsDir));
                }
            }
            Console.WriteLine("===============================================================================");
            Console.WriteLine();
            Console.WriteLine();

            // Copy ZIP Package
            if (!string.IsNullOrWhiteSpace(config.CopyPackageTo))
            {
                Console.WriteLine("=============================== Copy ZIP Package ==============================");
                {
                    Console.WriteLine("Copying package to '{0}'", config.CopyPackageTo);
                    if (!Directory.Exists(config.CopyPackageTo))
                    {
                        Directory.CreateDirectory(config.CopyPackageTo);
                    }
                    File.Copy(packagePath, Path.Combine(config.CopyPackageTo, config.PackageName), true);
                }
                Console.WriteLine("===============================================================================");
                Console.WriteLine();
                Console.WriteLine();
            }
        }
Exemplo n.º 11
0
        public static BuildResult Build(
            string solutionFile, string buildConfiguration = null,
            string buildPlatform = null, string buildTarget = "Build",
            string logVerbosity  = null, bool?inProcess     = null
            )
        {
            bool defaultInProcess = Debugger.IsAttached;

#if WINDOWS
            if (!inProcess.GetValueOrDefault(defaultInProcess))
            {
                var argsDict = new Dictionary <string, object> {
                    { "solutionFile", solutionFile },
                    { "buildConfiguration", buildConfiguration },
                    { "buildPlatform", buildPlatform },
                    { "buildTarget", buildTarget },
                    { "logVerbosity", logVerbosity }
                };
                return(OutOfProcessBuild(argsDict));
            }

            string configString = String.Format("{0}|{1}", buildConfiguration ?? "<default>", buildPlatform ?? "<default>");

            if ((buildConfiguration ?? buildPlatform) != null)
            {
                Console.Error.WriteLine("// Running target '{2}' of '{0}' ({1}) ...", JSIL.Compiler.Program.ShortenPath(solutionFile), configString, buildTarget);
            }
            else
            {
                Console.Error.WriteLine("// Running target '{1}' of '{0}' ...", JSIL.Compiler.Program.ShortenPath(solutionFile), buildTarget);
            }

            var pc               = new ProjectCollection();
            var parms            = new BuildParameters(pc);
            var globalProperties = new Dictionary <string, string> {
                { "JSIL", "building" },
                { "JSILVersion", Assembly.GetExecutingAssembly().GetName().ToString() }
            };

            var             hostServices  = new HostServices();
            var             eventRecorder = new BuildEventRecorder();
            LoggerVerbosity _logVerbosity;

            if ((logVerbosity == null) || !Enum.TryParse(logVerbosity, out _logVerbosity))
            {
                _logVerbosity = LoggerVerbosity.Quiet;
            }

            parms.Loggers = new ILogger[] {
                new ConsoleLogger(_logVerbosity), eventRecorder
            };

            var manager = BuildManager.DefaultBuildManager;

            Console.Error.Write("// Generating MSBuild projects for solution '{0}'...", Path.GetFileName(solutionFile));
            // Begin a fake build so the manager has a logger available.
            manager.BeginBuild(parms);

            var projects = ParseSolutionFile(
                solutionFile, buildConfiguration, buildPlatform,
                globalProperties, manager
                );

            manager.EndBuild();
            Console.Error.WriteLine(" {0} project(s) generated.", projects.Length);

            if (File.ReadAllText(solutionFile).Contains("ProjectSection(ProjectDependencies)"))
            {
                Console.Error.WriteLine("// WARNING: Your solution file contains project dependencies. MSBuild ignores these, so your build may fail. If it does, try building it in Visual Studio first to resolve the dependencies.");
            }

            var allItemsBuilt = new List <BuiltItem>();
            var resultFiles   = new HashSet <string>();

            foreach (var project in projects)
            {
                // Save out the generated msbuild project for each solution, to aid debugging.
                try {
                    project.ToProjectRootElement().Save(project.FullPath, Encoding.UTF8);
                } catch (Exception exc) {
                    Console.Error.WriteLine("// Failed to save generated project '{0}': {1}", Path.GetFileName(project.FullPath), exc.Message);
                }
            }

            foreach (var project in projects)
            {
                Console.Error.WriteLine("// Building project '{0}'...", project.FullPath);

                var request = new BuildRequestData(
                    project,
                    new string[] { buildTarget },
                    hostServices, BuildRequestDataFlags.None
                    );

                Microsoft.Build.Execution.BuildResult result;
                try {
                    result = manager.Build(parms, request);
                } catch (Exception exc) {
                    Console.Error.WriteLine("// Compilation failed: {0}", exc.Message);
                    continue;
                }

                allItemsBuilt.AddRange(ExtractChildProjectResults(manager));

                foreach (var kvp in result.ResultsByTarget)
                {
                    var targetResult = kvp.Value;

                    if ((targetResult.Exception != null) || (targetResult.ResultCode == TargetResultCode.Failure))
                    {
                        string errorMessage = "Unknown error";
                        if (targetResult.Exception != null)
                        {
                            errorMessage = targetResult.Exception.Message;
                        }
                        Console.Error.WriteLine("// Compilation failed for target '{0}': {1}", kvp.Key, errorMessage);
                    }
                }
            }

            // ResultsByTarget doesn't reliably produce all the output executables, so we must
            //  extract them by hand.
            foreach (var builtItem in allItemsBuilt)
            {
                if (builtItem.TargetName != "Build")
                {
                    continue;
                }

                if (!File.Exists(builtItem.OutputPath))
                {
                    Console.Error.WriteLine("// Ignoring nonexistent build output '" + Path.GetFileName(builtItem.OutputPath) + "'.");
                    continue;
                }

                var extension = Path.GetExtension(builtItem.OutputPath).ToLowerInvariant();

                switch (extension)
                {
                case ".exe":
                case ".dll":
                    resultFiles.Add(builtItem.OutputPath);
                    break;

                default:
                    Console.Error.WriteLine("// Ignoring build output '" + Path.GetFileName(builtItem.OutputPath) + "' due to unknown file type.");
                    break;
                }
            }

            return(new BuildResult(
                       Path.GetFullPath(solutionFile),
                       resultFiles.ToArray(),
                       eventRecorder.ProjectsById.Values.ToArray(),
                       eventRecorder.TargetFiles.ToArray(),
                       allItemsBuilt.ToArray()
                       ));
#else // !WINDOWS
            throw new NotImplementedException("Solution building is only supported on Windows when JSILc is compiled using MSBuild/Visual Studio.");
#endif
        }
Exemplo n.º 12
0
        /// <summary>
        /// Compiles the specified solution path.
        /// </summary>
        /// <param name="solutionPath">The solution path.</param>
        /// <param name="outputDir">The output dir.</param>
        /// <param name="configuration">The configuration.</param>
        /// <param name="platform">The platform.</param>
        /// <param name="emit">if set to <c>true</c> [emit].</param>
        /// <returns></returns>
        public static bool Compile(string solutionPath, string outputDir, string configuration = "Debug", string platform = "Any CPU", bool emit = false)
        {
            ProjectCollection pc = new ProjectCollection();

            if (emit)
            {
                SetEnv(EmitSolution, "1");
                SetEnv("MSBUILD_EXE_PATH", @"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin");
                SetEnv("VSINSTALLDIR", @"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional");
                SetEnv("VisualStudioVersion", @"15.0");

                if (Directory.GetFiles(Path.GetDirectoryName(solutionPath), "*.cache", SearchOption.TopDirectoryOnly).Length == 0)
                {
                    var foo = SolutionWrapperProject.Generate(
                        solutionPath,
                        ToolLocationHelper.CurrentToolsVersion,
                        null);

                    //Console.WriteLine(foo);
                }
            }
            else if (!emit && Environment.GetEnvironmentVariable(EmitSolution, EnvironmentVariableTarget.Machine) == "1")
            {
                Environment.SetEnvironmentVariable(EmitSolution, "0", EnvironmentVariableTarget.Machine);
            }

            // THERE ARE A LOT OF PROPERTIES HERE, THESE MAP TO THE MSBUILD CLI PROPERTIES

            Dictionary <string, string> globalProperties = new Dictionary <string, string>
            {
                { "Configuration", configuration }, // always "Debug"
                { "Platform", platform },           // always "Any CPU"
                { "ToolsVersion", ToolLocationHelper.CurrentToolsVersion },
                { "OutputPath", outputDir },
            };

            var logger = new ConsoleLogger();

            BuildParameters bp = new BuildParameters(pc)
            {
                Loggers = new[] { logger }
            };

            BuildRequestData buildRequest = new BuildRequestData(
                projectFullPath: solutionPath,
                globalProperties: globalProperties,
                toolsVersion: ToolLocationHelper.CurrentToolsVersion,
                targetsToBuild: new string[] { "Build" },
                hostServices: null);

            // THIS IS WHERE THE MAGIC HAPPENS - IN PROCESS MSBUILD
            try
            {
                BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, buildRequest);

                // A SIMPLE WAY TO CHECK THE RESULT
                return(buildResult.OverallResult == BuildResultCode.Success);
            }
            catch
            {
                throw;
            }
            finally
            {
                pc.UnregisterAllLoggers();
                logger.Shutdown();
            }
        }
Exemplo n.º 13
0
 private static BuildResult ExecuteBuild(BuildManager buildManager, BuildRequestData request) => buildManager.PendBuildRequest(request).Execute();
Exemplo n.º 14
0
 /// <summary>
 /// Initializes a configuration from a BuildRequestData structure.  Used by the BuildManager.
 /// Figures out the correct tools version to use, falling back to the provided default if necessary.
 /// May throw InvalidProjectFileException.
 /// </summary>
 /// <param name="data">The data containing the configuration information.</param>
 /// <param name="defaultToolsVersion">The default ToolsVersion to use as a fallback</param>
 /// <param name="getToolset">Callback used to get a Toolset based on a ToolsVersion</param>
 internal BuildRequestConfiguration(BuildRequestData data, string defaultToolsVersion, Utilities.GetToolset getToolset = null)
     : this(0, data, defaultToolsVersion, getToolset)
 {
 }
Exemplo n.º 15
0
        public static string CreateNewProject(string projName, string projFolder, ProjectTemplateInfo template)
        {
            // Create project folder
            projFolder = Path.Combine(projFolder, projName);
            if (!Directory.Exists(projFolder))
            {
                Directory.CreateDirectory(projFolder);
            }

            // Extract template
            if (template.SpecialTag == ProjectTemplateInfo.SpecialInfo.None)
            {
                template.ExtractTo(projFolder);

                // Update main directory
                foreach (string srcFile in Directory.GetFiles(Environment.CurrentDirectory, "*", SearchOption.TopDirectoryOnly))
                {
                    if (Path.GetFileName(srcFile) == "appdata.dat")
                    {
                        continue;
                    }
                    if (Path.GetFileName(srcFile) == "defaultuserdata.dat")
                    {
                        continue;
                    }
                    string dstFile = Path.Combine(projFolder, Path.GetFileName(srcFile));
                    File.Copy(srcFile, dstFile, true);
                }

                // Update plugin directory
                foreach (string dstFile in Directory.GetFiles(Path.Combine(projFolder, DualityApp.PluginDirectory), "*", SearchOption.AllDirectories))
                {
                    string srcFile = Path.Combine(DualityApp.PluginDirectory, Path.GetFileName(dstFile));
                    if (File.Exists(srcFile))
                    {
                        File.Copy(srcFile, dstFile, true);
                    }
                }
            }
            else if (template.SpecialTag == ProjectTemplateInfo.SpecialInfo.Current)
            {
                DualityEditorApp.SaveAllProjectData();
                PathHelper.CopyDirectory(Environment.CurrentDirectory, projFolder, true, delegate(string path)
                {
                    bool isDir      = Directory.Exists(path);
                    string fullPath = Path.GetFullPath(path);
                    if (isDir)
                    {
                        return(fullPath != Path.GetFullPath(EditorHelper.BackupDirectory));
                    }
                    else
                    {
                        return(true);
                    }
                });
            }
            else
            {
                PathHelper.CopyDirectory(Environment.CurrentDirectory, projFolder, true, delegate(string path)
                {
                    bool isDir      = Directory.Exists(path);
                    string fullPath = Path.GetFullPath(path);
                    if (isDir)
                    {
                        return
                        (fullPath != Path.GetFullPath(DualityApp.DataDirectory) &&
                         fullPath != Path.GetFullPath(EditorHelper.SourceDirectory) &&
                         fullPath != Path.GetFullPath(EditorHelper.BackupDirectory));
                    }
                    else
                    {
                        string fileName = Path.GetFileName(fullPath);
                        return(fileName != "appdata.dat" && fileName != "defaultuserdata.dat" && fileName != "designtimedata.dat");
                    }
                });
            }

            // Adjust current directory for further operations
            string oldPath = Environment.CurrentDirectory;

            Environment.CurrentDirectory = projFolder;

            // Initialize content
            if (Directory.Exists(DualityApp.DataDirectory))
            {
                // Read content source code data (needed to rename classes / namespaces)
                string oldRootNamespaceNameCore;
                string newRootNamespaceNameCore;
                DualityEditorApp.ReadPluginSourceCodeContentData(out oldRootNamespaceNameCore, out newRootNamespaceNameCore);

                // Rename classes / namespaces
                List <string> resFiles = Resource.GetResourceFiles(DualityApp.DataDirectory);
                foreach (string resFile in resFiles)
                {
                    MetaFormatHelper.FilePerformAction(resFile, d => d.ReplaceTypeStrings(oldRootNamespaceNameCore, newRootNamespaceNameCore));
                }
            }

            // Initialize AppData
            DualityAppData data;

            if (File.Exists(DualityApp.AppDataPath))
            {
                try
                {
                    using (FileStream str = File.OpenRead(DualityApp.AppDataPath))
                    {
                        using (var formatter = Formatter.Create(str))
                        {
                            data = formatter.ReadObject() as DualityAppData ?? new DualityAppData();
                        }
                    }
                }
                catch (Exception) { data = new DualityAppData(); }
            }
            else
            {
                data = new DualityAppData();
            }
            data.AppName    = projName;
            data.AuthorName = Environment.UserName;
            data.Version    = 0;
            using (FileStream str = File.Open(DualityApp.AppDataPath, FileMode.Create))
            {
                using (var formatter = Formatter.Create(str, FormattingMethod.Binary))
                {
                    formatter.WriteObject(data);
                }
            }

            // Initialize source code
            DualityEditorApp.InitPluginSourceCode();             // Force re-init to update namespaces, etc.
            DualityEditorApp.UpdatePluginSourceCode();

            // Compile plugins
            var buildProperties = new Dictionary <string, string>();

            buildProperties["Configuration"] = "Release";
            var buildRequest    = new BuildRequestData(EditorHelper.SourceCodeSolutionFile, buildProperties, null, new string[] { "Build" }, null);
            var buildParameters = new BuildParameters();
            var buildResult     = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest);

            Environment.CurrentDirectory = oldPath;
            return(Path.Combine(projFolder, "DualityEditor.exe"));
        }
Exemplo n.º 16
0
        /// <summary>
        /// Compiles the project and produces an output assembly
        /// </summary>
        /// <returns>
        /// Full name of the output assembly file.
        /// </returns>
        public static CompileResult Compile(CompileProject project)
        {
            string outputDirectory = string.Empty;
            if (project == null)
            {
                throw new ArgumentNullException(CompileMessages.ProjectNull);
            }

            try
            {
                //Create output directory for the compile project necessary files
                outputDirectory = CreateBuildDirectory();

                //Create necessary files for MSBuild
                CreateProjectFiles(project, outputDirectory);

                // Once all the temporary files are created, we can call MsBuild to compile.
                // NOTE: If you want to dynamic build, the Targect Framework of this project MUST be .NET Framework 4.
                // MUST NOT be the Client Profile, or you cannot reference Microsoft.Build.dll
                using (var projectPropertiesCollection = new ProjectCollection())
                {
                    var globalProperty = new Dictionary<string, string>();
                    var buildRequest = new BuildRequestData(
                        Path.Combine(outputDirectory, project.ProjectName) + ".csproj", globalProperty, null, new[] { "Build" }, null);

                    var buildResult =
                        BuildManager.DefaultBuildManager.Build(new BuildParameters(projectPropertiesCollection),
                                                               buildRequest);

                    if (buildResult == null)
                    {
                        return new CompileResult(BuildResultCode.Failure, outputDirectory, null);
                    }
                    switch (buildResult.OverallResult)
                    {
                        case BuildResultCode.Success:
                            var filePath = Path.Combine(outputDirectory, project.ProjectName) + ".dll";
                            var result = new CompileResult(buildResult.OverallResult, filePath, null);

                            try
                            {
                                File.Delete(Path.Combine(outputDirectory, PrivateKeyName));
                            }
                            catch (Exception) //Failure to delete key doesn't affect the result of the operation
                            {
                            }
                            return result;

                        default:
                            throw new CompileException(CompileMessages.ErrorCompiling, buildResult.Exception);
                    }
                }
            }
            catch (CompileException ex)
            {
                var result = new CompileResult(BuildResultCode.Failure, outputDirectory, ex);
                return result;
            }
            catch (AggregateException ex)
            {
                //More than one exception occured in one of the compile steps
                var result = new CompileResult(BuildResultCode.Failure, outputDirectory, ex);
                return result;
            }
            catch (ArgumentException ex)
            {
                var result = new CompileResult(BuildResultCode.Failure, outputDirectory, ex);
                return result;
            }
            catch (InvalidOperationException ex)
            {
                var result = new CompileResult(BuildResultCode.Failure, outputDirectory, ex);
                return result;
            }
        }
Exemplo n.º 17
0
        public static void Execute(ConfigProvider provider, Dictionary <string, string> props)
        {
            ValidateRequiredProperties(provider, props);
            if (provider.HasValidator)
            {
                ValidateVerbSpecificParameters(provider, props);
            }

            List <ILogger> loggers = new List <ILogger>
            {
                new ConsoleLogger(GetLoggerVerbosity(props))
            };

            props["GX_PROGRAM_DIR"] = Config.Default.GeneXusPath;

            string scriptPath = provider.AssemblyLocation.Replace(".dll", ".targets");

            if (!File.Exists(scriptPath))
            {
                scriptPath = provider.AssemblyLocation.Replace(".dll", ".msbuild");
                if (!File.Exists(scriptPath))
                {
                    throw new FileNotFoundException("Could not find MSBuild script", scriptPath);
                }
            }

            string[] targets = provider.Target.Split(new[] { ',' });
            foreach (string target in targets)
            {
                BuildRequestData requestData = new BuildRequestData(scriptPath, props, "4.0", new string[] { target }, null);

                var parameters = new BuildParameters(new ProjectCollection())
                {
                    Loggers = loggers
                };

                BuildResult result = BuildManager.DefaultBuildManager.Build(parameters, requestData);

                if (result.Exception != null)
                {
                    throw result.Exception;
                }

                if (result.OverallResult == BuildResultCode.Failure)
                {
                    return;
                }

                TargetResult tResult = result.ResultsByTarget[target];
                foreach (ITaskItem item in tResult.Items)
                {
                    string[] outProps = item.ItemSpec.Split(new[] { ',' });
                    foreach (string outProp in outProps)
                    {
                        string[] keyVal = outProp.Split(new[] { '=' });
                        if (keyVal.Length == 0 || keyVal.Length > 2)
                        {
                            continue;
                        }

                        if (keyVal.Length == 2)
                        {
                            props[keyVal[0]] = keyVal[1];
                        }
                        else if (keyVal.Length == 1)
                        {
                            props[keyVal[0]] = bool.TrueString;
                        }
                    }
                }
            }
        }