Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Added CR LF to LF Conversion for gradlew #2738

Merged
merged 9 commits into from Mar 26, 2021
8 changes: 8 additions & 0 deletions samcli/lib/init/__init__.py
Expand Up @@ -3,6 +3,7 @@
"""
import itertools
import logging
import platform

from pathlib import Path

Expand All @@ -11,6 +12,7 @@

from samcli.local.common.runtime_template import RUNTIME_DEP_TEMPLATE_MAPPING
from samcli.lib.utils.packagetype import ZIP
from samcli.lib.utils import osutils
from .exceptions import GenerateProjectFailedError, InvalidLocationError
from .arbitrary_project import generate_non_cookiecutter_project

Expand Down Expand Up @@ -90,6 +92,12 @@ def generate_project(
try:
LOG.debug("Baking a new template with cookiecutter with all parameters")
cookiecutter(**params)
# Fixes gradlew line ending issue caused by Windows git
# gradlew is a shell script which should not have CR LF line endings
# Putting the conversion after cookiecutter as cookiecutter processing will also change the line endings
# https://github.com/cookiecutter/cookiecutter/pull/1407
if platform.system().lower() == "windows":
osutils.convert_files_to_unix_line_endings(output_dir, ["gradlew"])
except RepositoryNotFound as e:
# cookiecutter.json is not found in the template. Let's just clone it directly without using cookiecutter
# and call it done.
Expand Down
19 changes: 19 additions & 0 deletions samcli/lib/utils/osutils.py
Expand Up @@ -8,6 +8,7 @@
import sys
import tempfile
from contextlib import contextmanager
from typing import List, Optional

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -154,3 +155,21 @@ def copytree(source, destination, ignore=None):
copytree(new_source, new_destination, ignore=ignore)
else:
shutil.copy2(new_source, new_destination)


def convert_files_to_unix_line_endings(path: str, target_files: Optional[List[str]] = None) -> None:
CoshUS marked this conversation as resolved.
Show resolved Hide resolved
for subdirectory, _, files in os.walk(path):
for file in files:
if target_files is not None and file not in target_files:
continue

file_path = os.path.join(subdirectory, file)
convert_to_unix_line_ending(file_path)


def convert_to_unix_line_ending(file_path: str) -> None:
with open(file_path, "rb") as file:
content = file.read()
content = content.replace(b"\r\n", b"\n")
with open(file_path, "wb") as file:
file.write(content)