Skip to content

Commit

Permalink
fix docker compose pathing
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliran-Turgeman committed Jul 16, 2024
1 parent fd20e6c commit 3d8a646
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 46 deletions.
63 changes: 26 additions & 37 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from textual.app import App, ComposeResult
from textual.containers import Container, Horizontal
from textual.widgets import Button, Footer, MarkdownViewer, Header, TextArea, Select, Label
from textual import log

from exercises_utils import EXERCISES_DIR, ExercisesUtils
from project_generators.base_project_generator import BaseProjectGenerator
Expand Down Expand Up @@ -43,67 +42,57 @@ def compose(self) -> ComposeResult:
def on_mount(self) -> None:
"""Initial setup when the app starts."""
self.show_menu()
menu = self.query_one("#menu")
menu.notify(title="Hello World!", message="Pick an exercise you find interesting, read it by clicking on "
"'View', create a template for a project by clicking on 'Start "
"Project', and once you are done implementing the exercise, "
"you can verify you did everything right by clicking on 'Run "
"Tests'. Pretty Simple. But not easy :)\n\n"
"Also... don't forget to read the README.md file once you click "
"'Start Project'", timeout=30)

def show_menu(self) -> None:
"""Display the menu with available exercises."""
log("entered show menu")
menu = self.query_one("#menu")
menu.remove_children()

exercises = list(EXERCISES_DIR.glob("*.md"))
log(f"exercises = {exercises}")
log(f"map = {self.files_view_names}")
exercise_names = [(self.files_view_names[exercise.stem], exercise.stem) for exercise in exercises]
select_file_widget = Select(options=exercise_names, prompt="Select Exercise", id="exercise_select", classes="menu_widget", tooltip="Select an exercise to preview")

menu.mount(select_file_widget)
menu.mount(Button("View", id="view", variant="primary", classes="menu_widget"))
menu.mount(Button("Run Tests", id="test", variant="success", classes="menu_widget"))

# select_lang_widget = Select(options=[('python', 'python'), ('javascript', 'javascript')], prompt="Select Programming Language", id="lang_select", classes="menu_widget", tooltip="Select a language to create your project's template")
# menu.mount(select_lang_widget)
menu.mount(Button("Start Project", id="start", variant="warning", classes="menu_widget"))
menu.mount(Button("Run Tests", id="test", variant="success", classes="menu_widget"))

async def on_button_pressed(self, event: Button.Pressed) -> None:
"""Handle button press events."""
button_id = event.button.id
select_widget = self.query_one("#exercise_select", Select)
exercise_name = select_widget.value
selected_exercise = EXERCISES_DIR / f"{exercise_name}.md"

# select_lang_widget = self.query_one("#lang_select", Select)
# lang = select_lang_widget.value

if button_id in ('view', 'test'):
if exercise_name == Select.BLANK:
select_widget.notify("Please select a file", severity="error", timeout=5)
return

if button_id == "view":
self.current_markdown_path = selected_exercise
markdown_viewer = self.query_one("#markdown_viewer", MarkdownViewer)
await markdown_viewer.go(selected_exercise)
self.query_one("#content").display = True

elif button_id == "test":
log("about to test")
self.run_tests(exercise_name)

if button_id == 'start':
if exercise_name == Select.BLANK:
select_widget.notify("Please select a file", severity="error", timeout=5)
return
# if lang == Select.BLANK:
# select_lang_widget.notify("Please select a language", severity="error")
# return
if exercise_name == Select.BLANK:
select_widget.notify("Please select a file", severity="error", timeout=5)
return

if button_id == "view":
self.current_markdown_path = selected_exercise
markdown_viewer = self.query_one("#markdown_viewer", MarkdownViewer)
await markdown_viewer.go(selected_exercise)
self.query_one("#content").display = True

elif button_id == "test":
self.run_tests(exercise_name)

elif button_id == 'start':
self.start_project(exercise_name)

def start_project(self, exercise_name: str) -> None:
test_output = self.query_one("#test_output", TextArea)
project_path = BaseProjectGenerator().generate(exercise_name)
test_output.notify(f'Creating project structure for lang project {exercise_name}')
test_output.notify(f'Creating project structure for project {exercise_name}')
ExercisesUtils.open_file_in_explorer(project_path)

def run_tests(self, exercise_name: str) -> None:
"""Run tests for the selected exercise and display the output."""
test_output = self.query_one("#test_output", TextArea)
docker_compose_file_name = ExercisesUtils.get_resource_path(f"exercises_test_suites/docker_compose_{exercise_name}.yml")
command = f"docker-compose -f {docker_compose_file_name} up --build"
Expand Down
23 changes: 14 additions & 9 deletions project_generators/base_project_generator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from exercises_utils import ExercisesUtils


class BaseProjectGenerator:
def generate(self, project_name: str) -> str:
Expand All @@ -8,26 +10,24 @@ def generate(self, project_name: str) -> str:
Returns the path to the Dockerfile.
"""
# Create the project directory
project_dir = os.path.join(os.getcwd(), 'my_solutions', project_name)
project_dir = ExercisesUtils.get_resource_path(f"my_solutions/{project_name}")
# project_dir = os.path.join(os.getcwd(), '', project_name)
src_dir = os.path.join(project_dir, 'src')
os.makedirs(src_dir, exist_ok=True)

# Define a generic Dockerfile content
dockerfile_content = """
dockerfile_content = f"""
# Use an official runtime as a parent image
FROM <runtime_image>
# Set the working directory in the container
WORKDIR /app
# Copy the requirements file into the container
COPY requirements.txt ./
WORKDIR /{project_name}
# Install any dependencies
RUN <install_command>
# Copy the current directory contents into the container at /app
COPY . .
COPY src/ .
# Make port 5000 available to the world outside this container
EXPOSE 5000
Expand All @@ -43,10 +43,15 @@ def generate(self, project_name: str) -> str:

# Create a README file with guidelines
readme_content = f"""
# {project_name}
# {' '.join(project_name.split('_')).title()}
##
## Supported Languages?
## Project Setup
**Every language and every API framework is supported.**
You will be required to correctly write a dockerfile (given the template and the guide below), and the tests are on us.
## Project Setup
1. **Install Dependencies**: Ensure you have Docker installed.
2. **Build the Docker Image**: Run the following command in the project directory:
```
Expand Down

0 comments on commit 3d8a646

Please sign in to comment.