def METHOD_NAME(target, source, env):
    # prepare the line separator
    linesep = env['LINESEPARATOR']
    if linesep is None:
        linesep = LINESEP  # os.linesep
    elif is_String(linesep):
        pass
    elif isinstance(linesep, Value):
        linesep = linesep.get_text_contents()
    else:
        raise SCons.Errors.UserError('unexpected type/class for LINESEPARATOR: %s'
                                     % repr(linesep), None)
    if 'b' in TEXTFILE_FILE_WRITE_MODE:
        linesep = to_bytes(linesep)
    # create a dictionary to use for the substitutions
    if 'SUBST_DICT' not in env:
        subs = None    # no substitutions
    else:
        subst_dict = env['SUBST_DICT']
        if is_Dict(subst_dict):
            subst_dict = list(subst_dict.items())
        elif is_Sequence(subst_dict):
            pass
        else:
            raise SCons.Errors.UserError('SUBST_DICT must be dict or sequence')
        subs = []
        for (k, value) in subst_dict:
            if callable(value):
                value = value()
            if is_String(value):
                value = env.subst(value, raw=1)
            else:
                value = str(value)
            subs.append((k, value))
    # Pull file encoding from the environment or default to UTF-8
    file_encoding = env.get('FILE_ENCODING', 'utf-8')
    # write the file
    try:
        target_file = open(target[0].get_path(), TEXTFILE_FILE_WRITE_MODE, newline='', encoding=file_encoding)
    except OSError as e:
        raise SCons.Errors.UserError("Can't write target file %s [%s]" % (target[0],e))
    # separate lines by 'linesep' only if linesep is not empty
    lsep = None
    for line in source:
        if lsep:
            target_file.write(lsep)
        target_file.write(_do_subst(line, subs))
        lsep = linesep
    target_file.close()