Skip to content

Commit

Permalink
Merge pull request #1172 from eip-ewi/better-console
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtOfCode- authored Aug 11, 2023
2 parents 2c8aabd + 116f0ea commit 85acba1
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ qpixel-import.tar.gz
*.swp

dump.rdb

# Ignore IRB files
.irbrc
.irb_history
1 change: 1 addition & 0 deletions .sample.irbrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Qpixel.irb! if defined?(Qpixel)
6 changes: 5 additions & 1 deletion INSTALLATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ Set up the database:
rails r db/scripts/create_tags_path_view.rb
rails db:migrate

We also recommend you load the QPixel console extensions for easier development:

cp .sample.irbrc .irbrc

You'll need to create a Community record and purge the Rails cache before you can seed the database.
In a Rails console (`rails c`), run:

Expand All @@ -138,7 +142,7 @@ Community.create(name: 'Dev Community', host: 'localhost:3000')
Rails.cache.clear
```

After that you can call `rails db:seed` to fill the database with necessary seed data, such as settings, help posts and default templates. (If you are preparing a production deployment, you might choose to edit some of the help seeds first. See "Help Topics" at the end of this guide.)
After that you can run `rails db:seed` to fill the database with necessary seed data, such as settings, help posts and default templates. (If you are preparing a production deployment, you might choose to edit some of the help seeds first. See "Help Topics" at the end of this guide.)

$ rails db:seed
Category: Created 2, skipped 0
Expand Down
5 changes: 5 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@ class Application < Rails::Application
Devise::Mailer.helper :users
Devise::Mailer.layout 'devise_mailer'
end

console do
require 'console_extension'
include ConsoleExtension
end
end
end
1 change: 1 addition & 0 deletions docker/local-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ cp ./docker/dummy.env ./docker/env
cp ./docker/compose-env .env
cp config/database.docker.yml config/database.yml
cp config/storage.docker.yml config/storage.yml
cp ./.irbrc.sample ./.irbrc
106 changes: 106 additions & 0 deletions lib/console_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
module ConsoleExtension
extend ActiveSupport::Concern

included do
console do
Rails.logger.silence do
console_init_community
end
end
end

def console_init_community
community_count = Community.count
if community_count.zero?
puts "\e[31m(!) You have not yet created any communities.\e[0m"
puts 'Create a community by entering:'
puts ''
puts " Community.create(name: 'my community name', host: 'my.site.com')"
puts ' Rails.cache.clear'
puts ''

if Rails.env.development?
begin
port = Rails::Server::Options.new.parse!(ARGV)[:Port] || 3000
rescue
port = 3000
end
puts "Since you are running in development mode, you probably want to set host to localhost:#{port}"
puts ''
elsif Rails.env.production?
puts 'Since you are running in production mode, set host to your fully qualified domain name without http(s).'
puts 'For example, if you host your site at https://meta.codidact.org, set host to meta.codidact.org'
puts ''
end
puts 'For more information, see the set up instructions.'
elsif community_count == 1
community = Community.first
RequestContext.community = community
puts "\e[32m(!) Found one community, set current community to #{community.name} @ #{community.host}\e[0m"
else
community = Community.find_by(host: 'localhost:3000') if Rails.env.development?
community ||= Community.first
RequestContext.community = community
puts "\e[32m(!) Found multiple communities, set current community to #{community.name} @ #{community.host}\e[0m"
puts ''
puts 'You can change your current community by entering:'
puts ''
puts ' RequestContext.community = Community.find_by(...)'
puts ''
puts "You can use `host: 'my.host'` or `name: 'community name'` in place of the dots"
end
puts ''
rescue
puts "\e[31m(!) Unable to load communities. Is your database configuration correct?\e[0m"
end
end

# Create module that can be included in the .irbrc:
#
# Qpixel.irb! if defined?(Qpixel)
module Qpixel
def self.irb!
IRB::Irb.class_eval do
private

def self.rails_environment
case Rails.env
when 'development'
"\e[32mdev\e[0m"
when 'production'
"\e[31mprod\e[0m"
when 'test'
"\e[32mtest\e[0m"
when 'staging'
"\e[32mstag\e[0m"
else
"\e[31m#{Rails.env}\e[0m"
end
end

def self.qpixel_prompt
c = RequestContext.community
"[#{rails_environment}] [\e[34m#{c&.name || '-'} @ #{c&.host || '-'}\e[0m]"
end
end

IRB::Irb.class_eval do
# Define an alternative string dup method which will redetermine the prompt part if community changes
qpixel_block = proc do |s|
def s.dup
IRB::Irb.qpixel_prompt + self
end
end

IRB.conf[:PROMPT][:QPIXEL] = {
PROMPT_I: ':%03n> '.tap(&qpixel_block),
PROMPT_N: ':%03n> '.tap(&qpixel_block),
PROMPT_S: ':%03n%l '.tap(&qpixel_block),
PROMPT_C: ':%03n* '.tap(&qpixel_block),
RETURN: IRB.conf[:PROMPT][:DEFAULT][:RETURN]
}

IRB.conf[:PROMPT_MODE] = :QPIXEL
end
end
end

0 comments on commit 85acba1

Please sign in to comment.