예제 #1
0
        protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            // Get active project
            // TODO: Instead of a custom code generator, we should have a context command or something like that.
            // This should also allow generation of multiple files

            var lines = Regex.Split(inputFileContent, "\r\n|\r|\n");

            if (lines.Length == 0 || lines[0].Length == 0)
            {
                throw new InvalidOperationException("Source should contain project filename.");
            }

            var projectFullName = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(inputFileName), lines[0]));

            if (!File.Exists(projectFullName))
            {
                throw new InvalidOperationException("Project file doesn't exist.");
            }

            string assemblyOutput, intermediateAssembly;

            // Get Evaluation Project
            Microsoft.Build.Evaluation.Project msbuildProject = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection.GetLoadedProjects(projectFullName).First();

            // Set ParadoxBuildStep variable and change IntermediateOutputPath
            var property1 = msbuildProject.SetProperty("ParadoxBuildStep", "StepData");
            var property2 = msbuildProject.SetProperty("IntermediateOutputPath", @"obj\StepData\");

            // Reevaluate dependent properties
            msbuildProject.ReevaluateIfNecessary();

            try
            {
                var outputPane = GetOutputPane();

                // Create logger
                var buildLogger = new IDEBuildLogger(outputPane, new TaskProvider(GlobalServiceProvider), VsHelper.GetCurrentHierarchy(GlobalServiceProvider));
                buildLogger.Verbosity = Microsoft.Build.Framework.LoggerVerbosity.Diagnostic;

                var evaluatedProperties = new Dictionary <string, Microsoft.Build.Evaluation.ProjectProperty>();
                foreach (var evaluatedProperty in msbuildProject.AllEvaluatedProperties)
                {
                    evaluatedProperties[evaluatedProperty.Name] = evaluatedProperty;
                }

                // Output properties
                foreach (var evaluatedProperty in evaluatedProperties)
                {
                    outputPane.OutputStringThreadSafe(string.Format(
                                                          "$({0}) = {1} was evaluated as {2}\n",
                                                          evaluatedProperty.Key,
                                                          evaluatedProperty.Value.UnevaluatedValue,
                                                          evaluatedProperty.Value.EvaluatedValue));
                }

                // Compile project (only intermediate assembly)
                // Dependencies will be built as well
                //var manager = BuildManager.DefaultBuildManager;
                using (var manager = new BuildManager())
                {
                    var pc = new Microsoft.Build.Evaluation.ProjectCollection();
                    var globalProperties = new Dictionary <string, string>();
                    globalProperties["SolutionName"] = evaluatedProperties["SolutionName"].EvaluatedValue;
                    globalProperties["SolutionDir"]  = evaluatedProperties["SolutionDir"].EvaluatedValue;
                    var projectInstance = new ProjectInstance(projectFullName, globalProperties, null);
                    var buildResult     = manager.Build(
                        new BuildParameters(pc)
                    {
                        Loggers         = new[] { buildLogger },
                        DetailedSummary = true,
                    },
                        new BuildRequestData(projectInstance, new[] { "Compile" }, null));

                    if (buildResult.OverallResult == BuildResultCode.Failure)
                    {
                        throw new InvalidOperationException(string.Format("Build of {0} failed.", projectFullName));
                    }
                }

                // Get TargetPath and IntermediateAssembly
                assemblyOutput       = msbuildProject.AllEvaluatedProperties.Last(x => x.Name == "TargetPath").EvaluatedValue;
                intermediateAssembly = msbuildProject.AllEvaluatedItems.First(x => x.ItemType == "IntermediateAssembly").EvaluatedInclude;
            }
            finally
            {
                msbuildProject.RemoveProperty(property1);
                msbuildProject.RemoveProperty(property2);
            }

            // Defer execution to current Paradox VS package plugin
            try
            {
                var remoteCommands = ParadoxCommandsProxy.GetProxy();
                return(remoteCommands.GenerateDataClasses(assemblyOutput, projectFullName, intermediateAssembly));
            }
            catch (Exception ex)
            {
                GeneratorError(4, ex.ToString(), 0, 0);

                return(new byte[0]);
            }
        }
예제 #2
0
        private static async Task CleanIntermediateAsset(DTE2 dte, Project project)
        {
            if (project.FileName == null || Path.GetExtension(project.FileName) != ".csproj")
                return;

            // Find current project active configuration
            var configManager = project.ConfigurationManager;
            var activeConfig = configManager.ActiveConfiguration;

            // Get global parameters for Configuration and Platform
            var globalProperties = new Dictionary<string, string>();
            globalProperties["Configuration"] = activeConfig.ConfigurationName;
            globalProperties["Platform"] = activeConfig.PlatformName == "Any CPU" ? "AnyCPU" : activeConfig.PlatformName;

            // Check if project has a SiliconStudioCurrentPackagePath
            var projectInstance = new ProjectInstance(project.FileName, globalProperties, null);
            var packagePathProperty = projectInstance.Properties.FirstOrDefault(x => x.Name == "SiliconStudioCurrentPackagePath");
            if (packagePathProperty == null)
                return;

            // Prepare build request
            var request = new BuildRequestData(project.FileName, globalProperties, null, new[] { "SiliconStudioCleanAsset" }, null);
            var pc = new Microsoft.Build.Evaluation.ProjectCollection();
            var buildParameters = new BuildParameters(pc);
            var buildLogger = new IDEBuildLogger(GetOutputPane(), new TaskProvider(ServiceProvider), VsHelper.ToHierarchy(project));
            buildParameters.Loggers = new[] { buildLogger };

            // Trigger async build
            buildLogger.OutputWindowPane.OutputStringThreadSafe(string.Format("Cleaning assets for project {0}...\r\n", project.Name));
            BuildManager.DefaultBuildManager.BeginBuild(buildParameters);
            var submission = BuildManager.DefaultBuildManager.PendBuildRequest(request);
            BuildResult buildResult = await submission.ExecuteAsync();
            BuildManager.DefaultBuildManager.EndBuild();
            buildLogger.OutputWindowPane.OutputStringThreadSafe("Done\r\n");
        }