processDefFileText function

String processDefFileText(
  1. String fileText, {
  2. String baseFolderPath = '',
  3. String filePath = '',
  4. String readTextFileFunction(
    1. String, [
    2. String
    ])? = readTextFile,
  5. bool hasImportCommands = true,
  6. String getImportedPathFunction(
    1. String
    )? = getImportedPath,
  7. List<String> getImportedFilePathArrayFunction(
    1. String
    )? = getImportedFilePathArray,
})

Implementation

String processDefFileText(
    String fileText,
    {
        String baseFolderPath = '',
        String filePath = '',
        String Function( String, [String] )? readTextFileFunction = readTextFile,
        bool hasImportCommands = true,
        String Function( String )? getImportedPathFunction = getImportedPath,
        List<String> Function( String )? getImportedFilePathArrayFunction = getImportedFilePathArray
    }
    )
{
    if ( hasImportCommands )
    {
        var folderPath = filePath.substring( 0, filePath.lastIndexOf( '/' ) + 1 );
        var lineArray = fileText.split( '\n' );

        for ( var lineIndex = 0;
              lineIndex < lineArray.length;
              ++lineIndex )
        {
            var line = lineArray[ lineIndex ];
            var trimmedLine = line.trim();
            var importedFileFilter = getImportedPathFunction!( trimmedLine );

            if ( importedFileFilter.isNotEmpty )
            {
                lineArray.removeAt( lineIndex );
                var importedFilePathArray = getImportedFilePathArrayFunction!( folderPath + importedFileFilter );

                for ( var importedFilePath in importedFilePathArray )
                {
                    var importedFileText =
                        readDefFileText(
                            importedFilePath,
                            baseFolderPath: baseFolderPath,
                            readTextFileFunction: readTextFileFunction,
                            hasImportCommands: hasImportCommands,
                            getImportedPathFunction: getImportedPathFunction,
                            getImportedFilePathArrayFunction: getImportedFilePathArrayFunction
                            );

                    var indentation = line.substring( 0, line.length - line.trimLeft().length );
                    var indentedLineArray = importedFileText.split( '\n' );

                    for ( var indentedLineIndex = 0;
                          indentedLineIndex < indentedLineArray.length;
                          ++indentedLineIndex )
                    {
                        indentedLineArray[ indentedLineIndex ] = indentation + indentedLineArray[ indentedLineIndex ];
                    }

                    lineArray.insertAll( lineIndex, indentedLineArray );
                    lineIndex += indentedLineArray.length;
                }

                --lineIndex;
            }
        }

        fileText = lineArray.join( '\n' );
    }

    return fileText;
}