SolutionInfo.cs 4.9 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2

3
using System;
P
Pilchie 已提交
4
using System.Collections.Generic;
5
using Microsoft.CodeAnalysis.Shared.Extensions;
P
Pilchie 已提交
6 7 8 9 10 11 12 13 14
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis
{
    /// <summary>
    /// A class that represents all the arguments necessary to create a new solution instance.
    /// </summary>
    public sealed class SolutionInfo
    {
15 16
        internal SolutionAttributes Attributes { get; }

P
Pilchie 已提交
17 18 19
        /// <summary>
        /// The unique Id of the solution.
        /// </summary>
20
        public SolutionId Id => Attributes.Id;
P
Pilchie 已提交
21 22 23 24

        /// <summary>
        /// The version of the solution.
        /// </summary>
25
        public VersionStamp Version => Attributes.Version;
P
Pilchie 已提交
26 27 28 29

        /// <summary>
        /// The path to the solution file, or null if there is no solution file.
        /// </summary>
30
        public string FilePath => Attributes.FilePath;
P
Pilchie 已提交
31 32 33 34

        /// <summary>
        /// A list of projects initially associated with the solution.
        /// </summary>
35
        public IReadOnlyList<ProjectInfo> Projects { get; }
P
Pilchie 已提交
36

37
        private SolutionInfo(SolutionAttributes attributes, IEnumerable<ProjectInfo> projects)
P
Pilchie 已提交
38
        {
39 40
            Attributes = attributes;
            Projects = projects.ToImmutableReadOnlyListOrEmpty();
P
Pilchie 已提交
41 42 43 44 45 46 47 48 49 50 51
        }

        /// <summary>
        /// Create a new instance of a SolutionInfo.
        /// </summary>
        public static SolutionInfo Create(
            SolutionId id,
            VersionStamp version,
            string filePath = null,
            IEnumerable<ProjectInfo> projects = null)
        {
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
            return new SolutionInfo(new SolutionAttributes(id, version, filePath), projects);
        }

        private SolutionInfo With(
            SolutionAttributes attributes = null,
            IEnumerable<ProjectInfo> projects = null)
        {
            var newAttributes = attributes ?? Attributes;
            var newProjects = projects ?? Projects;

            if (newAttributes == Attributes &&
                newProjects == Projects)
            {
                return this;
            }

            return new SolutionInfo(newAttributes, newProjects);
        }

        internal SolutionInfo WithVersion(VersionStamp version)
        {
            return With(attributes: new SolutionAttributes(Attributes.Id, version, Attributes.FilePath));
        }

        internal SolutionInfo WithFilePath(string filePath)
        {
            return With(attributes: new SolutionAttributes(Attributes.Id, Attributes.Version, filePath));
        }

        internal SolutionInfo WithProjects(IEnumerable<ProjectInfo> projects)
        {
            return With(projects: projects);
        }

        /// <summary>
        /// type that contains information regarding this solution itself but
        /// no tree information such as project info
        /// </summary>
        internal class SolutionAttributes : IChecksummedObject, IObjectWritable
        {
            /// <summary>
            /// The unique Id of the solution.
            /// </summary>
            public SolutionId Id { get; }

            /// <summary>
            /// The version of the solution.
            /// </summary>
            public VersionStamp Version { get; }

            /// <summary>
            /// The path to the solution file, or null if there is no solution file.
            /// </summary>
            public string FilePath { get; }

            public SolutionAttributes(SolutionId id, VersionStamp version, string filePath)
            {
C
CyrusNajmabadi 已提交
109
                Id = id ?? throw new ArgumentNullException(nameof(id));
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
                Version = version;
                FilePath = filePath;
            }

            public void WriteTo(ObjectWriter writer)
            {
                Id.WriteTo(writer);

                // TODO: figure out a way to send version info over as well.
                //       right now, version get updated automatically, so 2 can't be exactly match
                // info.Version.WriteTo(writer);

                writer.WriteString(FilePath);
            }

            public static SolutionAttributes ReadFrom(ObjectReader reader)
            {
                var solutionId = SolutionId.ReadFrom(reader);
                // var version = VersionStamp.ReadFrom(reader);
                var filePath = reader.ReadString();

                return new SolutionAttributes(solutionId, VersionStamp.Create(), filePath);
            }

            private Checksum _lazyChecksum;
            Checksum IChecksummedObject.Checksum
            {
                get
                {
                    if (_lazyChecksum == null)
                    {
                        _lazyChecksum = Checksum.Create(this, nameof(SolutionAttributes));
                    }

                    return _lazyChecksum;
                }
            }
P
Pilchie 已提交
147 148
        }
    }
149
}