diff --git a/README.md b/README.md new file mode 100644 index 0000000..6023394 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Cloud 9 (C9) Extension for BreatheCode Users + +All of Breathe Code's functionality inside the C9 Coding Editor. + +## Installation + +Please copy the content of the init.js script and paste inside the Cloud9 -> "Open Your Init Script" file. + +![Animated Installation Tutorial](https://breatheco-de.github.io/c9-scripts/assets/installation.gif "Animated Installation Tutorial") diff --git a/assets/gitignore b/assets/gitignore new file mode 100644 index 0000000..5a9224b --- /dev/null +++ b/assets/gitignore @@ -0,0 +1 @@ +/.c9/ diff --git a/assets/installation.gif b/assets/installation.gif new file mode 100644 index 0000000..84dd431 Binary files /dev/null and b/assets/installation.gif differ diff --git a/autoexec b/autoexec new file mode 100644 index 0000000..9bacdca --- /dev/null +++ b/autoexec @@ -0,0 +1,40 @@ +#!/bin/bash +if [ ! "$BC_C9_AUTOEXEC" = 3 ]; then + export BC_C9_NAME="breathecode/scripts" + export BC_C9_LIBRARY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + export BC_C9_WORKSPACE="$HOME/workspace" + + export PATH="$PATH:$BC_C9_LIBRARY/bash" + + # https://unix.stackexchange.com/questions/48381/fixing-scrolling-in-nano-running-in-tmux-in-mate-terminal + export TERM=screen + + # Increment this number each time you change something inside of this 'if'-block + export BC_C9_AUTOEXEC=3 + + echo "$BC_C9_NAME: BreatheCode Plugin #$BC_C9_AUTOEXEC" + + source ~/.nvm/nvm.sh + read version _ <<< $(nvm current) + if [[ "$version" == *"v6"* ]]; then + nvm i 8 + nvm alias default 8 + echo "$BC_C9_NAME: Node was upgraded to version 8" + fi + + # Path to .pyenv + if [ -d ~/.pyenv ]; then + export PATH="/home/ubuntu/.pyenv/bin:$PATH" + eval "$(pyenv init -)" + eval "$(pyenv virtualenv-init -)" + fi +fi + +# Functions +pathContainsDirectory() { if [[ ":$PATH:" == *":$1:"* ]]; then return 0; else return 1; fi } +randomStringAlphaNum() { cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c ${1:-32}; } + +# ~/workspace/bin +if [ -d "$HOME/workspace/bin" ] && ! pathContainsDirectory "$HOME/workspace/bin"; then + export PATH="$PATH:$HOME/workspace/bin" +fi diff --git a/bash/find-free-port b/bash/find-free-port new file mode 100644 index 0000000..3944caf --- /dev/null +++ b/bash/find-free-port @@ -0,0 +1,22 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +# https://docs.c9.io/docs/multiple-ports +declare -a PORTS=(8082 8081 8080) + +if [ "$1" = "-a" ]; then + for PORT in "${PORTS[@]}"; do + echo "$PORT" + done + exit 0 +fi + +for PORT in "${PORTS[@]}"; do + if ! nc "127.0.0.1" "$PORT" < /dev/null; then + echo "$PORT" + exit 0 + fi +done + +echo "$BC_C9_NAME: find-free-port: error: no free port available!" >&2 +exit 1 diff --git a/bash/get-helper-scripts b/bash/get-helper-scripts new file mode 100644 index 0000000..3f944a5 --- /dev/null +++ b/bash/get-helper-scripts @@ -0,0 +1,3 @@ +echo "$C9_USER: this are the helper-scripts you have available!" >&2 + +(cd ~/c9-scripts/utils/ && ls) diff --git a/bash/git-optimize-repo b/bash/git-optimize-repo new file mode 100644 index 0000000..b93caa1 --- /dev/null +++ b/bash/git-optimize-repo @@ -0,0 +1,11 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +git gc --prune=now && \ +git submodule foreach git gc --prune=now && \ + +git repack -adf --depth=250 --window=250 && \ +git submodule foreach git repack -adf --depth=250 --window=250 && \ + +git prune && \ +git submodule foreach git prune diff --git a/bash/godoc-http b/bash/godoc-http new file mode 100644 index 0000000..b7f0925 --- /dev/null +++ b/bash/godoc-http @@ -0,0 +1,14 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +# find free port +PORT="$(find-free-port)" || exit 1 + +# print url +echo "$BC_C9_NAME: godoc-http: info: godoc serves on https://$C9_HOSTNAME:$PORT" + +# run godoc +godoc -http="$IP:$PORT" \ + -play \ + "$@" \ + || exit 1 diff --git a/bash/install-adminer b/bash/install-adminer new file mode 100644 index 0000000..d9b109f --- /dev/null +++ b/bash/install-adminer @@ -0,0 +1,30 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +# run as root +if [ "$UID" != "0" ]; then + sudo -E "$0" "$@" + exit +fi + +# stop apache2 +service apache2 stop || exit 1 + +# find free port +PORT="$(find-free-port)" || exit 1 + +# update apache2 config +find-free-port -a | while read port; do sed "s/$port/$PORT/" -i /etc/apache2/ports.conf; done || exit 1 +find-free-port -a | while read port; do sed "s/$port/$PORT/" -i /etc/apache2/sites-available/001-cloud9.conf; done || exit 1 + +# install phpmyadmin, restart apache2 +phpmyadmin-ctl install | sed "s/.c9users.io/.c9users.io:$PORT/" || exit 1 + +# get adminer +wget 'https://www.adminer.org/latest.php' -O /usr/share/phpmyadmin/adminer.php || exit 1 + +# start apache2 +service apache2 start + +# return adminer url +echo "Adminer has been installed to https://$C9_HOSTNAME:$PORT/phpmyadmin/adminer.php" diff --git a/bash/install-appengine-go b/bash/install-appengine-go new file mode 100644 index 0000000..17267db --- /dev/null +++ b/bash/install-appengine-go @@ -0,0 +1,49 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +if [ -e "$HOME/appengine_go" ]; then + echo "$BC_C9_NAME: install_appengine_go: error: already installed. remove ~/appengine_go to reinstall." + exit 1 +fi + +SDK_URL='https://storage.googleapis.com/appengine-sdks/featured/go_appengine_sdk_linux_amd64-1.9.40.zip' + +if ! SDK_ZIP="/tmp/$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 8)"; then + echo "$BC_C9_NAME: install_appengine_go: error: could not generate random string." + exit 1 +fi + +if ! SDK_UNP="/tmp/$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 8)"; then + echo "$BC_C9_NAME: install_appengine_go: error: could not generate random string." + exit 1 +fi + +if ! wget -O "$SDK_ZIP" "$SDK_URL"; then + echo "$BC_C9_NAME: install_appengine_go: error: could not download sdk. invalid url?" + exit 1 +fi + +if ! mkdir "$SDK_UNP"; then + echo "$BC_C9_NAME: install_appengine_go: error: could not create temporary directory." + exit 1 +fi + +if ! unzip "$SDK_ZIP" -d "$SDK_UNP"; then + echo "$BC_C9_NAME: install_appengine_go: error: could not extract sdk." + exit 1 +fi + +if ! mv "$SDK_UNP/go_appengine" "$HOME/appengine_go"; then + echo "$BC_C9_NAME: install_appengine_go: error: could not rename $SDK_UNP/go_appengine to ~/appengine_go." + exit 1 +fi + +if ! rm "$SDK_ZIP"; then + echo "$BC_C9_NAME: install_appengine_go: error: could not remove appengine sdk zip ($SDK_ZIP)." + exit 1 +fi + +if ! rm -rf "$SDK_UNP"; then + echo "$BC_C9_NAME: install_appengine_go: error: could not remove appengine sdk tmp dir ($SDK_UNP)." + exit 1 +fi diff --git a/bash/install-appengine-python-php b/bash/install-appengine-python-php new file mode 100644 index 0000000..592825d --- /dev/null +++ b/bash/install-appengine-python-php @@ -0,0 +1,49 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +if [ -e "$HOME/appengine_python_php" ]; then + echo "$BC_C9_NAME: install_appengine_python_php: error: already installed. remove ~/appengine_python_php to reinstall." + exit 1 +fi + +SDK_URL='https://storage.googleapis.com/appengine-sdks/featured/google_appengine_1.9.40.zip' + +if ! SDK_ZIP="/tmp/$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 8)"; then + echo "$BC_C9_NAME: install_appengine_python_php: error: could not generate random string." + exit 1 +fi + +if ! SDK_UNP="/tmp/$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 8)"; then + echo "$BC_C9_NAME: install_appengine_python_php: error: could not generate random string." + exit 1 +fi + +if ! wget -O "$SDK_ZIP" "$SDK_URL"; then + echo "$BC_C9_NAME: install_appengine_python_php: error: could not download sdk. invalid url?" + exit 1 +fi + +if ! mkdir "$SDK_UNP"; then + echo "$BC_C9_NAME: install_appengine_python_php: error: could not create temporary directory." + exit 1 +fi + +if ! unzip "$SDK_ZIP" -d "$SDK_UNP"; then + echo "$BC_C9_NAME: install_appengine_python_php: error: could not extract sdk." + exit 1 +fi + +if ! mv "$SDK_UNP/google_appengine" "$HOME/appengine_python_php"; then + echo "$BC_C9_NAME: install_appengine_python_php: error: could not move $SDK_UNP/google_appengine to ~/appengine_python_php." + exit 1 +fi + +if ! rm "$SDK_ZIP"; then + echo "$BC_C9_NAME: install_appengine_python_php: error: could not remove appengine sdk zip ($SDK_ZIP)." + exit 1 +fi + +if ! rm -rf "$SDK_UNP"; then + echo "$BC_C9_NAME: install_appengine_go: error: could not remove appengine sdk tmp dir ($SDK_UNP)." + exit 1 +fi diff --git a/bash/install-atom b/bash/install-atom new file mode 100644 index 0000000..bc38210 --- /dev/null +++ b/bash/install-atom @@ -0,0 +1,17 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +# run as root +if [ "$UID" != "0" ]; then + sudo -E "$0" "$@" + exit +fi + +# add atom repository +add-apt-repository -y ppa:webupd8team/atom || exit 1 + +# update package cache +apt-get update || exit 1 + +# install atom +apt-get -y install atom || exit 1 diff --git a/bash/install-git-extras b/bash/install-git-extras new file mode 100644 index 0000000..882935c --- /dev/null +++ b/bash/install-git-extras @@ -0,0 +1,15 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +# run as root +if [ "$UID" != "0" ]; then + sudo -E "$0" "$@" + exit +fi + +# update package cache +apt-get update || exit 1 + +# install git-extras +apt-get -y install git-extras || exit 1 + diff --git a/bash/install-git-lfs b/bash/install-git-lfs new file mode 100644 index 0000000..796c053 --- /dev/null +++ b/bash/install-git-lfs @@ -0,0 +1,17 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +# run as root +if [ "$UID" != "0" ]; then + sudo -E "$0" "$@" + exit +fi + +# add repository +curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash || exit 1 + +# install git-lfs +apt-get -y install git-lfs || exit 1 + +# add git-lfs filters to git +git lfs install || exit 1 diff --git a/bash/install-gsutil b/bash/install-gsutil new file mode 100644 index 0000000..e42a0b7 --- /dev/null +++ b/bash/install-gsutil @@ -0,0 +1,6 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +sudo apt-get -y install gcc python-dev python-setuptools libffi-dev && \ +sudo apt-get -y install python-pip && \ +sudo pip install -U gsutil diff --git a/bash/install-mongodb b/bash/install-mongodb new file mode 100644 index 0000000..5c9d580 --- /dev/null +++ b/bash/install-mongodb @@ -0,0 +1,14 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +# run as root +if [ "$UID" != "0" ]; then + sudo -E "$0" "$@" + exit +fi + +# update package cache +apt-get update || exit 1 + +# install mongodb +apt-get install -y mongodb-org || exit 1 diff --git a/bash/install-phpmyadmin b/bash/install-phpmyadmin new file mode 100644 index 0000000..bc8e95e --- /dev/null +++ b/bash/install-phpmyadmin @@ -0,0 +1,21 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +# run as root +if [ "$UID" != "0" ]; then + sudo -E "$0" "$@" + exit +fi + +# stop apache2 +service apache2 stop || exit 1 + +# find free port +PORT="$(find-free-port)" || exit 1 + +# update apache2 config +find-free-port -a | while read port; do sed "s/$port/$PORT/" -i /etc/apache2/ports.conf; done || exit 1 +find-free-port -a | while read port; do sed "s/$port/$PORT/" -i /etc/apache2/sites-available/001-cloud9.conf; done || exit 1 + +# install phpmyadmin, restart apache2 +phpmyadmin-ctl install | sed "s/.c9users.io/.c9users.io:$PORT/" || exit 1 diff --git a/bash/install-s3cmd b/bash/install-s3cmd new file mode 100644 index 0000000..30979c1 --- /dev/null +++ b/bash/install-s3cmd @@ -0,0 +1,61 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +# run as root +if [ "$UID" != "0" ]; then + sudo -E "$0" "$@" + exit +fi + +# update package cache +apt-get update || exit 1 + +# install dependency package for s3cmd +apt-get -y install python-dateutil || exit 1 + +if [ -e "$HOME/s3cmd" ]; then + echo "$BC_C9_NAME: install_s3cmd: error: already installed. remove ~/s3cmd to reinstall." + exit 1 +fi + +S3CMD_URL='http://downloads.sourceforge.net/project/s3tools/s3cmd/1.6.0/s3cmd-1.6.0.zip' + +if ! S3CMD_ZIP="/tmp/$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 8)"; then + echo "$BC_C9_NAME: install_s3cmd: error: could not generate random string." + exit 1 +fi + +if ! S3CMD_UNP="/tmp/$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 8)"; then + echo "$BC_C9_NAME: install_s3cmd: error: could not generate random string." + exit 1 +fi + +if ! wget -O "$S3CMD_ZIP" "$S3CMD_URL"; then + echo "$BC_C9_NAME: install_s3cmd: error: could not download s3cmd. invalid url?" + exit 1 +fi + +if ! mkdir "$S3CMD_UNP"; then + echo "$BC_C9_NAME: install_s3cmd: error: could not create temporary directory." + exit 1 +fi + +if ! unzip "$S3CMD_ZIP" -d "$S3CMD_UNP"; then + echo "$BC_C9_NAME: install_s3cmd: error: could not extract s3cmd." + exit 1 +fi + +if ! mv "$S3CMD_UNP"/s3cmd-* "$HOME/s3cmd"; then + echo "$BC_C9_NAME: install_s3cmd: error: could not rename $S3CMD_UNP/s3cmd-* to ~/s3cmd." + exit 1 +fi + +if ! rm "$S3CMD_ZIP"; then + echo "$BC_C9_NAME: install_s3cmd: error: could not remove s3cmd zip ($S3CMD_ZIP)." + exit 1 +fi + +if ! rm -rf "$S3CMD_UNP"; then + echo "$BC_C9_NAME: install_s3cmd: error: could not remove s3cmd tmp dir ($S3CMD_UNP)." + exit 1 +fi diff --git a/bash/mongodb-cli b/bash/mongodb-cli new file mode 100644 index 0000000..12dbf60 --- /dev/null +++ b/bash/mongodb-cli @@ -0,0 +1,4 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +mongo diff --git a/bash/mongodb-start b/bash/mongodb-start new file mode 100644 index 0000000..25c2eb2 --- /dev/null +++ b/bash/mongodb-start @@ -0,0 +1,24 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +# run as root +if [ "$UID" != "0" ]; then + sudo -E "$0" "$@" + exit +fi + +# config +mongod_log="/var/log/mongodb.log" +mongod_data="/var/lib/mongodb" + +# create required directories +[ -d "$mongod_data" ] || mkdir "$mongod_data" || exit 1 + +# start mongod +# https://docs.mongodb.com/manual/reference/program/mongod/#options +mongod \ + --fork \ + --logpath="$mongod_log" \ + --dbpath="$mongod_data" \ + --nojournal \ + "$@" diff --git a/bash/mysql-cli b/bash/mysql-cli new file mode 100644 index 0000000..9379e9c --- /dev/null +++ b/bash/mysql-cli @@ -0,0 +1,4 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +mysql -u root diff --git a/bash/mysql-create-user-db b/bash/mysql-create-user-db new file mode 100644 index 0000000..651e908 --- /dev/null +++ b/bash/mysql-create-user-db @@ -0,0 +1,30 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +# gather information +echo -n "Username: " +read MYSQL_USER + +echo -n "Password: " +read MYSQL_PASS + +echo -n "Database: " +read MYSQL_DB + +echo -n "Collate [utf8_general_ci]: " +read MYSQL_COLLATE +if [ -z "$MYSQL_COLLATE" ]; then + MYSQL_COLLATE="utf8_general_ci" +fi + +# create user +mysql -u root -e "CREATE USER '$MYSQL_USER'@'localhost' IDENTIFIED BY '$MYSQL_PASS';" + +# create database +mysql -u root -e "CREATE DATABASE \`$MYSQL_DB\` COLLATE '$MYSQL_COLLATE';" + +# add user to database +mysql -u root -e "GRANT ALL PRIVILEGES ON \`$MYSQL_DB\`.* to '$MYSQL_USER';" + +# flush privileges +mysql -u root -e "FLUSH PRIVILEGES;" diff --git a/bash/mysql-start b/bash/mysql-start new file mode 100644 index 0000000..909829b --- /dev/null +++ b/bash/mysql-start @@ -0,0 +1,4 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +mysql-ctl start diff --git a/bash/postgres-cli b/bash/postgres-cli new file mode 100644 index 0000000..0a9bacb --- /dev/null +++ b/bash/postgres-cli @@ -0,0 +1,4 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +sudo sudo -u postgres psql diff --git a/bash/postgres-create-user-db b/bash/postgres-create-user-db new file mode 100644 index 0000000..9095859 --- /dev/null +++ b/bash/postgres-create-user-db @@ -0,0 +1,39 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +# run as postgres +if [ "$UID" != "$(id -u postgres)" ]; then + sudo -E sudo -u postgres -E "$0" "$@" + exit +fi + +# gather information +echo -n "Username: " +read PG_USER + +echo -n "Password: " +read PG_PASS + +echo -n "Database name: " +read PG_DB + +echo -n "Encoding [UTF-8]: " +read PG_ENCODING +if [ -z "$PG_ENCODNG" ]; then + PG_ENCODING="UTF-8" +fi + +echo -n "Template [template0]: " +read PG_TEMPLATE +if [ -z "$PG_TEMPLATE" ]; then + PG_TEMPLATE="template0" +fi + +# create user +psql -c "CREATE USER \"$PG_USER\" WITH PASSWORD '$PG_PASS';" + +# create database +psql -c "CREATE DATABASE \"$PG_DB\" WITH ENCODING='$PG_ENCODING' OWNER=\"$PG_USER\" TEMPLATE \"$PG_TEMPLATE\";" + +# add user to database +psql -c "GRANT ALL PRIVILEGES ON DATABASE \"$PG_DB\" to \"$PG_USER\";" diff --git a/bash/postgres-start b/bash/postgres-start new file mode 100644 index 0000000..a8be2e4 --- /dev/null +++ b/bash/postgres-start @@ -0,0 +1,18 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )/autoexec" || exit 1 + +# run as root +if [ "$UID" != "0" ]; then + sudo -E "$0" "$@" + exit +fi + +# password authentication for all local users, except postgres +pg_hba_conf="/etc/postgresql/*/main/pg_hba.conf" +echo 'local all postgres peer' > $pg_hba_conf +echo 'local all all password' >> $pg_hba_conf +echo 'host all all 127.0.0.1/32 password' >> $pg_hba_conf +echo 'host all all ::1/128 password' >> $pg_hba_conf + +# start postgres +service postgresql start diff --git a/init.js b/init.js new file mode 100644 index 0000000..8c6a132 --- /dev/null +++ b/init.js @@ -0,0 +1 @@ +({name:"c9-scripts",version:"1.0",remote:"https://github.com/breatheco-de/c9-scripts.git",absPath:"/home/ubuntu/c9-scripts",relativePath:"~/c9-scripts",debug:!0,log:function(r){this.debug&&console.log(r)},init:function(){this.log("initializing..."),plugin.breathecode=this,this.runScript("get_utils",r=>{plugin.breathecode=Object.assign(plugin.breathecode,r),this.runScript("get_scripts",r=>this.runScripts(r.beforeMount,()=>this.runScript("sync_scripts",()=>this.runScripts(r.afterMount,()=>this.log("All scripts done")),!0)))},!0)},runScripts:function(r,t=null){let i={};r.forEach((r,e)=>{i[r]={loading:!0,error:!1},this.runScript(r,e=>{"Error"==typeof e&&(i[r].error=!0),i[r].loading=!1,t&&!(r=>{for(let t in r)if(r[t].loading)return!0;return!1})()&&t()})})},runScript:function(r,t=null,i=!1){this.log("Retrieving script: "+r),!t&&debug&&console.error("runscript needs a function callback"),services.fs.readFile(this.relativePath+"/scripts/"+r+".js",(r,e)=>{r?(debug&&console.error(r),i||t(new Error(r))):new Function(e)()(services,plugin).then(r=>t(r)).catch(r=>{plugin.breathecode.notify().error(r),i||t(new Error(r))})})}}).init(); \ No newline at end of file diff --git a/init/init-0.1.js b/init/init-0.1.js new file mode 100644 index 0000000..742e829 --- /dev/null +++ b/init/init-0.1.js @@ -0,0 +1,54 @@ +({ + name: 'c9-plugin', + version: '1.0', + remote: 'https://github.com/breatheco-de/c9-plugin.git', + absPath: '/home/ubuntu/workspace/c9-plugin', + relativePath: '~/c9-plugin', + debug: true, + log: function(elm){ if(this.debug) console.log(elm) }, + init: function() { + this.log('initializing...'); + plugin['breathecode'] = this; + this.runScript('get_utils', (utils) => { + plugin['breathecode'] = Object.assign(plugin['breathecode'], utils); + this.runScript('get_scripts', (scripts) => + this.runScripts(scripts.beforeMount, () => + this.runScript('sync_scripts', () => + this.runScripts(scripts.afterMount, () => this.log('All scripts done')) + ,true) + ) + ) + },true); + }, + runScripts: function(scripts, callback=null){ + let requests = {}; + const hasPending = (req) => { + for(let name in req) if(req[name].loading) return true; + return false; + }; + scripts.forEach((scriptName, currentScriptIndex) => { + requests[scriptName] = { loading: true, error: false }; + this.runScript(scriptName, (result) => { + if(typeof(result) == 'Error') requests[scriptName].error = true; + requests[scriptName].loading = false; + if(callback && !hasPending()) callback(); + }); + }); + }, + runScript: function(scriptName, callback=null, blocking=false){ + this.log('Retrieving script: '+scriptName); + if(!callback && debug) console.error("runscript needs a function callback"); + services.fs.readFile(this.relativePath+"/scripts/"+scriptName+".js", (err, data) => { + if (err){ + if(debug) console.error(err); + if(!blocking) callback(new Error(err)); + } + else (new Function(data)()(services, plugin)) + .then(res => callback(res)) + .catch(err => { + plugin['breathecode'].notify().error(err); + if(!blocking) callback(new Error(err)); + }); + }); + } +}).init(); \ No newline at end of file diff --git a/install b/install new file mode 100644 index 0000000..dea838c --- /dev/null +++ b/install @@ -0,0 +1,51 @@ +#!/bin/bash +. "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/autoexec" || exit 1 + +EXTENSION=". \"$BC_C9_LIBRARY/autoexec\"" + +BASHRC_FILEPATH="$HOME/.bashrc" +if grep -r "^$EXTENSION$" "$BASHRC_FILEPATH" > /dev/null; then + echo "$BC_C9_NAME: install: info: ~/.bashrc extension already installed. skipping." +else + echo "$EXTENSION" >> "$BASHRC_FILEPATH" + INSTALLED=1 + chmod +x -R $BC_C9_LIBRARY/utils +fi + +PROFILE_FILEPATH="$HOME/.profile" +if grep -r "^$EXTENSION$" "$PROFILE_FILEPATH" > /dev/null; then + echo "$BC_C9_NAME: install: info: ~/.profile extension already installed. skipping." +else + echo "$EXTENSION" >> "$PROFILE_FILEPATH" + INSTALLED=1 +fi + +if diff "$BC_C9_LIBRARY/assets/gitignore" "$HOME/.gitignore" > /dev/null; then + echo "$BC_C9_NAME: install: info: ~/.gitignore replacement already installed. skipping." +else + cp "$BC_C9_LIBRARY/assets/gitignore" "$HOME/.gitignore" + INSTALLED=1 +fi + + +if [ -d ~/.pyenv ]; + then + echo "$BC_C9_NAME: pyenv already installed. skipping." + else + curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash + echo "$BC_C9_NAME: installing pyenv" +fi + + +if git config core.filemode; then + git config core.filemode false + git config --global core.fileMode false + echo "Removing git fileMode check" + INSTALLED=1 +else + echo "$BC_C9_NAME: git config core.filemode was already false. skipping." +fi + +if ! [ -z "$INSTALLED" ]; then + printf "Installation finished.\nRun \"source \"$BC_C9_LIBRARY/autoexec\"\" or start a new terminal.\n" +fi diff --git a/scripts/_close-console.js b/scripts/_close-console.js new file mode 100644 index 0000000..1f77835 --- /dev/null +++ b/scripts/_close-console.js @@ -0,0 +1,8 @@ +return (services, plugin) => new Promise(function(resolve, reject){ + const tabs = services.console.getTabs(); + if(tabs.length>0){ + tabs[0].meta.$ignore = true; + tabs[0].close(); + } + resolve(); +}); diff --git a/scripts/_hello.js b/scripts/_hello.js new file mode 100644 index 0000000..3c64aba --- /dev/null +++ b/scripts/_hello.js @@ -0,0 +1,5 @@ +return (services, plugin) => new Promise(function(resolve, reject){ + reject('fake error'); + plugin['breathecode'].log('Hello....'); + console.log('Hello 2'); +}); \ No newline at end of file diff --git a/scripts/_menu.js b/scripts/_menu.js new file mode 100644 index 0000000..569a654 --- /dev/null +++ b/scripts/_menu.js @@ -0,0 +1,81 @@ +return (services, plugin) => new Promise(function(resolve, reject){ + const items = [ + { + path: "/Student Platform", + actions: { + onclick: () => plugin['breathecode'].openTab('https://student.breatheco.de') + } + }, + { + path: "/Browse Assets", + actions: { + onclick: () => plugin['breathecode'].openTab('https://breatheco.de/en/assets/') + } + }, + { + path: "/Slack Channel", + actions: { + onclick: () => plugin['breathecode'].openTab('https://4geeksacademy.slack.com/messages') + } + }, + { + path: "/~", + type: "divider" + }, + { + path: "/Boilerplates", + items: [ + { + path: "/vanilla-js", + actions: { + onclick: () => plugin['breathecode'].openTab('https://github.com/4GeeksAcademy/vanillajs-hello') + } + }, + { + path: "/react", + actions: { + onclick: () => plugin['breathecode'].openTab('https://github.com/4GeeksAcademy/react-hello') + } + }, + { + path: "/react-flux", + actions: { + onclick: () => plugin['breathecode'].openTab('https://github.com/4GeeksAcademy/react-hello-flux') + } + }, + { + path: "/django-rest", + actions: { + onclick: () => plugin['breathecode'].openTab('https://github.com/4GeeksAcademy/django-rest-hello') + } + } + ] + } + ]; + plugin['breathecode'].log('addMenus'); + // Add a custom top-level menu to the menu bar. + // Add commands and dividers to this menu. + var menuCaption = "Breathe Code"; // Menu caption. + var menus = services["menus"]; // Access the menu bar. + menus.remove('Support'); + menus.remove('Run'); + + var MenuItem = services.MenuItem; // Use this to create a menu item. + var Divider = services.Divider; // Use this to create a menu divider. + + // Set the top-level menu caption. + menus.setRootMenu(menuCaption, 900, plugin); + + //Adding menu options to the root Breathe Code Menu + const addMenuLevel = (parentPath, items) => { + items.forEach((item) => { + plugin['breathecode'].log("addItemByPath", item); + const type = (item.type == 'divider') ? new Divider() : new MenuItem(item.actions || {}); + menus.addItemByPath(parentPath + item.path, type, 100, plugin); + if(Array.isArray(item.items) && item.items.length>0) + addMenuLevel(parentPath + item.path, item.items); + }); + }; + addMenuLevel(menuCaption, items); + resolve(); +}); \ No newline at end of file diff --git a/scripts/get_scripts.js b/scripts/get_scripts.js new file mode 100644 index 0000000..bff1830 --- /dev/null +++ b/scripts/get_scripts.js @@ -0,0 +1,6 @@ +return (services, plugin) => new Promise(function(resolve, reject){ + resolve({ + beforeMount: ['_close-console'], + afterMount: [ '_menu'] + }); +}); \ No newline at end of file diff --git a/scripts/sync_scripts.js b/scripts/sync_scripts.js new file mode 100644 index 0000000..3945c64 --- /dev/null +++ b/scripts/sync_scripts.js @@ -0,0 +1,96 @@ +return (services, plugin) => new Promise(function(resolve, reject){ + const _private = { + init: function() { + this.doInstallOrUpgradeIfHostedWorkspace(); + }, + onError: function(err) { + plugin['breathecode'].log(err); + plugin['breathecode'].notify().error(plugin['breathecode'].name + ' failed: ' + err.message); + reject(err); + }, + doInstallOrUpgradeIfHostedWorkspace: function() { + plugin['breathecode'].log('doInstallOrUpgradeIfHostedWorkspace'); + + services.proc.execFile('sh', { + args: ['-c', 'echo -n "$C9_HOSTNAME"'], cwd: '/' + }, (err, stdout, stderr) => _private.doInstallOrUpgradeIfHostedWorkspaceCallback(err, stdout, stderr)); + }, + doInstallOrUpgradeIfHostedWorkspaceCallback: function(err, stdout, stderr) { + plugin['breathecode'].log('doInstallOrUpgradeIfHostedWorkspaceCallback'); + + if (err) { + return _private.onError(err); + } + + if (stdout.endsWith('.c9users.io')) { + _private.doInstallOrUpgrade(); + } + }, + doInstallOrUpgrade: function() { + plugin['breathecode'].log('doInstallOrUpgrade'); + + services.proc.execFile('test', { + args: ['-d', plugin['breathecode'].absPath], cwd: '/' + }, (err, stdout, stderr) => _private.doInstallOrUpgradeCallback(err, stdout, stderr)); + }, + doInstallOrUpgradeCallback: function(err, stdout, stderr) { + plugin['breathecode'].log('doInstallOrUpgradeCallback'); + + if (err && (err.code == 1)) { + // not installed + _private.doClone(); + } else if (err) { + // error + _private.onError(err); + } else { + // installed + _private.doUpgrade(); + } + }, + doClone: function(callback) { + plugin['breathecode'].log('doClone'); + + services.proc.execFile('git', { + args: ['clone', plugin['breathecode'].remote, plugin['breathecode'].absPath], cwd: '/' + }, (err) => _private.doCloneCallback(err)); + }, + doCloneCallback: function(err) { + plugin['breathecode'].log( 'doCloneCallback'); + if (err) return _private.onError( err); + _private.doInstall(plugin['breathecode']); + }, + doUpgrade: function() { + plugin['breathecode'].log('doUpgrade'); + + services.proc.execFile('git', { + args: ['pull'], cwd: plugin['breathecode'].absPath + }, (err) => { + const version = plugin['breathecode'].version || null; + if(!version) _private.doUpgradeCallback(err); + else services.proc.execFile('git', { + args: ['checkout tags/'+version], cwd: plugin['breathecode'].absPath + }, (err) => _private.doUpgradeCallback(err)); + }); + }, + doUpgradeCallback: function(err) { + plugin['breathecode'].log( 'doUpgradeCallback'); + if (err) return _private.onError( err); + _private.doInstall(plugin['breathecode']); + }, + doInstall: function() { + plugin['breathecode'].log( 'doInstall'); + + services.proc.execFile('bash', { + args: ['./install'], cwd: plugin['breathecode'].absPath + }, (err, stdout) => _private.doInstallCallback(err, stdout)); + }, + doInstallCallback: function(err, stdout) { + plugin['breathecode'].log( 'doInstallCallback'); + if (err) return _private.onError(err); + + resolve(); + } + }; + + _private.init(); +}); \ No newline at end of file