public void Add(CodeFile codeFile)
        {
            var targetProj = this.GetContainingProject(codeFile.Location);

            if (targetProj != null)
            {
                foreach (var adapter in this.msBuildStoreAdapters)
                {
                    if (adapter.CodeFileSupported(codeFile))
                    {
                        adapter.AddOrUpdate(codeFile, targetProj);
                        return;
                    }
                }

                throw new InvalidOperationException($"No MsBuild store adapter was found for {codeFile}.");
            }
            else
            {
                var solutionDirectory = this.fileSystem.Path.GetDirectoryName(this.Solution.FullPath);
                var topRootPath       = this.GetFileTopRootPath(solutionDirectory);
                if (!string.IsNullOrEmpty(codeFile.Location.FilePath) &&
                    PathMaskHelper.DirectoryIsBaseOf(topRootPath, codeFile.Location.FilePath))
                {
                    if (this.msBuildStoreAdapters.Any(a => a.CodeFileSupported(codeFile)))
                    {
                        this.fileSystem.File.WriteAllText(codeFile.Location.FilePath, codeFile.SourceCodeText);
                    }
                }
                else
                {
                    throw new InvalidOperationException($"Unable to save '{codeFile.Name}' mapped to '{codeFile.Location.FilePath}', path should be non-empty and be under directory '{topRootPath}'.");
                }
            }
        }
        public void Remove(CodeFile codeFile)
        {
            var targetProj = this.GetContainingProject(codeFile.Location);

            if (targetProj != null)
            {
                foreach (var adapter in this.msBuildStoreAdapters)
                {
                    if (adapter.CodeFileSupported(codeFile))
                    {
                        adapter.Remove(codeFile, targetProj);
                        return;
                    }
                }
            }
            else
            {
                var solutionDirectory = this.fileSystem.Path.GetDirectoryName(this.Solution.FullPath);
                var topRootPath       = this.GetFileTopRootPath(solutionDirectory);
                if (!string.IsNullOrEmpty(codeFile.Location.FilePath) &&
                    PathMaskHelper.DirectoryIsBaseOf(topRootPath, codeFile.Location.FilePath))
                {
                    if (this.fileSystem.File.Exists(codeFile.Location.FilePath))
                    {
                        this.fileSystem.File.Delete(codeFile.Location.FilePath);
                    }
                }
                else
                {
                    throw new InvalidOperationException($"Unable to remove '{codeFile.Name}' mapped to '{codeFile.Location.FilePath}', path should be non-empty and be under directory '{topRootPath}'.");
                }
            }
        }
Пример #3
0
        public IEnumerable <CodeFile> Select(IEnumerable <ICodeStream> streams)
        {
            List <CodeFile> result = new List <CodeFile>();

            foreach (var stream in streams)
            {
                foreach (var filter in this.filters)
                {
                    if (PathMaskHelper.PathMatchMask(stream.Name, filter.Key))
                    {
                        foreach (var file in stream.Files)
                        {
                            foreach (var mask in filter.Value)
                            {
                                if (PathMaskHelper.PathMatchMask(file.Name, mask))
                                {
                                    if (!result.Contains(file))
                                    {
                                        result.Add(file);
                                    }

                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }
        public bool IsMatch(CodeFileLocation location)
        {
            if (location is WorkspaceCodeFileLocation workspaceCodeFileLocation)
            {
                return(this.documentPaths.Any(m => PathMaskHelper.PathMatchMask(workspaceCodeFileLocation.DocumentPath.ToString(), m)));
            }

            throw new NotSupportedException($"{nameof(WorkspaceCodeFileLocationFilter)} supports only locations of type: {typeof(WorkspaceCodeFileLocation)}");
        }
Пример #5
0
        public bool IsMatch(CodeFileLocation location)
        {
            foreach (var mask in this.pathMasks)
            {
                var startDirectoryMask = PathMaskHelper.GetAbsolutePathMask(mask, this.basePath, this.fileSystem);
                if (PathMaskHelper.PathMatchMask(location.FilePath, startDirectoryMask))
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #6
0
        public IEnumerable <MsBuildItem> AddItem(string itemType, string unevaluatedInclude)
        {
            var result = new List <MsBuildItem>();

            if (this.IsUsingDefaultItemsForItemType(itemType))
            {
                string fullPath         = null;
                string projectDirectory = this.fileSystem.Path.GetDirectoryName(this.FullPath);
                if (this.fileSystem.Path.IsPathRooted(unevaluatedInclude))
                {
                    fullPath = this.fileSystem.Path.GetFullPath(unevaluatedInclude);
                }
                else
                {
                    fullPath = this.fileSystem.Path.GetFullPath(this.fileSystem.Path.Combine(projectDirectory, unevaluatedInclude));
                }

                if (!PathMaskHelper.DirectoryIsBaseOf(projectDirectory, fullPath))
                {
                    throw new InvalidOperationException($"Project {this.Name} is using default items for {itemType}, item path {fullPath} should be under project path {projectDirectory}.");
                }

                var          itemDirectory = this.fileSystem.Path.GetDirectoryName(fullPath);
                DocumentPath docPath       = new DocumentPath(
                    this.Name,
                    itemDirectory.Remove(0, projectDirectory.Length).Split(new char[] { this.fileSystem.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries),
                    this.fileSystem.Path.GetFileName(fullPath));

                if (!this.fileSystem.File.Exists(fullPath))
                {
                    this.fileSystem.File.WriteAllText(fullPath, string.Empty);
                }

                var newItem = new MsBuildItem(docPath, fullPath, itemType, this);
                this.items.Add(newItem);
                result.Add(newItem);
            }
            else
            {
                foreach (var item in this.Project.AddItem(itemType, unevaluatedInclude))
                {
                    var newItem = new MsBuildItem(item, this);
                    this.items.Add(newItem);
                    result.Add(newItem);
                }
            }

            return(result);
        }
        protected string GetProjectRelativePath(CodeFileLocation location, MsBuildProject msBuildProject)
        {
            if (location is WorkspaceCodeFileLocation)
            {
                var wLocation = location as WorkspaceCodeFileLocation;

                if (!string.IsNullOrEmpty(wLocation.DocumentPath?.DocumentName))
                {
                    return(this.fileSystem.Path.Combine(string.Join(this.fileSystem.Path.DirectorySeparatorChar.ToString(), wLocation.DocumentPath.ProjectFolders), wLocation.DocumentPath.DocumentName));
                }
            }
            else
            {
                return(PathMaskHelper.GetRelativePath(msBuildProject.FullPath, location.FilePath, this.fileSystem));
            }

            return(null);
        }
        private MsBuildProject GetContainingProject(CodeFileLocation location)
        {
            if (location is WorkspaceCodeFileLocation)
            {
                var workspaceLocation = location as WorkspaceCodeFileLocation;
                return(this.Solution.GetProject(workspaceLocation.DocumentPath.ProjectName));
            }
            else
            {
                if (!string.IsNullOrEmpty(location.FilePath))
                {
                    foreach (var project in this.Solution.Projects)
                    {
                        if (PathMaskHelper.DirectoryIsBaseOf(this.fileSystem.Path.GetDirectoryName(project.Value.FullPath), location.FilePath))
                        {
                            return(project.Value);
                        }
                    }
                }
            }

            return(null);
        }