Exemple #1
0
        public static bool TryGetGlobalSettings(string path, out GlobalSettings globalSettings)
        {
            globalSettings = null;
            string globalJsonPath = null;

            if (Path.GetFileName(path) == FileName)
            {
                globalJsonPath = path;
                path = Path.GetDirectoryName(path);
            }
            else if (!HasGlobalFile(path))
            {
                return false;
            }
            else
            {
                globalJsonPath = Path.Combine(path, FileName);
            }

            globalSettings = new GlobalSettings();

            try
            {
                using (var fs = File.OpenRead(globalJsonPath))
                {
                    var reader = new StreamReader(fs);
                    var jobject = JsonDeserializer.Deserialize(reader) as JsonObject;

                    if (jobject == null)
                    {
                        throw new InvalidOperationException("The JSON file can't be deserialized to a JSON object.");
                    }

                    var projectSearchPaths = jobject.ValueAsStringArray("projects") ??
                                             jobject.ValueAsStringArray("sources") ??
                                             new string[] { };

                    globalSettings.ProjectSearchPaths = new List<string>(projectSearchPaths);
                    globalSettings.PackagesPath = jobject.ValueAsString("packages");
                    globalSettings.FilePath = globalJsonPath;
                }
            }
            catch (Exception ex)
            {
                throw FileFormatException.Create(ex, globalJsonPath);
            }

            return true;
        }
Exemple #2
0
 internal ProjectContext(
     GlobalSettings globalSettings,
     ProjectDescription rootProject,
     NuGetFramework targetFramework,
     string runtimeIdentifier,
     string packagesDirectory,
     LibraryManager libraryManager)
 {
     GlobalSettings = globalSettings;
     RootProject = rootProject;
     TargetFramework = targetFramework;
     RuntimeIdentifier = runtimeIdentifier;
     PackagesDirectory = packagesDirectory;
     LibraryManager = libraryManager;
 }
        public ProjectContext Build()
        {
            ProjectDirectory = Project?.ProjectDirectory ?? ProjectDirectory;

            if (GlobalSettings == null)
            {
                RootDirectory = ProjectRootResolver.ResolveRootDirectory(ProjectDirectory);

                GlobalSettings globalSettings;
                if (GlobalSettings.TryGetGlobalSettings(RootDirectory, out globalSettings))
                {
                    GlobalSettings = globalSettings;
                }
            }

            RootDirectory           = GlobalSettings?.DirectoryPath ?? RootDirectory;
            PackagesDirectory       = PackagesDirectory ?? PackageDependencyProvider.ResolvePackagesPath(RootDirectory, GlobalSettings);
            ReferenceAssembliesPath = ReferenceAssembliesPath ?? GetDefaultReferenceAssembliesPath();

            LockFileLookup lockFileLookup = null;

            EnsureProjectLoaded();

            var projectLockJsonPath = Path.Combine(ProjectDirectory, LockFile.FileName);

            if (LockFile == null && File.Exists(projectLockJsonPath))
            {
                LockFile = LockFileReader.Read(projectLockJsonPath);
            }

            var    validLockFile             = true;
            string lockFileValidationMessage = null;

            if (LockFile != null)
            {
                validLockFile = LockFile.IsValidForProject(Project, out lockFileValidationMessage);

                lockFileLookup = new LockFileLookup(LockFile);
            }

            var libraries       = new Dictionary <LibraryKey, LibraryDescription>();
            var projectResolver = new ProjectDependencyProvider();

            var mainProject = projectResolver.GetDescription(TargetFramework, Project);

            // Add the main project
            libraries.Add(new LibraryKey(mainProject.Identity.Name), mainProject);

            LockFileTarget target = null;

            if (lockFileLookup != null)
            {
                target = SelectTarget(LockFile);
                if (target != null)
                {
                    var packageResolver = new PackageDependencyProvider(PackagesDirectory);
                    ScanLibraries(target, lockFileLookup, libraries, packageResolver, projectResolver);
                }
            }

            var  frameworkReferenceResolver          = new FrameworkReferenceResolver(ReferenceAssembliesPath);
            var  referenceAssemblyDependencyResolver = new ReferenceAssemblyDependencyResolver(frameworkReferenceResolver);
            bool requiresFrameworkAssemblies;

            // Resolve the dependencies
            ResolveDependencies(libraries, referenceAssemblyDependencyResolver, out requiresFrameworkAssemblies);

            var diagnostics = new List <DiagnosticMessage>();

            // REVIEW: Should this be in NuGet (possibly stored in the lock file?)
            if (LockFile == null)
            {
                diagnostics.Add(new DiagnosticMessage(
                                    ErrorCodes.NU1009,
                                    $"The expected lock file doesn't exist. Please run \"dotnet restore\" to generate a new lock file.",
                                    Path.Combine(Project.ProjectDirectory, LockFile.FileName),
                                    DiagnosticMessageSeverity.Error));
            }

            if (!validLockFile)
            {
                diagnostics.Add(new DiagnosticMessage(
                                    ErrorCodes.NU1006,
                                    $"{lockFileValidationMessage}. Please run \"dotnet restore\" to generate a new lock file.",
                                    Path.Combine(Project.ProjectDirectory, LockFile.FileName),
                                    DiagnosticMessageSeverity.Warning));
            }

            if (requiresFrameworkAssemblies)
            {
                var frameworkInfo = Project.GetTargetFramework(TargetFramework);

                if (string.IsNullOrEmpty(ReferenceAssembliesPath))
                {
                    // If there was an attempt to use reference assemblies but they were not installed
                    // report an error
                    diagnostics.Add(new DiagnosticMessage(
                                        ErrorCodes.DOTNET1012,
                                        $"The reference assemblies directory was not specified. You can set the location using the DOTNET_REFERENCE_ASSEMBLIES_PATH environment variable.",
                                        filePath: Project.ProjectFilePath,
                                        severity: DiagnosticMessageSeverity.Error,
                                        startLine: frameworkInfo.Line,
                                        startColumn: frameworkInfo.Column
                                        ));
                }
                else if (!frameworkReferenceResolver.IsInstalled(TargetFramework))
                {
                    // If there was an attempt to use reference assemblies but they were not installed
                    // report an error
                    diagnostics.Add(new DiagnosticMessage(
                                        ErrorCodes.DOTNET1011,
                                        $"Framework not installed: {TargetFramework.DotNetFrameworkName} in {ReferenceAssembliesPath}",
                                        filePath: Project.ProjectFilePath,
                                        severity: DiagnosticMessageSeverity.Error,
                                        startLine: frameworkInfo.Line,
                                        startColumn: frameworkInfo.Column
                                        ));
                }
            }

            // Create a library manager
            var libraryManager = new LibraryManager(libraries.Values.ToList(), diagnostics, Project.ProjectFilePath);

            return(new ProjectContext(
                       GlobalSettings,
                       mainProject,
                       TargetFramework,
                       target?.RuntimeIdentifier,
                       PackagesDirectory,
                       libraryManager));
        }