refractor generate_file as a common service