Пример #1
0
        ProjectInfoResponse HandleProjectInfoRequest(ProjectInfoRequest req)
        {
            var targetsPath = typeof(AvaloniaIdeTask).GetTypeInfo().Assembly.GetModules()[0].FullyQualifiedName;

            targetsPath = Path.Combine(Path.GetDirectoryName(targetsPath), "avalonia-ide.targets");
            var props = new Dictionary <string, string>
            {
                ["DesignTimeBuild"]               = "true",
                ["BuildProjectReferences"]        = "false",
                ["_ResolveReferenceDependencies"] = "true",
                ["SolutionDir"] = req.SolutionDirectory,
                ["ProvideCommandLineInvocation"] = "true",
                ["ProvideCommandLineArgs"]       = "true",
                ["SkipCompilerExecution"]        = "true",
                ["TargetFramework"] = req.TargetFramework,
                ["CustomBeforeMicrosoftCommonTargets"] = targetsPath,
                ["AvaloniaRandom"]           = Guid.NewGuid().ToString(),
                ["AvaloniaForceCoreCompile"] = "true"
            };
            var outputs = new Dictionary <string, ITaskItem[]>();

            if (!BuildEngine.BuildProjectFile(req.FullPath, new[] { "ResolveAssemblyReferences", "GetTargetPath", "AvaloniaGetCscCommandLine", "AvaloniaGetEmbeddedResources", "AvaloniaGetAvaloniaResources" },
                                              props, outputs))
            {
                throw new Exception("Build failed");
            }

            var result = new ProjectInfoResponse
            {
                TargetPath        = outputs["GetTargetPath"][0].ItemSpec,
                EmbeddedResources = outputs["AvaloniaGetEmbeddedResources"].Select(x => x.ItemSpec).ToList(),
                AvaloniaResources = outputs["AvaloniaGetAvaloniaResources"].Select(x => x.ItemSpec).ToList(),
            };

            if (outputs.ContainsKey("ResolveAssemblyReferences"))
            {
                result.MetaDataReferences = outputs["ResolveAssemblyReferences"].Select(x => x.ItemSpec).ToList();
            }
            if (outputs.ContainsKey("AvaloniaGetCscCommandLine"))
            {
                result.CscCommandLine = outputs["AvaloniaGetCscCommandLine"].Select(x => x.ItemSpec).ToList();
            }
            return(result);
        }
Пример #2
0
        public async Task <(ProjectInfo info, List <string> projectReferences, string targetPath)> LoadProject(string solutionDirectory, string projectFile)
        {
            lock (outputLines)
            {
                outputLines.Clear();
                errorLines.Clear();
            }

            return(await Task.Run(() =>
            {
                var project = XDocument.Load(projectFile);

                var projectReferences = project.Descendants("ProjectReference").Select(e => e.Attribute("Include").Value).ToList();

                var frameworks = GetTargetFrameworks(project);

                var targetFramework = frameworks.FirstOrDefault();

                if (targetFramework != null)
                {
                    Console.WriteLine($"Automatically selecting {targetFramework} as TargetFramework");
                }
                else
                {
                    //throw new Exception("Must specify target framework to load project.");
                    Console.WriteLine($"Non-Dotnet core project trying anyway.");
                    targetFramework = "";
                }

                ProjectInfoResponse loadData = null;

                try
                {
                    loadData = SendRequest(new ProjectInfoRequest {
                        SolutionDirectory = solutionDirectory, FullPath = projectFile, TargetFramework = targetFramework
                    });
                }
                catch (Exception)
                {
                    return (null, null, null);
                }

                requestComplete.WaitOne();

                if (loadData.CscCommandLine != null && loadData.CscCommandLine.Count > 0)
                {
                    var projectOptions = ParseArguments(loadData.CscCommandLine.Skip(1));

                    var projectInfo = ProjectInfo.Create(
                        ProjectId.CreateNewId(),
                        VersionStamp.Create(),
                        name: Path.GetFileNameWithoutExtension(projectFile),
                        assemblyName: Path.GetFileNameWithoutExtension(projectOptions.outputFile),
                        language: LanguageNames.CSharp,
                        filePath: projectFile,
                        outputFilePath: projectOptions.outputFile,
                        compilationOptions: projectOptions.compilationOptions,
                        parseOptions: projectOptions.parseOptions,
                        metadataReferences: loadData.MetaDataReferences.Select(ar => MetadataReference.CreateFromFile(ar, documentation: IoC.Get <AvalonStudio.Projects.OmniSharp.Roslyn.DocumentationProvider>()?.GetDocumentationProvider(ar))));

                    return (projectInfo, projectReferences, loadData.TargetPath);
                }
                else
                {
                    IoC.Get <IConsole>($"Project may have failed to load correctly: {Path.GetFileNameWithoutExtension(projectFile)}");

                    var projectInfo = ProjectInfo.Create(
                        ProjectId.CreateNewId(),
                        VersionStamp.Create(),
                        Path.GetFileNameWithoutExtension(projectFile), Path.GetFileNameWithoutExtension(projectFile),
                        LanguageNames.CSharp,
                        projectFile);

                    return (projectInfo, projectReferences, loadData?.TargetPath);
                }
            }));
        }