예제 #1
0
        // Note: In the Tuple, the first item is the file full path, and the second item is any object that accompanies the file.
        public FileTreeWalker(IEnumerable <Tuple <string, object> > filesPathsAndUserState)
        {
            _thisRootFolder = new FolderWalker(".", "");

            foreach (Tuple <string, object> filePathAndUserState in filesPathsAndUserState)
            {
                string filePath  = filePathAndUserState.Item1;
                object userState = filePathAndUserState.Item2;

                string   normalizedFilePath = filePath.Replace('/', '\\'); //make sure we have a consistent separator between the different elements.
                string[] splittedPath       = normalizedFilePath.Split('\\');
                int      splittedPathLength = splittedPath.Length;

                //now we loop through the path's elements to create/go through the folders:
                FolderWalker currentFolder = _thisRootFolder;
                for (int i = 0; i < splittedPathLength - 1; ++i)
                {
                    string folderName = splittedPath[i];
                    if (!string.IsNullOrWhiteSpace(folderName))
                    {
                        currentFolder = currentFolder.GetSubFolderOrCreateIfNotExists(folderName);
                    }
                }

                if (!FoldersHelper.IsAFolder(filePath))
                {
                    //create the file.
                    currentFolder.GetFileOrCreateIfNotExists(splittedPath[splittedPathLength - 1], userState);
                }
            }
        }
예제 #2
0
        public static IEnumerable <ProjectItemWrapper> GetProjectItemsFromProject(Microsoft.Build.Evaluation.Project projectLoadedWithMsBuild)
        {
            // Projects loaded with MsBuild have a different way of iterating through the files.
            // Instead of returning only the items in the root, and then you recursively get the items in the sub-folders,
            // we can only get the full list of all the items at once, and we should deduce the folders from their path.
            // Therefore we have developed a class named "FileTreeWalker" which takes as input the full list of items,
            // and return a sort of file system, where you can get the root files, and then the folders, etc.

            //-------------------------------------
            // We initialize the FileTreeWalker by reading the list of items from the CSPROJ and passing that list to the FileTreeWalker:
            //-------------------------------------

            // Get the list of files from the CSPROJ:
            List <Tuple <string, object> > projectItemsPathsAndReference = new List <Tuple <string, object> >();

            foreach (Microsoft.Build.Evaluation.ProjectItem projectItem in projectLoadedWithMsBuild.Items)
            {
                // Keep only the items that may correspond to files or folders (not the <Reference>mscorlic</reference> and stuff like that)
                if (AllowedItemTypes.Contains(projectItem.ItemType.ToLower()))
                {
                    //--------------------
                    // Determine the file path relative to the project root:
                    //--------------------
                    string filePathRelativeToProjectRoot;

                    // If the file was added "As Link", we need to use the value of the "Link" metadata to know the path in the project structure:
                    string valueOfLinkMetadata = projectItem.GetMetadataValue("Link");
                    if (!string.IsNullOrEmpty(valueOfLinkMetadata))
                    {
                        filePathRelativeToProjectRoot = valueOfLinkMetadata;
                    }
                    else
                    {
                        filePathRelativeToProjectRoot = projectItem.EvaluatedInclude;
                    }

                    projectItemsPathsAndReference.Add(new Tuple <string, object>(filePathRelativeToProjectRoot, projectItem));
                }
            }

            // Initialize the FileTreeWalker:
            FileTreeWalker fileTreeWalker = new FileTreeWalker(projectItemsPathsAndReference);

            // Return the root items (files and folders):
            FolderWalker rootFolder = fileTreeWalker.GetRootFolder();

            return(GetChildItemsFromFolder(rootFolder));
        }
예제 #3
0
        static public IEnumerable <ProjectItemWrapper> GetChildItemsFromFolder(FolderWalker folderWalker)
        {
            // Return the files:
            // IMPORTANT: we sort the files by name so that the files ".XAML" are returned before their code-behind ".XAML.CS". This order is important because when we copy the former, the latter gets copied as well. So when we arrive to the code-behind, we skip it because we are able to check that it already exists. If, on the contrary, we did the other way around, we would get an error when copying the ".XAML" because it says that it cannot copy the ".XAML.CS" because it already exists.
            foreach (FileWalker fileWalker in folderWalker.GetFiles(sortByName: true))
            {
                Microsoft.Build.Evaluation.ProjectItem projectItem = (Microsoft.Build.Evaluation.ProjectItem)fileWalker.UserState;

                yield return(new ProjectItemWrapper(projectItem, fileWalker));
            }

            // And also return then the sub-folders:
            foreach (FolderWalker subFolderWalker in folderWalker.GetFolders())
            {
                yield return(new ProjectItemWrapper(subFolderWalker));
            }
        }
예제 #4
0
        /// <summary>
        /// Adds a subfolder to this folder and returns it. If the subfolder already exists, it is returned.
        /// </summary>
        /// <param name="folderName"></param>
        /// <returns></returns>
        internal FolderWalker GetSubFolderOrCreateIfNotExists(string folderName, bool raiseExceptionIfFolderAlreadyExists = false)
        {
            if (!_folderNameToFolder.ContainsKey(folderName))
            {
                string folderFullPath = Path.Combine(_fullPath, folderName);
                if (!folderFullPath.EndsWith("\\"))
                {
                    folderFullPath = folderFullPath + "\\";
                }
                FolderWalker folder = new FolderWalker(folderName, folderFullPath);
                _folderNameToFolder.Add(folderName, folder);
                return(folder);
            }
            else
            {
                if (raiseExceptionIfFolderAlreadyExists)
                {
                    throw new Exception(string.Format("A folder with the name '{0}' already exists.", folderName));
                }

                return(_folderNameToFolder[folderName]);
            }
        }
예제 #5
0
 public ProjectItemWrapper(FolderWalker folderWalker)
 {
     _folderWalkerInCaseOfProjectLoadedWithMsBuild = folderWalker;
 }