Skip to content

Commit

Permalink
Puede usar controladores stimulus de motores, y en modo desarrolo usa…
Browse files Browse the repository at this point in the history
… foreman y recarga viva
  • Loading branch information
vtamara committed Feb 22, 2022
1 parent 149015e commit dec0cbc
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ test/dummy/config/application.rb
test/dummy/public
test/dummy/app/assets/builds

app/javascript/controllers/sip
app/javascript/controllers/mr519_gen
app/javascript/controllers/heb412_gen
app/javascript/controllers/cor1440_gen
app/javascript/controllers/sal7711_gen
app/javascript/controllers/sivel2_gen
6 changes: 6 additions & 0 deletions test/dummy/.env.plantilla
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ if (test "$IPDES" = "") then {
if (test "$PUERTODES" = "") then {
export PUERTODES=3000
} fi;
if (test "nuevo.nocheyniebla.org" = "") then { # Conexión para recarga viva
export MAQRECVIVA="192.168.10.1"
} fi;
if (test "4600" = "") then { # Puerto para recarga viva
export PUERTORECVIVA=4500
} fi;


# Despliegue en modo producción con unicorn
Expand Down
2 changes: 2 additions & 0 deletions test/dummy/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rails: R=f bin/corre
js: yarn start --watch
1 change: 1 addition & 0 deletions test/dummy/app/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ document.addEventListener('turbo:load', (e) => {
sip_ejecutarAlCargarPagina(window)
})

import "./controllers"
9 changes: 9 additions & 0 deletions test/dummy/app/javascript/controllers/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Application } from "@hotwired/stimulus"

const application = Application.start()

// Configure Stimulus development experience
application.debug = false
window.Stimulus = application

export { application }
7 changes: 7 additions & 0 deletions test/dummy/app/javascript/controllers/hello_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
connect() {
this.element.textContent = "Hello World!"
}
}
8 changes: 8 additions & 0 deletions test/dummy/app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This file is auto-generated by ./bin/rails stimulus:manifest:update
// Run that command whenever you add a new controller or create them with
// ./bin/rails generate stimulus controllerName

import { application } from "./application"

import HelloController from "./hello_controller"
application.register("hello", HelloController)
36 changes: 23 additions & 13 deletions test/dummy/bin/corre
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/usr/bin/env ruby
# Ejecuta aplicación
# Ejecuta aplicación usando Procfile de ser posible
# Si hay un archivo .env toma variables de configuración de ese
# Las variables usadas son:
# RAILS_ENV: Modo de ejecución development, production (o test que no se usa)
# RC: Script de arranque en modo producción en /etc/rc.d/
# PUERTODES: En modo desarrollo puerto por usar
# IPDES: En modo desarrollo IP por usar
# R: Si tiene un valor ejecuta rápido (no instala dependencias ni elimina caches ni regenera recursos)
# Aunque si el valor es r recompila (sin eliminar dependencias)
# Y si el valor es f o si no hay Procfile no intenta correr con foreman

require 'fileutils'
#require 'byebug'
Expand Down Expand Up @@ -70,9 +72,13 @@ FileUtils.chdir RAIZ_AP do
end

if !ENV['R'] || ENV['R'] == '' || ENV['R'] == 'r'
puts "\n== Eliminando recursos =="
system! 'rm -rf public/packs/*'
system! "rm -rf public/#{ENV['RAILS_RELATIVE_URL_ROOT']}/assets/*"
puts "\n== Eliminando recursos public/packs* y public/#{ENV['RUTA_RELATIVA']}/assets/* =="
system! 'rm -rf public/packs*'
system! "rm -rf public/#{ENV['RUTA_RELATIVA']}/assets/*"
system! "rm -rf app/assets/builds/*"

puts "\n== Enlaza controladores stimulus de motores =="
system! 'bin/rails sip:stimulus_motores'

puts "\n== Crea recursos CSS y Javascript =="
system! "RAILS_ENV=#{ENV['RAILS_ENV']} bin/rails assets:precompile --trace"
Expand All @@ -81,16 +87,20 @@ FileUtils.chdir RAIZ_AP do
if ENV['RAILS_ENV'] == "development"
puts "\n== Ejecutando en modo desarrollo =="

ord = ""
if ENV['CONFIG_HOSTS'] && ENV['CONFIG_HOSTS'] != ''
ord += "CONFIG_HOSTS=\"#{ENV['CONFIG_HOSTS'].downcase}\" "
end
ord += "RAILS_RELATIVE_URL_ROOT='' "
ord += "bin/rails s "
if ENV['PUERTODES'] != ''
ord += "-p #{ENV['PUERTODES']} "
if File.exist?('Procfile') && ENV['R'] != 'f'
ord="foreman start -f Procfile"
else
ord = ""
if ENV['CONFIG_HOSTS'] && ENV['CONFIG_HOSTS'] != ''
ord += "CONFIG_HOSTS=\"#{ENV['CONFIG_HOSTS'].downcase}\" "
end
ord += "RAILS_RELATIVE_URL_ROOT='' "
ord += "bin/rails s "
if ENV['PUERTODES'] != ''
ord += "-p #{ENV['PUERTODES']} "
end
ord += "-b '#{ENV['IPDES']}' "
end
ord += "-b '#{ENV['IPDES']}' "
puts "#{ord}"
elsif ENV['RAILS_ENV'] == 'production'
if File.exists?('bin/corre-local')
Expand Down
19 changes: 18 additions & 1 deletion test/dummy/bin/detiene
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ if (test "$RAILS_ENV" = "production") then {
exit 1;
} fi;

doas sh /etc/rc.d/$RC -d stop
doas sh /etc/rc.d/$RC -d stop
} elif (test "$RAILS_ENV" = "development") then {
# Intentando detener rails
if (test -f tmp/pids/server.pid) then {
pp=`cat tmp/pids/server.pid`
ps -p $pp | grep "ruby: puma" > /dev/null
if (test "$?" = "0") then {
# Proceso activo y con ruby y puma seguramente es el de este
echo "Matando proceso con rails $pp"
kill -9 $pp
} fi;
} fi;
# Intentand detener proceso de recarga viva
pn=`fstat | grep $PUERTORECVIVA | sed -e "s/^[^\s]* node *\([0-9][0-9]*\) .*/\1/g" | sort -u`
if (test "$pn" != "") then {
echo "Matando proceso con node $pn"
kill -9 $pn
} fi;
} fi;

46 changes: 46 additions & 0 deletions test/dummy/esbuild-des.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Recarga viva (live reloading) durante desarrollo
// Recompila automáticamente ante cambios en javascript de app/javascript
// y refresca automáticamente en navegador
// Basado en https://www.colby.so/posts/live-reloading-with-esbuild-and-rails

const path = require('path')
const http = require('http')

const watch = process.argv.includes('--watch')
const clients = []

const watchOptions = {
onRebuild: (error, result) => {
if (error) {
console.error('Falló construcción:', error)
} else {
console.log('Construcción exitosa')
clients.forEach((res) => res.write('data: update\n\n'))
clients.length = 0
}
}
}

require("esbuild").build({
entryPoints: ["application.js"],
bundle: true,
preserveSymlinks: true,
outdir: path.join(process.cwd(), "app/assets/builds"),
absWorkingDir: path.join(process.cwd(), "app/javascript"),
watch: watch && watchOptions,
banner: {
js: ` (() => new EventSource("http://${process.env.MAQRECVIVA}:${process.env.PUERTORECVIVA}").onmessage = () => location.reload())();`,
},
}).catch(() => process.exit(1));

http.createServer((req, res) => {
return clients.push(
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Access-Control-Allow-Origin": "*",
Connection: "keep-alive",
}),
);
}).listen(process.env.PUERTORECVIVA, process.env.IPDES);

4 changes: 3 additions & 1 deletion test/dummy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"private": true,
"version": "0.1.0",
"scripts": {
"build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds"
"build": "esbuild app/javascript/*.* --preserve-symlinks --bundle --sourcemap --outdir=app/assets/builds",
"start": "node esbuild-des.config.js"
},
"dependencies": {
"@fortawesome/fontawesome-free": "^5.11.2",
"@hotwired/stimulus": "^3.0.1",
"@hotwired/turbo-rails": "^7.1.0",
"@popperjs/core": "^2.11.2",
"@rails/ujs": "^7.0.1",
Expand Down
5 changes: 5 additions & 0 deletions test/dummy/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free/-/fontawesome-free-5.15.4.tgz#ecda5712b61ac852c760d8b3c79c96adca5554e5"
integrity sha512-eYm8vijH/hpzr/6/1CJ/V/Eb1xQFW2nnUKArb3z+yUWv7HTwj6M7SP957oMjfZjAHU6qpoNc2wQvIxBLWYa/Jg==

"@hotwired/stimulus@^3.0.1":
version "3.0.1"
resolved "https://registry.yarnpkg.com/@hotwired/stimulus/-/stimulus-3.0.1.tgz#141f15645acaa3b133b7c247cad58ae252ffae85"
integrity sha512-oHsJhgY2cip+K2ED7vKUNd2P+BEswVhrCYcJ802DSsblJFv7mPFVk3cQKvm2vHgHeDVdnj7oOKrBbzp1u8D+KA==

"@hotwired/turbo-rails@^7.1.0":
version "7.1.1"
resolved "https://registry.yarnpkg.com/@hotwired/turbo-rails/-/turbo-rails-7.1.1.tgz#35c03b92b5c86f0137ed08bef843d955ec9bbe83"
Expand Down

0 comments on commit dec0cbc

Please sign in to comment.