sloop-base 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '039d060ff04d0f10736ca6b8cd6ad3989898378e3bb9eb817697e51c78603cd5'
4
+ data.tar.gz: 698ce459b4e7271b7c0c06f04cba3866db31366d2f8cdf5a9b8e4219bf78c9f1
5
+ SHA512:
6
+ metadata.gz: 390cbf41928524c47eac7bf5994a54872fb9f51c5558f82e457c08d6e5d7e2f470c60168321f2d5650c2f7f8f07d18a1235758cf183c2107f57872cc9e6d11d3
7
+ data.tar.gz: 4fb5a068ef11b9b003aae75b6d7ae461d6256bb777cfa400572ce5cea403752b254305135c9f0293932970c91a7541116ff44f6960388545ba0145dcc6e807d5
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # Sloop Base
2
+
3
+ Shared Rails application helpers for reading application configuration and
4
+ credentials.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "sloop-base"
12
+ ```
13
+
14
+ Then require it from the application:
15
+
16
+ ```ruby
17
+ require "sloop/base"
18
+ ```
19
+
20
+ The gem provides the top-level `App` and `Cred` helpers:
21
+
22
+ ```ruby
23
+ App[:name]
24
+ Cred[:database_password]
25
+ ```
26
+
27
+ ## Creating an application
28
+
29
+ With the gem and Rails installed, to change into the new application
30
+ automatically in Bash, add this line to
31
+ `~/.bashrc` and reload it (or run the line in your current shell):
32
+
33
+ ```bash
34
+ source "$(ruby -e 'print Gem::Specification.find_by_name("sloop-base").full_gem_path')/lib/sloop/base/sloop-base.bash"
35
+ ```
36
+
37
+ Then run:
38
+
39
+ ```sh
40
+ sloop-base init app_name
41
+ ```
42
+
43
+ This creates a Rails application with PostgreSQL, importmap and Bootstrap,
44
+ ignores `.idea/`, adds `sloop-base` to the Gemfile, runs `bundle install`, and installs a database
45
+ configuration based on the Menufio template. The Bash function changes into the application directory only on
46
+ success; without it, change directory manually. The configuration uses
47
+ `Cred[:db_user]` and `Cred[:db_pass]`; create those credentials with the
48
+ `cred` console command before accessing the database.
49
+ The warning includes an example with a randomly generated 24-character
50
+ production password; configure the PostgreSQL user with the same password.
51
+ After creating the credentials, create the database and start the server:
52
+
53
+ ```sh
54
+ rails db:create
55
+ rails s
56
+ ```
57
+
58
+ ## Releasing
59
+
60
+ Sign in to RubyGems with `gem signin`, update `lib/sloop/base/version.rb` to
61
+ an unpublished version, and run `rake release` from the repository. This builds
62
+ the gem and publishes it to RubyGems.org; the account must have permission to
63
+ publish `sloop-base`. Rake must be installed.
data/exe/sloop-base ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../lib/sloop/base/cli"
5
+
6
+ exit Sloop::Base::CLI.run(ARGV)
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class App
4
+ def self.[](name)
5
+ Rails.application.config_for(:application).fetch(name.to_sym)
6
+ end
7
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module Sloop
6
+ module Base
7
+ class CLI
8
+ def self.run(args)
9
+ unless args.length == 2 && args.first == "init" && args.last.match?(/\A[a-zA-Z][a-zA-Z0-9_-]*\z/)
10
+ warn "Usage: sloop-base init app_name"
11
+ return 1
12
+ end
13
+
14
+ app_name = args.last
15
+ if File.exist?(app_name) || File.symlink?(app_name)
16
+ warn "Error: #{app_name} already exists"
17
+ return 1
18
+ end
19
+
20
+ unless system("rails", "new", app_name, "-d", "postgresql", "--skip-docker", "--skip-kamal",
21
+ "--skip-ci", "-j", "importmap", "-c", "bootstrap")
22
+ warn "Error: rails new failed"
23
+ return 1
24
+ end
25
+
26
+ gitignore = File.join(app_name, ".gitignore")
27
+ content = File.exist?(gitignore) ? File.read(gitignore) : ""
28
+ unless content.lines.any? { |line| line.chomp == ".idea/" }
29
+ content += "\n" unless content.empty? || content.end_with?("\n")
30
+ File.write(gitignore, "#{content}.idea/\n")
31
+ end
32
+
33
+ template = File.read(File.join(__dir__, "templates", "database.yml"))
34
+ File.write(File.join(app_name, "config", "database.yml"), template.gsub("__APP_NAME__", app_name))
35
+
36
+ gemfile = File.join(app_name, "Gemfile")
37
+ gemfile_content = File.read(gemfile)
38
+ gemfile_content += "\n" unless gemfile_content.empty? || gemfile_content.end_with?("\n")
39
+ File.write(gemfile, "#{gemfile_content}gem \"sloop-base\", require: \"sloop/base\"\n")
40
+
41
+ unless system("bundle", "install", chdir: app_name)
42
+ warn "Error: bundle install failed in #{app_name}"
43
+ return 1
44
+ end
45
+
46
+ warn <<~WARNING
47
+ Warning: create the credentials Cred[:db_user] and Cred[:db_pass] using the console command 'cred':
48
+
49
+ development:
50
+ db_user: postgres
51
+ db_pass: postgres
52
+
53
+ production:
54
+ db_user: #{app_name}
55
+ db_pass: #{SecureRandom.alphanumeric(24)}
56
+
57
+ Once the credentials are configured, create the database and start the server:
58
+
59
+ rails db:create
60
+ rails s
61
+ WARNING
62
+ 0
63
+ rescue SystemCallError => e
64
+ warn "Error: #{e.message}"
65
+ 1
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Cred
4
+ def self.[](name)
5
+ env_result = Rails.application.credentials.dig(Rails.env.to_sym, name)
6
+ return Rails.application.credentials[name] unless env_result
7
+
8
+ env_result
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ sloop-base() {
2
+ if command sloop-base "$@"; then
3
+ if [[ $# -eq 2 && $1 == init ]]; then
4
+ builtin cd -- "./$2" || return
5
+ fi
6
+ else
7
+ return $?
8
+ fi
9
+ }
@@ -0,0 +1,35 @@
1
+ default: &default
2
+ adapter: postgresql
3
+ encoding: unicode
4
+ version: 19
5
+ port: 5432
6
+ host: localhost
7
+ username: <%= Cred[:db_user] %>
8
+ password: <%= Cred[:db_pass] %>
9
+ max_connections: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
10
+
11
+
12
+ development:
13
+ <<: *default
14
+ database: __APP_NAME___development
15
+
16
+ test:
17
+ <<: *default
18
+ database: __APP_NAME___test
19
+
20
+ production:
21
+ primary: &primary_production
22
+ <<: *default
23
+ database: __APP_NAME___production
24
+ cache:
25
+ <<: *primary_production
26
+ database: __APP_NAME___production_cache
27
+ migrations_paths: db/cache_migrate
28
+ queue:
29
+ <<: *primary_production
30
+ database: __APP_NAME___production_queue
31
+ migrations_paths: db/queue_migrate
32
+ cable:
33
+ <<: *primary_production
34
+ database: __APP_NAME___production_cable
35
+ migrations_paths: db/cable_migrate
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sloop
4
+ module Base
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
data/lib/sloop/base.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base/version"
4
+ require_relative "base/app"
5
+ require_relative "base/cred"
6
+
7
+ module Sloop
8
+ module Base
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sloop-base
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jorge Lebrijo
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: railties
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '7.0'
26
+ description: Provides the App and Cred helpers shared by Stableloop applications.
27
+ email:
28
+ - jlebrijo@gmail.com
29
+ executables:
30
+ - sloop-base
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - exe/sloop-base
36
+ - lib/sloop/base.rb
37
+ - lib/sloop/base/app.rb
38
+ - lib/sloop/base/cli.rb
39
+ - lib/sloop/base/cred.rb
40
+ - lib/sloop/base/sloop-base.bash
41
+ - lib/sloop/base/templates/database.yml
42
+ - lib/sloop/base/version.rb
43
+ homepage: https://github.com/jlebrijo/sloop-base
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 4.0.20
62
+ specification_version: 4
63
+ summary: Shared Rails application configuration and credentials helpers
64
+ test_files: []