public bool Execute(CakeOptions options) { _console.WriteLine(); _console.WriteLine("Usage: Cake.exe [build-script] [-verbosity=value]"); _console.WriteLine(" [-showdescription] [-dryrun] [..]"); _console.WriteLine(); _console.WriteLine("Example: Cake.exe"); _console.WriteLine("Example: Cake.exe build.cake -verbosity=quiet"); _console.WriteLine("Example: Cake.exe build.cake -showdescription"); _console.WriteLine(); _console.WriteLine("Options:"); _console.WriteLine(" -verbosity=value Specifies the amount of information to be displayed."); _console.WriteLine(" ({0})", string.Join(", ", Enum.GetNames(typeof(Verbosity)))); _console.WriteLine(" -showdescription Shows description about tasks."); _console.WriteLine(" -dryrun Performs a dry run."); _console.WriteLine(" -version Displays version information."); _console.WriteLine(" -help Displays usage information."); if (!_environment.IsUnix()) { _console.WriteLine(" -mono Uses the Mono Compiler, rather than Roslyn script engine."); } _console.WriteLine(" -experimental Uses the nightly builds of Roslyn script engine."); _console.WriteLine(); return(true); }
protected FilePath GetToolPath(TSettings settings, FilePath toolPath) { if (toolPath != null) { return(toolPath.MakeAbsolute(_environment)); } var toolExeNames = GetToolExecutableNames(); string[] pathDirs = null; // Look for each possible executable name in various places. foreach (var toolExeName in toolExeNames) { // First look in ./tools/ toolPath = _globber.GetFiles("./tools/**/" + toolExeName).FirstOrDefault(); if (toolPath != null) { return(toolPath.MakeAbsolute(_environment)); } // Cache the PATH directory list if we didn't already. if (pathDirs == null) { var pathEnv = _environment.GetEnvironmentVariable("PATH"); if (!string.IsNullOrEmpty(pathEnv)) { pathDirs = pathEnv.Split(new[] { _environment.IsUnix() ? ':' : ';' }, StringSplitOptions.RemoveEmptyEntries); } else { pathDirs = new string[] { }; } } // Look in every PATH directory for the file. foreach (var pathDir in pathDirs) { var file = new DirectoryPath(pathDir).CombineWithFilePath(toolExeName); if (_fileSystem.Exist(file)) { return(file.MakeAbsolute(_environment)); } } } // Look through all the alternative directories for the tool. var alternativePaths = GetAlternativeToolPaths(settings) ?? Enumerable.Empty <FilePath>(); foreach (var altPath in alternativePaths) { if (_fileSystem.Exist(altPath)) { return(altPath.MakeAbsolute(_environment)); } } return(null); }
/// <summary> /// Initializes a new instance of the <see cref="PathComparer"/> class. /// </summary> /// <param name="environment">The environment.</param> public PathComparer(ICakeEnvironment environment) { if (environment == null) { throw new ArgumentNullException("environment"); } _isCaseSensitive = environment.IsUnix(); }
/// <summary> /// Gets the alternative tool paths to CMake. /// </summary> /// <param name="settings">The settings.</param> /// <returns>The alternative tool paths to CMake.</returns> protected override IEnumerable <FilePath> GetAlternativeToolPaths(CMakeSettings settings) { if (!_environment.IsUnix()) { var programFiles = _environment.GetSpecialPath(SpecialPath.ProgramFilesX86); var cmakePath = programFiles.Combine("cmake/bin").CombineWithFilePath("cmake.exe"); return(new[] { cmakePath }); } return(new FilePath[] { }); }
private GlobNode ParseRoot(GlobParserContext context) { if (_environment.IsUnix()) { // Starts with a separator? if (context.CurrentToken.Kind == GlobTokenKind.PathSeparator) { return(new UnixRoot()); } } else { // Starts with a separator? if (context.CurrentToken.Kind == GlobTokenKind.PathSeparator) { if (context.Peek().Kind == GlobTokenKind.PathSeparator) { throw new NotSupportedException("UNC paths are not supported."); } // Get the drive from the working directory. var workingDirectory = _environment.WorkingDirectory; var root = workingDirectory.FullPath.Split(':').First(); return(new WindowsRoot(root)); } // Is this a drive? if (context.CurrentToken.Kind == GlobTokenKind.Identifier && context.CurrentToken.Value.Length == 1 && context.Peek().Kind == GlobTokenKind.WindowsRoot) { var identifier = ParseIdentifier(context); context.Accept(GlobTokenKind.WindowsRoot); return(new WindowsRoot(identifier.Value)); } } // Starts with an identifier? if (context.CurrentToken.Kind == GlobTokenKind.Identifier) { // Is the identifer indicating a current directory? if (context.CurrentToken.Value == ".") { context.Accept(); if (context.CurrentToken.Kind != GlobTokenKind.PathSeparator) { throw new InvalidOperationException(); } context.Accept(); } } return(new RelativeRoot()); }
/// <summary> /// Starts a process using the specified information. /// </summary> /// <param name="filePath">The file name such as an application or document with which to start the process.</param> /// <param name="settings">The information about the process to start.</param> /// <returns>A process handle.</returns> public IProcess Start(FilePath filePath, ProcessSettings settings) { if (filePath == null) { throw new ArgumentNullException("filePath"); } if (settings == null) { throw new ArgumentNullException("settings"); } // Get the fileName var fileName = _environment.IsUnix() ? filePath.FullPath : filePath.FullPath.Quote(); // Get the arguments. var arguments = settings.Arguments ?? new ProcessArgumentBuilder(); if (!settings.Silent) { // Log the filename and arguments. var message = string.Concat(fileName, " ", arguments.RenderSafe().TrimEnd()); _log.Verbose(Verbosity.Diagnostic, "Executing: {0}", message); } // Get the working directory. var workingDirectory = settings.WorkingDirectory ?? _environment.WorkingDirectory; settings.WorkingDirectory = workingDirectory.MakeAbsolute(_environment); // Create the process start info. var info = new ProcessStartInfo(fileName) { Arguments = arguments.Render(), WorkingDirectory = workingDirectory.FullPath, UseShellExecute = false, RedirectStandardOutput = settings.RedirectStandardOutput }; // Start and return the process. var process = Process.Start(info); if (process == null) { return(null); } var consoleOutputQueue = settings.RedirectStandardOutput ? SubscribeStandardConsoleOutputQueue(process) : null; return(new ProcessWrapper(process, _log, arguments.FilterUnsafe, consoleOutputQueue)); }
private RootNode ParseRoot() { if (_environment.IsUnix()) { // Starts with a separator? if (_currentToken.Kind == TokenKind.PathSeparator) { return(new UnixRoot()); } } else { // Starts with a separator? if (_currentToken.Kind == TokenKind.PathSeparator) { if (_scanner.Peek().Kind == TokenKind.PathSeparator) { throw new NotSupportedException("UNC paths are not supported."); } return(new WindowsRoot(string.Empty)); } // Is this a drive? if (_currentToken.Kind == TokenKind.Identifier && _currentToken.Value.Length == 1 && _scanner.Peek().Kind == TokenKind.WindowsRoot) { var identifier = ParseIdentifier(); Accept(TokenKind.WindowsRoot); return(new WindowsRoot(identifier.Identifier)); } } // Starts with an identifier? if (_currentToken.Kind == TokenKind.Identifier) { // Is the identifer indicating a current directory? if (_currentToken.Value == ".") { Accept(); if (_currentToken.Kind != TokenKind.PathSeparator) { throw new InvalidOperationException(); } Accept(); } return(new RelativeRoot()); } throw new NotImplementedException(); }
public static FilePath GetXBuildPath(IFileSystem fileSystem, ICakeEnvironment environment, XBuildToolVersion version) { _environment = environment; _fileSystem = fileSystem; if (_environment.IsUnix()) { return(GetWhichXBuild()); } else { return(GetWindowsXBuild()); } }
private List <DirectoryPath> GetPathDirectories() { var result = new List <DirectoryPath>(); var path = _environment.GetEnvironmentVariable("PATH"); if (!string.IsNullOrEmpty(path)) { var separator = new[] { _environment.IsUnix() ? ':' : ';' }; var paths = path.Split(separator, StringSplitOptions.RemoveEmptyEntries); result.AddRange(paths.Select(p => new DirectoryPath(p))); } return(result); }
/// <summary> /// Resolves the path to nuget.exe. /// </summary> /// <returns>The path to nuget.exe.</returns> public FilePath ResolvePath() { // Check if path already resolved if (_cachedPath != null && _cachedPath.Exists) { return(_cachedPath.Path); } // Try to resolve it with the regular tool resolver. var toolsExe = _tools.Resolve("nuget.exe"); if (toolsExe != null) { var toolsFile = _fileSystem.GetFile(toolsExe); if (toolsFile.Exists) { _cachedPath = toolsFile; return(_cachedPath.Path); } } // Check if path set to environment variable var environmentExe = _environment.GetEnvironmentVariable("NUGET_EXE"); if (!string.IsNullOrWhiteSpace(environmentExe)) { var envFile = _fileSystem.GetFile(environmentExe); if (envFile.Exists) { _cachedPath = envFile; return(_cachedPath.Path); } } // On Unix /usr/bin/nuget or /usr/local/bin/nuget are viable options if (_environment.IsUnix()) { foreach (var systemPath in _unixSystemPaths) { if (_fileSystem.Exist(systemPath)) { _cachedPath = _fileSystem.GetFile(systemPath); return(_cachedPath.Path); } } } throw new CakeException("Could not locate nuget.exe."); }
/// <summary> /// Initializes a new instance of the <see cref="Globber"/> class. /// </summary> /// <param name="fileSystem">The file system.</param> /// <param name="environment">The environment.</param> public Globber(IFileSystem fileSystem, ICakeEnvironment environment) { if (fileSystem == null) { throw new ArgumentNullException("fileSystem"); } if (environment == null) { throw new ArgumentNullException("environment"); } _environment = environment; _parser = new GlobParser(environment); _visitor = new GlobVisitor(fileSystem, environment); _comparer = new PathComparer(environment.IsUnix()); }
/// <summary> /// Returns <see cref="Path" /> instances matching the specified pattern. /// </summary> /// <param name="pattern">The pattern to match.</param> /// <returns> /// <see cref="Path" /> instances matching the specified pattern. /// </returns> public IEnumerable <Path> Match(string pattern) { var scanner = new Scanner(pattern); var parser = new Parser(scanner, _environment); var path = parser.Parse(); var rootNodes = new List <Node>(); while (path.Count > 0) { // Pop the first path item. var segment = path[0]; path.RemoveAt(0); if (segment.IsWildcard) { path.Insert(0, segment); break; } rootNodes.Add(segment); } // Fix up the tree. var newRoot = FixRootNode(rootNodes); if (newRoot != null) { rootNodes[0] = newRoot; } // Ge the root. var rootPath = string.Join("/", rootNodes.Select(x => x.Render())); // Nothing left in the path? if (path.Count == 0) { return(GetPath(rootPath)); } var rootDirectory = new DirectoryPath(rootPath); // Walk the root and return the unique results. var segments = new Stack <Node>(((IEnumerable <Node>)path).Reverse()); var results = Walk(rootDirectory, segments); return(new HashSet <Path>(results, new PathComparer(_environment.IsUnix())).ToArray()); }
public static FilePath GetPscpPath(IFileSystem fileSystem, ICakeEnvironment environment) { if (fileSystem == null) { throw new ArgumentNullException("fileSystem"); } if (environment == null) { throw new ArgumentNullException("environment"); } // Cake already searches the PATH for the executable tool names. // Check for other known locations. return(!environment.IsUnix() ? CheckCommonWindowsPaths(fileSystem) : null); }
/// <summary> /// Returns <see cref="Path" /> instances matching the specified pattern. /// </summary> /// <param name="pattern">The pattern to match.</param> /// <param name="predicate">The predicate used to filter directories based on file system information.</param> /// <returns> /// <see cref="Path" /> instances matching the specified pattern. /// </returns> public IEnumerable <Path> Match(string pattern, Func <IDirectory, bool> predicate) { if (pattern == null) { throw new ArgumentNullException("pattern"); } if (string.IsNullOrWhiteSpace(pattern)) { return(Enumerable.Empty <Path>()); } // Parse the pattern into an AST. var root = _parser.Parse(pattern, _environment.IsUnix()); // Visit all nodes in the parsed patterns and filter the result. return(_visitor.Walk(root, predicate) .Select(x => x.Path) .Distinct(_comparer)); }
/// <summary> /// Initializes a new instance of the <see cref="Zipper"/> class. /// </summary> /// <param name="fileSystem">The file system.</param> /// <param name="environment">The environment.</param> /// <param name="log">The log.</param> public Zipper(IFileSystem fileSystem, ICakeEnvironment environment, ICakeLog log) { if (fileSystem == null) { throw new ArgumentNullException("fileSystem"); } if (environment == null) { throw new ArgumentNullException("environment"); } if (log == null) { throw new ArgumentNullException("log"); } _fileSystem = fileSystem; _environment = environment; _log = log; _comparison = environment.IsUnix() ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; }
public FakeFileSystemTree(ICakeEnvironment environment) { if (environment == null) { throw new ArgumentNullException("environment"); } if (environment.WorkingDirectory == null) { throw new ArgumentException("Working directory not set."); } if (environment.WorkingDirectory.IsRelative) { throw new ArgumentException("Working directory cannot be relative."); } _comparer = new PathComparer(environment.IsUnix()); _root = new FakeDirectory(this, "/"); _root.Create(); }
/// <summary> /// Initializes a new instance of the <see cref="Globber"/> class. /// </summary> /// <param name="fileSystem">The file system.</param> /// <param name="environment">The environment.</param> public Globber(IFileSystem fileSystem, ICakeEnvironment environment) { if (fileSystem == null) { throw new ArgumentNullException("fileSystem"); } if (environment == null) { throw new ArgumentNullException("environment"); } _fileSystem = fileSystem; _environment = environment; _options = RegexOptions.Singleline; if (!_environment.IsUnix()) { // On non unix systems, we should ignore case. _options |= RegexOptions.IgnoreCase; } }
/// <summary> /// Gets alternative file paths which the tool may exist in /// </summary> /// <param name="settings">The settings.</param> /// <returns>The default tool path.</returns> protected override IEnumerable <FilePath> GetAlternativeToolPaths(CakeSettings settings) { const string homebrewCakePath = "/usr/local/Cellar/cake/"; if (!_environment.IsUnix()) { return(Enumerable.Empty <FilePath>()); } if (!_fileSystem.Exist(new DirectoryPath(homebrewCakePath))) { return(Enumerable.Empty <FilePath>()); } var files = _globber.GetFiles(homebrewCakePath + "**/Cake.exe"); var filePaths = files as FilePath[] ?? files.ToArray(); return(filePaths.Length == 0 ? Enumerable.Empty <FilePath>() : filePaths.OrderByDescending(f => f.FullPath)); }
public GlobberFixture(bool isFileSystemCaseSensitive) { FileSystem = new FakeFileSystem(isFileSystemCaseSensitive); FileSystem.GetCreatedDirectory("/Temp"); FileSystem.GetCreatedDirectory("/Temp/Hello"); FileSystem.GetCreatedDirectory("/Temp/Hello/World"); FileSystem.GetCreatedDirectory("/Temp/Goodbye"); FileSystem.GetCreatedFile("/Presentation.ppt"); FileSystem.GetCreatedFile("/Budget.xlsx"); FileSystem.GetCreatedFile("/Text.txt"); FileSystem.GetCreatedFile("/Temp"); FileSystem.GetCreatedFile("/Temp/Hello/World/Text.txt"); FileSystem.GetCreatedFile("/Temp/Hello/World/Picture.png"); FileSystem.GetCreatedFile("/Temp/Goodbye/OtherText.txt"); FileSystem.GetCreatedFile("/Temp/Goodbye/OtherPicture.png"); FileSystem.GetCreatedFile("/Working/Text.txt"); FileSystem.GetCreatedFile("C:/Temp/Hello/World/Text.txt"); Environment = Substitute.For <ICakeEnvironment>(); Environment.IsUnix().Returns(isFileSystemCaseSensitive); Environment.WorkingDirectory.Returns("/Temp"); }
/// <summary> /// Resolves the path to nuget.exe. /// </summary> /// <returns>The path to nuget.exe.</returns> public FilePath ResolvePath() { // Check if path allready resolved if (_cachedPath != null && _cachedPath.Exists) { return(_cachedPath.Path); } // Check if tool exists in tool folder const string expression = "./tools/**/NuGet.exe"; var toolsExe = _globber.GetFiles(expression).FirstOrDefault(); if (toolsExe != null) { var toolsFile = _fileSystem.GetFile(toolsExe); if (toolsFile.Exists) { _cachedPath = toolsFile; return(_cachedPath.Path); } } // Check if path set to environment variable var environmentExe = _environment.GetEnvironmentVariable("NUGET_EXE"); if (!string.IsNullOrWhiteSpace(environmentExe)) { var envFile = _fileSystem.GetFile(environmentExe); if (envFile.Exists) { _cachedPath = envFile; return(_cachedPath.Path); } } // On Unix /usr/bin/nuget is a viable option if (_environment.IsUnix()) { var nugetUnixPath = new FilePath("/usr/bin/nuget"); if (_fileSystem.Exist(nugetUnixPath)) { _cachedPath = _fileSystem.GetFile(nugetUnixPath); return(_cachedPath.Path); } } // Last resort try path var envPath = _environment.GetEnvironmentVariable("path"); if (!string.IsNullOrWhiteSpace(envPath)) { var pathFile = envPath .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) .Select(path => _fileSystem.GetDirectory(path)) .Where(path => path.Exists) .Select(path => path.Path.CombineWithFilePath("nuget.exe")) .Select(_fileSystem.GetFile) .FirstOrDefault(file => file.Exists); if (pathFile != null) { _cachedPath = pathFile; return(_cachedPath.Path); } } throw new CakeException("Could not locate nuget.exe."); }