private void Include(string args, ref Input input) { var match = IncludeRegex.Match(args); if (!match.Success) { throw new SqlCmdException("Invalid syntax in :r directive."); } string path; path = match.Groups[nameof(path)].Value; path = Unquote(path); path = ReplaceVariables(path); var text = IoHelper.ReadText(path); input = new Input(path, text, input); }
public override void Show(IInputService input) { base.Show(input); if (!string.IsNullOrEmpty(CommonName)) { input.Show("Common name", CommonName, level: 1); } // Site filter if (IncludeSiteIds != null && IncludeSiteIds.Any()) { input.Show("Sites", string.Join(",", IncludeSiteIds), level: 1); } else { input.Show("Sites", "All", level: 1); } // Binding filter if (IncludeRegex != default) { input.Show("Regex", IncludeRegex.ToString(), level: 1); } else if (!string.IsNullOrWhiteSpace(IncludePattern)) { input.Show("Pattern", IncludePattern, level: 1); } else if (IncludeHosts != null && IncludeHosts.Any()) { input.Show("Hosts", string.Join(',', IncludeHosts), level: 1); } else { input.Show("Hosts", "All", level: 1); } // Last-minute exclude if (ExcludeHosts != null && ExcludeHosts.Any()) { input.Show("Exclude", string.Join(',', ExcludeHosts), level: 1); } }
/// <summary> /// /// </summary> /// <returns></returns> public override bool Execute() { try { Log.LogMessage("Copy directory from '{0}' to '{1}'", SourceFolder, DestinationFolder); if (Path.DirectorySeparatorChar == '/') { if (!string.IsNullOrEmpty(IncludeRegex)) { IncludeRegex = IncludeRegex.Replace('/', '\\').Replace(@"\\", "/"); Log.LogMessage("Include regex: {0}", IncludeRegex); } if (!string.IsNullOrEmpty(ExcludeRegex)) { ExcludeRegex = ExcludeRegex.Replace('/', '\\').Replace(@"\\", "/"); Log.LogMessage("Exclude regex: {0}", ExcludeRegex); } } var include = string.IsNullOrEmpty(IncludeRegex) ? null : new Regex(IncludeRegex, RegexOptions.IgnoreCase); var exclude = string.IsNullOrEmpty(ExcludeRegex) ? null : new Regex(ExcludeRegex, RegexOptions.IgnoreCase); var fullSource = Path.GetFullPath(SourceFolder).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar; var fullDest = Path.GetFullPath(DestinationFolder).TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar; var sources = new List <string>(); var dirs = Directory.GetDirectories(fullSource, "*", SearchOption.AllDirectories); for (var i = 0; i < dirs.Length; i++) { dirs[i] = dirs[i] + Path.DirectorySeparatorChar; } sources.AddRange(dirs); sources.AddRange(Directory.GetFiles(fullSource, "*", SearchOption.AllDirectories)); foreach (var source in sources) { var fileRelative = Path.DirectorySeparatorChar + source.Substring(fullSource.Length); if ((include != null && !include.IsMatch(fileRelative)) || (exclude != null && exclude.IsMatch(fileRelative))) { continue; } var destPath = Path.Combine(fullDest, fileRelative.Trim(Path.DirectorySeparatorChar)); if (Directory.Exists(source)) { Log.LogMessage("Create directory '{0}'", destPath); Directory.CreateDirectory(destPath); } else if (!File.Exists(destPath) || ReplaceFiles) { Log.LogMessage("Copy file from '{0}' to '{1}'", source, destPath); File.Copy(source, destPath); } } return(true); } catch (Exception err) { Log.LogErrorFromException(err); return(false); } }
private static IReadOnlyList <string> ScanConfigFiles([NotNull] string entryFilePath, [NotNull] Encoding encoding) { var result = new List <string>(); var q = new Queue <(string Path, string BasePath)>(); entryFilePath = Path.GetFullPath(entryFilePath); q.Enqueue((Path: Path.GetFileName(entryFilePath), BasePath: Path.GetDirectoryName(entryFilePath))); while (q.Count > 0) { var(path, basePath) = q.Dequeue(); if (!Path.IsPathRooted(path)) { // Treat as absolute path. path = Path.Combine(basePath, path); } var globCharIndex = path.IndexOfAny(GlobHelper.PartialGlobChars); try { // The path has been expanded, e.g. xxx/**/*yyy.zzz -> C:\dir\xxx\**\*.yyy.zzz, // so a glob char should not appear be the first char. if (globCharIndex == 0) { throw new ApplicationException("Unexpected: glob char index == 0"); } if (globCharIndex > 0) { var sepIndex = path.LastIndexOfAny(GlobHelper.PathSeparatorChars, globCharIndex - 1); if (sepIndex < 0) { throw new ArgumentOutOfRangeException(nameof(sepIndex)); } basePath = path.Substring(0, sepIndex + 1); var dirInfo = new DirectoryInfo(basePath); if (dirInfo.Exists) { var subPath = path.Substring(sepIndex + 1); foreach (var fileInfo in dirInfo.GlobFiles(subPath)) { if (result.Contains(fileInfo.FullName)) { continue; } q.Enqueue((Path: fileInfo.Name, BasePath: fileInfo.DirectoryName)); } } else { GameLog.Warn("{0}: Config file {1}: Cannot find directory '{2}'.", ReflectionHelper.GetCallerName(), entryFilePath, dirInfo.FullName); } continue; } else { if (!result.Contains(path)) { result.Add(path); } } using (var fileStream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var streamReader = new StreamReader(fileStream, encoding)) { // Reads until the first line which: // - is not blank; // - does not start with "#", or // the content inside the comments does not start with "@include" while (!streamReader.EndOfStream) { var line = streamReader.ReadLine(); if (line == null) { break; } if (string.IsNullOrWhiteSpace(line)) { continue; } line = line.TrimStart(); if (!line.StartsWith("#")) { break; } line = line.Substring(1).Trim(); var match = IncludeRegex.Match(line); if (!match.Success) { break; } var includePath = match.Groups["path"].Value; if (string.IsNullOrEmpty(includePath)) { continue; } q.Enqueue((Path: includePath, BasePath: basePath)); } } } } catch (Exception) { GameLog.Error("An exception occurred while parsing config file '{0}'.", path); throw; } } return(result); }