public FileSystemPath GetActualAppPathForSolution()
        {
            if (mySolution.IsVirtualSolution())
            {
                return(FileSystemPath.Empty);
            }

            foreach (var project in GetTopLevelProjectWithReadLock(mySolution))
            {
                var path = myUnityProjectFileCache.GetAppPath(project);
                if (path != null)
                {
                    return(path);
                }
            }
            return(FileSystemPath.Empty);
        }
Example #2
0
        private void SetProjectLangVersion(IProject project, ISettingsStorageMountPoint mountPoint)
        {
            // Only if it's a generated project. Class libraries can use whatever language version they want
            if (!project.IsUnityGeneratedProject())
            {
                return;
            }

            #region Explanation
            // Make sure we don't suggest code changes that won't compile in Unity due to mismatched C# language levels
            // (e.g. C#6 "elvis" operator)
            //
            // * Unity prior to 5.5 uses an old mono compiler that only supports C# 4
            // * Unity 5.5 and later adds C# 6 support as an option. This is enabled by setting
            //   the API compatibility level to NET_4_6
            // * The CSharp60Support plugin replaces the compiler with either C# 6 or C# 7.0 or 8.0
            //   It can be recognised by a folder called `CSharp60Support` or `CSharp70Support` or `CSharp80Support`
            //   in the root of the project
            //   (https://bitbucket.org/alexzzzz/unity-c-5.0-and-6.0-integration)
            // * Note that since Unity 2017.2 till 2018.3, we've been special-cased in the Unity csproj generation
            //   and we've been getting v4.5 for old runtime and default values (4.7.1) for new. So where
            //   it says 3.5 below, that depends on the version of Unity. Older versions will give us 3.5,
            //   newer versions 4.5.
            //
            // Scenarios:
            // * No VSTU installed (including Unity 5.5)
            //   .csproj has NO `LangVersion`. `TargetFrameworkVersion` will be `v3.5`
            // * Early versions of VSTU
            //   .csproj has NO `LangVersion`. `TargetFrameworkVersion` will be `v3.5`
            // * Later versions of VSTU
            //   `LangVersion` is correctly set to "4". `TargetFrameworkVersion` will be `v3.5`
            //   OR `LangVersion` is set to "6" or "latest".
            // * VSTU for 5.5
            //   `LangVersion` is set to "default". `TargetFrameworkVersion` will be `v3.5` or `v4.6`
            //   Note that "default" for VS"15" or Rider will be C# 7.0!
            // * Unity3dRider is installed
            //   Uses Unity's own generation and adds correct `LangVersion`
            //   `TargetFrameworkVersion` will be correct for the selected runtime
            // * CSharp60Support is installed
            //   .csproj has NO `LangVersion`
            //   `TargetFrameworkVersion` is NOT accurate (support for C# 6 is not dependent on/trigger by .net 4.6)
            //   Look for `CSharp60Support` or `CSharp70Support` folders
            // * Unity 2018.x+. LangVersion is `latest`. MSBuild 16 treats `newest` as C# 8. Re# and Rider start suggesting it.
            //
            // Actions:
            // * If `LangVersion` is missing or "default"
            // then override based on `TargetFrameworkVersion` or presence of `CSharp60Support`/`CSharp70Support`
            // else do nothing
            // * If `LangVersion` is "latest"
            // then override based on `CSharp80Support` presence or LangVersion matching Roslyn bundled in Unity
            //
            // Notes:
            // * Unity and VSTU have two separate .csproj routines. VSTU adds extra references,
            //   the VSTU project flavour GUID and imports UnityVs.targets, which disables the
            //   `GenerateTargetFrameworkMonikerAttribute` target
            // * CSharp60Support post-processes the .csproj file directly if VSTU is not installed.
            //   If it is installed, it registers a delegate with `ProjectFilesGenerator.ProjectFileGeneration`
            //   and removes it before it's written to disk
            // * `LangVersion` can be conditionally specified, which makes checking for "default" awkward
            // * If Unity3dRider + CSharp60Support are both installed, last write wins
            //   Order of post-processing is non-deterministic, so Rider's LangVersion might be removed
            #endregion

            var languageLevel = ReSharperSettingsCSharpLanguageLevel.Default;
            if (IsLangVersionMissing(project) || IsLangVersionDefault(project))
            {
                // Support for https://bitbucket.org/alexzzzz/unity-c-5.0-and-6.0-integration
                // See also https://github.com/JetBrains/resharper-unity/issues/50#issuecomment-257611218
                if (project.Location.CombineWithShortName("CSharp70Support").ExistsDirectory)
                {
                    languageLevel = ReSharperSettingsCSharpLanguageLevel.CSharp70;
                }
                else if (project.Location.CombineWithShortName("CSharp60Support").ExistsDirectory)
                {
                    languageLevel = ReSharperSettingsCSharpLanguageLevel.CSharp60;
                }
                else
                {
                    languageLevel = IsTargetFrameworkAtLeast46(project)
                        ? ReSharperSettingsCSharpLanguageLevel.CSharp60
                        : ReSharperSettingsCSharpLanguageLevel.CSharp40;
                }
            }

            if (IsLangVersionLatest(project))
            {
                if (project.Location.CombineWithShortName("CSharp80Support").ExistsDirectory) // https://forum.unity.com/threads/would-the-roslyn-compiler-compile-c-8-0-preview.598069/
                {
                    languageLevel = ReSharperSettingsCSharpLanguageLevel.CSharp80;
                }
                else
                {
                    var appPath     = myUnityProjectFileCache.GetAppPath(project);
                    var contentPath = UnityInstallationFinder.GetApplicationContentsPath(appPath);
                    if (!contentPath.IsNullOrEmpty())
                    {
                        var roslynDir = contentPath.Combine("Tools").Combine("Roslyn");
                        if (roslynDir.ExistsDirectory)
                        {
                            languageLevel = myLanguageLevelProjectProperty.GetLatestAvailableLanguageLevel(roslynDir).ToSettingsLanguageLevel();
                        }
                    }
                }
            }

            // Always set a value. It's either the overridden value, or Default, which resets to whatever is in the
            // project file
            SetValue(mountPoint, (CSharpLanguageProjectSettings s) => s.LanguageLevel, languageLevel);
        }