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
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "snowpack/cli/application/command"
4
+ require_relative "structure/dump"
5
+ require_relative "utils/database"
6
+
7
+ module Snowpack
8
+ module CLI
9
+ module Application
10
+ module Commands
11
+ module DB
12
+ class CreateMigration < Command
13
+ desc "Create new migration file"
14
+
15
+ argument :name, desc: "Migration file name"
16
+
17
+ def call(name:, **)
18
+ migrator = database.migrator
19
+ version = migrator.generate_version
20
+
21
+ measure "migration #{version}_#{name} created" do
22
+ migrator.create_file(name, version)
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def database
29
+ @database ||= Utils::Database.for_application(application)
30
+ end
31
+ end
32
+ end
33
+
34
+ register "db create_migration", DB::CreateMigration
35
+ end
36
+ end
37
+ end
38
+ 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 Drop < Command
12
+ desc "Delete database"
13
+
14
+ def call(**)
15
+ db_name = database_config.db_name
16
+
17
+ system(database_config.cli_env_vars, "dropdb #{Shellwords.escape(db_name)}")
18
+ out.puts "=> database #{db_name} dropped" 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 drop", Commands::DB::Drop
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "snowpack/cli/application/command"
4
+ require_relative "structure/dump"
5
+ require_relative "utils/database"
6
+
7
+ module Snowpack
8
+ module CLI
9
+ module Application
10
+ module Commands
11
+ module DB
12
+ class Migrate < Command
13
+ desc "Migrates database"
14
+
15
+ option :target, desc: "Target migration number", aliases: ["-t"]
16
+
17
+ def call(target: nil, **)
18
+ measure "database #{database.name} migrated" do
19
+ if target
20
+ run_migrations(target: target)
21
+ else
22
+ run_migrations
23
+ end
24
+ end
25
+
26
+ run_command Structure::Dump
27
+ end
28
+
29
+ private
30
+
31
+ def run_migrations(**options )
32
+ ROM::SQL.with_gateway(database.gateway) do
33
+ database.migrator.run(options)
34
+ end
35
+ end
36
+
37
+ def database
38
+ @database ||= Utils::Database.for_application(application)
39
+ end
40
+ end
41
+ end
42
+
43
+ register "db migrate", DB::Migrate
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "snowpack/cli/application/command"
4
+ require_relative "drop"
5
+ require_relative "create"
6
+ require_relative "migrate"
7
+
8
+ module Snowpack
9
+ module CLI
10
+ module Application
11
+ module Commands
12
+ module DB
13
+ class Reset < Command
14
+ desc "Drop, create, and migrate database"
15
+
16
+ def call(**)
17
+ run_command Drop
18
+ run_command Create
19
+ run_command Migrate
20
+ end
21
+ end
22
+ end
23
+
24
+ register "db reset", Commands::DB::Reset
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "snowpack/cli/application/command"
4
+ require_relative "structure/dump"
5
+ require_relative "utils/database"
6
+
7
+ module Snowpack
8
+ module CLI
9
+ module Application
10
+ module Commands
11
+ module DB
12
+ class Rollback < Command
13
+ desc "Rollback database to a previous migration"
14
+
15
+ option :target, desc: "Target migration number", aliases: ["-t"]
16
+
17
+ def call(target: nil, **)
18
+ migration_code, migration_name = find_migration(target)
19
+
20
+ measure "database #{database.name} rolled back to #{migration_name}" do
21
+ database.gateway.run_migrations(target: Integer(migration_code))
22
+ end
23
+
24
+ run_command Structure::Dump
25
+ end
26
+
27
+ private
28
+
29
+ def database
30
+ @database ||= Utils::Database.for_application(application)
31
+ end
32
+
33
+ def find_migration(code)
34
+ migration = database.applied_migrations.yield_self { |migrations|
35
+ if code
36
+ migrations.detect { |m| m.split("_").first == code }
37
+ else
38
+ migrations.last
39
+ end
40
+ }
41
+
42
+ migration_code = code || migration.split("_").first
43
+ migration_name = File.basename(migration, ".*")
44
+
45
+ [migration_code, migration_name]
46
+ end
47
+ end
48
+ end
49
+
50
+ register "db rollback", Commands::DB::Rollback
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "snowpack/cli/application/command"
4
+ require_relative "structure/dump"
5
+
6
+ module Snowpack
7
+ module CLI
8
+ module Application
9
+ module Commands
10
+ module DB
11
+ class SampleData < Command
12
+ desc "Load sample data"
13
+
14
+ def call(**)
15
+ if has_file?
16
+ measure "Sample data loaded" do
17
+ application.boot!
18
+ load file_path
19
+ end
20
+ else
21
+ out.puts "=> no sample data available"
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def file_path
28
+ File.join(application.root, "db/sample_data.rb")
29
+ end
30
+
31
+ def has_file?
32
+ File.exist?(file_path)
33
+ end
34
+ end
35
+ end
36
+
37
+ register "db sample_data", DB::SampleData
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "snowpack/cli/application/command"
4
+ require_relative "structure/dump"
5
+
6
+ module Snowpack
7
+ module CLI
8
+ module Application
9
+ module Commands
10
+ module DB
11
+ class Seed < Command
12
+ desc "Load database seeds"
13
+
14
+ def call(**)
15
+ if has_file?
16
+ measure "Database seeds loaded" do
17
+ application.boot
18
+ load file_path
19
+ end
20
+ else
21
+ out.puts "=> no database seeds available"
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def file_path
28
+ File.join(application.root, "db/seed.rb")
29
+ end
30
+
31
+ def has_file?
32
+ File.exist?(seeds_path)
33
+ end
34
+ end
35
+ end
36
+
37
+ register "db seed", DB::Seed
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "snowpack/cli/application/command"
4
+
5
+ module Snowpack
6
+ module CLI
7
+ module Application
8
+ module Commands
9
+ module DB
10
+ module Structure
11
+ class Dump < Command
12
+ desc "Dumps database structure to db/structure.sql file"
13
+
14
+ def call(*)
15
+ measure("#{database_config.db_name} structure dumped to db/structure.sql") do
16
+ cmd = "pg_dump --schema-only --no-owner #{Shellwords.escape(database_config.db_name)} > #{output_file}"
17
+ system(database_config.cli_env_vars, cmd)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def database_config
24
+ @database_config ||= Utils::DatabaseConfig.for_application(application)
25
+ end
26
+
27
+ def output_file
28
+ "#{application.root}/db/structure.sql"
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ register "db structure dump", DB::Structure::Dump
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/cli"
4
+ require "snowpack/cli/command"
5
+
6
+ module Snowpack
7
+ module CLI
8
+ module Application
9
+ module Commands
10
+ module DB
11
+ module Utils
12
+ class Database
13
+ def self.for_application(application)
14
+ application.init :persistence
15
+
16
+ new(
17
+ config: application["persistence.config"],
18
+ root_path: application.root,
19
+ )
20
+ end
21
+
22
+ attr_reader :config
23
+ attr_reader :root_path
24
+
25
+ def initialize(config:, root_path:)
26
+ @config = config
27
+ @root_path = root_path
28
+ end
29
+
30
+ def url
31
+ gateway.connection.url
32
+ end
33
+
34
+ def name
35
+ url.split("/").last
36
+ end
37
+
38
+ def applied_migrations
39
+ sequel_migrator.applied_migrations
40
+ end
41
+
42
+ def gateway
43
+ config.gateways[:default]
44
+ end
45
+
46
+ def connection
47
+ gateway.connection
48
+ end
49
+
50
+ def migrator
51
+ @migrator ||= ROM::SQL::Migration::Migrator.new(
52
+ connection,
53
+ path: File.join(root_path, "db/migrate"),
54
+ )
55
+ end
56
+
57
+ def applied_migrations
58
+ sequel_migrator.applied_migrations
59
+ end
60
+
61
+ private
62
+
63
+ def sequel_migrator
64
+ Sequel::TimestampMigrator.new(migrator.connection, migrations_path, {})
65
+ end
66
+
67
+ def migrations_path
68
+ File.join(application.root, "db/migrate")
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/cli"
4
+ require "uri"
5
+
6
+ module Snowpack
7
+ module CLI
8
+ module Application
9
+ module Commands
10
+ module DB
11
+ module Utils
12
+ class DatabaseConfig
13
+ def self.for_application(application)
14
+ new(application[:settings].database_url)
15
+ end
16
+
17
+ attr_reader :url
18
+
19
+ def initialize(database_url)
20
+ @url = database_url
21
+ end
22
+
23
+ def uri
24
+ @uri ||= URI.parse(url)
25
+ end
26
+
27
+ def hostname
28
+ uri.hostname
29
+ end
30
+
31
+ def db_name
32
+ @db_name ||= uri.path.gsub(/^\//, '')
33
+ end
34
+
35
+ def cli_env_vars
36
+ @cli_env_vars ||= {}.tap do |vars|
37
+ vars["PGHOST"] = uri.host.to_s
38
+ vars["PGPORT"] = uri.port.to_s if uri.port
39
+ vars["PGUSER"] = uri.user.to_s if uri.user
40
+ vars["PGPASSWORD"] = uri.password.to_s if uri.password
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hanami/cli"
4
+ require "snowpack/cli/application/command"
5
+ require_relative "structure/dump"
6
+ require_relative "utils/database"
7
+
8
+ module Snowpack
9
+ module CLI
10
+ module Application
11
+ module Commands
12
+ module DB
13
+ class Version < Command
14
+ desc "Print schema version"
15
+
16
+ option :target, desc: "Target migration number", aliases: ["-t"]
17
+
18
+ def call(target: nil, **)
19
+ migration = database.applied_migrations.last
20
+ version = migration ? File.basename(migration, ".*") : "not available"
21
+
22
+ out.puts "=> current schema version is #{version}"
23
+ end
24
+
25
+ private
26
+
27
+ def database
28
+ @database ||= Utils::Database.for_application(application)
29
+ end
30
+ end
31
+ end
32
+
33
+ register "db version", DB::Version
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "snowpack/cli/application/command"
4
+ require "snowpack/generators/slice/generator"
5
+
6
+ module Snowpack
7
+ module CLI
8
+ module Application
9
+ module Commands
10
+ module Generate
11
+ class Slice < Command
12
+ desc "Generate slice"
13
+
14
+ argument :name, desc: "Name for the slice"
15
+ option :web, type: :boolean, default: true, desc: "Include web routing and views support in generated slice"
16
+
17
+ def call(name:, web:, **)
18
+ name ||= path
19
+
20
+ # TODO raise ArgumentError, "invalid name blah blah"
21
+
22
+ generator = Generators::Slice::Generator.new
23
+ generator.(application: application, slice_name: name, web: web)
24
+ end
25
+ end
26
+ end
27
+
28
+ register "generate slice", Generate::Slice
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "snowpack/cli/application/command"
4
+
5
+ module Snowpack
6
+ module CLI
7
+ module Application
8
+ module Commands
9
+ module Routes
10
+ class Update < Command
11
+ desc "Update Roda routes.json"
12
+
13
+ def call(**)
14
+ measure "routes.json updated" do
15
+ `bundle exec roda-parse_routes #{route_files} -f #{routes_json_file}`
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def route_files
22
+ Dir["#{application.root}/apps/**/web/routes/**/*.rb"].join(" ")
23
+ end
24
+
25
+ def routes_json_file
26
+ "#{application.root}/config/routes.json"
27
+ end
28
+ end
29
+ end
30
+
31
+ register "routes update", Routes::Update
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Snowpack
4
+ module CLI
5
+ module Application
6
+ module Commands
7
+ extend Hanami::CLI::Registry
8
+
9
+ require_relative "commands/assets/clobber"
10
+ require_relative "commands/assets/compile"
11
+ require_relative "commands/console"
12
+ require_relative "commands/generate/slice"
13
+ require_relative "commands/db/create"
14
+ require_relative "commands/db/create_migration"
15
+ require_relative "commands/db/drop"
16
+ require_relative "commands/db/migrate"
17
+ require_relative "commands/db/reset"
18
+ require_relative "commands/db/rollback"
19
+ require_relative "commands/db/sample_data"
20
+ require_relative "commands/db/seed"
21
+ require_relative "commands/db/structure/dump"
22
+ require_relative "commands/db/version"
23
+ require_relative "commands/routes/update"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/inflector"
4
+ require "hanami/cli/command"
5
+ require "hanami/utils/files"
6
+
7
+ module Snowpack
8
+ module CLI
9
+ class Command < Hanami::CLI::Command
10
+ attr_reader :out
11
+ attr_reader :inflector
12
+ attr_reader :files
13
+
14
+ # TODO: consider injecting `out, files` from level of Snowpack::CLI?
15
+ def initialize(
16
+ command_name:,
17
+ out: $stdout,
18
+ inflector: Dry::Inflector.new,
19
+ files: Hanami::Utils::Files
20
+ )
21
+ super(command_name: command_name)
22
+
23
+ @out = out
24
+ @inflector = inflector
25
+ @files = files
26
+ end
27
+
28
+ private
29
+
30
+ def run_command(klass, *args)
31
+ klass.new(
32
+ command_name: klass.name,
33
+ application: application,
34
+ out: out,
35
+ files: files,
36
+ ).call(*args)
37
+ end
38
+
39
+ def measure(desc, &block)
40
+ start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
41
+ block.call
42
+ stop = Process.clock_gettime(Process::CLOCK_MONOTONIC)
43
+
44
+ out.puts "=> #{desc} in #{(stop - start).round(1)}s"
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,14 @@
1
+ require "hanami/cli"
2
+ require_relative "commands"
3
+
4
+ module Snowpack
5
+ module CLI
6
+ module Standalone
7
+ class CLI < Hanami::CLI
8
+ def initialize(commands = Commands)
9
+ super
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "snowpack/cli/command"
4
+ require "snowpack/generators/application/generator"
5
+
6
+ module Snowpack
7
+ module CLI
8
+ module Standalone
9
+ module Commands
10
+ class New < Command
11
+ argument :path, desc: "Path for new application"
12
+ option :name, desc: "Name for the application"
13
+
14
+ def call(path:, name: nil, **)
15
+ name ||= path
16
+
17
+ # TODO raise ArgumentError, "invalid name application"
18
+
19
+ out.puts "Generating into #{path}..."
20
+
21
+ generator = Generators::Application::Generator.new
22
+ generator.(path, name)
23
+
24
+ out.puts "Bootstrapping..."
25
+ system "#{path}/script/bootstrap"
26
+ end
27
+ end
28
+
29
+ register "new", New
30
+ end
31
+ end
32
+ end
33
+ end