Exemple #1
0
        public static LockFileTargetLibrary CreateLockFileTargetLibrary(LockFileProjectLibrary library,
                                                                        Runtime.Project projectInfo,
                                                                        RestoreContext context)
        {
            var lockFileLib = new LockFileTargetLibrary
            {
                Name    = library.Name,
                Version = library.Version,
                Type    = "project"
            };

            var targetFrameworkInfo = projectInfo.GetTargetFramework(context.FrameworkName);
            var dependencies        = projectInfo.Dependencies.Concat(targetFrameworkInfo.Dependencies);

            foreach (var dependency in dependencies)
            {
                if (dependency.LibraryRange.IsGacOrFrameworkReference)
                {
                    lockFileLib.FrameworkAssemblies.Add(
                        LibraryRange.GetAssemblyName(dependency.LibraryRange.Name));
                }
                else
                {
                    lockFileLib.Dependencies.Add(new PackageDependency(
                                                     dependency.LibraryRange.Name,
                                                     dependency.LibraryRange.VersionRange));
                }
            }

            return(lockFileLib);
        }
Exemple #2
0
        public MSBuildProjectDescription GetDescription(NuGetFramework targetFramework,
                                                        LockFileProjectLibrary projectLibrary,
                                                        LockFileTargetLibrary targetLibrary,
                                                        bool isDesignTime)
        {
            // During design time fragment file could be missing. When fragment file is missing none of the
            // assets can be found but it is acceptable during design time.
            var compatible = targetLibrary.FrameworkAssemblies.Any() ||
                             targetLibrary.CompileTimeAssemblies.Any() ||
                             targetLibrary.RuntimeAssemblies.Any() ||
                             isDesignTime;

            var dependencies = new List <LibraryRange>(targetLibrary.Dependencies.Count + targetLibrary.FrameworkAssemblies.Count);

            PopulateDependencies(dependencies, targetLibrary, targetFramework);

            var msbuildProjectFilePath      = GetMSBuildProjectFilePath(projectLibrary);
            var msbuildProjectDirectoryPath = Path.GetDirectoryName(msbuildProjectFilePath);

            var exists = Directory.Exists(msbuildProjectDirectoryPath);

            var projectFile = projectLibrary.Path == null ? null : _projectResolver(projectLibrary.Path);

            var msbuildPackageDescription = new MSBuildProjectDescription(
                msbuildProjectDirectoryPath,
                msbuildProjectFilePath,
                projectLibrary,
                targetLibrary,
                projectFile,
                dependencies,
                compatible,
                resolved: compatible && exists);

            return(msbuildPackageDescription);
        }
Exemple #3
0
        private string GetMSBuildProjectFilePath(LockFileProjectLibrary projectLibrary)
        {
            if (_rootProject == null)
            {
                throw new InvalidOperationException("Root xproj project does not exist. Cannot compute the path of its referenced csproj projects.");
            }

            var rootProjectPath        = Path.GetDirectoryName(_rootProject.ProjectFilePath);
            var msbuildProjectFilePath = Path.Combine(rootProjectPath, projectLibrary.MSBuildProject);

            return(Path.GetFullPath(msbuildProjectFilePath));
        }
Exemple #4
0
        public static bool IsMSBuildProjectLibrary(LockFileProjectLibrary projectLibrary)
        {
            var msbuildProjectPath = projectLibrary.MSBuildProject;

            if (msbuildProjectPath == null)
            {
                return(false);
            }

            var extension = Path.GetExtension(msbuildProjectPath);

            return(!string.Equals(extension, ".xproj", StringComparison.OrdinalIgnoreCase));
        }
Exemple #5
0
        public static LockFileProjectLibrary CreateLockFileProjectLibrary(Runtime.Project project,
                                                                          Runtime.Project projectDependency)
        {
            var result = new LockFileProjectLibrary()
            {
                Name    = projectDependency.Name,
                Version = projectDependency.Version
            };

            result.Path = PathUtility.GetRelativePath(project.ProjectFilePath, projectDependency.ProjectFilePath, '/');

            return(result);
        }
Exemple #6
0
        public static LockFileProjectLibrary CreateLockFileProjectLibrary(LockFileProjectLibrary previousLibrary,
                                                                          Runtime.Project project,
                                                                          Runtime.Project library)
        {
            var result = new LockFileProjectLibrary()
            {
                Name    = library.Name,
                Version = library.Version
            };

            if (previousLibrary?.Name == library.Name && previousLibrary?.Version == library.Version)
            {
                result.Path = previousLibrary.Path;
            }
            else
            {
                result.Path = PathUtility.GetRelativePath(project.ProjectFilePath, library.ProjectFilePath, '/');
            }

            return(result);
        }
 public MSBuildProjectDescription(
     string path,
     string msbuildProjectPath,
     LockFileProjectLibrary projectLibrary,
     LockFileTargetLibrary lockFileLibrary,
     Project projectFile,
     IEnumerable <LibraryRange> dependencies,
     bool compatible,
     bool resolved)
     : base(
         new LibraryIdentity(projectLibrary.Name, projectLibrary.Version, LibraryType.MSBuildProject),
         string.Empty,   //msbuild projects don't have hashes
         path,
         lockFileLibrary,
         dependencies,
         resolved: resolved,
         compatible: compatible,
         framework: null)
 {
     MSBuildProjectPath = msbuildProjectPath;
     ProjectFile        = projectFile;
     ProjectLibrary     = projectLibrary;
 }
Exemple #8
0
        public MSBuildProjectDescription GetDescription(NuGetFramework targetFramework, LockFileProjectLibrary projectLibrary, LockFileTargetLibrary targetLibrary)
        {
            var compatible = targetLibrary.FrameworkAssemblies.Any() ||
                             targetLibrary.CompileTimeAssemblies.Any() ||
                             targetLibrary.RuntimeAssemblies.Any();

            var dependencies = new List <LibraryRange>(targetLibrary.Dependencies.Count + targetLibrary.FrameworkAssemblies.Count);

            PopulateDependencies(dependencies, targetLibrary, targetFramework);

            var msbuildProjectFilePath      = GetMSBuildProjectFilePath(projectLibrary);
            var msbuildProjectDirectoryPath = Path.GetDirectoryName(msbuildProjectFilePath);

            var exists = Directory.Exists(msbuildProjectDirectoryPath);

            var projectFile = projectLibrary.Path == null ? null : _projectResolver(projectLibrary.Path);

            var msbuildPackageDescription = new MSBuildProjectDescription(
                msbuildProjectDirectoryPath,
                msbuildProjectFilePath,
                projectLibrary,
                targetLibrary,
                projectFile,
                dependencies,
                compatible,
                resolved: compatible && exists);

            return(msbuildPackageDescription);
        }
Exemple #9
0
        private void WriteLockFile(LockFile previousLockFile,
                                   string projectLockFilePath,
                                   Runtime.Project project,
                                   List <GraphItem> graphItems,
                                   PackageRepository repository,
                                   IProjectResolver projectResolver,
                                   IEnumerable <TargetContext> contexts)
        {
            var resolver = new DefaultPackagePathResolver(repository.RepositoryRoot.Root);
            var previousProjectLibraries = previousLockFile?.ProjectLibraries.ToDictionary(l => Tuple.Create(l.Name, l.Version));
            var previousPackageLibraries = previousLockFile?.PackageLibraries.ToDictionary(l => Tuple.Create(l.Name, l.Version));

            var lockFile = new LockFile();

            lockFile.Islocked = Lock;

            // Use empty string as the key of dependencies shared by all frameworks
            lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
                                                         string.Empty,
                                                         project.Dependencies.Select(x => x.LibraryRange.ToString())));

            foreach (var frameworkInfo in project.GetTargetFrameworks())
            {
                lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
                                                             frameworkInfo.FrameworkName.ToString(),
                                                             frameworkInfo.Dependencies.Select(x => x.LibraryRange.ToString())));
            }

            // Record all libraries used
            foreach (var item in graphItems.OrderBy(x => x.Match.Library, new LibraryComparer()))
            {
                var library = item.Match.Library;
                if (library.Name == project.Name)
                {
                    continue;
                }

                var packageInfo = repository.FindPackagesById(library.Name)
                                  .FirstOrDefault(p => p.Version == library.Version);

                var projectInfo = projectResolver.FindProject(library.Name);

                if (projectInfo != null)
                {
                    LockFileProjectLibrary previousLibrary = null;
                    previousProjectLibraries?.TryGetValue(Tuple.Create(library.Name, library.Version), out previousLibrary);

                    lockFile.ProjectLibraries.Add(LockFileUtils.CreateLockFileProjectLibrary(
                                                      previousLibrary,
                                                      project: project,
                                                      library: projectInfo));
                }
                else if (packageInfo != null)
                {
                    LockFilePackageLibrary previousLibrary = null;
                    previousPackageLibraries?.TryGetValue(Tuple.Create(library.Name, library.Version), out previousLibrary);

                    var package = packageInfo.Package;

                    // The previousLibrary can't be a project, otherwise exception has been thrown.
                    lockFile.PackageLibraries.Add(LockFileUtils.CreateLockFilePackageLibrary(
                                                      previousLibrary,
                                                      resolver,
                                                      package,
                                                      correctedPackageName: library.Name));
                }
                else
                {
                    throw new InvalidOperationException($"Unresolved library: {library.Name}");
                }
            }

            var packageLibraries = lockFile.PackageLibraries.ToDictionary(lib => Tuple.Create(lib.Name, lib.Version));
            var projectLibraries = lockFile.ProjectLibraries.ToDictionary(lib => Tuple.Create(lib.Name, lib.Version));

            // Add the contexts
            foreach (var context in contexts)
            {
                var target = new LockFileTarget();
                target.TargetFramework   = context.RestoreContext.FrameworkName;
                target.RuntimeIdentifier = context.RestoreContext.RuntimeName;

                foreach (var library in context.Libraries.OrderBy(x => x, new LibraryComparer()))
                {
                    if (library.Name == project.Name)
                    {
                        continue;
                    }

                    var packageInfo = repository.FindPackagesById(library.Name)
                                      .FirstOrDefault(p => p.Version == library.Version);

                    var projectInfo = projectResolver.FindProject(library.Name);

                    if (projectInfo != null)
                    {
                        target.Libraries.Add(LockFileUtils.CreateLockFileTargetLibrary(
                                                 projectLibraries[Tuple.Create(library.Name, library.Version)],
                                                 projectInfo,
                                                 context.RestoreContext));
                    }
                    else if (packageInfo != null)
                    {
                        var package = packageInfo.Package;

                        target.Libraries.Add(LockFileUtils.CreateLockFileTargetLibrary(
                                                 packageLibraries[Tuple.Create(library.Name, library.Version)],
                                                 package,
                                                 context.RestoreContext,
                                                 correctedPackageName: library.Name));
                    }
                }

                lockFile.Targets.Add(target);
            }

            var lockFileFormat = new LockFileFormat();

            lockFileFormat.Write(projectLockFilePath, lockFile);
        }