Skip to content

Commit

Permalink
Merge pull request #3 from poppy-project/setup
Browse files Browse the repository at this point in the history
Installation scripts & exporting poppy-scratch
  • Loading branch information
antoinepringalle authored Jul 6, 2021
2 parents 8dc25fa + 12021d3 commit 53caf4a
Show file tree
Hide file tree
Showing 12 changed files with 349 additions and 139 deletions.
132 changes: 84 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,93 @@
# scratch-poppy
### Scratch-poppy is a Scratch 3 extension for Poppy robots
Scratch-poppy is a Scratch 3 extension for [Poppy robots](https://github.com/poppy-project).

## Getting started
## :inbox_tray: Installation

### Scratch VM and Scratch GUI installation

This requires you to have **Git** and **Node.js** installed.
First, you will have to clone **scratch-vm** and **scratch-gui** and to link both. For this, open a Command Prompt or Terminal in your development environment and follow the next steps:

git clone https://github.com/LLK/scratch-gui
git clone https://github.com/LLK/scratch-vm
cd scratch-vm
curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh -o install_nvm.sh
bash install_nvm.sh
source ~/.profile
rm install_nvm.sh
nvm install 10.21.0
nvm use 10.21.0
npm install
npm link
cd ../scratch-gui
npm install
npm link scratch-vm


### Scratch Poppy installation

Now, you can clone this repository in the same development environment. For the installation you just have to execute the following lines to copy folders and some extension's additions at the right place:

git clone https://github.com/poppy-project/scratch-poppy
cd scratch-poppy
bash install.sh
### :page_facing_up: Scripted installation

Requirements : [git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git "sudo apt install git-all"), [node](https://nodejs.org/en/download/package-manager/ "sudo apt install nodejs"), [zip](https://linux.die.net/man/1/zip "sudo apt install zip"),

#### Install scratch files & poppy's extension
We need [scratch-vm](https://github.com/LLK/scratch-vm "GitHub scratch-vm repository") and [scratch-gui](https://github.com/LLK/scratch-gui "GitHub scratch-gui repository") repositories to create our application, we download and install them with **install-scratch-files.sh** script. Then, we install our Poppy extension with **install-extension.sh** script.
```bash
bash install-scratch-files.sh
bash install-extension.sh
```

### Launching and editing
#### Exporting application
To use scratch with Poppy without having to use node on the raspberry, we export static files we just built as an archive. The archive will be uploaded as a release of poppy-scratch repository, and can be downloaded if the user wants to use Scratch.
```bash
bash export-scratch.sh
```

Now it is possible to launch scratch in the repository **scratch-gui** with:

cd scratch-gui
npm start
### :keyboard: Manual installation

Then go to http://localhost:8601/ in your web browser.
Select the **Poppy extension** in the extension menu (with the button at the bottom left corner).
#### Scratch VM and Scratch GUI installation

If you want to edit blocks, the code are now situated at this path: `scratch-vm/src/extensions/scratch3_poppy/index.js`. You can find more informations to create a block here: https://github.com/LLK/scratch-vm/blob/develop/docs/extensions.md

### TODO:

* To implement the button "sequentially" and "play senquentially". These buttons allow to play different actions, for the first one, and moves, for the second, one after the other.

* To implement the "wait" option in the "setMotorsGoTo" button to wait until a motor reaches its position before moving a second motor.

* To add the different lists ("all motors", "all motors position", "all recorded moves" ...) directly in scratch (in the index.js file or in the scratch project)

See the `Advanced.sb3` scratch project to see the last two points.
First, you will have to clone **scratch-vm** and **scratch-gui** and to link both. For this, open a Command Prompt or Terminal in your development environment and follow the next steps:
```bash
git clone https://github.com/LLK/scratch-gui
git clone https://github.com/LLK/scratch-vm
cd scratch-vm
curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh -o install_nvm.sh
bash install_nvm.sh
source ~/.profile
rm install_nvm.sh
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # loads nvm without reopening a terminal
nvm install 10.21.0
nvm use 10.21.0
npm install
npm link
cd ../scratch-gui
npm install
npm link scratch-vm
```

Then you will need to install Poppy's extension in Scratch files. In **scratch-poppy** run:
```bash
bash install-extension.sh
```

#### Building application
To build the application, go to **scratch-gui** folder and run :
```bash
sudo npm run build
```

#### Exporting application
To use scratch with Poppy without having to use node on the raspberry, we export static files we just built as an archive. The archive will be uploaded as a release of poppy-scratch repository, and can be downloaded if the user wants to use Scratch.
To export the application as a zip archive, we need to rename the build folder as **scratch-application** in **scratch-gui**.
```bash
mv build scratch-application
```
Then zip it:
```bash
zip -r scratch-application.zip scratch-application
```

## :memo: Edit Poppy's Extension

You can add more blocks by modifying **scratch_vm_files/scratch3_poppy** files.
You can find information to create a block here: https://github.com/LLK/scratch-vm/blob/develop/docs/extensions.md

To see and test your blocks run the script install_extension.sh and then go to **scratch-gui** folder. You can either:
- run a server that will auto reload Scratch server each time you run the install_extension.sh script.
For this, run:
```bash
sudo npm start
```
Then go to http://localhost:8601/ in your web browser.
Make sure to install the **Poppy extension** in the extension menu (button in the bottom left corner).


- build the static files and open index.html with your browser. You will have to run those steps each time you run install_extention.sh script.
```bash
sudo npm run build
cd build
firefox index.html # Or google-chrome, chromium-browser, ...
```

## :scroll: Todo

* Implement the button "sequentially" and "play sequentially". These buttons allow playing different actions, for the first one, and moves, for the second, one after the other.
Binary file removed SB3/Advanced.sb3
Binary file not shown.
Binary file removed SB3/Draw.sb3
Binary file not shown.
Binary file removed SB3/For.sb3
Binary file not shown.
32 changes: 32 additions & 0 deletions export-scratch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

#################################################################
# Script Name : Export Scratch
# Description : Builds the scratch application with the
# Poppy extension and exports it as a zip archive.
#################################################################

ITALIC="\033[3m"
GREEN="\033[92m"
YELLOW="\033[93m"
CLEAR="\033[0m"

installation_folder=".."
archive_name="scratch-application"

# building application
echo -e "${YELLOW}Building Scratch application (needs permissions)${CLEAR}"
cd "$installation_folder/scratch-gui" || exit 2
sudo npm run build
echo -e "${GREEN}Build done!${CLEAR}\n"

# renaming folder
echo -ne "${YELLOW}Renaming ${ITALIC}build${CLEAR}${YELLOW} folder to ${ITALIC}${archive_name}${CLEAR}..."
mv build $archive_name
echo -e "${GREEN}Done!${CLEAR}\n"

# zipping folder
echo -e "${YELLOW}Zipping built application to ${ITALIC}${archive_name}.zip${CLEAR}..."
cd - || exit 2
zip -r "$archive_name.zip" "$installation_folder/scratch-gui/$archive_name"
echo -e "${GREEN}Application successfully zipped!${CLEAR}\n"
70 changes: 70 additions & 0 deletions install-extension.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash

##############################################################
# Script Name : Install Extension
# Description : Installs the Poppy Extension to Scratch files
##############################################################

ITALIC="\033[3m"
GREEN="\033[92m"
YELLOW="\033[93m"
CLEAR="\033[0m"

installation_folder=".."

##############################################################
# Function add_require()
# Adds a require line to a js file, if line does not already exist.
# It first scans the 'file' for 'search' arg. If 'search' is not found,
# it replaces 'where' arg by 'replace' arg.
#
# ARGUMENTS:
# $1: search = line(s) to add, to check if it already exists
# $2: where = where to put it ?
# $3: replace = where "+" search
# $4: file
#
# OUTPUTS:
# Some echos of the progress of the script
##############################################################

add_require () {
echo -e "Adding a require to ${ITALIC}$4${CLEAR}"
if grep -q "$1" "$4"
then
# code if found
echo -e "${GREEN}Require already satisfied!${CLEAR}"
else
# code if not found
echo -e "${YELLOW}Require not satisfied!${CLEAR}"
sed -i "s/$2/$3/" "$4"
echo -e "${GREEN}Require has been added!${CLEAR}"
fi
}


find="poppy: () => require('..\/extensions\/scratch3_poppy'),"
where="boost: () => require('..\/extensions\/scratch3_boost'),"
replace="$where\n $find"
file="$installation_folder/scratch-vm/src/extension-support/extension-manager.js"
add_require "$find" "$where" "$replace" "$file"


find="import poppyInsetIconURL from '.\/poppy\/poppy-small.png';"
where="import boostConnectionTipIconURL from '.\/boost\/boost-button-illustration.svg';"
replace="$where\n\nimport poppyIconURL from '.\/poppy\/poppy.png';\n$find"
file="$installation_folder/scratch-gui/src/lib/libraries/extensions/index.jsx"
add_require "$find" "$where" "$replace" "$file"

find='extensionId: "poppy",'
where="export default \["
replace='export default [\n {\n name: "Poppy",\n extensionId: "poppy",\n collaborator: "Poppy-Station",\n iconURL: poppyIconURL,\n insetIconURL: poppyInsetIconURL,\n description: (\n <FormattedMessage\n defaultMessage="Control your Poppy robot"\n description="Poppy controller extension"\n id="gui.extension.poppy.description"\n \/>\n ),\n featured: true,\n disabled: false,\n internetConnectionRequired: true,\n bluetoothRequired: false,\n },'
file="$installation_folder/scratch-gui/src/lib/libraries/extensions/index.jsx"
add_require "$find" "$where" "$replace" "$file"

echo -e "\nCopying Poppy extension code to ${ITALIC}scratch-vm${CLEAR}... \c"
cp -r ./scratch_vm_files/scratch3_poppy $installation_folder/scratch-vm/src/extensions
echo -e "${GREEN}DONE!${CLEAR}"
echo -e "Copying Poppy images to ${ITALIC}scratch-gui${CLEAR}... \c"
cp -r ./scratch_gui_files/poppy $installation_folder/scratch-gui/src/lib/libraries/extensions
echo -e "${GREEN}DONE!${CLEAR}"
132 changes: 132 additions & 0 deletions install-scratch-files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/bin/bash

##############################################################
# Script Name : Install Scratch Files
# Description : Clones the Scratch-VM and Scratch-GUI
# repositories to the parent folder, if they do
# not already exist. The script also takes care
# of the installation of the different repos.
##############################################################

ITALIC="\033[3m"
GREEN="\033[92m"
YELLOW="\033[93m"
CLEAR="\033[0m"

installation_folder=".."


##############################################################
# Function clone_repository()
# Clones a github repository in a given folder if the repo is
# not already cloned.
#
# ARGUMENTS:
# $1: repo link
# $2: folder
#
# OUTPUTS:
# Some echos of the progress of the script
##############################################################

clone_repository() {
repo_name=$(basename -s .git "$1")
echo -e "Cloning ${ITALIC}${YELLOW}$repo_name${CLEAR}"

if [ -d "$2/$repo_name" ]; then
# code if found
echo -e "${GREEN}Repository already installed!${CLEAR}\n"
else
# code if not found
echo -e "${YELLOW}Repository not installed!${CLEAR}"
git clone "$1" "$2/$repo_name"
echo -e "${GREEN}Repository has been installed!${CLEAR}\n"
fi
}

# Cloning vm & gui repos
# if VM:
# if GUI:
# nothing to do, exit script
# else:
# install GUI
# else:
# if GUI
# install VM
# else:
# install VM & GUI

if [ -d "$installation_folder/scratch-vm" ]
then
if [ -d "$installation_folder/scratch-gui" ]
then
echo -e "${GREEN}Nothing to do${CLEAR}"
echo -e "Script found scratch-vm & scratch-gui repositories in ${installation_folder}/ folder."
echo -e "If you have not installed these folders using the Poppy script, you have three choices to continue the installation:"
echo -e "- Delete the existing folders to get a fresh start and then re-run this script (install-scratch-files)."
echo -e "- Change the installation folder to reclone the Scratch folders elsewhere"
echo -e "- Install the folders by hand, following the steps described in the README, in the manual installation section."
exit 0
else
echo -e "${YELLOW}> Installing GUI${CLEAR}"
clone_repository "https://github.com/LLK/scratch-gui.git" $installation_folder
fi
else
if [ -d "$installation_folder/scratch-gui" ]
then
echo -e "${YELLOW}> Installing VM${CLEAR}"
clone_repository "https://github.com/LLK/scratch-vm.git" $installation_folder
else
echo -e "${YELLOW}> Installing VM & GUI${CLEAR}"
clone_repository "https://github.com/LLK/scratch-vm.git" $installation_folder
clone_repository "https://github.com/LLK/scratch-gui.git" $installation_folder
fi
fi
echo -e "\n${GREEN}All repositories are downloaded!${CLEAR}\n"


# Installing nvm
echo -e "${YELLOW}Installing nvm...${CLEAR}"
cd "$installation_folder/scratch-vm" || exit 2
curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh -o install_nvm.sh
bash install_nvm.sh
source ~/.profile
rm install_nvm.sh
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
nvm install 10.21.0
nvm use 10.21.0
echo -e "${GREEN}NVM installed!${CLEAR}\n"


# Installing scratch-vm node modules
echo -e "${YELLOW}Installing scratch-vm node modules...${CLEAR}"
npm install
echo -e "${GREEN}scratch-vm node modules installed!${CLEAR}\n"


# Linking scratch-vm
echo -e "${YELLOW}Linking scratch-vm...${CLEAR}"
npm link
echo -e "${GREEN}scratch-vm linked!${CLEAR}\n"


# Changing directory to scratch-gui
cd ../scratch-gui || exit 2


# Installing scratch-gui node modules
echo -e "${YELLOW}Installing scratch-gui node modules...${CLEAR}"
npm install
echo -e "${GREEN}scratch-gui node modules installed!${CLEAR}\n"


# Installing scratch-vm node modules
echo -e "${YELLOW}Linking scratch-gui to scratch-vm...${CLEAR}"
npm link scratch-vm
echo -e "${GREEN}scratch-gui linked to scratch-vm!${CLEAR}\n"


# Success message
echo -e "${GREEN}Installation of scratch files complete!${CLEAR}"

Loading

0 comments on commit 53caf4a

Please sign in to comment.