public override bool Execute() {
            try {
                if (string.IsNullOrEmpty(RootDirectory)) {
                    Log.LogError("RootDirectory cannot be empty");
                    return false;
                }
                if (!Directory.Exists(this.RootDirectory)) {
                    Log.LogError("RootDirectory not found at [{0}]", this.RootDirectory);
                    return false;
                }

                string rootDirFullPath = Path.GetFullPath(this.RootDirectory);
                // parse the XML file
                TemplateInfo templateInfo = TemplateInfo.BuildTemplateInfoFrom(this.TemplateInfoFile.GetMetadata("FullPath"));

                var replacer = new RobustReplacer();
                StringBuilder logger = new StringBuilder();
                replacer.ReplaceInFiles(rootDirFullPath, templateInfo.Include, templateInfo.Exclude, templateInfo.Replacements, logger);
                Log.LogMessage(logger.ToString());

            }
            catch (Exception ex) {
                Log.LogError(ex.ToString());
                return false;
            }
            return true;
        }
        public override bool Execute()
        {
            try {
                if (string.IsNullOrEmpty(RootDirectory))
                {
                    Log.LogError("RootDirectory cannot be empty");
                    return(false);
                }
                if (!Directory.Exists(this.RootDirectory))
                {
                    Log.LogError("RootDirectory not found at [{0}]", this.RootDirectory);
                    return(false);
                }

                string rootDirFullPath = Path.GetFullPath(this.RootDirectory);
                // parse the XML file
                TemplateInfo templateInfo = TemplateInfo.BuildTemplateInfoFrom(this.TemplateInfoFile.GetMetadata("FullPath"));

                IReplacer     replacer = new RobustReplacer();
                StringBuilder logger   = new StringBuilder();
                replacer.ReplaceInFiles(rootDirFullPath, templateInfo.Include, templateInfo.Exclude, templateInfo.Replacements, logger);
                Log.LogMessage(logger.ToString());
            }
            catch (Exception ex) {
                Log.LogError(ex.ToString());
                return(false);
            }
            return(true);
        }
示例#3
0
        public void ReplaceInFiles(string rootDir, string include, string exclude, IDictionary <string, string> replacements, StringBuilder logger = null)
        {
            if (string.IsNullOrEmpty(rootDir))
            {
                throw new ArgumentNullException("rootDir");
            }
            if (!Directory.Exists(rootDir))
            {
                throw new ArgumentException(string.Format("rootDir doesn't exist at [{0}]", rootDir));
            }

            string rootDirFullPath = Path.GetFullPath(rootDir);

            // search for all include files
            List <string> pathsToInclude = new List <string>();
            List <string> pathsToExclude = new List <string>();

            if (!string.IsNullOrEmpty(include))
            {
                string[] includeParts = include.Split(';');
                foreach (string includeStr in includeParts)
                {
                    var results = RobustReplacer.Search(rootDirFullPath, includeStr);
                    foreach (var result in results)
                    {
                        if (!pathsToInclude.Contains(result))
                        {
                            pathsToInclude.Add(result);
                        }
                    }
                }
            }

            if (!string.IsNullOrEmpty(exclude))
            {
                string[] excludeParts = exclude.Split(';');
                foreach (string excludeStr in excludeParts)
                {
                    var results = RobustReplacer.Search(rootDirFullPath, excludeStr);
                    foreach (var result in results)
                    {
                        if (!pathsToExclude.Contains(result))
                        {
                            pathsToExclude.Add(result);
                        }
                    }
                }
            }

            int numFilesExcluded = pathsToInclude.RemoveAll(p => pathsToExclude.Contains(p));

            LogMessageLine(logger, "Number of files excluded based on pattern: [{0}]", numFilesExcluded);

            foreach (string file in pathsToInclude)
            {
                string fileFullPath = Path.GetFullPath(file);
                bool   modified     = false;

                using (var fileStream = File.Open(fileFullPath, FileMode.Open, FileAccess.ReadWrite)) {
                    using (var replacer = new TokenReplacer(fileStream)) {
                        foreach (string key in replacements.Keys)
                        {
                            modified |= replacer.Replace(key, replacements[key]);
                        }
                    }

                    fileStream.Flush();
                }

                if (modified)
                {
                    LogMessageLine(logger, "Updating text after replacements in file [{0}]", fileFullPath);
                }
                else
                {
                    LogMessageLine(logger, "Not writing out file because no replacments detected [{0}]", fileFullPath);
                }
            }
        }