예제 #1
0
        protected virtual void AddFileCore(string path, Action addFileAction)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            bool fileExistsInProject = FileExistsInProject(path);

            string fileName = Path.GetFileName(path);

            string lockFileFullPath = PackagesConfigLockFileUtility.GetPackagesLockFilePath(ProjectFullPath, GetPropertyValue("NuGetLockFilePath")?.ToString(), ProjectName);

            if (File.Exists(Path.Combine(ProjectFullPath, path)) &&
                !fileExistsInProject &&
                !fileName.Equals(Constants.PackageReferenceFile) &&
                !fileName.Equals("packages." + ProjectName + ".config") &&
                !fileName.Equals("web.config") &&
                !fileName.Equals("app.config") &&
                !fileName.Equals(Path.GetFileName(lockFileFullPath)))
            {
                NuGetProjectContext.Log(MessageLevel.Warning, "File already exists", path);
                return;
            }

            addFileAction?.Invoke();

            if (!fileExistsInProject)
            {
                AddFileToProject(path);
            }
        }
예제 #2
0
        public void GetPackagesLockFilePath_PackagesLockJson()
        {
            // Arrange
            var projectName = "testproj";
            var logger      = new TestLogger();

            using (var rootFolder = TestDirectory.Create())
            {
                var projectFolder   = new DirectoryInfo(Path.Combine(rootFolder, projectName));
                var targetFramework = NuGetFramework.Parse("net46");

                var msBuildNuGetProjectSystem = new TestMSBuildNuGetProjectSystem(targetFramework, new TestNuGetProjectContext(), projectFolder.FullName);
                var project = new TestMSBuildNuGetProject(msBuildNuGetProjectSystem, rootFolder, projectFolder.FullName);

                // Act
                var lockFile = PackagesConfigLockFileUtility.GetPackagesLockFilePath(project);

                // Assert
                Assert.Equal(Path.Combine(projectFolder.FullName, "packages.lock.json"), lockFile);
            }
        }
예제 #3
0
        private Task AddFileCoreAsync(string path, Action addFile)
        {
            // Do not try to add file to project, if the path is null or empty.
            if (string.IsNullOrEmpty(path))
            {
                return(Task.FromResult(false));
            }

            var fileExistsInProject = FileExistsInProject(path);

            // If the file exists on disk but not in the project then skip it.
            // One exception is the 'packages.config' file, in which case we want to include
            // it into the project.
            // Other exceptions are 'web.config' and 'app.config'
            var fileName         = Path.GetFileName(path);
            var lockFileFullPath = PackagesConfigLockFileUtility.GetPackagesLockFilePath(ProjectFullPath, GetPropertyValue("NuGetLockFilePath")?.ToString(), ProjectName);

            if (File.Exists(Path.Combine(ProjectFullPath, path)) &&
                !fileExistsInProject &&
                !fileName.Equals(ProjectManagement.Constants.PackageReferenceFile) &&
                !fileName.Equals("packages." + ProjectName + ".config") &&
                !fileName.Equals(EnvDTEProjectInfoUtility.WebConfig) &&
                !fileName.Equals(EnvDTEProjectInfoUtility.AppConfig) &&
                !fileName.Equals(Path.GetFileName(lockFileFullPath))
                )
            {
                NuGetProjectContext.Log(ProjectManagement.MessageLevel.Warning, Strings.Warning_FileAlreadyExists, path);
            }
            else
            {
                EnvDTEProjectUtility.EnsureCheckedOutIfExists(VsProjectAdapter.Project, ProjectFullPath, path);
                addFile();
                if (!fileExistsInProject)
                {
                    return(AddFileToProjectAsync(path));
                }
            }

            return(Task.FromResult(false));
        }
예제 #4
0
        public void GetPackagesLockFilePath_MsbuildProperty()
        {
            // Arrage
            var projectName = "testproj";
            var logger      = new TestLogger();
            var expected    = "somewhere\\my.lock.json";

            using (var rootFolder = TestDirectory.Create())
            {
                var projectFolder   = new DirectoryInfo(Path.Combine(rootFolder, projectName));
                var targetFramework = NuGetFramework.Parse("net46");

                var msBuildNuGetProjectSystem = new TestMSBuildNuGetProjectSystem(targetFramework, new TestNuGetProjectContext(), projectFolder.FullName);
                var project = new TestMSBuildNuGetProject(msBuildNuGetProjectSystem, rootFolder, projectFolder.FullName);
                msBuildNuGetProjectSystem.SetPropertyValue("NuGetLockFilePath", expected);

                // Act
                var lockFile = PackagesConfigLockFileUtility.GetPackagesLockFilePath(project);

                // Assert
                Assert.Equal(Path.Combine(projectFolder.FullName, expected), lockFile);
            }
        }