Exemple #1
0
 /// <summary>
 /// Add the given project as a child to this node.
 /// </summary>
 /// <param name="childProject">The child project to add.</param>
 public void AddChildProject(Project childProject)
 {
     AddChildNode(childProject);
 }
Exemple #2
0
        private static Workspace GetWorkspaceFromBuild(this StLogger.Build build, params string[] preprocessorSymbols)
        {
            StLogger.Project csproj = build.Children.OfType <StLogger.Project>().FirstOrDefault();
            if (csproj == null)
            {
                throw new InvalidOperationException("cannot find cs project build");
            }

            StLogger.Item[] compileItems = Array.Empty <StLogger.Item>();
            var             properties   = new Dictionary <string, StLogger.Property>();

            foreach (StLogger.Folder folder in csproj.Children.OfType <StLogger.Folder>())
            {
                if (folder.Name == "Items")
                {
                    StLogger.Folder compileFolder = folder.Children.OfType <StLogger.Folder>().FirstOrDefault(x => x.Name == "Compile");
                    if (compileFolder == null)
                    {
                        throw new InvalidOperationException("failed to get compililation documents");
                    }

                    compileItems = compileFolder.Children.OfType <StLogger.Item>().ToArray();
                }
                else if (folder.Name == "Properties")
                {
                    properties = folder.Children.OfType <StLogger.Property>().ToDictionary(x => x.Name);
                }
            }

            StLogger.Item[] assemblies = Array.Empty <StLogger.Item>();
            foreach (StLogger.Target target in csproj.Children.OfType <StLogger.Target>())
            {
                if (target.Name == "ResolveReferences")
                {
                    StLogger.Folder folder = target.Children.OfType <StLogger.Folder>().Where(x => x.Name == "TargetOutputs").FirstOrDefault();
                    if (folder == null)
                    {
                        throw new InvalidOperationException("cannot find result of resolving assembly");
                    }

                    assemblies = folder.Children.OfType <StLogger.Item>().ToArray();
                }
            }

            var     ws            = new AdhocWorkspace();
            Project roslynProject = ws.AddProject(Path.GetFileNameWithoutExtension(csproj.ProjectFile), Microsoft.CodeAnalysis.LanguageNames.CSharp);
            var     projectDir    = properties["ProjectDir"].Value;
            Guid    pguid         = properties.ContainsKey("ProjectGuid") ? Guid.Parse(properties["ProjectGuid"].Value) : Guid.NewGuid();
            var     projectGuid   = ProjectId.CreateFromSerialized(pguid);

            foreach (StLogger.Item compile in compileItems)
            {
                var filePath    = compile.Text;
                var absFilePath = Path.Combine(projectDir, filePath);
                roslynProject = roslynProject.AddDocument(filePath, File.ReadAllText(absFilePath)).Project;
            }

            foreach (StLogger.Item asm in assemblies)
            {
                roslynProject = roslynProject.AddMetadataReference(MetadataReference.CreateFromFile(asm.Text));
            }

            var compopt = roslynProject.CompilationOptions as CSharpCompilationOptions;

            compopt = roslynProject.CompilationOptions as CSharpCompilationOptions;
            compopt = compopt ?? new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
            OutputKind kind;

            switch (properties["OutputType"].Value)
            {
            case "Exe":
                kind = OutputKind.ConsoleApplication;
                break;

            case "Library":
                kind = OutputKind.DynamicallyLinkedLibrary;
                break;

            default:
                kind = OutputKind.DynamicallyLinkedLibrary;
                break;
            }

            roslynProject = roslynProject.WithCompilationOptions(compopt.WithOutputKind(kind).WithAllowUnsafe(true));
            var parseopt = roslynProject.ParseOptions as CSharpParseOptions;

            roslynProject = roslynProject.WithParseOptions(parseopt.WithPreprocessorSymbols(preprocessorSymbols));
            if (!ws.TryApplyChanges(roslynProject.Solution))
            {
                throw new InvalidOperationException("failed to apply solution changes to workspace");
            }

            return(ws);
        }
Exemple #3
0
        /// <summary>
        /// Gets a project instance for the given identifier. Will create if it doesn't exist.
        /// </summary>
        /// <remarks>If the ProjectStartedEventArgs is not known at this time (null), a stub project is created.</remarks>
        /// <param name="projectId">The project identifier.</param>
        /// <param name="projectStartedEventArgs">The <see cref="ProjectStartedEventArgs"/> instance containing the event data.</param>
        /// <param name="parentProject">The parent project, if any.</param>
        /// <returns>Project object</returns>
        public Project GetOrAddProject(int projectId, ProjectStartedEventArgs projectStartedEventArgs = null, Project parentProject = null)
        {
            Project result = _projectIdToProjectMap.GetOrAdd(projectId,
                id =>
                    new Project(id, projectStartedEventArgs,
                        parentProject == null ? Properties : parentProject.Properties));

            if (projectStartedEventArgs != null)
            {
                result.TryUpdate(projectStartedEventArgs);
            }

            return result;
        }