Ejemplo n.º 1
0
        // the host project or one of the .props file that it imports might contain some custom settings that needs to be copied, sth like
        // <NetCoreAppImplicitPackageVersion>2.0.0-beta-001607-00</NetCoreAppImplicitPackageVersion>
        // <RuntimeFrameworkVersion>2.0.0-beta-001607-00</RuntimeFrameworkVersion>
        internal (string customProperties, string sdkName) GetSettingsThatNeedsToBeCopied(TextReader streamReader, FileInfo projectFile)
        {
            if (!string.IsNullOrEmpty(RuntimeFrameworkVersion)) // some power users knows what to configure, just do it and copy nothing more
            {
                return($"<RuntimeFrameworkVersion>{RuntimeFrameworkVersion}</RuntimeFrameworkVersion>", DefaultSdkName);
            }

            var customProperties = new StringBuilder();
            var sdkName          = DefaultSdkName;

            string line;

            while ((line = streamReader.ReadLine()) != null)
            {
                var trimmedLine = line.Trim();

                foreach (string setting in SettingsWeWantToCopy)
                {
                    if (trimmedLine.Contains(setting))
                    {
                        customProperties.AppendLine(trimmedLine);
                    }
                }

                if (trimmedLine.StartsWith("<Import Project"))
                {
                    string propsFilePath = trimmedLine.Split('"')[1]; // its sth like   <Import Project="..\..\build\common.props" />
                    var    directoryName = projectFile.DirectoryName ?? throw new DirectoryNotFoundException(projectFile.DirectoryName);
                    string absolutePath  = File.Exists(propsFilePath)
                        ? propsFilePath                               // absolute path or relative to current dir
                        : Path.Combine(directoryName, propsFilePath); // relative to csproj

                    if (File.Exists(absolutePath))
                    {
                        using (var importedFile = new StreamReader(File.OpenRead(absolutePath)))
                            customProperties.Append(GetSettingsThatNeedsToBeCopied(importedFile, new FileInfo(absolutePath)).customProperties);
                    }
                }

                // custom SDKs are not added for non-netcoreapp apps (like net471), so when the TFM != netcoreapp we dont parse "<Import Sdk="
                // we don't allow for that mostly to prevent from edge cases like the following
                // <Import Sdk="Microsoft.NET.Sdk.WindowsDesktop" Project="Sdk.props" Condition="'$(TargetFramework)'=='netcoreapp3.0'"/>
                if (trimmedLine.StartsWith("<Project Sdk=\"") ||
                    (TargetFrameworkMoniker.StartsWith("netcoreapp", StringComparison.InvariantCultureIgnoreCase) && trimmedLine.StartsWith("<Import Sdk=\"")))
                {
                    sdkName = trimmedLine.Split('"')[1]; // its sth like Sdk="name"
                }
            }

            return(customProperties.ToString(), sdkName);
        }