From c0d05637f2f8cb697141146d0bcb7b8fcdbe020f Mon Sep 17 00:00:00 2001 From: loiswells97 Date: Wed, 9 Feb 2022 10:55:50 +0000 Subject: [PATCH 01/17] Create draft PR for #8 From c8ea798cc0c020fd28efe19275b54bbe952c8571 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Wed, 9 Feb 2022 10:56:55 +0000 Subject: [PATCH 02/17] Changed title and minor code update --- lib/tasks/projects.rake | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/tasks/projects.rake b/lib/tasks/projects.rake index ae00f9e8..10866f17 100644 --- a/lib/tasks/projects.rake +++ b/lib/tasks/projects.rake @@ -3,7 +3,7 @@ namespace :projects do task create_starter: :environment do Project.find_by(identifier: "python-hello-starter")&.destroy - project = Project.new(identifier: "python-hello-starter", name: "Hello World!") + project = Project.new(identifier: "python-hello-starter", name: "Hello 🌍🌎🌏") project.components << Component.new(name: "main", extension: "py", content: main_content) project.components << Component.new(name: "emoji", extension: "py", content: emoji_content) project.components << Component.new(name: "noemoji", extension: "py", content: no_emoji_content) @@ -32,7 +32,6 @@ from random import randint # Put code to run under here -print('Hello ', world) END end From ee2909fa8543673b04aa33f5b4722012732df2a2 Mon Sep 17 00:00:00 2001 From: loiswells97 Date: Wed, 9 Feb 2022 12:02:32 +0000 Subject: [PATCH 03/17] Create draft PR for #10 From e59879c860be58d13153dfd93c467d8e32ebbc86 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Wed, 9 Feb 2022 12:20:40 +0000 Subject: [PATCH 04/17] Added archery project to rake task --- lib/tasks/projects.rake | 109 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/lib/tasks/projects.rake b/lib/tasks/projects.rake index 10866f17..ba54c459 100644 --- a/lib/tasks/projects.rake +++ b/lib/tasks/projects.rake @@ -15,6 +15,16 @@ namespace :projects do project.components << Component.new(name: "emoji", extension: "py", content: emoji_content) project.components << Component.new(name: "noemoji", extension: "py", content: no_emoji_content) project.save + + Project.find_by(identifier: "python-archery-example")&.destroy + project = Project.new(identifier: "python-archery-example", name: "Target Practice Example") + project.components << Component.new(name: "main", extension: "py", content: archery_content) + project.save + + Project.find_by(identifier: "python-archery-starter")&.destroy + project = Project.new(identifier: "python-archery-starter", name: "Target Practice") + project.components << Component.new(name: "main", extension: "py", content: archery_starter) + project.save end end @@ -142,3 +152,102 @@ hobbies() # Call the hobbies function END end +def archery_content + archery_content = <<-END +#!/bin/python3 + +# Import library code +from p5 import * +from math import * +from random import randint + +# The mouse_pressed function goes here +def mouse_pressed(): + if hit_color == outer: + print('You hit the outer circle, 50 points!') #Like functions, 'if' statements are indented + elif hit_color == inner: + print('You hit the inner circle, 200 points!') + elif hit_color == bullseye: + print('You hit the bullseye, 500 points!') + else: + print('You missed! No points!') + +# The shoot_arrow function goes here +def shoot_arrow(): + global hit_color + arrow_x = randint(100, 300) + arrow_y = randint(100, 300) + hit_color = get(arrow_x, arrow_y) + ellipse(arrow_x, arrow_y, 15, 15) + +def setup(): +# Setup your game here + size(400, 400) # width and height + frame_rate(2) + + +def draw(): +# Things to do in every frame + global outer, inner, bullseye + sky = color(92, 204, 206) # Red = 92, Green = 204, Blue = 206 + grass = color(149, 212, 122) + wood = color(145, 96, 51) + outer = color(0, 120, 180) + inner = color(210, 60, 60) + bullseye = color(220, 200, 0) + + no_stroke() + fill(sky) + rect(0, 0, 400, 250) + fill(grass) + rect(0, 250, 400, 150) + + fill(wood) + triangle(150, 350, 200, 150, 250, 350) + fill(outer) + ellipse(200, 200, 170, 170) + fill(inner) + ellipse(200, 200, 110, 110) #Inner circle + fill(bullseye) + ellipse(200, 200, 30, 30) #Bullseye + + fill(wood) + shoot_arrow() +# Keep this to run your code +run() + + END +end + +def archery_starter + archery_starter = <<-END +#!/bin/python3 + +# Import library code +from p5 import * +from math import * +from random import randint + +# The mouse_pressed function goes here + +# The shoot_arrow function goes here + +def setup(): +# Setup your game here + size(400, 400) # width and height + frame_rate(2) + + +def draw(): +# Things to do in every frame + sky = color(92, 204, 206) # Red = 92, Green = 204, Blue = 206 + grass = color(149, 212, 122) + wood = color(145, 96, 51) + outer = color(0, 120, 180) + + +# Keep this to run your code +run() + + END +end From 2498e4c3271e8b0c409fc889998fafe862eaba00 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Thu, 10 Feb 2022 15:37:32 +0000 Subject: [PATCH 05/17] Updated rake task to loop over projects --- .../project_components/archery_example.py | 61 ++++ .../project_components/archery_starter.py | 27 ++ lib/tasks/project_components/emoji.py | 27 ++ lib/tasks/project_components/hello_example.py | 45 +++ lib/tasks/project_components/hello_starter.py | 12 + lib/tasks/project_components/noemoji.py | 19 ++ lib/tasks/projects.rake | 288 +++--------------- 7 files changed, 232 insertions(+), 247 deletions(-) create mode 100644 lib/tasks/project_components/archery_example.py create mode 100644 lib/tasks/project_components/archery_starter.py create mode 100644 lib/tasks/project_components/emoji.py create mode 100644 lib/tasks/project_components/hello_example.py create mode 100644 lib/tasks/project_components/hello_starter.py create mode 100644 lib/tasks/project_components/noemoji.py diff --git a/lib/tasks/project_components/archery_example.py b/lib/tasks/project_components/archery_example.py new file mode 100644 index 00000000..8d0da567 --- /dev/null +++ b/lib/tasks/project_components/archery_example.py @@ -0,0 +1,61 @@ +#!/bin/python3 + +# Import library code +from p5 import * +from math import * +from random import randint + +# The mouse_pressed function goes here +def mouse_pressed(): + if hit_color == outer: + print('You hit the outer circle, 50 points!') #Like functions, 'if' statements are indented + elif hit_color == inner: + print('You hit the inner circle, 200 points!') + elif hit_color == bullseye: + print('You hit the bullseye, 500 points!') + else: + print('You missed! No points!') + +# The shoot_arrow function goes here +def shoot_arrow(): + global hit_color + arrow_x = randint(100, 300) + arrow_y = randint(100, 300) + hit_color = get(arrow_x, arrow_y) + ellipse(arrow_x, arrow_y, 15, 15) + +def setup(): +# Setup your game here + size(400, 400) # width and height + frame_rate(2) + + +def draw(): +# Things to do in every frame + global outer, inner, bullseye + sky = color(92, 204, 206) # Red = 92, Green = 204, Blue = 206 + grass = color(149, 212, 122) + wood = color(145, 96, 51) + outer = color(0, 120, 180) + inner = color(210, 60, 60) + bullseye = color(220, 200, 0) + + no_stroke() + fill(sky) + rect(0, 0, 400, 250) + fill(grass) + rect(0, 250, 400, 150) + + fill(wood) + triangle(150, 350, 200, 150, 250, 350) + fill(outer) + ellipse(200, 200, 170, 170) + fill(inner) + ellipse(200, 200, 110, 110) #Inner circle + fill(bullseye) + ellipse(200, 200, 30, 30) #Bullseye + + fill(wood) + shoot_arrow() +# Keep this to run your code +run() diff --git a/lib/tasks/project_components/archery_starter.py b/lib/tasks/project_components/archery_starter.py new file mode 100644 index 00000000..a90ab85c --- /dev/null +++ b/lib/tasks/project_components/archery_starter.py @@ -0,0 +1,27 @@ +#!/bin/python3 + +# Import library code +from p5 import * +from math import * +from random import randint + +# The mouse_pressed function goes here + +# The shoot_arrow function goes here + +def setup(): +# Setup your game here + size(400, 400) # width and height + frame_rate(2) + + +def draw(): +# Things to do in every frame + sky = color(92, 204, 206) # Red = 92, Green = 204, Blue = 206 + grass = color(149, 212, 122) + wood = color(145, 96, 51) + outer = color(0, 120, 180) + + +# Keep this to run your code +run() diff --git a/lib/tasks/project_components/emoji.py b/lib/tasks/project_components/emoji.py new file mode 100644 index 00000000..13dae5d0 --- /dev/null +++ b/lib/tasks/project_components/emoji.py @@ -0,0 +1,27 @@ +# Emoji variables to use in your project + +world = '🌍🌎🌏' +python = '🐍⌨️' +sums = '✖️➗➖➕' +calendar = '📅' +clock = '🕒' +projects = '🎨🎮🔬' +fun = '🎉🕶️' +dice = '🎲' +unicorn = '🦄' +space = '🚀' +happy = '😃' +silly = '😜' +heart = '❤️' +games = '🎮' +books = '📚' +sports = '⚽🎾👟' +green = '♻️' +nature = '🌳' +fire = '🔥' +sparkles = '✨' +plead = '🥺' +hundred = '💯' +star = '⭐' +yellow_heart = '💛' +rainbow = '🌈' \ No newline at end of file diff --git a/lib/tasks/project_components/hello_example.py b/lib/tasks/project_components/hello_example.py new file mode 100644 index 00000000..f89e0563 --- /dev/null +++ b/lib/tasks/project_components/hello_example.py @@ -0,0 +1,45 @@ +#!/bin/python3 + +from emoji import * +from datetime import * +from random import randint + +# Put function definitions under here + +def roll_dice(): + print(python, 'can make a', dice) + max = input('How many sides?') # get input from the user + print('That is a D', max) # use the number the user entered + roll = randint(1, int(max)) # generate a random number + print('You rolled a', roll) # print the value of the roll variable + print(fire * roll) # repeat the fire text roll times + +def hobbies(): + hobby = input('What do you like?') + print('That sounds', fun) + print('You could make a', python, 'project about', hobby) + +# Put code to run under here +# Useful characters :',()*_/.# + +print('Hello', world) +print('Welcome to', python) + +input() # wait for the user to tap Enter + +print(python, 'is very good at', sums) +print(230 * 5782 ** 2 / 23781) + +input() + +now = datetime.now() # get the current date and time +print('The', calendar, clock, 'is', now) # print with emoji + +input() + +roll_dice() # Call the roll_dice function + +input() + +hobbies() # Call the hobbies function + diff --git a/lib/tasks/project_components/hello_starter.py b/lib/tasks/project_components/hello_starter.py new file mode 100644 index 00000000..6d157065 --- /dev/null +++ b/lib/tasks/project_components/hello_starter.py @@ -0,0 +1,12 @@ +#!/bin/python3 + +from emoji import * +from datetime import * +from random import randint + +# Put function definitions under here + +# Useful characters :',()*_/.# + +# Put code to run under here + diff --git a/lib/tasks/project_components/noemoji.py b/lib/tasks/project_components/noemoji.py new file mode 100644 index 00000000..67b400e0 --- /dev/null +++ b/lib/tasks/project_components/noemoji.py @@ -0,0 +1,19 @@ +# Add ASCII art alternatives +# Before emoji we used emoticons made from characters + +world = 'o' +python = '~~~-<' +happy = ':-)' +heart = '♡' # or '<3' +star = '☆' +sparkles = '✺' +silly = ';)' +sums = '+-*/' +hundred = '100!' +plead = '◔◔' +fire = '/\\' +books = '≣' +rainbow = '⌒' +dice = '⊡' +clock = '◷' +calendar = '▦' diff --git a/lib/tasks/projects.rake b/lib/tasks/projects.rake index ba54c459..979fada3 100644 --- a/lib/tasks/projects.rake +++ b/lib/tasks/projects.rake @@ -2,252 +2,46 @@ namespace :projects do desc "Import starter projects" task create_starter: :environment do - Project.find_by(identifier: "python-hello-starter")&.destroy - project = Project.new(identifier: "python-hello-starter", name: "Hello 🌍🌎🌏") - project.components << Component.new(name: "main", extension: "py", content: main_content) - project.components << Component.new(name: "emoji", extension: "py", content: emoji_content) - project.components << Component.new(name: "noemoji", extension: "py", content: no_emoji_content) - project.save - - Project.find_by(identifier: "python-emoji-example")&.destroy - project = Project.new(identifier: "python-emoji-example", name: "Emoji Example Project") - project.components << Component.new(name: "main", extension: "py", content: main_runner_content) - project.components << Component.new(name: "emoji", extension: "py", content: emoji_content) - project.components << Component.new(name: "noemoji", extension: "py", content: no_emoji_content) - project.save - - Project.find_by(identifier: "python-archery-example")&.destroy - project = Project.new(identifier: "python-archery-example", name: "Target Practice Example") - project.components << Component.new(name: "main", extension: "py", content: archery_content) - project.save - - Project.find_by(identifier: "python-archery-starter")&.destroy - project = Project.new(identifier: "python-archery-starter", name: "Target Practice") - project.components << Component.new(name: "main", extension: "py", content: archery_starter) - project.save + projects = [ + { + identifier: "python-hello-starter", + name: "Hello 🌍🌎🌏", + components: ["hello_starter.py", "emoji.py", "noemoji.py"], + main_component: "hello_starter.py" + }, + { + identifier: "python-hello-example", + name: "Hello 🌍🌎🌏 Example", + components: ["hello_example.py", "emoji.py", "noemoji.py"], + main_component: "hello_example.py" + }, + { + identifier: "python-archery-starter", + name: "Target Practice", + components: ["archery_starter.py"], + main_component: "archery_starter.py" + }, + { + identifier: "python-archery-example", + name: "Target Practice Example", + components: ["archery_example.py"], + main_component: "archery_example.py" + } + ] + + projects.each do |project| + Project.find_by( identifier: project[:identifier] )&.destroy + new_project = Project.new(identifier: project[:identifier], name: project[:name]) + project[:components].each do |component| + file = File.open(File.dirname(__FILE__) + "/project_components/#{component}") + component_code = file.read + file.close + component_name = component.split('.')[0] + component_name = (component == project[:main_component]) ? "main" : component_name + component_extension = component.split('.').drop(1).join('.') + new_project.components << Component.new( name: component_name, extension: component_extension, content: component_code ) + end + new_project.save + end end end - -def main_content - main_content = <<-END -#!/bin/python3 - -from emoji import * -from datetime import * -from random import randint - -# Put function definitions under here - -# Useful characters :',()*_/.# - -# Put code to run under here - - END -end - -def emoji_content - emoji_content = <<-END -# Emoji variables to use in your project - -world = '🌍🌎🌏' -python = '🐍⌨️' -sums = '✖️➗➖➕' -calendar = '📅' -clock = '🕒' -projects = '🎨🎮🔬' -fun = '🎉🕶️' -dice = '🎲' -unicorn = '🦄' -space = '🚀' -happy = '😃' -silly = '😜' -heart = '❤️' -games = '🎮' -books = '📚' -sports = '⚽🎾👟' -green = '♻️' -nature = '🌳' -fire = '🔥' -sparkles = '✨' -plead = '🥺' -hundred = '💯' -star = '⭐' -yellow_heart = '💛' -rainbow = '🌈' - END -end - -def no_emoji_content - no_emoji_content = <<-END -# Add ASCII art alternatives -# Before emoji we used emoticons made from characters - -world = 'o' -python = '~~~-<' -happy = ':-)' -heart = '♡' # or '<3' -star = '☆' -sparkles = '✺' -silly = ';)' -sums = '+-*/' -hundred = '100!' -plead = '◔◔' -fire = '/\\' -books = '≣' -rainbow = '⌒' -dice = '⊡' -clock = '◷' -calendar = '▦' - - END -end - -def main_runner_content - - main_runner_content = <<-END -#!/bin/python3 - -from emoji import * -from datetime import * -from random import randint - -# Put function definitions under here - -def roll_dice(): - print(python, 'can make a', dice) - max = input('How many sides?') # get input from the user - print('That is a D', max) # use the number the user entered - roll = randint(1, int(max)) # generate a random number - print('You rolled a', roll) # print the value of the roll variable - print(fire * roll) # repeat the fire text roll times - -def hobbies(): - hobby = input('What do you like?') - print('That sounds', fun) - print('You could make a', python, 'project about', hobby) - -# Put code to run under here -# Useful characters :',()*_/.# - -print('Hello', world) -print('Welcome to', python) - -input() # wait for the user to tap Enter - -print(python, 'is very good at', sums) -print(230 * 5782 ** 2 / 23781) - -input() - -now = datetime.now() # get the current date and time -print('The', calendar, clock, 'is', now) # print with emoji - -input() - -roll_dice() # Call the roll_dice function - -input() - -hobbies() # Call the hobbies function -END -end - -def archery_content - archery_content = <<-END -#!/bin/python3 - -# Import library code -from p5 import * -from math import * -from random import randint - -# The mouse_pressed function goes here -def mouse_pressed(): - if hit_color == outer: - print('You hit the outer circle, 50 points!') #Like functions, 'if' statements are indented - elif hit_color == inner: - print('You hit the inner circle, 200 points!') - elif hit_color == bullseye: - print('You hit the bullseye, 500 points!') - else: - print('You missed! No points!') - -# The shoot_arrow function goes here -def shoot_arrow(): - global hit_color - arrow_x = randint(100, 300) - arrow_y = randint(100, 300) - hit_color = get(arrow_x, arrow_y) - ellipse(arrow_x, arrow_y, 15, 15) - -def setup(): -# Setup your game here - size(400, 400) # width and height - frame_rate(2) - - -def draw(): -# Things to do in every frame - global outer, inner, bullseye - sky = color(92, 204, 206) # Red = 92, Green = 204, Blue = 206 - grass = color(149, 212, 122) - wood = color(145, 96, 51) - outer = color(0, 120, 180) - inner = color(210, 60, 60) - bullseye = color(220, 200, 0) - - no_stroke() - fill(sky) - rect(0, 0, 400, 250) - fill(grass) - rect(0, 250, 400, 150) - - fill(wood) - triangle(150, 350, 200, 150, 250, 350) - fill(outer) - ellipse(200, 200, 170, 170) - fill(inner) - ellipse(200, 200, 110, 110) #Inner circle - fill(bullseye) - ellipse(200, 200, 30, 30) #Bullseye - - fill(wood) - shoot_arrow() -# Keep this to run your code -run() - - END -end - -def archery_starter - archery_starter = <<-END -#!/bin/python3 - -# Import library code -from p5 import * -from math import * -from random import randint - -# The mouse_pressed function goes here - -# The shoot_arrow function goes here - -def setup(): -# Setup your game here - size(400, 400) # width and height - frame_rate(2) - - -def draw(): -# Things to do in every frame - sky = color(92, 204, 206) # Red = 92, Green = 204, Blue = 206 - grass = color(149, 212, 122) - wood = color(145, 96, 51) - outer = color(0, 120, 180) - - -# Keep this to run your code -run() - - END -end From 6a116bf26cbd2b74adc3fcb8992bcedef9e676b2 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 11 Feb 2022 09:51:32 +0000 Subject: [PATCH 06/17] Making rake task iterate over file structure --- .../{ => hello_world_example}/emoji.py | 0 .../main.py} | 2 +- .../{ => hello_world_example}/noemoji.py | 0 .../hello_world_example/project_config.yml | 2 + .../hello_world_starter/emoji.py | 27 +++++++++ .../main.py} | 0 .../hello_world_starter/noemoji.py | 19 +++++++ .../hello_world_starter/project_config.yml | 2 + .../main.py} | 0 .../project_config.yml | 2 + .../main.py} | 0 .../project_config.yml | 2 + lib/tasks/projects.rake | 56 ++++++------------- 13 files changed, 72 insertions(+), 40 deletions(-) rename lib/tasks/project_components/{ => hello_world_example}/emoji.py (100%) rename lib/tasks/project_components/{hello_example.py => hello_world_example/main.py} (97%) rename lib/tasks/project_components/{ => hello_world_example}/noemoji.py (100%) create mode 100644 lib/tasks/project_components/hello_world_example/project_config.yml create mode 100644 lib/tasks/project_components/hello_world_starter/emoji.py rename lib/tasks/project_components/{hello_starter.py => hello_world_starter/main.py} (100%) create mode 100644 lib/tasks/project_components/hello_world_starter/noemoji.py create mode 100644 lib/tasks/project_components/hello_world_starter/project_config.yml rename lib/tasks/project_components/{archery_example.py => target_practice_example/main.py} (100%) create mode 100644 lib/tasks/project_components/target_practice_example/project_config.yml rename lib/tasks/project_components/{archery_starter.py => target_practice_starter/main.py} (100%) create mode 100644 lib/tasks/project_components/target_practice_starter/project_config.yml diff --git a/lib/tasks/project_components/emoji.py b/lib/tasks/project_components/hello_world_example/emoji.py similarity index 100% rename from lib/tasks/project_components/emoji.py rename to lib/tasks/project_components/hello_world_example/emoji.py diff --git a/lib/tasks/project_components/hello_example.py b/lib/tasks/project_components/hello_world_example/main.py similarity index 97% rename from lib/tasks/project_components/hello_example.py rename to lib/tasks/project_components/hello_world_example/main.py index f89e0563..7d9ab51a 100644 --- a/lib/tasks/project_components/hello_example.py +++ b/lib/tasks/project_components/hello_world_example/main.py @@ -1,6 +1,6 @@ #!/bin/python3 -from emoji import * +from noemoji import * from datetime import * from random import randint diff --git a/lib/tasks/project_components/noemoji.py b/lib/tasks/project_components/hello_world_example/noemoji.py similarity index 100% rename from lib/tasks/project_components/noemoji.py rename to lib/tasks/project_components/hello_world_example/noemoji.py diff --git a/lib/tasks/project_components/hello_world_example/project_config.yml b/lib/tasks/project_components/hello_world_example/project_config.yml new file mode 100644 index 00000000..5a061e0b --- /dev/null +++ b/lib/tasks/project_components/hello_world_example/project_config.yml @@ -0,0 +1,2 @@ +NAME: "Hello 🌍🌎🌏 Example" +IDENTIFIER: "python-emoji-example" \ No newline at end of file diff --git a/lib/tasks/project_components/hello_world_starter/emoji.py b/lib/tasks/project_components/hello_world_starter/emoji.py new file mode 100644 index 00000000..13dae5d0 --- /dev/null +++ b/lib/tasks/project_components/hello_world_starter/emoji.py @@ -0,0 +1,27 @@ +# Emoji variables to use in your project + +world = '🌍🌎🌏' +python = '🐍⌨️' +sums = '✖️➗➖➕' +calendar = '📅' +clock = '🕒' +projects = '🎨🎮🔬' +fun = '🎉🕶️' +dice = '🎲' +unicorn = '🦄' +space = '🚀' +happy = '😃' +silly = '😜' +heart = '❤️' +games = '🎮' +books = '📚' +sports = '⚽🎾👟' +green = '♻️' +nature = '🌳' +fire = '🔥' +sparkles = '✨' +plead = '🥺' +hundred = '💯' +star = '⭐' +yellow_heart = '💛' +rainbow = '🌈' \ No newline at end of file diff --git a/lib/tasks/project_components/hello_starter.py b/lib/tasks/project_components/hello_world_starter/main.py similarity index 100% rename from lib/tasks/project_components/hello_starter.py rename to lib/tasks/project_components/hello_world_starter/main.py diff --git a/lib/tasks/project_components/hello_world_starter/noemoji.py b/lib/tasks/project_components/hello_world_starter/noemoji.py new file mode 100644 index 00000000..67b400e0 --- /dev/null +++ b/lib/tasks/project_components/hello_world_starter/noemoji.py @@ -0,0 +1,19 @@ +# Add ASCII art alternatives +# Before emoji we used emoticons made from characters + +world = 'o' +python = '~~~-<' +happy = ':-)' +heart = '♡' # or '<3' +star = '☆' +sparkles = '✺' +silly = ';)' +sums = '+-*/' +hundred = '100!' +plead = '◔◔' +fire = '/\\' +books = '≣' +rainbow = '⌒' +dice = '⊡' +clock = '◷' +calendar = '▦' diff --git a/lib/tasks/project_components/hello_world_starter/project_config.yml b/lib/tasks/project_components/hello_world_starter/project_config.yml new file mode 100644 index 00000000..04c44adc --- /dev/null +++ b/lib/tasks/project_components/hello_world_starter/project_config.yml @@ -0,0 +1,2 @@ +NAME: "Hello 🌍🌎🌏" +IDENTIFIER: "python-hello-starter" \ No newline at end of file diff --git a/lib/tasks/project_components/archery_example.py b/lib/tasks/project_components/target_practice_example/main.py similarity index 100% rename from lib/tasks/project_components/archery_example.py rename to lib/tasks/project_components/target_practice_example/main.py diff --git a/lib/tasks/project_components/target_practice_example/project_config.yml b/lib/tasks/project_components/target_practice_example/project_config.yml new file mode 100644 index 00000000..df54a632 --- /dev/null +++ b/lib/tasks/project_components/target_practice_example/project_config.yml @@ -0,0 +1,2 @@ +NAME: "Target Practice Example" +IDENTIFIER: "python-archery-example" \ No newline at end of file diff --git a/lib/tasks/project_components/archery_starter.py b/lib/tasks/project_components/target_practice_starter/main.py similarity index 100% rename from lib/tasks/project_components/archery_starter.py rename to lib/tasks/project_components/target_practice_starter/main.py diff --git a/lib/tasks/project_components/target_practice_starter/project_config.yml b/lib/tasks/project_components/target_practice_starter/project_config.yml new file mode 100644 index 00000000..6698db91 --- /dev/null +++ b/lib/tasks/project_components/target_practice_starter/project_config.yml @@ -0,0 +1,2 @@ +NAME: "Target Practice" +IDENTIFIER: "python-archery-starter" \ No newline at end of file diff --git a/lib/tasks/projects.rake b/lib/tasks/projects.rake index 979fada3..7567d6e4 100644 --- a/lib/tasks/projects.rake +++ b/lib/tasks/projects.rake @@ -1,45 +1,23 @@ +require 'yaml' + namespace :projects do desc "Import starter projects" task create_starter: :environment do - - projects = [ - { - identifier: "python-hello-starter", - name: "Hello 🌍🌎🌏", - components: ["hello_starter.py", "emoji.py", "noemoji.py"], - main_component: "hello_starter.py" - }, - { - identifier: "python-hello-example", - name: "Hello 🌍🌎🌏 Example", - components: ["hello_example.py", "emoji.py", "noemoji.py"], - main_component: "hello_example.py" - }, - { - identifier: "python-archery-starter", - name: "Target Practice", - components: ["archery_starter.py"], - main_component: "archery_starter.py" - }, - { - identifier: "python-archery-example", - name: "Target Practice Example", - components: ["archery_example.py"], - main_component: "archery_example.py" - } - ] - - projects.each do |project| - Project.find_by( identifier: project[:identifier] )&.destroy - new_project = Project.new(identifier: project[:identifier], name: project[:name]) - project[:components].each do |component| - file = File.open(File.dirname(__FILE__) + "/project_components/#{component}") - component_code = file.read - file.close - component_name = component.split('.')[0] - component_name = (component == project[:main_component]) ? "main" : component_name - component_extension = component.split('.').drop(1).join('.') - new_project.components << Component.new( name: component_name, extension: component_extension, content: component_code ) + + Dir.each_child("#{File.dirname(__FILE__)}/project_components") do |dir_name| + config = YAML.load(File.open("#{File.dirname(__FILE__)}/project_components/#{dir_name}/project_config.yml").read) + Project.find_by( identifier: config["IDENTIFIER"] )&.destroy + new_project = Project.new(identifier: config["IDENTIFIER"], name: config["NAME"]) + + Dir.each_child("#{File.dirname(__FILE__)}/project_components/#{dir_name}") do |component| + if component != "project_config.yml" + file = File.open(File.dirname(__FILE__) + "/project_components/#{dir_name}/#{component}") + component_code = file.read + file.close + component_name = component.split('.')[0] + component_extension = component.split('.').drop(1).join('.') + new_component = Component.new( name: component_name, extension: component_extension, content: component_code ) + end end new_project.save end From 05ed026829e0308da48773a14f8adc0a89591977 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 11 Feb 2022 10:48:05 +0000 Subject: [PATCH 07/17] Added index to the components --- .../20220211102958_add_index_field_to_project_components.rb | 5 +++++ db/schema.rb | 3 ++- lib/tasks/projects.rake | 4 +++- 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20220211102958_add_index_field_to_project_components.rb diff --git a/db/migrate/20220211102958_add_index_field_to_project_components.rb b/db/migrate/20220211102958_add_index_field_to_project_components.rb new file mode 100644 index 00000000..77ae188a --- /dev/null +++ b/db/migrate/20220211102958_add_index_field_to_project_components.rb @@ -0,0 +1,5 @@ +class AddIndexFieldToProjectComponents < ActiveRecord::Migration[7.0] + def change + add_column :components, :index, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 43e38b3f..c8e28b00 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_01_11_141057) do +ActiveRecord::Schema.define(version: 2022_02_11_102958) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -23,6 +23,7 @@ t.text "content", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.integer "index" t.index ["project_id"], name: "index_components_on_project_id" end diff --git a/lib/tasks/projects.rake b/lib/tasks/projects.rake index 7567d6e4..b9d01def 100644 --- a/lib/tasks/projects.rake +++ b/lib/tasks/projects.rake @@ -16,7 +16,9 @@ namespace :projects do file.close component_name = component.split('.')[0] component_extension = component.split('.').drop(1).join('.') - new_component = Component.new( name: component_name, extension: component_extension, content: component_code ) + component_index = (component == "main.py" ? 1 : new_project.components.length+1) + new_component = Component.new( name: component_name, extension: component_extension, content: component_code, index: component_index ) + new_project.components << new_component end end new_project.save From 9b4ef9210ea2dffc6fc3f3c96e6de520372d7439 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 11 Feb 2022 12:06:24 +0000 Subject: [PATCH 08/17] Fixed ake task --- lib/tasks/projects.rake | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/tasks/projects.rake b/lib/tasks/projects.rake index b9d01def..b22ac470 100644 --- a/lib/tasks/projects.rake +++ b/lib/tasks/projects.rake @@ -8,7 +8,8 @@ namespace :projects do config = YAML.load(File.open("#{File.dirname(__FILE__)}/project_components/#{dir_name}/project_config.yml").read) Project.find_by( identifier: config["IDENTIFIER"] )&.destroy new_project = Project.new(identifier: config["IDENTIFIER"], name: config["NAME"]) - + num_extra_components = 0 + Dir.each_child("#{File.dirname(__FILE__)}/project_components/#{dir_name}") do |component| if component != "project_config.yml" file = File.open(File.dirname(__FILE__) + "/project_components/#{dir_name}/#{component}") @@ -16,7 +17,12 @@ namespace :projects do file.close component_name = component.split('.')[0] component_extension = component.split('.').drop(1).join('.') - component_index = (component == "main.py" ? 1 : new_project.components.length+1) + if component == "main.py" + component_index = 0 + else + num_extra_components += 1 + component_index = num_extra_components + end new_component = Component.new( name: component_name, extension: component_extension, content: component_code, index: component_index ) new_project.components << new_component end From df6368651b2cc302d70f877f3d9b291d16a4cd89 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 11 Feb 2022 12:06:56 +0000 Subject: [PATCH 09/17] Adding pry byebug --- .pryrc | 7 +++++++ Gemfile | 1 + Gemfile.lock | 9 +++++++++ 3 files changed, 17 insertions(+) create mode 100644 .pryrc diff --git a/.pryrc b/.pryrc new file mode 100644 index 00000000..0d5e73f2 --- /dev/null +++ b/.pryrc @@ -0,0 +1,7 @@ +if defined?(PryByebug) + Pry.config.pager = false + Pry.commands.alias_command 'c', 'continue' + Pry.commands.alias_command 's', 'step' + Pry.commands.alias_command 'n', 'next' + Pry.commands.alias_command 'f', 'finish' +end \ No newline at end of file diff --git a/Gemfile b/Gemfile index d7b391af..2ab63bd0 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,7 @@ group :development, :test do gem 'dotenv-rails' gem 'factory_bot_rails' gem 'faker' + gem 'pry-byebug' gem 'rspec' gem 'rspec_junit_formatter' gem 'rspec-rails' diff --git a/Gemfile.lock b/Gemfile.lock index ca7e1b59..65f3f321 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -64,6 +64,8 @@ GEM bootsnap (1.9.3) msgpack (~> 1.0) builder (3.2.4) + byebug (11.1.3) + coderay (1.1.3) concurrent-ruby (1.1.9) crass (1.0.6) diff-lcs (1.4.4) @@ -112,6 +114,12 @@ GEM parser (3.0.3.2) ast (~> 2.4.1) pg (1.2.3) + pry (0.13.1) + coderay (~> 1.1) + method_source (~> 1.0) + pry-byebug (3.9.0) + byebug (~> 11.0) + pry (~> 0.13.0) puma (5.5.2) nio4r (~> 2.0) racc (1.6.0) @@ -227,6 +235,7 @@ DEPENDENCIES importmap-rails jbuilder pg (~> 1.1) + pry-byebug puma (~> 5.0) rack-cors rails (~> 7.0.0) From 9ca3c66aa281849a825c8344ff7817e2ce1fcbfc Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 11 Feb 2022 12:20:11 +0000 Subject: [PATCH 10/17] Ordered the components returned by the api --- app/controllers/api/projects/phrases_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/projects/phrases_controller.rb b/app/controllers/api/projects/phrases_controller.rb index 6e1e245f..5885c4a0 100644 --- a/app/controllers/api/projects/phrases_controller.rb +++ b/app/controllers/api/projects/phrases_controller.rb @@ -6,7 +6,8 @@ class PhrasesController < ApiController require 'phrase_identifier' def show - @project = Project.find_by!(identifier: params[:id]) + projects = Project.all.includes(:components).order('components.index') + @project = projects.find_by!(identifier: params[:id]) render '/api/projects/show', formats: [:json] end From e499870a580e0a00d87685bbf431635a57b6821c Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 11 Feb 2022 12:29:53 +0000 Subject: [PATCH 11/17] Rubocop fixes --- lib/tasks/projects.rake | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/tasks/projects.rake b/lib/tasks/projects.rake index 7f4e2373..5d538655 100644 --- a/lib/tasks/projects.rake +++ b/lib/tasks/projects.rake @@ -4,28 +4,28 @@ require 'yaml' namespace :projects do desc 'Import starter projects' task create_starter: :environment do - + Dir.each_child("#{File.dirname(__FILE__)}/project_components") do |dir_name| - config = YAML.load(File.open("#{File.dirname(__FILE__)}/project_components/#{dir_name}/project_config.yml").read) - Project.find_by( identifier: config["IDENTIFIER"] )&.destroy - new_project = Project.new(identifier: config["IDENTIFIER"], name: config["NAME"]) + config = YAML.safe_load(File.open("#{File.dirname(__FILE__)}/project_components/#{dir_name}/project_config.yml").read) + Project.find_by(identifier: config['IDENTIFIER'])&.destroy + new_project = Project.new(identifier: config['IDENTIFIER'], name: config['NAME']) num_extra_components = 0 Dir.each_child("#{File.dirname(__FILE__)}/project_components/#{dir_name}") do |component| - if component != "project_config.yml" + if component != 'project_config.yml' file = File.open(File.dirname(__FILE__) + "/project_components/#{dir_name}/#{component}") - component_code = file.read + code = file.read file.close - component_name = component.split('.')[0] - component_extension = component.split('.').drop(1).join('.') - if component == "main.py" - component_index = 0 + name = component.split('.')[0] + extension = component.split('.').drop(1).join('.') + if component == 'main.py' + index = 0 else num_extra_components += 1 - component_index = num_extra_components + index = num_extra_components end - new_component = Component.new( name: component_name, extension: component_extension, content: component_code, index: component_index ) - new_project.components << new_component + new_component = Component.new(name: name, extension: extension, content: code, index: index) + new_project.components << new_component end end new_project.save From 7938c6b13252fa849c244a45b4b0e7ee40ccc75b Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 11 Feb 2022 12:37:50 +0000 Subject: [PATCH 12/17] More rubocop fixes --- .pryrc | 4 +++- lib/tasks/projects.rake | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.pryrc b/.pryrc index 0d5e73f2..c150ac88 100644 --- a/.pryrc +++ b/.pryrc @@ -1,7 +1,9 @@ +# frozen_string_literal: true + if defined?(PryByebug) Pry.config.pager = false Pry.commands.alias_command 'c', 'continue' Pry.commands.alias_command 's', 'step' Pry.commands.alias_command 'n', 'next' Pry.commands.alias_command 'f', 'finish' -end \ No newline at end of file +end diff --git a/lib/tasks/projects.rake b/lib/tasks/projects.rake index 5d538655..af73ebd7 100644 --- a/lib/tasks/projects.rake +++ b/lib/tasks/projects.rake @@ -1,19 +1,19 @@ # frozen_string_literal: true require 'yaml' + namespace :projects do desc 'Import starter projects' task create_starter: :environment do - - Dir.each_child("#{File.dirname(__FILE__)}/project_components") do |dir_name| - config = YAML.safe_load(File.open("#{File.dirname(__FILE__)}/project_components/#{dir_name}/project_config.yml").read) + Dir.each_child("#{File.dirname(__FILE__)}/project_components") do |dir| + config = YAML.safe_load(File.open("#{File.dirname(__FILE__)}/project_components/#{dir}/project_config.yml").read) Project.find_by(identifier: config['IDENTIFIER'])&.destroy new_project = Project.new(identifier: config['IDENTIFIER'], name: config['NAME']) num_extra_components = 0 - Dir.each_child("#{File.dirname(__FILE__)}/project_components/#{dir_name}") do |component| + Dir.each_child("#{File.dirname(__FILE__)}/project_components/#{dir}") do |component| if component != 'project_config.yml' - file = File.open(File.dirname(__FILE__) + "/project_components/#{dir_name}/#{component}") + file = File.open(File.dirname(__FILE__) + "/project_components/#{dir}/#{component}") code = file.read file.close name = component.split('.')[0] From 067b55227a28ee09cb1b7e2a5046ee43aca80d01 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 11 Feb 2022 12:42:24 +0000 Subject: [PATCH 13/17] rubocop --- lib/tasks/projects.rake | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/tasks/projects.rake b/lib/tasks/projects.rake index af73ebd7..01b3e1c1 100644 --- a/lib/tasks/projects.rake +++ b/lib/tasks/projects.rake @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'yaml' +require 'yaml' namespace :projects do desc 'Import starter projects' @@ -12,7 +12,9 @@ namespace :projects do num_extra_components = 0 Dir.each_child("#{File.dirname(__FILE__)}/project_components/#{dir}") do |component| - if component != 'project_config.yml' + if component == 'project_config.yml' + next + end file = File.open(File.dirname(__FILE__) + "/project_components/#{dir}/#{component}") code = file.read file.close @@ -26,7 +28,7 @@ namespace :projects do end new_component = Component.new(name: name, extension: extension, content: code, index: index) new_project.components << new_component - end + # end end new_project.save end From efca881eaee09a551b859e5dc21742fc37995b1f Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 11 Feb 2022 12:54:18 +0000 Subject: [PATCH 14/17] Final rubocop fixes --- .../hello_world_example/main.py | 2 +- lib/tasks/projects.rake | 28 ++++++++----------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/tasks/project_components/hello_world_example/main.py b/lib/tasks/project_components/hello_world_example/main.py index 7d9ab51a..f89e0563 100644 --- a/lib/tasks/project_components/hello_world_example/main.py +++ b/lib/tasks/project_components/hello_world_example/main.py @@ -1,6 +1,6 @@ #!/bin/python3 -from noemoji import * +from emoji import * from datetime import * from random import randint diff --git a/lib/tasks/projects.rake b/lib/tasks/projects.rake index 01b3e1c1..088e10d4 100644 --- a/lib/tasks/projects.rake +++ b/lib/tasks/projects.rake @@ -12,23 +12,19 @@ namespace :projects do num_extra_components = 0 Dir.each_child("#{File.dirname(__FILE__)}/project_components/#{dir}") do |component| - if component == 'project_config.yml' - next + next if component == 'project_config.yml' + + code = File.read(File.dirname(__FILE__) + "/project_components/#{dir}/#{component}") + name = component.split('.')[0] + extension = component.split('.').drop(1).join('.') + if component == 'main.py' + index = 0 + else + num_extra_components += 1 + index = num_extra_components end - file = File.open(File.dirname(__FILE__) + "/project_components/#{dir}/#{component}") - code = file.read - file.close - name = component.split('.')[0] - extension = component.split('.').drop(1).join('.') - if component == 'main.py' - index = 0 - else - num_extra_components += 1 - index = num_extra_components - end - new_component = Component.new(name: name, extension: extension, content: code, index: index) - new_project.components << new_component - # end + new_component = Component.new(name: name, extension: extension, content: code, index: index) + new_project.components << new_component end new_project.save end From 470cc8a8914f8ee14463ea98b4e4b0435ee3f044 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 11 Feb 2022 14:19:32 +0000 Subject: [PATCH 15/17] Refactoring --- app/controllers/api/projects/phrases_controller.rb | 3 +-- app/models/project.rb | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/projects/phrases_controller.rb b/app/controllers/api/projects/phrases_controller.rb index 5885c4a0..6e1e245f 100644 --- a/app/controllers/api/projects/phrases_controller.rb +++ b/app/controllers/api/projects/phrases_controller.rb @@ -6,8 +6,7 @@ class PhrasesController < ApiController require 'phrase_identifier' def show - projects = Project.all.includes(:components).order('components.index') - @project = projects.find_by!(identifier: params[:id]) + @project = Project.find_by!(identifier: params[:id]) render '/api/projects/show', formats: [:json] end diff --git a/app/models/project.rb b/app/models/project.rb index 4dce289f..aacb722c 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -5,7 +5,7 @@ class Project < ApplicationRecord before_validation :check_unique_not_null, on: :create validates :identifier, presence: true, uniqueness: true - has_many :components, dependent: :destroy + has_many :components, -> { order(:index) }, dependent: :destroy private From 9f0142f20dd1c723e17f6449192e3f874af2d1ff Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 11 Feb 2022 15:06:01 +0000 Subject: [PATCH 16/17] Refactor to make more use of config yaml --- app/models/component.rb | 1 + app/models/project.rb | 2 +- .../hello_world_example/project_config.yml | 15 +++++++++- .../hello_world_starter/project_config.yml | 15 +++++++++- .../project_config.yml | 7 ++++- .../project_config.yml | 7 ++++- lib/tasks/projects.rake | 30 +++++++------------ 7 files changed, 53 insertions(+), 24 deletions(-) diff --git a/app/models/component.rb b/app/models/component.rb index 5f605f05..ef514130 100644 --- a/app/models/component.rb +++ b/app/models/component.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true class Component < ApplicationRecord + belongs_to :project end diff --git a/app/models/project.rb b/app/models/project.rb index aacb722c..23217a0e 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -5,7 +5,7 @@ class Project < ApplicationRecord before_validation :check_unique_not_null, on: :create validates :identifier, presence: true, uniqueness: true - has_many :components, -> { order(:index) }, dependent: :destroy + has_many :components, -> { order(:index) }, dependent: :destroy, inverse_of: :project private diff --git a/lib/tasks/project_components/hello_world_example/project_config.yml b/lib/tasks/project_components/hello_world_example/project_config.yml index 5a061e0b..0d29c426 100644 --- a/lib/tasks/project_components/hello_world_example/project_config.yml +++ b/lib/tasks/project_components/hello_world_example/project_config.yml @@ -1,2 +1,15 @@ NAME: "Hello 🌍🌎🌏 Example" -IDENTIFIER: "python-emoji-example" \ No newline at end of file +IDENTIFIER: "python-emoji-example" +COMPONENTS: + - name: "main" + extension: "py" + location: "main.py" + index: 0 + - name: "emoji" + extension: "py" + location: "emoji.py" + index: 1 + - name: "noemoji" + extension: "py" + location: "noemoji.py" + index: 2 \ No newline at end of file diff --git a/lib/tasks/project_components/hello_world_starter/project_config.yml b/lib/tasks/project_components/hello_world_starter/project_config.yml index 04c44adc..8e1bc82c 100644 --- a/lib/tasks/project_components/hello_world_starter/project_config.yml +++ b/lib/tasks/project_components/hello_world_starter/project_config.yml @@ -1,2 +1,15 @@ NAME: "Hello 🌍🌎🌏" -IDENTIFIER: "python-hello-starter" \ No newline at end of file +IDENTIFIER: "python-hello-starter" +COMPONENTS: + - name: "main" + extension: "py" + location: "main.py" + index: 0 + - name: "emoji" + extension: "py" + location: "emoji.py" + index: 1 + - name: "noemoji" + extension: "py" + location: "noemoji.py" + index: 2 \ No newline at end of file diff --git a/lib/tasks/project_components/target_practice_example/project_config.yml b/lib/tasks/project_components/target_practice_example/project_config.yml index df54a632..b221ea73 100644 --- a/lib/tasks/project_components/target_practice_example/project_config.yml +++ b/lib/tasks/project_components/target_practice_example/project_config.yml @@ -1,2 +1,7 @@ NAME: "Target Practice Example" -IDENTIFIER: "python-archery-example" \ No newline at end of file +IDENTIFIER: "python-archery-example" +COMPONENTS: + - name: "main" + extension: "py" + location: "main.py" + index: 0 \ No newline at end of file diff --git a/lib/tasks/project_components/target_practice_starter/project_config.yml b/lib/tasks/project_components/target_practice_starter/project_config.yml index 6698db91..12de0e7f 100644 --- a/lib/tasks/project_components/target_practice_starter/project_config.yml +++ b/lib/tasks/project_components/target_practice_starter/project_config.yml @@ -1,2 +1,7 @@ NAME: "Target Practice" -IDENTIFIER: "python-archery-starter" \ No newline at end of file +IDENTIFIER: "python-archery-starter" +COMPONENTS: + - name: "main" + extension: "py" + location: "main.py" + index: 0 \ No newline at end of file diff --git a/lib/tasks/projects.rake b/lib/tasks/projects.rake index 088e10d4..5f3bb8f8 100644 --- a/lib/tasks/projects.rake +++ b/lib/tasks/projects.rake @@ -6,25 +6,17 @@ namespace :projects do desc 'Import starter projects' task create_starter: :environment do Dir.each_child("#{File.dirname(__FILE__)}/project_components") do |dir| - config = YAML.safe_load(File.open("#{File.dirname(__FILE__)}/project_components/#{dir}/project_config.yml").read) - Project.find_by(identifier: config['IDENTIFIER'])&.destroy - new_project = Project.new(identifier: config['IDENTIFIER'], name: config['NAME']) - num_extra_components = 0 - - Dir.each_child("#{File.dirname(__FILE__)}/project_components/#{dir}") do |component| - next if component == 'project_config.yml' - - code = File.read(File.dirname(__FILE__) + "/project_components/#{dir}/#{component}") - name = component.split('.')[0] - extension = component.split('.').drop(1).join('.') - if component == 'main.py' - index = 0 - else - num_extra_components += 1 - index = num_extra_components - end - new_component = Component.new(name: name, extension: extension, content: code, index: index) - new_project.components << new_component + project_config = YAML.safe_load(File.open("#{File.dirname(__FILE__)}/project_components/#{dir}/project_config.yml").read) + Project.find_by(identifier: project_config['IDENTIFIER'])&.destroy + new_project = Project.new(identifier: project_config['IDENTIFIER'], name: project_config['NAME']) + components = project_config["COMPONENTS"] + components.each do |component| + name = component['name'] + extension = component['extension'] + code = File.read(File.dirname(__FILE__) + "/project_components/#{dir}/#{component['location']}") + index = component['index'] + project_component = Component.new(name: name, extension: extension, content: code, index: index) + new_project.components << project_component end new_project.save end From d8e29fc109502f2fe248c9c3a7d349cc35934ea1 Mon Sep 17 00:00:00 2001 From: Lois Wells Date: Fri, 11 Feb 2022 15:10:45 +0000 Subject: [PATCH 17/17] rubocop --- app/models/component.rb | 2 +- lib/tasks/projects.rake | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/component.rb b/app/models/component.rb index ef514130..f8b53364 100644 --- a/app/models/component.rb +++ b/app/models/component.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true class Component < ApplicationRecord - belongs_to :project + belongs_to :project end diff --git a/lib/tasks/projects.rake b/lib/tasks/projects.rake index 5f3bb8f8..0e17816f 100644 --- a/lib/tasks/projects.rake +++ b/lib/tasks/projects.rake @@ -6,10 +6,10 @@ namespace :projects do desc 'Import starter projects' task create_starter: :environment do Dir.each_child("#{File.dirname(__FILE__)}/project_components") do |dir| - project_config = YAML.safe_load(File.open("#{File.dirname(__FILE__)}/project_components/#{dir}/project_config.yml").read) - Project.find_by(identifier: project_config['IDENTIFIER'])&.destroy - new_project = Project.new(identifier: project_config['IDENTIFIER'], name: project_config['NAME']) - components = project_config["COMPONENTS"] + proj_config = YAML.safe_load(File.read("#{File.dirname(__FILE__)}/project_components/#{dir}/project_config.yml")) + Project.find_by(identifier: proj_config['IDENTIFIER'])&.destroy + new_project = Project.new(identifier: proj_config['IDENTIFIER'], name: proj_config['NAME']) + components = proj_config['COMPONENTS'] components.each do |component| name = component['name'] extension = component['extension']