예제 #1
0
        private FileEntry Replace(string sourcePath, FileEntry entry)
        {
            if (_replacementOptions.Rules.Any())
            {
                if (_ignoreReplaceParser.IsIgnore(sourcePath))
                {
                    // 它的路径还是要替换的

                    var oldPath    = entry.Name.Replace('/', Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar);
                    var postName   = oldPath.Split(Path.DirectorySeparatorChar).Last();
                    var newPrePath = ReplacementHelper.ReplaceText(oldPath.RemovePostFix(postName), _replacementOptions.Rules);

                    if (newPrePath != Path.DirectorySeparatorChar.ToString())
                    {
                        var newName = Path.Combine(newPrePath, postName);

                        if (entry.Name.EndsWith(Path.DirectorySeparatorChar))
                        {
                            newName = newName.EnsureEndsWith(Path.DirectorySeparatorChar);
                        }

                        entry.SetName(newName);
                        WriteLine($"{entry}");
                    }
                }
                else
                {
                    ReplacementHelper.Replace(entry, _replacementOptions.Rules);
                    WriteLine($"{entry}");
                }
            }

            return(entry);
        }
예제 #2
0
        private void ReplaceInZipFile()
        {
            var entries     = _replacementOptions.SourcePath.ToFileEntryList();
            var ignoreFiles = entries.Where(x => x.Name.EndsWith(".gitignore"));

            foreach (var fileEntry in ignoreFiles)
            {
                _ignoreCopyParser.AddIgnoreFile(fileEntry.Name, fileEntry.GetLines());
            }

            foreach (var fileEntry in entries)
            {
                if (_ignoreCopyParser.IsIgnore(fileEntry.Name))
                {
                    continue;
                }

                var newEntry = Replace(fileEntry.Name, fileEntry);
                _outputFileEntryList.Add(newEntry);
            }

            var sourceFile  = new FileInfo(_replacementOptions.SourcePath);
            var newFileName = ReplacementHelper.ReplaceText(sourceFile.Name, _replacementOptions.Rules);

            if (!_outputFolderPath.EndsWith(newFileName))
            {
                _outputFolderPath = Path.Combine(_outputFolderPath, $"{newFileName}");
            }

            _outputFileEntryList.SaveToZipFile(_outputFolderPath);
        }
예제 #3
0
        private void ReplaceInDirectory()
        {
            var rootFolder    = new DirectoryInfo(_replacementOptions.SourcePath);
            var newFolderName = ReplacementHelper.ReplaceText(rootFolder.Name, _replacementOptions.Rules);

            if (!_outputFolderPath.TrimEnd(Path.DirectorySeparatorChar).EndsWith(newFolderName))
            {
                _outputFolderPath = Path.Combine(_outputFolderPath, $"{newFolderName}");
            }

            Directory.CreateDirectory(_outputFolderPath);

            var ignoreFiles = rootFolder.GetFiles(".gitignore", SearchOption.AllDirectories);

            _ignoreCopyParser.AddIgnoreFiles(ignoreFiles);

            CopyAndReplace(rootFolder);

            //TODO:保存 zip?
        }