private static List <Folder> GetFolders(string sourceDir, WixIgnore wixIgnore)
        {
            var folders = new List <Folder>();

            var directoryPaths = Directory.GetDirectories(sourceDir);

            if (directoryPaths.Any())
            {
                foreach (var directoryPath in directoryPaths)
                {
                    if (!wixIgnore.Contains(directoryPath))
                    {
                        var childFolders = GetFolders(directoryPath, wixIgnore);
                        var folder       = new Folder(directoryPath, childFolders);
                        folders.Add(folder);
                    }
                }
            }

            return(folders);
        }
Beispiel #2
0
        public static WixIgnore GenerateWixIgnore(string sourceDir)
        {
            try
            {
                var wixIgnoreFilePath = AppDomain.CurrentDomain.BaseDirectory + ".wixignore";
                if (!System.IO.File.Exists(wixIgnoreFilePath))
                {
                    var wixIgnoreFile = System.IO.File.Create(wixIgnoreFilePath);
                    wixIgnoreFile.Close();
                }

                var filePaths   = Directory.GetFiles(sourceDir, "*.*", SearchOption.AllDirectories);
                var folderPaths = Directory.GetDirectories(sourceDir, "*", SearchOption.AllDirectories);

                var ignoredfilePaths   = new List <string>();
                var ignoredfolderPaths = new List <string>();

                using (var sr = new StreamReader(wixIgnoreFilePath))
                {
                    string line = null;

                    do
                    {
                        line = sr.ReadLine();
                        if (line != null)
                        {
                            if (!line.StartsWith("#"))
                            {
                                // Extension (e.g. *.pdb)
                                if (line.StartsWith("*."))
                                {
                                    var splitString = line.Split('.');
                                    if (splitString.Length == 2)
                                    {
                                        var extension = splitString[1];

                                        foreach (var filePath in filePaths)
                                        {
                                            if (filePath.EndsWith(extension))
                                            {
                                                ignoredfilePaths.Add(filePath);
                                            }
                                        }
                                    }
                                }
                                // Directory
                                else if (line.StartsWith("/"))
                                {
                                    // e.g. /Directory/ or /Directory_1/Directory_2/
                                    if (line.EndsWith("/"))
                                    {
                                        var directoryString  = line.Substring(1, line.Length - 2);
                                        var splitDirectories = directoryString.Split('/');

                                        foreach (var folderPath in folderPaths)
                                        {
                                            var directoryInfo = new DirectoryInfo(folderPath);
                                            if (directoryInfo.Exists)
                                            {
                                                if (directoryInfo.Name == splitDirectories[splitDirectories.Length - 1])
                                                {
                                                    var result = HasMatchingDirectoryStructure(directoryInfo, splitDirectories, splitDirectories.Length - 2, true);
                                                    if (result)
                                                    {
                                                        ignoredfolderPaths.Add(folderPath);

                                                        var filesInFolder = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories);
                                                        foreach (var file in filesInFolder)
                                                        {
                                                            ignoredfilePaths.Add(file);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    // e.g. /*.xml or /Directory_1/Directory_2/*.xml
                                    else
                                    {
                                        var directoryString  = line;
                                        var splitDirectories = directoryString.Split('/');

                                        var extensionString = splitDirectories[splitDirectories.Length - 1];

                                        var splitExtensionString = extensionString.Split('.');
                                        if (splitExtensionString.Length == 2)
                                        {
                                            var extension     = splitExtensionString[1];
                                            var sourceDirInfo = new DirectoryInfo(sourceDir);
                                            var parentFolder  = sourceDirInfo.Name;

                                            if (splitDirectories.Length != 1)
                                            {
                                                if (!string.IsNullOrEmpty(splitDirectories[splitDirectories.Length - 2]))
                                                {
                                                    parentFolder = splitDirectories[splitDirectories.Length - 2];
                                                }
                                            }

                                            foreach (var filePath in filePaths)
                                            {
                                                if (filePath.EndsWith(extension))
                                                {
                                                    var fileInfo = new FileInfo(filePath);
                                                    if (fileInfo.Exists)
                                                    {
                                                        if (fileInfo.Directory != null && fileInfo.Directory.Name == parentFolder)
                                                        {
                                                            var result = HasMatchingDirectoryStructure(fileInfo.Directory, splitDirectories, splitDirectories.Length - 3, true);
                                                            if (result)
                                                            {
                                                                ignoredfilePaths.Add(filePath);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                // Specific file (e.g. test.txt) or a random string which will not match any file paths
                                else
                                {
                                    foreach (var filePath in filePaths)
                                    {
                                        var fileInfo = new FileInfo(filePath);
                                        if (fileInfo.Exists && line == fileInfo.Name)
                                        {
                                            ignoredfilePaths.Add(filePath);
                                        }
                                    }
                                }
                            }
                        }
                    } while (line != null);
                }

                var wixIgnore = new WixIgnore(ignoredfilePaths, ignoredfolderPaths);

                return(wixIgnore);
            }
            catch (Exception e)
            {
                throw e;
            }
        }