示例#1
0
        MSProjFile ReloadProjectFile(FileSystem fs, IActivityMonitor m, Dictionary <NormalizedPath, MSProjFile> cache)
        {
            _file = MSProjFile.FindOrLoadProjectFile(fs, m, Path, cache);
            if (_file != null)
            {
                XElement f = _file.Document.Root
                             .Elements("PropertyGroup")
                             .Elements()
                             .Where(x => x.Name.LocalName == "TargetFramework" || x.Name.LocalName == "TargetFrameworks")
                             .SingleOrDefault();
                if (f == null)
                {
                    m.Error($"There must be one and only one TargetFramework or TargetFrameworks element in {Path}.");
                    _file = null;
                }
                else
                {
                    TargetFrameworks = Savors.FindOrCreate(f.Value);

                    LangVersion = _file.Document.Root.Elements("PropertyGroup").Elements("LangVersion").LastOrDefault()?.Value;
                    OutputType  = _file.Document.Root.Elements("PropertyGroup").Elements("OutputType").LastOrDefault()?.Value;
                    IsPackable  = (bool?)_file.Document.Root.Elements("PropertyGroup").Elements("IsPackable").LastOrDefault();
                    DoInitializeDependencies(m);
                    if (!_dependencies.IsInitialized)
                    {
                        _file = null;
                    }
                }
            }
            if (_file == null)
            {
                TargetFrameworks = Savors.EmptyTrait;
            }
            return(_file);
        }
示例#2
0
        internal void Initialize()
        {
            var start = new MSProjFile[] { this };

            _allFiles = start
                        .Concat(
                Imports.Where(i => i.ImportedFile != null)
                .SelectMany(i => i.ImportedFile.AllFiles))
                        .Distinct()
                        .ToArray();
        }
示例#3
0
        /// <summary>
        /// Finds from the cache or loads a Xml project file.
        /// </summary>
        /// <param name="m">The monitor to use.</param>
        /// <param name="path">The file path relative to the <see cref="FileSystem"/>.</param>
        /// <param name="cache">Cache by path.</param>
        /// <returns>The file or null if unable to load it.</returns>
        public static MSProjFile FindOrLoadProjectFile(
            FileSystem fs,
            IActivityMonitor m,
            NormalizedPath path,
            Dictionary <NormalizedPath, MSProjFile> cache)
        {
            if (cache.TryGetValue(path, out MSProjFile f))
            {
                return(f);
            }
            using (m.OpenTrace($"Loading project file {path}."))
            {
                try
                {
                    var fP = fs.GetFileInfo(path);
                    if (!fP.Exists)
                    {
                        m.Warn($"Unable to find project file '{path}'. This project is ignored. This may be a case sensivity issue!");
                        return(null);
                    }
                    XDocument content = fP.ReadAsXDocument();
                    var       imports = new List <Import>();
                    f           = new MSProjFile(path, content, imports);
                    cache[path] = f;
                    var folder = path.RemoveLastPart();
                    imports.AddRange(content.Root.Descendants("Import")
                                     .Where(i => i.Attribute("Sdk") == null)
                                     .Select(i => (E: i, P: (string)i.Attribute("Project")))
                                     .Where(i => i.P != null)
                                     .Select(i => new Import(i.E, FindOrLoadProjectFile(fs, m, folder.Combine(i.P).ResolveDots(), cache))));

                    f.Initialize();
                    return(f);
                }
                catch (Exception ex)
                {
                    m.Error(ex);
                    cache.Remove(path);
                    return(null);
                }
            }
        }
示例#4
0
        MSProjFile ReloadProjectFile(FileSystem fs, IActivityMonitor m, Dictionary <NormalizedPath, MSProjFile> cache)
        {
            _primaryFile = MSProjFile.FindOrLoadProjectFile(fs, m, Path, cache);
            if (_primaryFile != null)
            {
                XElement f = _primaryFile.Document.Root
                             .Elements("PropertyGroup")
                             .Elements()
                             .Where(x => x.Name.LocalName == "TargetFramework" || x.Name.LocalName == "TargetFrameworks")
                             .SingleOrDefault();
                if (f == null)
                {
                    m.Error($"There must be one and only one TargetFramework or TargetFrameworks element in {Path}.");
                    _primaryFile = null;
                }
                else
                {
                    TargetFrameworks = Savors.FindOrCreate(f.Value);

                    LangVersion = _primaryFile.Document.Root.Elements("PropertyGroup").Elements("LangVersion").LastOrDefault()?.Value;
                    OutputType  = _primaryFile.Document.Root.Elements("PropertyGroup").Elements("OutputType").LastOrDefault()?.Value;
                    IsPackable  = (bool?)_primaryFile.Document.Root.Elements("PropertyGroup").Elements("IsPackable").LastOrDefault();

                    bool useMicrosoftBuildCentralPackageVersions = _primaryFile.Document.Root.Elements("Sdk")
                                                                   .Attributes("Name")
                                                                   .Any(a => a.Value == "Microsoft.Build.CentralPackageVersions");
                    if (useMicrosoftBuildCentralPackageVersions)
                    {
                        NormalizedPath packageFile;
                        var            definer = _primaryFile.AllFiles.Select(file => file.Document.Root)
                                                 .SelectMany(root => root.Elements("PropertyGroup"))
                                                 .Elements()
                                                 .FirstOrDefault(e => e.Name.LocalName == "CentralPackagesFile");
                        if (definer != null)
                        {
                            m.Info($"Found Property '{definer}' that defines CentralPackagesFile.");
                            var fileDefiner = _primaryFile.AllFiles.Single(file => file.Document == definer.Document);
                            packageFile = definer.Value.Replace("$(MSBuildThisFileDirectory)", fileDefiner.Path.RemoveLastPart() + '/');
                        }
                        else
                        {
                            packageFile = Solution.SolutionFolderPath.AppendPart("Packages.props");
                        }
                        _centralPackagesFile = MSProjFile.FindOrLoadProjectFile(fs, m, packageFile, cache);
                        if (_centralPackagesFile == null)
                        {
                            // Emits an error: reading the missing Version attribute will fail.
                            m.Error($"Failed to read '{packageFile}' central package file.");
                        }
                    }
                    DoInitializeDependencies(m);
                    if (!_dependencies.IsInitialized)
                    {
                        _primaryFile = null;
                    }
                }
            }
            if (_primaryFile == null)
            {
                TargetFrameworks = Savors.EmptyTrait;
            }
            return(_primaryFile);
        }
示例#5
0
 internal Import(XElement e, MSProjFile f)
 {
     ImportElement = e;
     ImportedFile  = f;
 }