Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        public void HandleTask(TaskType taskType, TaskMethod taskMethod, string[] args)
        {
            switch (taskType)
            {
            case TaskType.File:
                if (taskMethod == TaskMethod.Delete)
                {
                    if (args.Length <= 0)
                    {
                        throw new ArgumentException("args must be longer then 0!");
                    }
                    var fileTask = new FileTask();
                    Console.WriteLine(fileTask.ClearFile(ReplacementHelper.SanatizePathname(args[0])));
                }
                break;

            case TaskType.Directory:
                if (taskMethod == TaskMethod.Delete)
                {
                    if (args.Length == 0)
                    {
                        throw new ArgumentException("args must be longer then 0!");
                    }
                    var dirTask = new DirectoryTask();
                    Console.WriteLine(dirTask.DeleteDirectory(ReplacementHelper.SanatizePathname(args[0])));
                }
                else if (taskMethod == TaskMethod.Clean)
                {
                    if (args.Length == 0)
                    {
                        throw new ArgumentException("args must be longer then 0!");
                    }

                    var dirTask = new DirectoryTask();
                    dirTask.CleanDirectory(ReplacementHelper.SanatizePathname(args[0])).ForEach((dir) => { Console.WriteLine(dir); });
                }
                break;

            case TaskType.Recycling:
                var recyclingTask = new RecyclingTask();
                recyclingTask.ClearRecycling();
                break;
            }
        }
Ejemplo n.º 4
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?
        }