예제 #1
0
        public async Task CanExecute()
        {
            var scriptFile      = MockFile(@"Custom\Scripts\MyScript.cs", "...");
            var filteredFile    = MockFile(@"Saves\Filtered\FilteredScript.cs", "...");
            var scriptListChild = MockFile(@"Custom\Scripts\Complex\Child.cs", "...");
            var scriptListFile  = MockFile(@"Custom\Scripts\Complex\Complex.cslist", "...");

            scriptListFile.Children = new List <FreeFile> {
                scriptListChild
            };
            var files = new List <FreeFile> {
                scriptFile, filteredFile, scriptListFile
            };
            var matches = new List <FreeFilePackageMatch>
            {
                new FreeFilePackageMatch(
                    new VarPackage(VarPackageName.TryGet("Author.Name.1.var", out var name) ? name : null, "absolute-path", new List <VarPackageFile>()),
                    new VarPackageFile(@"Custom\Scripts\MyScript.cs", "hash"),
                    new[] { scriptFile, scriptListFile }
                    )
            };
            var op = new DeleteMatchedFilesOperation(_consoleOutput.Object, _fs, Mock.Of <IRecycleBin>(), Mock.Of <ILogger>());

            await op.ExecuteAsync(files, matches, DeleteOptions.Permanent, GivenExcludes(@"Saves\Filtered"), VerbosityOptions.Default, ExecutionOptions.Default);

            Assert.That(!_fs.FileExists(scriptFile.Path));
            Assert.That(!_fs.FileExists(scriptListFile.Path));
            Assert.That(!_fs.FileExists(scriptListChild.Path));
            Assert.That(_fs.FileExists(filteredFile.Path));
            Assert.That(files, Is.EquivalentTo(new[] { filteredFile }));
        }
예제 #2
0
 private static FreeFilePackageMatch GivenPackageMatch(string filename, FreeFile scriptFile, int additionalFiles = 0)
 {
     return(new FreeFilePackageMatch(
                new VarPackage(
                    VarPackageName.TryGet(filename, out var name) ? name : null,
                    "absolute-path",
                    Enumerable
                    .Range(0, 1 + additionalFiles)
                    .Select(i => new VarPackageFile($@"Custom\Scripts\{i}.cs", $"hash:{i}"))
                    .ToList()),
                new VarPackageFile(@"Custom\Scripts\MyScript.cs", "hash"),
                new[] { scriptFile }));
 }
예제 #3
0
 public bool IsFiltered(VarPackageName package)
 {
     if (_excludePackages != null)
     {
         foreach (var exclude in _excludePackages)
         {
             if (exclude.Author == "*")
             {
                 continue;
             }
             if (exclude.Author == package.Author)
             {
                 return(true);
             }
             if (exclude.Name == "*")
             {
                 continue;
             }
             if (exclude.Name == package.Name)
             {
                 return(true);
             }
             if (exclude.Version == -1)
             {
                 continue;
             }
             if (exclude.Version == package.Version)
             {
                 return(true);
             }
         }
     }
     if (_includePackages != null)
     {
         foreach (var include in _includePackages)
         {
             if (
                 (include.Author == "*" || include.Author == package.Author) &&
                 (include.Name == "*" || include.Name == package.Name) &&
                 (include.Version == -1 || include.Version == package.Version))
             {
                 return(false);
             }
         }
         return(true);
     }
     return(false);
 }
예제 #4
0
        public static IFilter From(string vam, string[]?include, string[]?exclude)
        {
            if (include == null && exclude == null)
            {
                return(None);
            }
            var includeSplit = include?.Select(f => VarPackageName.TryGet(f, out var name) ? new VarOrPath {
                Var = name
            } : new VarOrPath {
                Path = f
            }).ToList();
            var excludeSplit = exclude?.Select(f => VarPackageName.TryGet(f, out var name) ? new VarOrPath {
                Var = name
            } : new VarOrPath {
                Path = f
            }).ToList();

            return(new IncludeExcludeFilter(
                       includeSplit?.Where(f => !string.IsNullOrWhiteSpace(f.Path)).Select(f => f.Path !.Trim()).Tap(f => SanitizeFilterPath(vam, f)).ToArray(),
                       includeSplit?.Where(f => f.Var != null).Select(f => f.Var !).ToArray(),
                       excludeSplit?.Where(f => !string.IsNullOrWhiteSpace(f.Path)).Select(f => f.Path !.Trim()).ToArray(),
                       excludeSplit?.Where(f => f.Var != null).Select(f => f.Var !).ToArray()));
        }
예제 #5
0
        public void PackageFilter(string[] includes, string[] excludes, string name, bool included)
        {
            var filter = Filter.From(@"C:\VaM", includes, excludes);

            Assert.That(!filter.IsFiltered(VarPackageName.TryGet(name, out var x) ? x : null), Is.EqualTo(included));
        }
예제 #6
0
        private async Task ExecuteOneAsync(IProgress <ProgressInfo> reporter, IFilter filter, int packageFilesCount, string file)
        {
            var filename = _fs.Path.GetFileName(file);

            if (!VarPackageName.TryGet(filename, out var name) || name == null)
            {
                throw new VarbsorberException($"Invalid var package name: '{filename}'");
            }
            if (filter.IsFiltered(name))
            {
                return;
            }
            reporter.Report(new ProgressInfo(Interlocked.Increment(ref _scanned), packageFilesCount, filename));

            try
            {
                var files = new List <VarPackageFile>();
                using var stream  = _fs.File.OpenRead(file);
                using var archive = new ZipArchive(stream);
                var metaEntry = archive.Entries.FirstOrDefault(e => e.FullName == "meta.json");
                if (metaEntry == null)
                {
                    throw new InvalidOperationException($"No meta.json available in .var package");
                }
                dynamic?meta;
                using (var metaStream = metaEntry.Open())
                    using (var streamReader = new StreamReader(metaStream))
                        using (var jsonReader = new JsonTextReader(streamReader))
                        {
                            meta = _serializer.Deserialize(jsonReader);
                        }
                if (meta == null)
                {
                    throw new InvalidOperationException($"Could not deserialize meta.json from .var package (deserialized as null)");
                }
                var preloadMorphs = meta.customOptions?.preloadMorphs == "true";
                foreach (var entry in archive.Entries)
                {
                    if (entry.FullName.EndsWith(@"/"))
                    {
                        continue;
                    }
                    if (entry.FullName == "meta.json")
                    {
                        continue;
                    }
                    if (!preloadMorphs && _morphExtensions.Contains(_fs.Path.GetExtension(entry.FullName).ToLowerInvariant()))
                    {
                        continue;
                    }
                    var packageFile = await ReadPackageFileAsync(entry);

                    files.Add(packageFile);
                    Interlocked.Increment(ref _files);
                }
                if (files.Count > 0)
                {
                    _packages.Add(new VarPackage(name, file, files));
                }
            }
            catch (Exception exc)
            {
                var message = $"[ERROR] Error loading var {filename}: {exc.Message}";
                _errors.Add(message);
                _logger.Log(message);
            }
        }
예제 #7
0
 public bool IsFiltered(VarPackageName package) => false;