Exemplo n.º 1
0
        public static (FileInfo[] files, DirectoryInfo[] directories, DirectoryInfo rootDirectory) GetSourceInfo(this SyncTarget st)
        {
            var files = new List <FileInfo>();

            DirectoryInfo[] directories   = null;
            DirectoryInfo   rootDirectory = null;

            if (st.source.IsFile())
            {
                var file = st.source.ToFileInfo();
                files.Add(file);
                rootDirectory = file.Directory;
                directories   = new DirectoryInfo[1] {
                    rootDirectory
                };
            }
            else if (st.source.IsDirectory())
            {
                rootDirectory = st.source.ToDirectoryInfo();
                directories   = rootDirectory.GetDirectories(recursive: st.recursive).Merge(rootDirectory);
                files         = FileHelper.GetFiles(st.source, recursive: st.recursive).ToList();

                if (!st.filesIgnore.IsNullOrEmpty() && !files.IsNullOrEmpty()) //remove files that should be ignored
                {
                    foreach (var ignore in st.filesIgnore)
                    {
                        if (ignore.IsNullOrEmpty())
                        {
                            continue;
                        }

                        if (files.IsNullOrEmpty())
                        {
                            break;
                        }

                        var remove = FileHelper.GetFiles(st.source, recursive: st.recursive, pattern: ignore);
                        foreach (var r in remove)
                        {
                            var toRemove = files.FirstOrDefault(x => x.FullName == r.FullName);
                            if (toRemove != null)
                            {
                                files.Remove(toRemove);
                            }

                            if (files.IsNullOrEmpty())
                            {
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                var message = $"Could not process, source not found: '{st.source}'";
                if (st.throwIfSourceNotFound)
                {
                    throw new Exception(message);
                }
                else
                {
                    Console.WriteLine(message);
                }
            }

            return(files?.ToArray(), directories, rootDirectory);
        }