public void NestedFolderTests()
        {
            var pathMatcher = new PathMatcher("/folder/*/file.txt", string.Empty);

            Assert.IsFalse(pathMatcher.IsMatch("file.txt"));
            Assert.IsFalse(pathMatcher.IsMatch("/folder/file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/folder/file.txt"));
        }
        public void NestedFolderTests()
        {
            var pathMatcher = new PathMatcher("/folder/*/file.txt", string.Empty);

            Assert.IsFalse(pathMatcher.IsMatch("file.txt"));
            Assert.IsFalse(pathMatcher.IsMatch("/folder/file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/folder/file.txt"));
        }
        public void IncludeTests()
        {
            var pathMatcher = new PathMatcher("**/*", string.Empty);

            Assert.IsTrue(pathMatcher.IsMatch("file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/folder/file.txt"));
        }
        public void IncludeTests()
        {
            var pathMatcher = new PathMatcher("**/*", string.Empty);

            Assert.IsTrue(pathMatcher.IsMatch("file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/folder/file.txt"));
        }
        public void DeepExcludeFolderTests()
        {
            var pathMatcher = new PathMatcher("/folder/**/file.txt", "**/*.cs");

            Assert.IsFalse(pathMatcher.IsMatch("file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/folder/file.txt"));
            Assert.IsFalse(pathMatcher.IsMatch("/folder/folder/file.cs"));
        }
        public void DeepExcludeFolderTests()
        {
            var pathMatcher = new PathMatcher("/folder/**/file.txt", "**/*.cs");

            Assert.IsFalse(pathMatcher.IsMatch("file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/folder/file.txt"));
            Assert.IsFalse(pathMatcher.IsMatch("/folder/folder/file.cs"));
        }
        public override void Compile(ICompileContext context, IProjectItem projectItem)
        {
            var include = context.Configuration.GetString(Constants.Configuration.BuildProjectCompileBinFilesInclude);
            if (string.IsNullOrEmpty(include))
            {
                return;
            }

            var binFile = projectItem as BinFile;
            Assert.Cast(binFile, nameof(binFile));

            var exclude = context.Configuration.GetString(Constants.Configuration.BuildProjectCompileBinFilesExclude);

            var pathMatcher = new PathMatcher(include, exclude);
            if (!pathMatcher.IsMatch(binFile.FilePath))
            {
                return;
            }

            try
            {
                var assembly = Assembly.LoadFrom(binFile.Snapshots.First().SourceFile.AbsoluteFileName);

                foreach (var type in assembly.GetExportedTypes())
                {
                    context.Pipelines.Resolve<BinFileCompilerPipeline>().Execute(context, binFile, type);
                }
            }
            catch (Exception ex)
            {
                context.Trace.TraceError(Msg.C1059, ex.Message, binFile.FilePath);
            }
        }
        public static FileContext GetFileContext([NotNull] IProject project, [NotNull] IConfiguration configuration, [NotNull] ISourceFile sourceFile)
        {
            var localFileName = "/" + PathHelper.NormalizeItemPath(PathHelper.UnmapPath(project.Options.ProjectDirectory, sourceFile.AbsoluteFileName)).TrimStart('/');

            string database = null;
            var isExtern = false;
            var itemPathConfig = string.Empty;
            var localFileDirectory = string.Empty;
            var serverFileDirectory = string.Empty;
            var uploadMedia = true;

            foreach (var pair in configuration.GetSubKeys(Constants.Configuration.BuildProjectFiles))
            {
                var key = Constants.Configuration.BuildProjectFiles + ":" + pair.Key;
                var localDirectory = PathHelper.NormalizeItemPath(configuration.GetString(key + ":project-directory"));

                if (!localFileName.StartsWith(localDirectory, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                var includes = configuration.GetString(key + ":include");
                var excludes = configuration.GetString(key + ":exclude");

                if (!string.IsNullOrEmpty(includes) && !string.IsNullOrEmpty(localDirectory))
                {
                    includes = PathHelper.NormalizeItemPath(localDirectory).TrimEnd('/') + "/" + PathHelper.NormalizeItemPath(includes).TrimStart('/');
                }

                if (!string.IsNullOrEmpty(excludes) && !string.IsNullOrEmpty(localDirectory))
                {
                    includes = PathHelper.NormalizeItemPath(localDirectory).TrimEnd('/') + "/" + PathHelper.NormalizeItemPath(excludes).TrimStart('/');
                }

                if (!string.IsNullOrEmpty(includes) || !string.IsNullOrEmpty(excludes))
                {
                    var pathMatcher = new PathMatcher(includes, excludes);
                    if (!pathMatcher.IsMatch(localFileName))
                    {
                        continue;
                    }
                }

                localFileDirectory = localDirectory;
                serverFileDirectory = PathHelper.NormalizeItemPath(configuration.GetString(key + ":website-directory"));
                itemPathConfig = configuration.GetString(key + ":item-path");
                database = configuration.Get(key + ":database");
                isExtern = configuration.GetBool(key + ":external-references");
                uploadMedia = configuration.GetBool(key + ":upload-media", true);

                break;
            }

            var filePath = PathHelper.GetFilePath(project, sourceFile, localFileDirectory, serverFileDirectory);
            var itemName = PathHelper.GetItemName(sourceFile);
            var itemPath = PathHelper.GetItemPath(project, sourceFile, localFileDirectory, itemPathConfig);
            var databaseName = !string.IsNullOrEmpty(database) ? database : project.Options.DatabaseName;

            return new FileContext(itemName, itemPath, filePath, databaseName, isExtern, uploadMedia);
        }
        public void FileTests()
        {
            var pathMatcher = new PathMatcher("/folder/file.txt", string.Empty);
            Assert.IsFalse(pathMatcher.IsMatch("file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/file.txt"));
            Assert.IsFalse(pathMatcher.IsMatch("/folder/file.cs"));
            Assert.IsFalse(pathMatcher.IsMatch("/folder/folder/file.txt"));

            pathMatcher = new PathMatcher("/folder/file.*", string.Empty);
            Assert.IsFalse(pathMatcher.IsMatch("file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/file.cs"));
            Assert.IsFalse(pathMatcher.IsMatch("/folder/folder/file.txt"));

            pathMatcher = new PathMatcher("/folder/*", string.Empty);
            Assert.IsFalse(pathMatcher.IsMatch("file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/file.cs"));
            Assert.IsFalse(pathMatcher.IsMatch("/folder/folder/file.txt"));
        }
        public void FileTests()
        {
            var pathMatcher = new PathMatcher("/folder/file.txt", string.Empty);

            Assert.IsFalse(pathMatcher.IsMatch("file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/file.txt"));
            Assert.IsFalse(pathMatcher.IsMatch("/folder/file.cs"));
            Assert.IsFalse(pathMatcher.IsMatch("/folder/folder/file.txt"));

            pathMatcher = new PathMatcher("/folder/file.*", string.Empty);
            Assert.IsFalse(pathMatcher.IsMatch("file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/file.cs"));
            Assert.IsFalse(pathMatcher.IsMatch("/folder/folder/file.txt"));

            pathMatcher = new PathMatcher("/folder/*", string.Empty);
            Assert.IsFalse(pathMatcher.IsMatch("file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/file.txt"));
            Assert.IsTrue(pathMatcher.IsMatch("/folder/file.cs"));
            Assert.IsFalse(pathMatcher.IsMatch("/folder/folder/file.txt"));
        }
        protected virtual void DeleteFiles([Diagnostics.NotNull] IConfiguration configuration, [Diagnostics.NotNull] string area, [Diagnostics.NotNull] string baseDirectory)
        {
            foreach (var pair in configuration.GetSubKeys("reset-website:" + area))
            {
                var key = "reset-website:" + area + ":" + pair.Key;

                var path = configuration.GetString(key + ":path");
                var include = configuration.GetString(key + ":include");
                var exclude = configuration.GetString(key + ":exclude");

                path = PathHelper.NormalizeFilePath(Path.Combine(baseDirectory, PathHelper.NormalizeFilePath(path).TrimStart('\\'))).TrimEnd('\\');

                if (File.Exists(path))
                {
                    FileUtil.Delete(path);
                    continue;
                }

                if (Directory.Exists(path) && string.IsNullOrEmpty(include) && string.IsNullOrEmpty(exclude))
                {
                    FileUtil.DeleteDirectory(path, true);
                    continue;
                }

                if (!Directory.Exists(path))
                {
                    continue;
                }

                include = path + "\\" + PathHelper.NormalizeFilePath(include).TrimStart('\\');
                exclude = path + "\\" + PathHelper.NormalizeFilePath(exclude).TrimStart('\\');

                var matcher = new PathMatcher(include, exclude);

                foreach (var fileName in Directory.GetFiles(path, "*", SearchOption.AllDirectories))
                {
                    if (!matcher.IsMatch(fileName))
                    {
                        continue;
                    }

                    try
                    {
                        File.Delete(fileName);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }
        public override void Run(IBuildContext context)
        {
            if (context.Project.HasErrors)
            {
                context.Trace.TraceInformation(Msg.D1017, Texts.Package_contains_errors_and_will_not_be_deployed);
                context.IsAborted = true;
                return;
            }

            context.Trace.TraceInformation(Msg.D1018, Texts.Creating_Nupkg_file___);

            var packageDirectory = context.Configuration.Get(Constants.Configuration.PackNugetDirectory);

            string directory;
            if (context.Configuration.GetBool(Constants.Configuration.BuildingWithNoConfig))
            {
                directory = PathHelper.Combine(context.Configuration.GetString(Constants.Configuration.ToolsDirectory), "files\\project.noconfig\\sitecore.project");
            }
            else
            {
                directory = PathHelper.Combine(context.ProjectDirectory, packageDirectory);
            }

            var pathMatcher = new PathMatcher(context.Configuration.Get(Constants.Configuration.PackNugetInclude), context.Configuration.Get(Constants.Configuration.PackNugetExclude));

            foreach (var nuspecFileName in context.FileSystem.GetFiles(directory, "*", SearchOption.AllDirectories))
            {
                if (!pathMatcher.IsMatch(nuspecFileName))
                {
                    continue;
                }

                string nupkgFileName;
                if (context.IsBuildingWithNoConfig)
                {
                    nupkgFileName = Path.Combine(context.ProjectDirectory, "sitecore.project\\" + Path.GetFileNameWithoutExtension(nuspecFileName) + ".nupkg");
                    context.FileSystem.CreateDirectoryFromFileName(nupkgFileName);
                }
                else
                {
                    nupkgFileName = Path.ChangeExtension(nuspecFileName, ".nupkg");
                }

                Pack(context, nuspecFileName, nupkgFileName);
            }
        }
        public override void Run(IBuildContext context)
        {
            if (context.Project.HasErrors)
            {
                context.Trace.TraceInformation(Msg.D1017, Texts.Package_contains_errors_and_will_not_be_deployed);
                context.IsAborted = true;
                return;
            }

            context.Trace.TraceInformation(Msg.D1018, Texts.Creating_Nupkg_file___);

            var packageFileName = context.Configuration.Get(Constants.Configuration.PackNugetDirectory);
            var directory = PathHelper.Combine(context.ProjectDirectory, packageFileName);
            var pathMatcher = new PathMatcher(context.Configuration.Get(Constants.Configuration.PackNugetInclude), context.Configuration.Get(Constants.Configuration.PackNugetExclude));

            foreach (var fileName in context.FileSystem.GetFiles(directory, "*", SearchOption.AllDirectories))
            {
                if (pathMatcher.IsMatch(fileName))
                {
                    Pack(context, fileName);
                }
            }
        }
        private void ImportFiles([Diagnostics.NotNull] IAppService app, [Diagnostics.NotNull] string key)
        {
            var sourceDirectory = PathHelper.NormalizeFilePath(Path.Combine(WebsiteDirectory, PathHelper.NormalizeFilePath(app.Configuration.GetString(key + ":website-directory")).TrimStart('\\'))).TrimEnd('\\');
            var projectDirectory = PathHelper.NormalizeFilePath(Path.Combine(app.ProjectDirectory, PathHelper.NormalizeFilePath(app.Configuration.GetString(key + ":project-directory")).TrimStart('\\'))).TrimEnd('\\');

            if (!FileSystem.DirectoryExists(sourceDirectory))
            {
                return;
            }

            var searchPattern = app.Configuration.GetString(key + ":search-pattern", "*");
            var include = app.Configuration.GetString(key + ":include");
            var exclude = app.Configuration.GetString(key + ":exclude");

            IEnumerable<string> fileNames;

            var fileNameList = app.Configuration.GetList(key + ":file-names").ToList();
            if (fileNameList.Any())
            {
                fileNames = fileNameList.Select(f => Path.Combine(sourceDirectory, PathHelper.NormalizeFilePath(f).TrimStart('\\')));
            }
            else if (!string.IsNullOrEmpty(include) || !string.IsNullOrEmpty(exclude))
            {
                var pathMatcher = new PathMatcher(include, exclude);
                fileNames = FileSystem.GetFiles(sourceDirectory, searchPattern, SearchOption.AllDirectories).Where(f => pathMatcher.IsMatch(f)).ToList();
            }
            else
            {
                fileNames = FileSystem.GetFiles(sourceDirectory, searchPattern, SearchOption.AllDirectories);
            }

            FileSystem.CreateDirectory(projectDirectory);
            foreach (var fileName in fileNames)
            {
                if (!FileSystem.FileExists(fileName))
                {
                    continue;
                }

                var targetFileName = Path.Combine(projectDirectory, PathHelper.UnmapPath(sourceDirectory, fileName));
                try
                {
                    FileSystem.Copy(fileName, targetFileName);
                }
                catch (Exception ex)
                {
                    Trace.TraceError(Msg.M1022, ex.Message, fileName);
                }
            }
        }