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 override void Run(IBuildContext context)
        {
            Context = context;
            Context.IsAborted = true;

            var include = context.Configuration.GetString(Constants.Configuration.WatchProjectInclude, "**");
            var exclude = context.Configuration.GetString(Constants.Configuration.WatchProjectExclude, "**");
            _pathMatcher = new PathMatcher(include, exclude);

            _publishDatabase = context.Configuration.GetBool(Constants.Configuration.WatchProjectPublishDatabase, true);

            _fileWatcher = new FileSystemWatcher(context.ProjectDirectory)
            {
                IncludeSubdirectories = true,
                NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName
            };

            _fileWatcher.Changed += FileChanged;
            _fileWatcher.Deleted += FileChanged;
            _fileWatcher.Renamed += FileChanged;
            _fileWatcher.Created += FileChanged;
            _fileWatcher.Created += FileChanged;

            _fileWatcher.EnableRaisingEvents = true;

            Console.WriteLine(Texts.Type__q__to_quit___);

            string input;
            do
            {
                input = Console.ReadLine();
            }
            while (!string.Equals(input, "q", StringComparison.OrdinalIgnoreCase));
        }
        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 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 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 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"));
        }
        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 void WebsiteFileNameToProjectFileNameTests()
        {
            var pathMapperService = new PathMapperService();
            var pathMatcher       = new PathMatcher("**/*.cshtml", "**/*.png");

            pathMapperService.WebsiteDirectoryToProjectDirectories.Add(new WebsiteDirectoryToProjectDirectoryMapper(pathMatcher, "/sitecore modules/shell/module", "/module"));
            pathMapperService.WebsiteDirectoryToProjectDirectories.Add(new WebsiteDirectoryToProjectDirectoryMapper(pathMatcher, "/", "/wwwroot"));

            string projectFileName;

            Assert.IsTrue(pathMapperService.TryGetProjectFileName("/sitecore modules/shell/module/overview.cshtml", out projectFileName));
            Assert.AreEqual("module\\overview.cshtml", projectFileName);

            Assert.IsTrue(pathMapperService.TryGetProjectFileName("/views/news.cshtml", out projectFileName));
            Assert.AreEqual("wwwroot\\views\\news.cshtml", projectFileName);

            Assert.IsFalse(pathMapperService.TryGetProjectFileName("/img/about.png", out projectFileName));
        }
        public void ProjectFileNameToWebsiteItemPathTests()
        {
            var pathMapperService = new PathMapperService();
            var pathMatcher       = new PathMatcher("**/*.item.json", "**/*.aspx");

            pathMapperService.ProjectDirectoryToWebsiteItemPaths.Add(new ProjectDirectoryToWebsiteItemPathMapper(pathMatcher, "/content/master/sitecore", "master", "/sitecore", false, true));

            string itemPath;
            string databaseName;
            bool   isImport;
            bool   uploadMedia;

            Assert.IsTrue(pathMapperService.TryGetWebsiteItemPath("/content/master/sitecore/content/Home/about.item.json", out databaseName, out itemPath, out isImport, out uploadMedia));
            Assert.AreEqual("/sitecore/content/Home/about", itemPath);

            Assert.IsFalse(pathMapperService.TryGetWebsiteItemPath("/content/master/sitecore/content/Home/about.item.xml", out databaseName, out itemPath, out isImport, out uploadMedia));
            Assert.IsFalse(pathMapperService.TryGetWebsiteItemPath("/content/master/sitecore/content/Home/about.aspx", out databaseName, out itemPath, out isImport, out uploadMedia));
            Assert.IsFalse(pathMapperService.TryGetWebsiteItemPath("/content/core/sitecore/content/Home/about.aspx", out databaseName, out itemPath, out isImport, out uploadMedia));
        }
        public void ProjectFileNameToWebsiteFileNameTests()
        {
            var pathMapperService = new PathMapperService();
            var pathMatcher       = new PathMatcher("**/*.html", "**/*.aspx");

            pathMapperService.ProjectDirectoryToWebsiteDirectories.Add(new ProjectDirectoryToWebsiteDirectoryMapper(pathMatcher, "/wwwroot", "/"));

            string websiteFileName;

            Assert.IsTrue(pathMapperService.TryGetWebsiteFileName("/wwwroot/about.html", out websiteFileName));
            Assert.AreEqual("~/about.html", websiteFileName);

            Assert.IsTrue(pathMapperService.TryGetWebsiteFileName("/wwwroot/files/about.html", out websiteFileName));
            Assert.AreEqual("~/files/about.html", websiteFileName);

            Assert.IsFalse(pathMapperService.TryGetWebsiteFileName("/wwwroot/files/about.aspx", out websiteFileName));
            Assert.IsFalse(pathMapperService.TryGetWebsiteFileName("/wwwroot/files/about.ashx", out websiteFileName));
            Assert.IsFalse(pathMapperService.TryGetWebsiteFileName("/files/about.html", out websiteFileName));
        }
        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 WebsiteItemPathToProjectFileNameTests()
        {
            var pathMapperService = new PathMapperService();

            var itemNamePathMatcher = new PathMatcher("\\sitecore\\content\\Home\\CleanBlog\\**", string.Empty);

            var websiteItemPathToProjectDirectoryMapper = new WebsiteItemPathToProjectDirectoryMapper(itemNamePathMatcher, null, "master", "/sitecore/content/Home", "/content/master/sitecore/content/Home", "item.json");

            pathMapperService.WebsiteItemPathToProjectDirectories.Add(websiteItemPathToProjectDirectoryMapper);

            string projectFileName;
            string format;
            var    condition = pathMapperService.TryGetProjectFileName("/sitecore/content/Home/CleanBlog/Posts/Post1", string.Empty, out projectFileName, out format);

            Assert.IsTrue(condition);
            Assert.AreEqual("content\\master\\sitecore\\content\\Home\\CleanBlog\\Posts\\Post1.item.json", projectFileName);
            Assert.AreEqual("item.json", format);

            Assert.IsFalse(pathMapperService.TryGetProjectFileName("/sitecore/content/Home/TodoMvc/Posts/Post1", string.Empty, out projectFileName, out format));
        }
        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 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);
                }
            }
        }