Example #1
0
        private void RunInternal(IEngineEnvironmentSettings environmentSettings, IDirectory sourceDir, string targetDir, IGlobalRunSpec spec)
        {
            EngineConfig cfg      = new EngineConfig(environmentSettings, EngineConfig.DefaultWhitespaces, EngineConfig.DefaultLineEndings, spec.RootVariableCollection);
            IProcessor   fallback = Processor.Create(cfg, spec.Operations);

            List <KeyValuePair <IPathMatcher, IProcessor> > fileGlobProcessors = CreateFileGlobProcessors(sourceDir.MountPoint.EnvironmentSettings, spec);

            foreach (IFile file in sourceDir.EnumerateFiles("*", SearchOption.AllDirectories))
            {
                string sourceRel = file.PathRelativeTo(sourceDir);
                string fileName  = Path.GetFileName(sourceRel);
                bool   checkingDirWithPlaceholderFile = false;

                if (spec.IgnoreFileNames.Contains(fileName))
                {   // The placeholder file should never get copied / created / processed. It just causes the dir to get created if needed.
                    // The change checking / reporting is different, setting this variable tracks it.
                    checkingDirWithPlaceholderFile = true;
                }

                foreach (IPathMatcher include in spec.Include)
                {
                    if (include.IsMatch(sourceRel))
                    {
                        bool excluded = false;
                        foreach (IPathMatcher exclude in spec.Exclude)
                        {
                            if (exclude.IsMatch(sourceRel))
                            {
                                excluded = true;
                                break;
                            }
                        }

                        if (!excluded)
                        {
                            bool copy = false;
                            foreach (IPathMatcher copyOnly in spec.CopyOnly)
                            {
                                if (copyOnly.IsMatch(sourceRel))
                                {
                                    copy = true;
                                    break;
                                }
                            }

                            spec.LocalizationOperations.TryGetValue(sourceRel, out IReadOnlyList <IOperationProvider> locOperations);

                            if (checkingDirWithPlaceholderFile)
                            {
                                CreateTargetDir(environmentSettings, sourceRel, targetDir, spec);
                            }
                            else if (!copy)
                            {
                                ProcessFile(file, sourceRel, targetDir, spec, fallback, fileGlobProcessors, locOperations);
                            }
                            else
                            {
                                string targetPath = CreateTargetDir(environmentSettings, sourceRel, targetDir, spec);

                                using (Stream sourceStream = file.OpenRead())
                                    using (Stream targetStream = environmentSettings.Host.FileSystem.CreateFile(targetPath))
                                    {
                                        sourceStream.CopyTo(targetStream);
                                    }
                            }
                        }

                        break;
                    }
                }
            }
        }
Example #2
0
        private IReadOnlyList <IFileChange2> GetFileChangesInternal(IDirectory sourceDir, string targetDir, IGlobalRunSpec spec)
        {
            EngineConfig cfg      = new EngineConfig(_logger, EngineConfig.DefaultWhitespaces, EngineConfig.DefaultLineEndings, spec.RootVariableCollection);
            IProcessor   fallback = Processor.Create(cfg, spec.Operations);

            List <IFileChange2> changes = new List <IFileChange2>();
            List <KeyValuePair <IPathMatcher, IProcessor> > fileGlobProcessors = CreateFileGlobProcessors(_logger, spec);

            foreach (IFile file in sourceDir.EnumerateFiles("*", SearchOption.AllDirectories))
            {
                string sourceRel = file.PathRelativeTo(sourceDir);
                string fileName  = Path.GetFileName(sourceRel);
                bool   checkingDirWithPlaceholderFile = false;

                if (spec.IgnoreFileNames.Contains(fileName))
                {
                    // The placeholder file should never get copied / created / processed. It just causes the dir to get created if needed.
                    // The change checking / reporting is different, setting this variable tracks it.
                    checkingDirWithPlaceholderFile = true;
                }

                foreach (IPathMatcher include in spec.Include)
                {
                    if (include.IsMatch(sourceRel))
                    {
                        bool excluded = false;
                        foreach (IPathMatcher exclude in spec.Exclude)
                        {
                            if (exclude.IsMatch(sourceRel))
                            {
                                excluded = true;
                                break;
                            }
                        }

                        if (!excluded)
                        {
                            if (!spec.TryGetTargetRelPath(sourceRel, out string targetRel))
                            {
                                targetRel = sourceRel;
                            }

                            string targetPath = Path.Combine(targetDir, targetRel);

                            if (checkingDirWithPlaceholderFile)
                            {
                                targetPath = Path.GetDirectoryName(targetPath);
                                targetRel  = Path.GetDirectoryName(targetRel);

                                if (_fileSystem.DirectoryExists(targetPath))
                                {
                                    changes.Add(new FileChange(sourceRel, targetRel, ChangeKind.Overwrite));
                                }
                                else
                                {
                                    changes.Add(new FileChange(sourceRel, targetRel, ChangeKind.Create));
                                }
                            }
                            else if (_fileSystem.FileExists(targetPath))
                            {
                                changes.Add(new FileChange(sourceRel, targetRel, ChangeKind.Overwrite));
                            }
                            else
                            {
                                changes.Add(new FileChange(sourceRel, targetRel, ChangeKind.Create));
                            }
                        }

                        break;
                    }
                }
            }

            return(changes);
        }