예제 #1
0
        private MainWindowViewModel?BuildMainWindowDataContext(MainWindow mainWindow)
        {
            var projFiles = Directory.GetFiles(Environment.CurrentDirectory, "*.*proj");

            var projectPath      = projFiles.Length == 1 ? projFiles[0] : OpenProjectFile();
            var projectDirectory = Path.GetDirectoryName(projectPath);

            // TODO: replace with File.Exists and Directory.Exists when nullable annotations are correct
            if (!FileExists(projectPath) || !DirectoryExists(projectDirectory))
            {
                return(null);
            }

            IDotNetSdkResolver dotnetSdkResolver = new DotNetSdkResolver();

            if (!dotnetSdkResolver.TryResolveSdkPath(projectDirectory, out var dotnetSdkPath))
            {
                return(null);
            }

            var dotnetSdkPaths = new DotNetSdkPaths(dotnetSdkPath);

            IMSBuildLoader msBuildLoader = new MSBuildLoader(dotnetSdkPaths);

            AssemblyLoadContext.Default.Resolving += (context, name) =>
            {
                if (name.Name != null && msBuildLoader.TryResolveMSBuildAssembly(context, name.Name, out var assembly))
                {
                    return(assembly);
                }

                return(null);
            };

            var msBuildProject = new MSBuildProject(dotnetSdkPaths, projectPath);

            return(new MainWindowViewModel(
                       msBuildProject,
                       new DialogService <UnsavedChangesDialog, UnsavedChangesDialogViewModel>(NewUnsavedChangesDialog, mainWindow),
                       new OpenFileDialogService(mainWindow),
                       new ThemeService(this)));
        }
예제 #2
0
        public MSBuildProject(DotNetSdkPaths dotnetSdkPaths, string projectPath)
        {
            _dotnetSdkPaths = dotnetSdkPaths;

            _projectPath = projectPath;

            var solutionDir = Path.GetDirectoryName(projectPath) ?? throw new InvalidOperationException();

            // Set global properties
            _globalProperties = new Dictionary <string, string>
            {
                { MSBuildProperties.SolutionDir, solutionDir },
                { MSBuildProperties.MSBuildExtensionsPath, _dotnetSdkPaths.ExtensionsPath },
                { MSBuildProperties.MSBuildSDKsPath, _dotnetSdkPaths.SdksPath },
                { MSBuildProperties.RoslynTargetsPath, _dotnetSdkPaths.RoslynTargetsPath },
                //{ MSBuildProperties.DesignTimeBuild, "true" },
                { MSBuildProperties.BuildProjectReferences, "false" },
                { MSBuildProperties.SkipCompilerExecution, "true" },
                { MSBuildProperties.ProvideCommandLineArgs, "true" },
                // Workaround for a problem with resource files, see https://github.com/dotnet/sdk/issues/346#issuecomment-257654120
                { MSBuildProperties.GenerateResourceMSBuildArchitecture, "CurrentArchitecture" }
            };
        }