public void TokeniseDirectory(string rootDirectoryPath, List <Token> Tokens, bool recursive = true) { var allFiles = service.GetAllFiles(rootDirectoryPath, "*.xml"); // Regex r = new Regex("{{[A-Z0-9]}}"); foreach (var item in allFiles) { string fileContent = service.GetFileContent(item); bool fileModified = false; foreach (Token token in Tokens) { if (fileContent.Contains(token.TokenName)) { fileContent = fileContent.Replace(token.TokenName, token.TokenValue); fileModified = true; } } if (fileModified) { service.SaveFileContent(item, fileContent); } } }
/// <inheritdoc /> public virtual void RunNonVersionScripts( IDbConnection connection, IDbTransaction transaction, string workingPath, List <KeyValuePair <string, string> > tokenKeyPairs = null, string bulkSeparator = null, int?commandTimeout = null, string environmentCode = null ) { //extract and filter out scripts when environment code is used var sqlScriptFiles = _directoryService.GetAllFiles(workingPath, "*.sql").ToList(); sqlScriptFiles = _directoryService.FilterFiles(workingPath, environmentCode, sqlScriptFiles).ToList(); _traceService.Info($"Found {sqlScriptFiles.Count} script files on {workingPath}" + (sqlScriptFiles.Count > 0 ? Environment.NewLine: string.Empty) + $"{string.Join(Environment.NewLine, sqlScriptFiles.Select(s => " + " + new FileInfo(s).Name))}"); //execute all script files in the target folder sqlScriptFiles.Sort(); sqlScriptFiles.ForEach(scriptFile => { var sqlStatementRaw = _fileService.ReadAllText(scriptFile); var sqlStatements = _dataService.BreakStatements(sqlStatementRaw) .Where(s => !string.IsNullOrWhiteSpace(s)) .ToList(); sqlStatements.ForEach(sqlStatement => { try { sqlStatement = _tokenReplacementService.Replace(tokenKeyPairs, sqlStatement); _traceService.Debug($"Executing sql statement as part of : {scriptFile}"); _configurationDataService.ExecuteSql( connection: connection, commandText: sqlStatement, transaction: transaction, commandTimeout: commandTimeout, traceService: _traceService); } catch (Exception) { _traceService.Error($"Failed to execute sql statements in script file {scriptFile}.{Environment.NewLine}" + $"The failing statement starts here --------------------------{Environment.NewLine}" + $"{sqlStatement} {Environment.NewLine}" + $"The failing statement ends here --------------------------"); throw; } }); _traceService.Info($"Executed script file {scriptFile}."); }); }
private void RunNonVersionScripts( IDbConnection connection, IDbTransaction transaction, string workingPath, List <KeyValuePair <string, string> > tokenKeyPairs = null, string delimiter = null, int?commandTimeout = null, string environmentCode = null ) { //extract and filter out scripts when environment code is used var sqlScriptFiles = _directoryService.GetAllFiles(workingPath, "*.sql").ToList(); sqlScriptFiles = _directoryService.FilterFiles(workingPath, environmentCode, sqlScriptFiles).ToList(); _traceService.Info($"Found the {sqlScriptFiles.Count} script files on {workingPath}"); _traceService.Info($"{string.Join(@"\r\n\t", sqlScriptFiles.Select(s => new FileInfo(s).Name))}"); //execute all script files in the target folder sqlScriptFiles.Sort(); sqlScriptFiles.ForEach(scriptFile => { //https://stackoverflow.com/questions/25563876/executing-sql-batch-containing-go-statements-in-c-sharp/25564722#25564722 var sqlStatementRaw = _fileService.ReadAllText(scriptFile); var sqlStatements = _dataService.BreakStatements(sqlStatementRaw) .Where(s => !string.IsNullOrWhiteSpace(s)) .ToList(); sqlStatements.ForEach(sqlStatement => { //replace tokens with values from the cli sqlStatement = _tokenReplacementService.Replace(tokenKeyPairs, sqlStatement); _traceService.Debug($"Executing sql statement as part of : {scriptFile}{Environment.NewLine}{sqlStatement}"); _configurationDataService.ExecuteSql( connection: connection, commandText: sqlStatement, transaction: transaction, commandTimeout: commandTimeout, traceService: _traceService); }); _traceService.Info($"Executed script file {scriptFile}."); }); }