snowpack 1.0.0.alpha2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +3 -0
  4. data/.tool-versions +1 -0
  5. data/.travis.yml +16 -0
  6. data/Brewfile +2 -0
  7. data/CHANGELOG.md +25 -0
  8. data/Gemfile +13 -0
  9. data/LICENSE +21 -0
  10. data/README.md +116 -0
  11. data/Rakefile +6 -0
  12. data/bin/bootstrap +25 -0
  13. data/exe/snowpack +7 -0
  14. data/lib/snowpack/application.rb +98 -0
  15. data/lib/snowpack/application_cli.rb +39 -0
  16. data/lib/snowpack/cli/application/cli.rb +32 -0
  17. data/lib/snowpack/cli/application/command.rb +53 -0
  18. data/lib/snowpack/cli/application/commands/assets/clobber.rb +31 -0
  19. data/lib/snowpack/cli/application/commands/assets/compile.rb +32 -0
  20. data/lib/snowpack/cli/application/commands/console.rb +55 -0
  21. data/lib/snowpack/cli/application/commands/db/create.rb +33 -0
  22. data/lib/snowpack/cli/application/commands/db/create_migration.rb +38 -0
  23. data/lib/snowpack/cli/application/commands/db/drop.rb +33 -0
  24. data/lib/snowpack/cli/application/commands/db/migrate.rb +47 -0
  25. data/lib/snowpack/cli/application/commands/db/reset.rb +28 -0
  26. data/lib/snowpack/cli/application/commands/db/rollback.rb +54 -0
  27. data/lib/snowpack/cli/application/commands/db/sample_data.rb +41 -0
  28. data/lib/snowpack/cli/application/commands/db/seed.rb +41 -0
  29. data/lib/snowpack/cli/application/commands/db/structure/dump.rb +38 -0
  30. data/lib/snowpack/cli/application/commands/db/utils/database.rb +76 -0
  31. data/lib/snowpack/cli/application/commands/db/utils/database_config.rb +49 -0
  32. data/lib/snowpack/cli/application/commands/db/version.rb +37 -0
  33. data/lib/snowpack/cli/application/commands/generate/slice.rb +32 -0
  34. data/lib/snowpack/cli/application/commands/routes/update.rb +35 -0
  35. data/lib/snowpack/cli/application/commands.rb +27 -0
  36. data/lib/snowpack/cli/command.rb +48 -0
  37. data/lib/snowpack/cli/standalone/cli.rb +14 -0
  38. data/lib/snowpack/cli/standalone/commands/new.rb +33 -0
  39. data/lib/snowpack/cli/standalone/commands.rb +13 -0
  40. data/lib/snowpack/components/formalist.rb +27 -0
  41. data/lib/snowpack/components/persistence.rb +56 -0
  42. data/lib/snowpack/components.rb +5 -0
  43. data/lib/snowpack/console/context.rb +32 -0
  44. data/lib/snowpack/console/plugins/relation_readers.rb +17 -0
  45. data/lib/snowpack/console/plugins/slice_readers.rb +33 -0
  46. data/lib/snowpack/generator.rb +57 -0
  47. data/lib/snowpack/generators/application/generator.rb +67 -0
  48. data/lib/snowpack/generators/application/templates/.env-example.tt +10 -0
  49. data/lib/snowpack/generators/application/templates/.env.test-example.tt +1 -0
  50. data/lib/snowpack/generators/application/templates/.gitignore +20 -0
  51. data/lib/snowpack/generators/application/templates/.rspec +2 -0
  52. data/lib/snowpack/generators/application/templates/Brewfile +4 -0
  53. data/lib/snowpack/generators/application/templates/Gemfile.tt +46 -0
  54. data/lib/snowpack/generators/application/templates/Guardfile +7 -0
  55. data/lib/snowpack/generators/application/templates/Procfile.dev +3 -0
  56. data/lib/snowpack/generators/application/templates/Procfile.support +1 -0
  57. data/lib/snowpack/generators/application/templates/README.md.tt +45 -0
  58. data/lib/snowpack/generators/application/templates/Rakefile +5 -0
  59. data/lib/snowpack/generators/application/templates/bin/run +7 -0
  60. data/lib/snowpack/generators/application/templates/config/application.rb.tt +19 -0
  61. data/lib/snowpack/generators/application/templates/config/puma.rb.tt +14 -0
  62. data/lib/snowpack/generators/application/templates/config/routes.rb.tt +15 -0
  63. data/lib/snowpack/generators/application/templates/config.ru +5 -0
  64. data/lib/snowpack/generators/application/templates/db/migrate/.keep +0 -0
  65. data/lib/snowpack/generators/application/templates/db/sample_data.rb +1 -0
  66. data/lib/snowpack/generators/application/templates/db/seed.rb +1 -0
  67. data/lib/snowpack/generators/application/templates/lib/__application_path__/operation.rb.tt +12 -0
  68. data/lib/snowpack/generators/application/templates/lib/__application_path__/persistence/relations/.keep +0 -0
  69. data/lib/snowpack/generators/application/templates/lib/__application_path__/types.rb.tt +11 -0
  70. data/lib/snowpack/generators/application/templates/lib/__application_path__/web/action.rb.tt +56 -0
  71. data/lib/snowpack/generators/application/templates/lib/__application_path__/web/view/context.rb.tt +31 -0
  72. data/lib/snowpack/generators/application/templates/lib/hanami/action/csrf_protection.rb +225 -0
  73. data/lib/snowpack/generators/application/templates/log/.keep +0 -0
  74. data/lib/snowpack/generators/application/templates/package.json.tt +25 -0
  75. data/lib/snowpack/generators/application/templates/public/.keep +0 -0
  76. data/lib/snowpack/generators/application/templates/script/bootstrap +32 -0
  77. data/lib/snowpack/generators/application/templates/script/console +26 -0
  78. data/lib/snowpack/generators/application/templates/script/server +13 -0
  79. data/lib/snowpack/generators/application/templates/script/setup +27 -0
  80. data/lib/snowpack/generators/application/templates/script/support +9 -0
  81. data/lib/snowpack/generators/application/templates/script/test +19 -0
  82. data/lib/snowpack/generators/application/templates/script/update +13 -0
  83. data/lib/snowpack/generators/application/templates/spec/spec_helper.rb +13 -0
  84. data/lib/snowpack/generators/application/templates/spec/suite/.keep +0 -0
  85. data/lib/snowpack/generators/application/templates/spec/support/suite.rb +9 -0
  86. data/lib/snowpack/generators/application/templates/system/__application_path__/import.rb.tt +5 -0
  87. data/lib/snowpack/generators/application/templates/system/boot/assets.rb.tt +17 -0
  88. data/lib/snowpack/generators/application/templates/system/boot/logger.rb.tt +10 -0
  89. data/lib/snowpack/generators/application/templates/system/boot/monitor.rb.tt +9 -0
  90. data/lib/snowpack/generators/application/templates/system/boot/persistence.rb.tt +9 -0
  91. data/lib/snowpack/generators/application/templates/system/boot/settings.rb.tt +18 -0
  92. data/lib/snowpack/generators/application/templates/system/boot/web.rb.tt +9 -0
  93. data/lib/snowpack/generators/slice/generator.rb +62 -0
  94. data/lib/snowpack/generators/slice/templates/lib/__slice_path__/.keep +0 -0
  95. data/lib/snowpack/generators/slice/templates/lib/__slice_path__/web/action.rb.tt +10 -0
  96. data/lib/snowpack/generators/slice/templates/lib/__slice_path__/web/actions/.keep +0 -0
  97. data/lib/snowpack/generators/slice/templates/lib/__slice_path__/web/view.rb.tt +17 -0
  98. data/lib/snowpack/generators/slice/templates/lib/__slice_path__/web/views/.keep +0 -0
  99. data/lib/snowpack/generators/slice/templates/system/__slice_path__/import.rb.tt +9 -0
  100. data/lib/snowpack/generators/slice/templates/system/__slice_path__/slice.rb.tt +10 -0
  101. data/lib/snowpack/instrumentation/appsignal/appsignal_ext.rb +13 -0
  102. data/lib/snowpack/instrumentation/appsignal/que.rb +70 -0
  103. data/lib/snowpack/instrumentation/appsignal/rack.rb +84 -0
  104. data/lib/snowpack/roda/web.rb +47 -0
  105. data/lib/snowpack/slice.rb +85 -0
  106. data/lib/snowpack/test/suite.rb +164 -0
  107. data/lib/snowpack/test/tasks.rake +40 -0
  108. data/lib/snowpack/test_tasks.rb +4 -0
  109. data/lib/snowpack/types.rb +9 -0
  110. data/lib/snowpack/version.rb +5 -0
  111. data/lib/snowpack/view/part_builder.rb +45 -0
  112. data/lib/snowpack/view/parts/pager.rb +107 -0
  113. data/lib/snowpack/view/parts/paginated.rb +22 -0
  114. data/lib/snowpack/web/application.rb +38 -0
  115. data/lib/snowpack/web/assets.rb +56 -0
  116. data/lib/snowpack/web/endpoint_resolver.rb +66 -0
  117. data/lib/snowpack/web/form.rb +38 -0
  118. data/lib/snowpack/web/plugin.rb +44 -0
  119. data/lib/snowpack/web/rack_logger.rb +73 -0
  120. data/lib/snowpack/web/router.rb +58 -0
  121. data/lib/snowpack/web.rb +20 -0
  122. data/lib/snowpack.rb +27 -0
  123. data/snowpack.gemspec +34 -0
  124. metadata +327 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 117edd0edd3fc41b750a9123cc92c8d7629f6d07273b92782716fbb273dc1994
4
+ data.tar.gz: 19d7853057dd9ab75dc9e2de63b2cb7c4f17dceb7135abcb677c9ec598326460
5
+ SHA512:
6
+ metadata.gz: 35241faca184cdb00a5ccf8061f3b4df83760c1afbcf282cea88ab9deb6923d81c49aa43f7306a283f215a6b498044bafd9dbb413338d0b5e0e81bc67513e6b8
7
+ data.tar.gz: 3b1b3308b56b87579230df9c7f59a18598953ca296cadfba884a7fd36009ec3be29a23572df88151af9d52c946db65ec77446e44c816fdd56916747677558cab
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ # dummy app files created in specs
15
+ spec/dummy/db/migrate/*
16
+ !spec/dummy/db/migrate/.keep
17
+ spec/dummy/db/structure.sql
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --order random
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 2.6.2
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ dist: trusty
2
+ sudo: required
3
+ language: ruby
4
+ cache: bundler
5
+ addons:
6
+ postgresql: 9.5
7
+ services:
8
+ - postgresql
9
+ script:
10
+ - bundle exec rake spec
11
+ rvm:
12
+ - 2.6.2
13
+ notifications:
14
+ on_success: false
15
+ on_failure: false
16
+ on_start: false
data/Brewfile ADDED
@@ -0,0 +1,2 @@
1
+ tap "icelab/devtools"
2
+ brew "asdf"
data/CHANGELOG.md ADDED
@@ -0,0 +1,25 @@
1
+ # Changelog
2
+
3
+ ## [1.0.0.alpha2] - 2019-07-01 [YANKED]
4
+
5
+ ### Changed
6
+
7
+ - Fix case of gem name (now "snowpack", fully lowercase)
8
+
9
+ ## [1.0.0.alpha1] - 2019-07-01 [YANKED]
10
+
11
+ ### Added
12
+
13
+ - Introduce app/slices structure via `Snowpack::Application` and `Snowpack::Slice`
14
+ - Introduce `Snowpack::Web` module for holding route definitions and preparing rack app
15
+ - Introduce `Snowpack::Web::Application` and `Snowpack::Web::Plugin` dry-system plugin, which provide a rack-mountable web application (and supporting infrastructure) depending upon hanami-router (unstable branch) for routing and hanami-controller (unstable branch) for HTTP endpoint handling
16
+ - Introduce `snowpack` CLI with `new` command for generating new applications
17
+ - Add `generate slice` application CLI command
18
+
19
+ ### Changed
20
+
21
+ - Rebuild application CLI using hanami-cli
22
+
23
+ ### Removed
24
+
25
+ - Remove repo readers from console environment
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ gem "hanami-cli", github: "hanami/cli", branch: "unstable"
8
+
9
+ gem "rom", github: "rom-rb/rom"
10
+ gem "rom-sql", github: "rom-rb/rom-sql"
11
+ gem "pg"
12
+
13
+ gem "pry-byebug"
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-2019 Icelab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # Disclaimer
2
+
3
+ **This project is not intended for public usage and is not open for contributions. Icelab takes no responsibility for usage in your projects**.
4
+
5
+ # Snowpack
6
+
7
+ [![Build Status](https://travis-ci.org/icelab/snowpack.svg?branch=master)](https://travis-ci.org/icelab/snowpack)
8
+
9
+ Snowpack is lightwight application framework for Icelab’s Ruby applications. It builds upon [dry-system][dry-system], and integrates Hanami [router][hanami-router]/[controller][hanami-controller] (unstable branches) for HTTP request handling, and [rom-rb] for database persistence.
10
+
11
+ [dry-system]: https://github.com/dry-rb/dry-system
12
+ [hanami-router]: https://github.com/hanami/router
13
+ [hanami-controller]: https://github.com/hanami/controller
14
+ [rom-rb]: https://rom-rb.org/
15
+
16
+ ## Getting started
17
+
18
+ Install the gem:
19
+
20
+ ```
21
+ gem install snowpack -v "1.0.0.alpha1"
22
+ ```
23
+
24
+ Generate a new application:
25
+
26
+ ```
27
+ snowpack new my_app
28
+ ```
29
+
30
+ Then enter the application and (if required) generate a new slice:
31
+
32
+ ```
33
+ ./bin/run generate slice main
34
+ ```
35
+
36
+ ## CLI
37
+
38
+ Snowpack’ application CLI is accessible via the `bin/run` executable, which provides access to various commands. To learn about these, simply run:
39
+
40
+ ```
41
+ $ ./bin/run
42
+ Commands:
43
+ run assets [SUBCOMMAND]
44
+ run console # Open interactive console
45
+ run db [SUBCOMMAND]
46
+ run generate [SUBCOMMAND]
47
+ ```
48
+
49
+ To learn about sub-commands, run the parent command:
50
+
51
+ ```
52
+ $ ./bin/run db
53
+ Commands:
54
+ run db create # Create database
55
+ run db create_migration [NAME] # Create new migration file
56
+ run db drop # Delete database
57
+ run db migrate # Migrates database
58
+ run db reset # Drop, create, and migrate database
59
+ run db rollback # Rollback database to a previous migration
60
+ run db sample_data # Load sample data
61
+ run db seed # Load database seeds
62
+ run db structure [SUBCOMMAND]
63
+ run db version # Print schema version
64
+ ```
65
+
66
+ To learn about any specific command, pass the `-h` or `--help` option:
67
+
68
+ ```
69
+ $ ./bin/run db migrate --help
70
+ Command:
71
+ run db migrate
72
+
73
+ Usage:
74
+ run db migrate
75
+
76
+ Description:
77
+ Migrates database
78
+
79
+ Options:
80
+ --env=VALUE, -e VALUE # Application environment
81
+ --target=VALUE, -t VALUE # Target migration number
82
+ --help, -h # Print this help
83
+ ```
84
+
85
+ ## Console
86
+
87
+ An application console is available via `bin/run console`. It provides convenient access to various components:
88
+
89
+ ```
90
+ => starting my_app[development] console
91
+ => my_app[development] booted in in 0.4s
92
+ my_app[development]>
93
+ ```
94
+
95
+ ### Console helpers
96
+
97
+ The console offers convenient access to all rom-rb relations:
98
+
99
+ ```
100
+ my_app[development]> users
101
+ => #<Persistence::Relations::Users dataset=#<Sequel::Postgres::Dataset: "SELECT \"id\", \"email\", \"encrypted_password\", \"access_token\", \"access_token_expiration\", \"active\", \"created_at\", \"updated_at\", \"name\" FROM \"users\" ORDER BY \"users\".\"id\"">>
102
+ ```
103
+
104
+ As well as all slices:
105
+
106
+ ```
107
+ my_app[development]> main
108
+ => MyApp::Main::Slice
109
+ ```
110
+
111
+ As well as any root-level registrations on the slices:
112
+
113
+ ```
114
+ my_app[development]> main.user_repo
115
+ # => #<MyApp::Main::UserRepo struct_namespace=MyApp::Main::Entities auto_struct=true>
116
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/bootstrap ADDED
@@ -0,0 +1,25 @@
1
+ #!/bin/sh
2
+ # bootstrap script - install or update project dependencies
3
+ set -e
4
+
5
+ cd "$(dirname "$0")/.."
6
+
7
+ if [ "$(uname -s)" = "Darwin" ]; then
8
+ brew bundle check >/dev/null 2>&1 || {
9
+ echo "==> Installing Homebrew dependencies…"
10
+ brew bundle
11
+ }
12
+
13
+ echo "==> Installing package versions…"
14
+ brew bootstrap-asdf
15
+ fi
16
+
17
+ bundle check >/dev/null 2>&1 || {
18
+ echo "==> Installing gem dependencies…"
19
+ bundle install --quiet
20
+ }
21
+
22
+ bundle check >/dev/null 2>&1 || {
23
+ echo "==> Installing node packages…"
24
+ yarn --silent
25
+ }
data/exe/snowpack ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "snowpack/cli/standalone/cli"
6
+
7
+ Snowpack::CLI::Standalone::CLI.new.call
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/inflector"
4
+ require "dry/monitor" # from dry-web
5
+ require "dry/system/container"
6
+ require "dry/system/components"
7
+ require "pathname"
8
+ require_relative "../snowpack"
9
+
10
+ Dry::Monitor.load_extensions :rack # from dry-web
11
+
12
+ module Snowpack
13
+ class Application < Dry::System::Container
14
+ setting :inflector, Dry::Inflector.new, reader: true
15
+ setting :slices_dir, "slices"
16
+
17
+ use :env, inferrer: -> { ENV.fetch("RACK_ENV", "development").to_sym }
18
+ use :logging
19
+ use :notifications
20
+ use :monitoring
21
+
22
+ @_mutex = Mutex.new
23
+
24
+ def self.inherited(klass)
25
+ super
26
+
27
+ klass.after :configure do
28
+ register_inflector
29
+ load_paths! "lib"
30
+ end
31
+
32
+ @_mutex.synchronize do
33
+ Snowpack.application = klass
34
+ end
35
+ end
36
+
37
+ def self.slices
38
+ @slices ||= load_slices
39
+ end
40
+
41
+ def self.load_slices
42
+ @slices ||= Dir[File.join(config.root, config.slices_dir, "*")]
43
+ .map(&method(:load_slice))
44
+ .compact
45
+ .to_h
46
+ end
47
+
48
+ # We can't call this `.boot` because it is the name used for registering
49
+ # bootable components. (It would be good to change that)
50
+ def self.boot!
51
+ return self if booted?
52
+
53
+ finalize! freeze: false
54
+
55
+ load_slices
56
+ slices.values.each(&:boot!)
57
+
58
+ @booted = true
59
+
60
+ freeze
61
+ self
62
+ end
63
+
64
+ def self.booted?
65
+ @booted.equal?(true)
66
+ end
67
+
68
+ MODULE_DELIMITER = "::"
69
+
70
+ def self.module
71
+ inflector.constantize(name.split(MODULE_DELIMITER)[0..-2].join(MODULE_DELIMITER))
72
+ end
73
+
74
+ private
75
+
76
+ def self.load_slice(base_path)
77
+ base_path = Pathname(base_path)
78
+ full_defn_path = Dir["#{base_path}/system/**/slice.rb"].first
79
+
80
+ return unless full_defn_path
81
+
82
+ require full_defn_path
83
+
84
+ const_path = Pathname(full_defn_path)
85
+ .relative_path_from(base_path.join("system")).to_s
86
+ .yield_self { |path| path.sub(/#{File.extname(path)}$/, "") }
87
+
88
+ const = inflector.constantize(inflector.camelize(const_path))
89
+
90
+ [File.basename(base_path).to_sym, const]
91
+ end
92
+
93
+ def self.register_inflector
94
+ return self if key?(:inflector)
95
+ register :inflector, inflector
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/cli"
4
+ require_relative "../snowpack"
5
+ require_relative "cli/command"
6
+ require_relative "cli/commands"
7
+
8
+ module Snowpack
9
+ class ApplicationCLI < Hanami::CLI
10
+ def self.application
11
+ if Snowpack.application?
12
+ Snowpack.application
13
+ else
14
+ nil
15
+ end
16
+ end
17
+
18
+ attr_reader :application
19
+
20
+ def initialize(application: self.class.application, commands: Commands)
21
+ super(commands)
22
+ @application = application
23
+ end
24
+
25
+ private
26
+
27
+ # TODO: we should make a prepare_command method upstream
28
+ def parse(result, out)
29
+ command, arguments = super
30
+
31
+ if command.respond_to?(:with_application)
32
+ application.config.env = arguments[:env] if arguments[:env]
33
+ [command.with_application(application), arguments]
34
+ else
35
+ [command, arguments]
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,32 @@
1
+ require "hanami/cli"
2
+ require "snowpack"
3
+ require_relative "commands"
4
+
5
+ module Snowpack
6
+ module CLI
7
+ module Application
8
+ class CLI < Hanami::CLI
9
+ attr_reader :application
10
+
11
+ def initialize(application: Snowpack.application, commands: Commands)
12
+ super(commands)
13
+ @application = application
14
+ end
15
+
16
+ private
17
+
18
+ # TODO: we should make a prepare_command method upstream
19
+ def parse(result, out)
20
+ command, arguments = super
21
+
22
+ if command.respond_to?(:with_application)
23
+ application.config.env = arguments[:env] if arguments[:env]
24
+ [command.with_application(application), arguments]
25
+ else
26
+ [command, arguments]
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../command"
4
+
5
+ module Snowpack
6
+ module CLI
7
+ module Application
8
+ class Command < Snowpack::CLI::Command
9
+ def self.inherited(klass)
10
+ super
11
+
12
+ klass.option :env, aliases: ["-e"], default: nil, desc: "Application environment"
13
+ end
14
+
15
+ attr_reader :application
16
+
17
+ def initialize(application: nil, **opts)
18
+ super(**opts)
19
+ @application = application
20
+ end
21
+
22
+ # TODO: inject application inflector
23
+ def with_application(application)
24
+ self.class.new(
25
+ command_name: @command_name,
26
+ application: application,
27
+ out: out,
28
+ files: files,
29
+ )
30
+ end
31
+
32
+ private
33
+
34
+ def run_command(klass, *args)
35
+ klass.new(
36
+ command_name: klass.name,
37
+ application: application,
38
+ out: out,
39
+ files: files,
40
+ ).call(*args)
41
+ end
42
+
43
+ def measure(desc, &block)
44
+ start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
45
+ block.call
46
+ stop = Process.clock_gettime(Process::CLOCK_MONOTONIC)
47
+
48
+ out.puts "=> #{desc} in #{(stop - start).round(1)}s"
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/cli"
4
+ require "snowpack/cli/application/command"
5
+
6
+ module Snowpack
7
+ module CLI
8
+ module Application
9
+ module Commands
10
+ module Assets
11
+ class Clobber < Command
12
+ desc "Delete compiled assets"
13
+
14
+ def call(**)
15
+ FileUtils.rm_rf(compiled_assets_path)
16
+ out.puts "=> assets removed from #{compiled_assets_path}"
17
+ end
18
+
19
+ private
20
+
21
+ def compiled_assets_path
22
+ File.join(application.root, "public/assets")
23
+ end
24
+ end
25
+ end
26
+
27
+ register "assets clobber", Assets::Clobber
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/cli"
4
+ require "snowpack/cli/application/command"
5
+
6
+ module Snowpack
7
+ module CLI
8
+ module Application
9
+ module Commands
10
+ module Assets
11
+ class Compile < Command
12
+ desc "Compiles assets"
13
+
14
+ def call(**)
15
+ measure "assets compiled into #{compiled_assets_path}" do
16
+ `yarn run build-production` # TODO use shell helper here
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def compiled_assets_path
23
+ File.join(application.root, "public/assets")
24
+ end
25
+ end
26
+ end
27
+
28
+ register "assets compile", Assets::Compile, aliases: ["precompile"]
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/cli"
4
+ require "snowpack/cli/application/command"
5
+ require "snowpack/console/context"
6
+
7
+ module Snowpack
8
+ module CLI
9
+ module Application
10
+ module Commands
11
+ class Console < Command
12
+ REPL = begin
13
+ require 'pry'
14
+ Pry
15
+ rescue LoadError
16
+ require 'irb'
17
+ IRB
18
+ end
19
+
20
+ desc "Open interactive console"
21
+
22
+ def call(**)
23
+ measure "#{prompt_prefix} booted in" do
24
+ out.puts "=> starting #{prompt_prefix} console"
25
+ application.boot!
26
+ end
27
+
28
+ start_repl
29
+ end
30
+
31
+ private
32
+
33
+ def start_repl
34
+ context = Snowpack::Console::Context.new(application)
35
+ REPL.start(context, prompt: [proc { default_prompt }, proc { indented_prompt }])
36
+ end
37
+
38
+ def default_prompt
39
+ "#{prompt_prefix}> "
40
+ end
41
+
42
+ def indented_prompt
43
+ "#{prompt_prefix}* "
44
+ end
45
+
46
+ def prompt_prefix
47
+ "#{inflector.underscore(application.config.name)}[#{application.env}]"
48
+ end
49
+ end
50
+
51
+ register "console", Console
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "snowpack/cli/application/command"
4
+ require_relative "utils/database_config"
5
+
6
+ module Snowpack
7
+ module CLI
8
+ module Application
9
+ module Commands
10
+ module DB
11
+ class Create < Command
12
+ desc "Create database"
13
+
14
+ def call(**)
15
+ db_name = database_config.db_name
16
+
17
+ system(database_config.cli_env_vars, "createdb #{Shellwords.escape(db_name)}")
18
+ out.puts "=> database #{db_name} created" if $?.success?
19
+ end
20
+
21
+ private
22
+
23
+ def database_config
24
+ @database_config ||= Utils::DatabaseConfig.for_application(application)
25
+ end
26
+ end
27
+ end
28
+
29
+ register "db create", DB::Create
30
+ end
31
+ end
32
+ end
33
+ end