public TemporaryZipFile(string inputFileName, string filter, ILogger logger) { this.inputFileName = inputFileName; this.logger = logger; dataDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(dataDirectory); logger.LogInformation($"Extracting zip file {inputFileName}"); ZipFile.ExtractToDirectory(inputFileName, dataDirectory); var filesInDir = Directory.EnumerateFiles(dataDirectory, "*.*", SearchOption.AllDirectories); FilesInDirectory = filesInDir.ToList(); // don't allow parent directory traversal filter = filter.Replace(@"..\", "").Replace("../", ""); var globs = filter.Split('\n').Where(s => !string.IsNullOrWhiteSpace(s)).ToList(); if (globs.Count > 0) { var files = Globber.GetFiles(new DirectoryInfo(dataDirectory), globs); FilteredFilesInDirectory = files.Select(f => f.FullName).ToList(); } // If no filtered, default to all if (FilteredFilesInDirectory == null) { FilteredFilesInDirectory = FilesInDirectory.ToList(); } FilesExceptFiltered = FilesInDirectory.Except(FilteredFilesInDirectory).ToList(); }
public IEnumerable <IFile> GetFiles(IDirectory directory, IEnumerable <string> patterns) { if (directory == null) { throw new ArgumentNullException(nameof(directory)); } return(patterns .Where(x => x != null) .Select(x => { bool negated = x[0] == '!'; FilePath filePath = negated ? new FilePath(x.Substring(1)) : new FilePath(x); if (filePath.IsAbsolute) { // The globber doesn't support absolute paths, so get the root directory of this path (including provider) IDirectory rootDirectory = GetDirectory(filePath.Root); FilePath relativeFilePath = filePath.RootRelative.Collapse(); return Tuple.Create(rootDirectory, negated ? ('!' + relativeFilePath.FullPath) : relativeFilePath.FullPath); } return Tuple.Create(directory, x); }) .GroupBy(x => x.Item1, x => x.Item2, new DirectoryEqualityComparer()) .SelectMany(x => Globber.GetFiles(x.Key, x))); }
public void ShouldThrowForRootPatterns(string pattern) { // Given IFileProvider fileProvider = GetFileProvider(); IDirectory directory = fileProvider.GetDirectory("/a"); // When, Then Assert.Throws <ArgumentException>(() => Globber.GetFiles(directory, pattern)); }
/// <inheritdoc /> protected override IEnumerable <IDocument> Execute(IExecutionContext context) { DocumentFileProvider fileProvider = new DocumentFileProvider(context.Inputs); IEnumerable <IDirectory> directories = context.FileSystem .GetInputDirectories() .Select(x => fileProvider.GetDirectory(x.Path)); IEnumerable <IFile> matches = directories.SelectMany(x => Globber.GetFiles(x, _patterns)); return(matches.Select(x => x.Path).Distinct().Select(match => fileProvider.GetDocument(match))); }
public AppxFile(string inputFileName, string publisher, ILogger logger, IDirectoryUtility directoryUtility, string makeAppxPath, string filter) { this.inputFileName = inputFileName; this.publisher = publisher; this.logger = logger; this.directoryUtility = directoryUtility; this.makeAppxPath = makeAppxPath; dataDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(dataDirectory); Unpack(); UpdateManifestPublisher(); var filesInDir = Directory.EnumerateFiles(dataDirectory, "*.*", SearchOption.AllDirectories); FilesInDirectory = filesInDir.ToList(); // don't allow parent directory traversal filter = filter.Replace(@"..\", "").Replace("../", ""); var globs = filter.Split('\n').Where(s => (!string.IsNullOrWhiteSpace(s))) .Where(s => (!s.StartsWith("!"))) .ToList(); var antiglobs = filter.Split('\n').Where(s => (!string.IsNullOrWhiteSpace(s))) .Where(s => (s.StartsWith("!"))) .Select(s => s.Substring(1)) .ToList(); if (globs.Count > 0) { var files = Globber.GetFiles(new DirectoryInfo(dataDirectory), globs); FilteredFilesInDirectory = files.Select(f => f.FullName).ToList(); } // If no filtered, default to all if (FilteredFilesInDirectory == null) { FilteredFilesInDirectory = FilesInDirectory.ToList(); } if (antiglobs.Count > 0) { var antifiles = Globber.GetFiles(new DirectoryInfo(dataDirectory), antiglobs) .Select(f => f.FullName) .ToList(); FilteredFilesInDirectory = FilteredFilesInDirectory.Except(antifiles).ToList(); } FilesExceptFiltered = FilesInDirectory.Except(FilteredFilesInDirectory).ToList(); }
public void ShouldReturnMatchedFiles(string directoryPath, string[] patterns, string[] resultPaths) { // Given IFileProvider fileProvider = GetFileProvider(); IDirectory directory = fileProvider.GetDirectory(directoryPath); // When IEnumerable <IFile> matches = Globber.GetFiles(directory, patterns); // Then CollectionAssert.AreEquivalent(resultPaths, matches.Select(x => x.Path.FullPath)); }
public CakeContextFixture() { IsRunningOnAppVeyor = string.Equals(System.Environment.GetEnvironmentVariable("APPVEYOR"), "true", StringComparison.OrdinalIgnoreCase); ServiceEndpoint = !IsRunningOnAppVeyor ? "http://localhost/reportserver/ReportService2010.asmx" : "http://appvyr-win/Reports_SQL2014/reportserver/ReportService2010.asmx"; var cakeRuntime = Substitute.For <ICakeRuntime>(); cakeRuntime.BuiltFramework.Returns(new FrameworkName(".NET Framework", new Version(4, 6, 1))); cakeRuntime.CakeVersion.Returns(typeof(ICakeRuntime).GetTypeInfo().Assembly.GetName().Version); Log = Substitute.For <ICakeLog>(); Arguments = Substitute.For <ICakeArguments>(); ProcessRunner = Substitute.For <IProcessRunner>(); Registry = Substitute.For <IRegistry>(); Tools = Substitute.For <IToolLocator>(); FileSystem = Substitute.For <IFileSystem>(); Environment = Substitute.For <ICakeEnvironment>(); Environment.Runtime.Returns(cakeRuntime); Environment.WorkingDirectory.Returns(new DirectoryPath(AppContext.BaseDirectory)); Configuration = Substitute.For <ICakeConfiguration>(); Globber = Substitute.For <IGlobber>(); Globber.GetFiles(Arg.Is <string>("./App_Data/**/Emp*.rsd")).Returns(new FilePath[] { new FilePath("./App_Data/DataSets/EmployeeSalesDetail.rsd"), new FilePath("./App_Data/DataSets/EmpSalesMonth.rsd"), new FilePath("./App_Data/DataSets/EmployeeSalesYearOverYear.rsd") }); Globber.GetFiles(Arg.Is <string>("./App_Data/**/*.rdl")).Returns(new FilePath[] { new FilePath("./App_Data/Reports/Company Sales.rdl"), new FilePath("./App_Data/Reports/Sales Order Detail.rdl"), new FilePath("./App_Data/Reports/Store_Contacts.rdl") }); Globber.GetFiles(Arg.Is <string>("./App_Data/**/*.rds")).Returns(new FilePath[] { new FilePath("./App_Data/DataSources/AdventureWorks.rds") }); ReportsDirectory = System.IO.Path.Combine(AppContext.BaseDirectory, "App_Data", "Reports"); DataSetsDirectory = System.IO.Path.Combine(AppContext.BaseDirectory, "App_Data", "DataSets"); DataSourcesDirectory = System.IO.Path.Combine(AppContext.BaseDirectory, "App_Data", "DataSources"); }
public XmlDocExampleCodeParserFixture() { XmlFilePath = "/Working/Cake.Common.xml"; Pattern = "/Working/Cake.*.xml"; var fileSystem = new FakeFileSystem(false); fileSystem.GetCreatedFile(XmlFilePath.FullPath, Resources.XmlDoc_ExampeCode_Cake_Common_Xml); fileSystem.GetCreatedFile("/Working/Cake.UnCommon.xml", Resources.XmlDoc_ExampeCode_Cake_Common_Xml); FileSystem = fileSystem; Globber = Substitute.For <IGlobber>(); Globber.GetFiles(Pattern).Returns(new FilePath[] { "/Working/Cake.Common.xml", "/Working/Cake.UnCommon.xml" }); }
public XmlDocExampleCodeParserFixture() { XmlFilePath = "/Working/Cake.Common.xml"; Pattern = "/Working/Cake.*.xml"; var environment = FakeEnvironment.CreateUnixEnvironment(); var fileSystem = new FakeFileSystem(environment); fileSystem.CreateFile(XmlFilePath.FullPath).SetContent(Resources.XmlDoc_ExampeCode_Cake_Common_Xml); fileSystem.CreateFile("/Working/Cake.UnCommon.xml").SetContent(Resources.XmlDoc_ExampeCode_Cake_Common_Xml); FileSystem = fileSystem; Globber = Substitute.For <IGlobber>(); Globber.GetFiles(Pattern).Returns(new FilePath[] { "/Working/Cake.Common.xml", "/Working/Cake.UnCommon.xml" }); }
public void NestedFoldersWilcard() { // Given TestFileProvider fileProvider = new TestFileProvider { { "/a/b/c/x.txt" }, { "/a/bar/foo/y.txt" } }; IDirectory directory = fileProvider.GetDirectory("/a"); // When IEnumerable <IFile> matches = Globber.GetFiles(directory, new[] { "**/*.txt" }); // Then CollectionAssert.AreEquivalent(new[] { "/a/b/c/x.txt", "/a/bar/foo/y.txt" }, matches.Select(x => x.Path.FullPath)); }
public void RecursiveWildcardTests(string directoryPath, string[] patterns, string[] resultPaths) { // Given TestFileProvider fileProvider = new TestFileProvider { { "/a/b/c/d/e/1/2/3.txt" } }; IDirectory directory = fileProvider.GetDirectory(directoryPath); // When IEnumerable <IFile> matches = Globber.GetFiles(directory, patterns); IEnumerable <IFile> matchesReversedSlash = Globber.GetFiles(directory, patterns.Select(x => x.Replace("/", "\\"))); // Then CollectionAssert.AreEquivalent(resultPaths, matches.Select(x => x.Path.FullPath)); CollectionAssert.AreEquivalent(resultPaths, matchesReversedSlash.Select(x => x.Path.FullPath)); }
public void DoubleWildcardShouldMatchZeroOrMorePathSegments() { // Given TestFileProvider fileProvider = new TestFileProvider { { "/root/a/x.txt" }, { "/root/a/b/x.txt" }, { "/root/d/x.txt" } }; IDirectory directory = fileProvider.GetDirectory("/"); // When IEnumerable <IFile> matches = Globber.GetFiles(directory, new[] { "root/{a,}/**/x.txt" }); // Then matches.Select(x => x.Path.FullPath).ShouldBe( new[] { "/root/a/x.txt", "/root/a/b/x.txt", "/root/d/x.txt" }, true); }
public ProjectParserFixture() { ProjFilePath = "/Working/Cake.Sample.csproj"; Pattern = "/Working/Cake.*.csproj"; var environment = FakeEnvironment.CreateUnixEnvironment(); Environment = environment; var fileSystem = new FakeFileSystem(environment); fileSystem.CreateFile(ProjFilePath.FullPath).SetContent(Resources.Csproj_ProjectFile); fileSystem.CreateFile("/Working/Cake.Incomplete.csproj").SetContent(Resources.Csproj_IncompleteFile); FileSystem = fileSystem; Globber = Substitute.For <IGlobber>(); Globber.GetFiles(Pattern).Returns(new FilePath[] { "/Working/Cake.Sample.csproj", "/Working/Cake.Incomplete.csproj" }); Log = Substitute.For <ICakeLog>(); }
private string ResolveLiquibaseJarFile(string liquibaseJarPattern) { Path jarFile = null; try { jarFile = Tools.Resolve(liquibaseJarPattern); } catch (ArgumentException) { // illegal characters in path (when using *) // fall back to globber. } if (jarFile == null) { // try file globber jarFile = Globber.GetFiles(liquibaseJarPattern).FirstOrDefault(); } return(jarFile?.FullPath); }
public void WildcardShouldMatchZeroOrMore() { // Given TestFileProvider fileProvider = new TestFileProvider(); fileProvider.AddDirectory("/"); fileProvider.AddDirectory("/root"); fileProvider.AddDirectory("/root/a"); fileProvider.AddDirectory("/root/a/b"); fileProvider.AddDirectory("/root/d"); fileProvider.AddFile("/root/a/x.txt"); fileProvider.AddFile("/root/a/b/x.txt"); fileProvider.AddFile("/root/a/b/.txt"); fileProvider.AddFile("/root/d/x.txt"); IDirectory directory = fileProvider.GetDirectory("/"); // When IEnumerable <IFile> matches = Globber.GetFiles(directory, new[] { "root/**/*.txt" }); // Then matches.Select(x => x.Path.FullPath).ShouldBe( new[] { "/root/a/x.txt", "/root/a/b/x.txt", "/root/a/b/.txt", "/root/d/x.txt" }, true); }
public int Sign ( CommandOption configFile, CommandOption inputFile, CommandOption baseDirectory, CommandOption outputFile, CommandOption fileList, CommandOption clientSecret, CommandOption username, CommandOption name, CommandOption description, CommandOption descriptionUrl, CommandOption maxConcurrency ) { try { // verify required parameters if (!configFile.HasValue()) { signCommandLineApplication.Error.WriteLine("--config parameter is required"); return(EXIT_CODES.INVALID_OPTIONS); } if (!inputFile.HasValue()) { signCommandLineApplication.Error.WriteLine("--input parameter is required"); return(EXIT_CODES.INVALID_OPTIONS); } if (!name.HasValue()) { signCommandLineApplication.Error.WriteLine("--name parameter is required"); return(EXIT_CODES.INVALID_OPTIONS); } if (!maxConcurrency.HasValue()) { maxConcurrency.Values.Add("4"); // default to 4 } if (baseDirectory.HasValue()) { // Make sure this is rooted if (!Path.IsPathRooted(baseDirectory.Value())) { signCommandLineApplication.Error.WriteLine("--directory parameter must be rooted if specified"); return(EXIT_CODES.INVALID_OPTIONS); } } if (!baseDirectory.HasValue()) { baseDirectory.Values.Add(Environment.CurrentDirectory); } List <FileInfo> inputFiles; // If we're going to glob, we can't be fully rooted currently (fix me later) var isGlob = inputFile.Value().Contains('*'); if (isGlob) { if (Path.IsPathRooted(inputFile.Value())) { signCommandLineApplication.Error.WriteLine("--input parameter cannot be rooted when using a glob. Use a path relative to the working directory"); return(EXIT_CODES.INVALID_OPTIONS); } inputFiles = Globber.GetFiles(new DirectoryInfo(baseDirectory.Value()), inputFile.Value()) .ToList(); } else { inputFiles = new List <FileInfo> { new FileInfo(ExpandFilePath(inputFile.Value())) }; } var builder = new ConfigurationBuilder() .AddJsonFile(ExpandFilePath(configFile.Value())) .AddEnvironmentVariables(); var configuration = builder.Build(); Func <Task <string> > getAccessToken; var authority = $"{configuration["SignClient:AzureAd:AADInstance"]}{configuration["SignClient:AzureAd:TenantId"]}"; var clientId = configuration["SignClient:AzureAd:ClientId"]; var resourceId = configuration["SignClient:Service:ResourceId"]; // See if we have a Username option if (username.HasValue()) { // ROPC flow var pca = PublicClientApplicationBuilder.Create(clientId) .WithAuthority(authority) .Build(); var secret = new NetworkCredential("", clientSecret.Value()).SecurePassword; getAccessToken = async() => { var tokenResult = await pca.AcquireTokenByUsernamePassword(new[] { $"{resourceId}/user_impersonation" }, username.Value(), secret).ExecuteAsync(); return(tokenResult.AccessToken); }; } else { var context = ConfidentialClientApplicationBuilder.Create(clientId) .WithAuthority(authority) .WithClientSecret(clientSecret.Value()) .Build(); getAccessToken = async() => { // Client credential flow var res = await context.AcquireTokenForClient(new[] { $"{resourceId}/.default" }).ExecuteAsync(); return(res.AccessToken); }; } // Setup Refit var settings = new RefitSettings { AuthorizationHeaderValueGetter = getAccessToken }; var client = RestService.For <ISignService>(configuration["SignClient:Service:Url"], settings); client.Client.Timeout = Timeout.InfiniteTimeSpan; // TODO: Make configurable on command line // var max concurrency if (!int.TryParse(maxConcurrency.Value(), out var maxC) || maxC < 1) { signCommandLineApplication.Error.WriteLine("--maxConcurrency parameter is not valid"); return(EXIT_CODES.INVALID_OPTIONS); } if (inputFiles.Count == 0) { signCommandLineApplication.Error.WriteLine("No inputs found to sign."); return(EXIT_CODES.NO_INPUTS_FOUND); } Parallel.ForEach(inputFiles, new ParallelOptions { MaxDegreeOfParallelism = maxC }, input => { FileInfo output; var sw = Stopwatch.StartNew(); // Special case if there's only one input file and the output has a value, treat it as a file if (inputFiles.Count == 1 && outputFile.HasValue()) { // See if it has a file extension and if not, treat as a directory and use the input file name var outFileValue = outputFile.Value(); if (Path.HasExtension(outFileValue)) { output = new FileInfo(ExpandFilePath(outputFile.Value())); } else { output = new FileInfo(Path.Combine(ExpandFilePath(outFileValue), inputFiles[0].Name)); } } else { // if the output is specified, treat it as a directory, if not, overwrite the current file if (!outputFile.HasValue()) { output = new FileInfo(input.FullName); } else { var relative = Path.GetRelativePath(baseDirectory.Value(), input.FullName); var basePath = Path.IsPathRooted(outputFile.Value()) ? outputFile.Value() : $"{baseDirectory.Value()}{Path.DirectorySeparatorChar}{outputFile.Value()}"; var fullOutput = Path.Combine(basePath, relative); output = new FileInfo(fullOutput); } } // Ensure the output directory exists Directory.CreateDirectory(output.DirectoryName); // Do action HttpResponseMessage response; signCommandLineApplication.Out.WriteLine($"Submitting '{input.FullName}' for signing."); response = client.SignFile(input, fileList.HasValue() ? new FileInfo(ExpandFilePath(fileList.Value())) : null, HashMode.Sha256, name.Value(), description.Value(), descriptionUrl.Value()).Result; // Check response if (!response.IsSuccessStatusCode) { signCommandLineApplication.Error.WriteLine($"Error signing '{input.FullName}'"); signCommandLineApplication.Error.WriteLine($"Server returned non Ok response: {(int)response.StatusCode} {response.ReasonPhrase}"); response.EnsureSuccessStatusCode(); // force the throw to break out of the loop } var str = response.Content.ReadAsStreamAsync().Result; // If we're replacing the file, make sure to the existing one first using var fs = new FileStream(output.FullName, FileMode.Create); str.CopyTo(fs); signCommandLineApplication.Out.WriteLine($"Successfully signed '{output.FullName}' in {sw.ElapsedMilliseconds} ms"); }); } catch (AuthenticationException e) { signCommandLineApplication.Error.WriteLine(e.Message); return(EXIT_CODES.FAILED); } catch (Exception e) { signCommandLineApplication.Error.WriteLine("Exception: " + e); return(EXIT_CODES.FAILED); } return(EXIT_CODES.SUCCESS); string ExpandFilePath(string file) { if (!Path.IsPathRooted(file)) { return($"{baseDirectory.Value()}{Path.DirectorySeparatorChar}{file}"); } return(file); } }