sinatra_to_the_moon 0.2.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.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +18 -0
  3. data/CODE_OF_CONDUCT.md +132 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +106 -0
  6. data/bin/console +11 -0
  7. data/bin/flyme +6 -0
  8. data/bin/setup +8 -0
  9. data/lib/sinatra_to_the_moon/cli.rb +75 -0
  10. data/lib/sinatra_to_the_moon/error.rb +15 -0
  11. data/lib/sinatra_to_the_moon/generator.rb +41 -0
  12. data/lib/sinatra_to_the_moon/project.rb +28 -0
  13. data/lib/sinatra_to_the_moon/templates/api/Gemfile.tt +17 -0
  14. data/lib/sinatra_to_the_moon/templates/api/app/http/json.rb.tt +20 -0
  15. data/lib/sinatra_to_the_moon/templates/api/app/routes/users.rb.tt +18 -0
  16. data/lib/sinatra_to_the_moon/templates/api/app.rb.tt +26 -0
  17. data/lib/sinatra_to_the_moon/templates/api/spec/requests/users_spec.rb.tt +20 -0
  18. data/lib/sinatra_to_the_moon/templates/base/Gemfile.tt +17 -0
  19. data/lib/sinatra_to_the_moon/templates/base/README.md.tt +21 -0
  20. data/lib/sinatra_to_the_moon/templates/base/Rakefile.tt +9 -0
  21. data/lib/sinatra_to_the_moon/templates/base/app/routes/health.rb.tt +16 -0
  22. data/lib/sinatra_to_the_moon/templates/base/app.rb.tt +14 -0
  23. data/lib/sinatra_to_the_moon/templates/base/config.ru.tt +5 -0
  24. data/lib/sinatra_to_the_moon/templates/base/spec/requests/health_spec.rb.tt +13 -0
  25. data/lib/sinatra_to_the_moon/templates/base/spec/spec_helper.rb.tt +25 -0
  26. data/lib/sinatra_to_the_moon/templates/graphql/Gemfile.tt +18 -0
  27. data/lib/sinatra_to_the_moon/templates/graphql/app/graphql/schema.rb.tt +19 -0
  28. data/lib/sinatra_to_the_moon/templates/graphql/app/http/json.rb.tt +20 -0
  29. data/lib/sinatra_to_the_moon/templates/graphql/app/routes/graphql.rb.tt +20 -0
  30. data/lib/sinatra_to_the_moon/templates/graphql/app.rb.tt +19 -0
  31. data/lib/sinatra_to_the_moon/templates/graphql/spec/requests/graphql_spec.rb.tt +13 -0
  32. data/lib/sinatra_to_the_moon/templates/web/Gemfile.tt +18 -0
  33. data/lib/sinatra_to_the_moon/templates/web/README.md.tt +28 -0
  34. data/lib/sinatra_to_the_moon/templates/web/Rakefile.tt +24 -0
  35. data/lib/sinatra_to_the_moon/templates/web/app/controllers/home.rb.tt +21 -0
  36. data/lib/sinatra_to_the_moon/templates/web/app/models/page.rb.tt +7 -0
  37. data/lib/sinatra_to_the_moon/templates/web/app.rb.tt +17 -0
  38. data/lib/sinatra_to_the_moon/templates/web/assets/stylesheets/application.css.tt +3 -0
  39. data/lib/sinatra_to_the_moon/templates/web/spec/requests/home_spec.rb.tt +14 -0
  40. data/lib/sinatra_to_the_moon/templates/web/views/home.erb.tt +10 -0
  41. data/lib/sinatra_to_the_moon/templates/web/views/layout.erb.tt +12 -0
  42. data/lib/sinatra_to_the_moon/templates.rb +30 -0
  43. data/lib/sinatra_to_the_moon/version.rb +5 -0
  44. data/lib/sinatra_to_the_moon.rb +8 -0
  45. data/sig/sinatra_to_the_moon.rbs +7 -0
  46. metadata +159 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 44c5cff5aa8f26b3bb617970e32ada8b09feaa64825be328e6606ac3901e48c6
4
+ data.tar.gz: f289bb7375cc16c50a68943b6269b65a488d7dfb419753687892a875063af25b
5
+ SHA512:
6
+ metadata.gz: c3c1044f4d7785b97955ee79e4d73523c5925d4927c120920576a34afc016e51ad4846660998769ddfc4b2ff1f81bb2e640b48cc3a654e5053d02afe30d78a18
7
+ data.tar.gz: ddc801d7a71fd63be30da048ba9f24c644bc4011cb1f2272238dd7e6b4c67b654732e048f0d1520c4913f9a171224e6a11f2c0556b361715eeaf7d9fc50c2724
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ ## [Unreleased]
2
+
3
+ - Rename the gem to `sinatra_to_the_moon` and its executable to `flyme`.
4
+ - Generate modular, class-light Sinatra applications.
5
+ - Add minimal, REST API, and GraphQL profiles.
6
+ - Make the MVC-style web profile with ERB views and Tailwind CSS the default.
7
+ - Add dry-run, overwrite protection, project-name validation, and generator specs.
8
+ - Replace Standard with RuboCop and add `why-classes` analysis.
9
+
10
+ ## [0.1.0] - 2024-08-16
11
+
12
+ - Initial release
13
+
14
+ ## [0.1.1] - 2024-08-18
15
+
16
+ - Update `config.ru` file
17
+ - Added `rackup` gem
18
+ - Added `rerun` gem to enable auto restart of server after code changes
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Gabriel Quaresma
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # Sinatra to the Moon
2
+
3
+ `sinatra_to_the_moon` generates concise, modular, and testable Sinatra web applications, REST APIs, and GraphQL APIs. It provides the `flyme` command while keeping the generated code close to ordinary Sinatra.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ gem install sinatra_to_the_moon
9
+ ```
10
+
11
+ ## Generate an application
12
+
13
+ Create an MVC-style Sinatra web app with ERB views and Tailwind CSS:
14
+
15
+ ```sh
16
+ flyme new moon_app
17
+ ```
18
+
19
+ The web profile is the default. It uses the latest `tailwindcss-ruby` release, so no Node.js toolchain is required. Start its CSS watcher with:
20
+
21
+ ```sh
22
+ cd moon_app
23
+ bundle exec rake assets:watch
24
+ ```
25
+
26
+ Create a minimal API-shaped scaffold without views:
27
+
28
+ ```sh
29
+ flyme new moon_service --minimal
30
+ ```
31
+
32
+ Create a REST API:
33
+
34
+ ```sh
35
+ flyme new moon_api --api
36
+ ```
37
+
38
+ Create a GraphQL API:
39
+
40
+ ```sh
41
+ flyme new moon_graph --graphql
42
+ ```
43
+
44
+ The explicit profile form is also available:
45
+
46
+ ```sh
47
+ flyme new moon_api --profile api
48
+ ```
49
+
50
+ Preview files without writing them:
51
+
52
+ ```sh
53
+ flyme new moon_app --dry-run
54
+ ```
55
+
56
+ `flyme` refuses to write into an existing destination unless `--force` is passed.
57
+
58
+ After generation:
59
+
60
+ ```sh
61
+ cd moon_app
62
+ bundle install
63
+ bundle exec rackup
64
+ ```
65
+
66
+ Run the generated application checks with:
67
+
68
+ ```sh
69
+ bundle exec rake
70
+ ```
71
+
72
+ ## Design
73
+
74
+ Generated projects use modules for stateless behavior. Classes are reserved for objects that retain state and framework contracts such as `Sinatra::Base` and `GraphQL::Schema`.
75
+
76
+ Profiles remain small:
77
+
78
+ - `web` is the default and provides MVC-style modules, ERB views, and Tailwind CSS.
79
+ - `minimal` provides a modular Sinatra app and health endpoint.
80
+ - `api` adds JSON request/response helpers and example REST routes.
81
+ - `graphql` adds a GraphQL schema and endpoint.
82
+
83
+ Every generated project includes request specs and RuboCop configuration.
84
+
85
+ The generator follows the same approach: Thor owns the stateful CLI class, while project validation, template rendering, and generation are organized as stateless modules. `why-classes` checks this convention during development.
86
+
87
+ ## Development
88
+
89
+ Install dependencies and run every check:
90
+
91
+ ```sh
92
+ bundle install
93
+ bundle exec rake
94
+ ```
95
+
96
+ The default task runs RSpec, RuboCop, and `why-classes`.
97
+
98
+ Build the gem locally with:
99
+
100
+ ```sh
101
+ gem build sinatra_to_the_moon.gemspec
102
+ ```
103
+
104
+ Generator specs create every profile in a temporary directory, run its request specs, and lint the resulting application. Changes to CLI behavior or templates should include regression coverage.
105
+
106
+ See [AGENTS.md](AGENTS.md) for contributor conventions. AI-assisted work should also follow [CLAUDE.md](CLAUDE.md).
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "sinatra_to_the_moon"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ require "irb"
11
+ IRB.start(__FILE__)
data/bin/flyme ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "sinatra_to_the_moon"
5
+
6
+ SinatraToTheMoon::CLI.start(ARGV)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+ require "sinatra_to_the_moon/error"
5
+ require "sinatra_to_the_moon/generator"
6
+ require "sinatra_to_the_moon/version"
7
+
8
+ module SinatraToTheMoon
9
+ class CLI < Thor
10
+ package_name "flyme"
11
+
12
+ def self.exit_on_failure?
13
+ true
14
+ end
15
+
16
+ desc "new APP_NAME", "Create a concise, testable Sinatra application"
17
+ method_option :profile,
18
+ aliases: "-p",
19
+ default: "web",
20
+ enum: Project::PROFILES,
21
+ desc: "Application profile"
22
+ method_option :web, type: :boolean, default: false, desc: "Shortcut for --profile web"
23
+ method_option :minimal, type: :boolean, default: false, desc: "Shortcut for --profile minimal"
24
+ method_option :api, type: :boolean, default: false, desc: "Shortcut for --profile api"
25
+ method_option :graphql, type: :boolean, default: false, desc: "Shortcut for --profile graphql"
26
+ method_option :force, type: :boolean, default: false, desc: "Overwrite generated files"
27
+ method_option :dry_run, type: :boolean, default: false, desc: "Print files without writing them"
28
+ def new(app_name)
29
+ profile = selected_profile
30
+ files = Generator.generate(
31
+ name: app_name,
32
+ profile: profile,
33
+ force: options[:force],
34
+ dry_run: options[:dry_run]
35
+ )
36
+
37
+ action = options[:dry_run] ? "would create" : "created"
38
+ files.each { |file| say("#{action} #{file}", :green) }
39
+ say "\nReady for launch: #{launch_command(app_name, profile)}"
40
+ rescue Error => e
41
+ raise Thor::Error, e.message
42
+ end
43
+
44
+ desc "version", "Print the flyme version"
45
+ def version
46
+ say SinatraToTheMoon::VERSION
47
+ end
48
+
49
+ map %w[-v --version] => :version
50
+
51
+ private
52
+
53
+ def selected_profile
54
+ shortcuts = selected_shortcuts
55
+ raise Thor::Error, "Choose only one profile shortcut" if shortcuts.length > 1
56
+ if shortcuts.any? && options[:profile] != "web"
57
+ raise Thor::Error,
58
+ "Use either --profile or a profile shortcut"
59
+ end
60
+
61
+ shortcuts.first || options[:profile]
62
+ end
63
+
64
+ def selected_shortcuts
65
+ Project::PROFILES.select { |profile| options[profile.to_sym] }
66
+ end
67
+
68
+ def launch_command(app_name, profile)
69
+ commands = ["cd #{app_name}", "bundle install"]
70
+ commands << "bundle exec rake assets:build" if profile == "web"
71
+ commands << "bundle exec rackup"
72
+ commands.join(" && ")
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SinatraToTheMoon
4
+ class Error < StandardError
5
+ end
6
+
7
+ class InvalidProjectNameError < Error
8
+ end
9
+
10
+ class DestinationExistsError < Error
11
+ end
12
+
13
+ class UnknownProfileError < Error
14
+ end
15
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+
5
+ require "sinatra_to_the_moon/error"
6
+ require "sinatra_to_the_moon/project"
7
+ require "sinatra_to_the_moon/templates"
8
+
9
+ module SinatraToTheMoon
10
+ module Generator
11
+ module_function
12
+
13
+ def generate(name:, profile: "web", destination: Dir.pwd, force: false, dry_run: false)
14
+ Project.validate_name!(name)
15
+ Project.validate_profile!(profile)
16
+
17
+ target = File.expand_path(name, destination)
18
+ ensure_destination_available!(target, force: force)
19
+
20
+ locals = { app_name: name, constant_name: Project.constant_name(name), profile: profile }
21
+ files = Templates.files_for(profile)
22
+
23
+ files.each do |relative_path, template|
24
+ path = File.join(target, relative_path)
25
+ next if dry_run
26
+
27
+ FileUtils.mkdir_p(File.dirname(path))
28
+ File.write(path, Templates.render(template, locals))
29
+ end
30
+
31
+ files.keys.map { |path| File.join(name, path) }
32
+ end
33
+
34
+ def ensure_destination_available!(target, force:)
35
+ return unless File.exist?(target)
36
+ return if force
37
+
38
+ raise DestinationExistsError, "Destination already exists: #{target}. Pass --force to overwrite generated files"
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SinatraToTheMoon
4
+ module Project
5
+ NAME_PATTERN = /\A[a-z][a-z0-9_-]*\z/
6
+ PROFILES = %w[web minimal api graphql].freeze
7
+
8
+ module_function
9
+
10
+ def validate_name!(name)
11
+ return name if NAME_PATTERN.match?(name)
12
+
13
+ raise InvalidProjectNameError,
14
+ "Project name must start with a lowercase letter and contain only " \
15
+ "lowercase letters, numbers, dashes, or underscores"
16
+ end
17
+
18
+ def validate_profile!(profile)
19
+ return profile if PROFILES.include?(profile)
20
+
21
+ raise UnknownProfileError, "Unknown profile #{profile.inspect}. Choose: #{PROFILES.join(', ')}"
22
+ end
23
+
24
+ def constant_name(name)
25
+ name.split(/[-_]/).map(&:capitalize).join
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "puma"
6
+ gem "rackup"
7
+ gem "sinatra"
8
+
9
+ group :development, :test do
10
+ gem "rake"
11
+ gem "rubocop", require: false
12
+ end
13
+
14
+ group :test do
15
+ gem "rack-test"
16
+ gem "rspec"
17
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module <%= constant_name %>
6
+ module HTTP
7
+ module JSON
8
+ def json(payload, status: 200)
9
+ content_type :json
10
+ halt status, ::JSON.generate(payload)
11
+ end
12
+
13
+ def json_params
14
+ ::JSON.parse(request.body.read, symbolize_names: true)
15
+ rescue ::JSON::ParserError
16
+ json({ error: "invalid_json" }, status: 400)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= constant_name %>
4
+ module Routes
5
+ module Users
6
+ def self.registered(app)
7
+ app.get "/api/users" do
8
+ json({ data: [{ id: 1, name: "Frank" }] })
9
+ end
10
+
11
+ app.post "/api/users" do
12
+ attributes = json_params
13
+ json({ data: { id: 2, name: attributes.fetch(:name) } }, status: 201)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sinatra/base"
4
+ require_relative "app/http/json"
5
+ require_relative "app/routes/health"
6
+ require_relative "app/routes/users"
7
+
8
+ module <%= constant_name %>
9
+ class App < Sinatra::Base
10
+ helpers HTTP::JSON
11
+ register Routes::Health
12
+ register Routes::Users
13
+
14
+ configure do
15
+ set :show_exceptions, false
16
+ end
17
+
18
+ not_found do
19
+ json({ error: "not_found" }, status: 404)
20
+ end
21
+
22
+ error do
23
+ json({ error: "internal_server_error" }, status: 500)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe "Users API" do
6
+ it "lists users" do
7
+ get "/api/users"
8
+
9
+ expect(last_response).to be_ok
10
+ expect(json_response).to eq(data: [{ id: 1, name: "Frank" }])
11
+ end
12
+
13
+ it "creates a user" do
14
+ header "content-type", "application/json"
15
+ post "/api/users", JSON.generate(name: "Luna")
16
+
17
+ expect(last_response.status).to eq(201)
18
+ expect(json_response).to eq(data: { id: 2, name: "Luna" })
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "puma"
6
+ gem "rackup"
7
+ gem "sinatra"
8
+
9
+ group :development, :test do
10
+ gem "rake"
11
+ gem "rubocop", require: false
12
+ end
13
+
14
+ group :test do
15
+ gem "rack-test"
16
+ gem "rspec"
17
+ end
@@ -0,0 +1,21 @@
1
+ # <%= app_name %>
2
+
3
+ A concise Sinatra application generated by `flyme` using the `<%= profile %>` profile.
4
+
5
+ ## Setup
6
+
7
+ ```sh
8
+ bundle install
9
+ ```
10
+
11
+ ## Run
12
+
13
+ ```sh
14
+ bundle exec rackup
15
+ ```
16
+
17
+ ## Verify
18
+
19
+ ```sh
20
+ bundle exec rake
21
+ ```
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec/core/rake_task"
4
+ require "rubocop/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ RuboCop::RakeTask.new(:rubocop)
8
+
9
+ task default: %i[spec rubocop]
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module <%= constant_name %>
6
+ module Routes
7
+ module Health
8
+ def self.registered(app)
9
+ app.get "/health" do
10
+ content_type :json
11
+ JSON.generate(status: "ok")
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sinatra/base"
4
+ require_relative "app/routes/health"
5
+
6
+ module <%= constant_name %>
7
+ class App < Sinatra::Base
8
+ register Routes::Health
9
+
10
+ configure do
11
+ set :show_exceptions, false
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "app"
4
+
5
+ run <%= constant_name %>::App
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe "GET /health" do
6
+ it "reports that the application is healthy" do
7
+ get "/health"
8
+
9
+ expect(last_response).to be_ok
10
+ expect(last_response.content_type).to start_with("application/json")
11
+ expect(json_response).to eq(status: "ok")
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ ENV["RACK_ENV"] = "test"
4
+
5
+ require "rack/test"
6
+ require "rspec"
7
+ require_relative "../app"
8
+
9
+ module RequestHelpers
10
+ include Rack::Test::Methods
11
+
12
+ def app
13
+ <%= constant_name %>::App
14
+ end
15
+
16
+ def json_response
17
+ JSON.parse(last_response.body, symbolize_names: true)
18
+ end
19
+ end
20
+
21
+ RSpec.configure do |config|
22
+ config.include RequestHelpers
23
+ config.disable_monkey_patching!
24
+ config.order = :random
25
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "graphql"
6
+ gem "puma"
7
+ gem "rackup"
8
+ gem "sinatra"
9
+
10
+ group :development, :test do
11
+ gem "rake"
12
+ gem "rubocop", require: false
13
+ end
14
+
15
+ group :test do
16
+ gem "rack-test"
17
+ gem "rspec"
18
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "graphql"
4
+
5
+ module <%= constant_name %>
6
+ module GraphQL
7
+ class Query < ::GraphQL::Schema::Object
8
+ field :hello, String, null: false
9
+
10
+ def hello
11
+ "Fly me to the moon"
12
+ end
13
+ end
14
+
15
+ class Schema < ::GraphQL::Schema
16
+ query Query
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module <%= constant_name %>
6
+ module HTTP
7
+ module JSON
8
+ def json(payload, status: 200)
9
+ content_type :json
10
+ halt status, ::JSON.generate(payload)
11
+ end
12
+
13
+ def json_params
14
+ ::JSON.parse(request.body.read, symbolize_names: true)
15
+ rescue ::JSON::ParserError
16
+ json({ errors: [{ message: "Invalid JSON" }] }, status: 400)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= constant_name %>
4
+ module Routes
5
+ module GraphQL
6
+ def self.registered(app)
7
+ app.post "/graphql" do
8
+ input = json_params
9
+ result = <%= constant_name %>::GraphQL::Schema.execute(
10
+ input.fetch(:query),
11
+ variables: input[:variables] || {},
12
+ operation_name: input[:operationName],
13
+ context: { request: request }
14
+ )
15
+ json result.to_h
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sinatra/base"
4
+ require_relative "app/graphql/schema"
5
+ require_relative "app/http/json"
6
+ require_relative "app/routes/graphql"
7
+ require_relative "app/routes/health"
8
+
9
+ module <%= constant_name %>
10
+ class App < Sinatra::Base
11
+ helpers HTTP::JSON
12
+ register Routes::GraphQL
13
+ register Routes::Health
14
+
15
+ configure do
16
+ set :show_exceptions, false
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe "POST /graphql" do
6
+ it "executes a query" do
7
+ header "content-type", "application/json"
8
+ post "/graphql", JSON.generate(query: "{ hello }")
9
+
10
+ expect(last_response).to be_ok
11
+ expect(json_response).to eq(data: { hello: "Fly me to the moon" })
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "puma"
6
+ gem "rackup"
7
+ gem "sinatra"
8
+ gem "tailwindcss-ruby"
9
+
10
+ group :development, :test do
11
+ gem "rake"
12
+ gem "rubocop", require: false
13
+ end
14
+
15
+ group :test do
16
+ gem "rack-test"
17
+ gem "rspec"
18
+ end
@@ -0,0 +1,28 @@
1
+ # <%= app_name %>
2
+
3
+ A concise Sinatra web application with ERB views and Tailwind CSS.
4
+
5
+ ## Setup
6
+
7
+ ```sh
8
+ bundle install
9
+ bundle exec rake assets:build
10
+ ```
11
+
12
+ ## Run
13
+
14
+ ```sh
15
+ bundle exec rackup
16
+ ```
17
+
18
+ Watch CSS changes in another terminal:
19
+
20
+ ```sh
21
+ bundle exec rake assets:watch
22
+ ```
23
+
24
+ ## Verify
25
+
26
+ ```sh
27
+ bundle exec rake
28
+ ```
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
6
+
7
+ namespace :assets do
8
+ desc "Compile Tailwind CSS"
9
+ task :build do
10
+ FileUtils.mkdir_p("public")
11
+ sh "tailwindcss", "-i", "assets/stylesheets/application.css", "-o", "public/application.css", "--minify"
12
+ end
13
+
14
+ desc "Watch and compile Tailwind CSS"
15
+ task :watch do
16
+ FileUtils.mkdir_p("public")
17
+ sh "tailwindcss", "-i", "assets/stylesheets/application.css", "-o", "public/application.css", "--watch"
18
+ end
19
+ end
20
+
21
+ RSpec::Core::RakeTask.new(:spec)
22
+ RuboCop::RakeTask.new(:rubocop)
23
+
24
+ task default: ["assets:build", :spec, :rubocop]
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../models/page"
4
+
5
+ module <%= constant_name %>
6
+ module Controllers
7
+ module Home
8
+ def self.registered(app)
9
+ app.get "/" do
10
+ @page = Models::Page.new(
11
+ title: "<%= app_name %>",
12
+ heading: "Sinatra to the Moon",
13
+ message: "A concise Sinatra app with views and Tailwind CSS."
14
+ )
15
+
16
+ erb :home
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= constant_name %>
4
+ module Models
5
+ Page = Data.define(:title, :heading, :message)
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sinatra/base"
4
+ require_relative "app/controllers/home"
5
+ require_relative "app/routes/health"
6
+
7
+ module <%= constant_name %>
8
+ class App < Sinatra::Base
9
+ register Controllers::Home
10
+ register Routes::Health
11
+
12
+ configure do
13
+ set :show_exceptions, false
14
+ set :views, File.expand_path("views", __dir__)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ @import "tailwindcss";
2
+ @source "../../views";
3
+ @source "../../app";
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe "GET /" do
6
+ it "renders the home view and stylesheet" do
7
+ get "/"
8
+
9
+ expect(last_response).to be_ok
10
+ expect(last_response.content_type).to start_with("text/html")
11
+ expect(last_response.body).to include("Sinatra to the Moon")
12
+ expect(last_response.body).to include('href="/application.css"')
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ <main class="mx-auto flex min-h-screen max-w-5xl items-center px-6 py-16">
2
+ <section class="space-y-6">
3
+ <p class="text-sm font-semibold uppercase tracking-[0.3em] text-cyan-400">Welcome aboard</p>
4
+ <h1 class="text-5xl font-bold tracking-tight sm:text-7xl"><%%= @page.heading %></h1>
5
+ <p class="max-w-2xl text-lg leading-8 text-slate-300"><%%= @page.message %></p>
6
+ <a class="inline-flex rounded-full bg-cyan-400 px-5 py-3 font-semibold text-slate-950 hover:bg-cyan-300" href="/health">
7
+ Check system status
8
+ </a>
9
+ </section>
10
+ </main>
@@ -0,0 +1,12 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title><%%= @page&.title || "<%= app_name %>" %></title>
7
+ <link rel="stylesheet" href="/application.css">
8
+ </head>
9
+ <body class="min-h-screen bg-slate-950 text-slate-100 antialiased">
10
+ <%%= yield %>
11
+ </body>
12
+ </html>
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "erb"
4
+
5
+ module SinatraToTheMoon
6
+ module Templates
7
+ ROOT = File.expand_path("templates", __dir__)
8
+
9
+ module_function
10
+
11
+ def files_for(profile)
12
+ template_files("base")
13
+ .merge(template_files(profile))
14
+ .transform_keys { |path| path.delete_suffix(".tt") }
15
+ end
16
+
17
+ def render(template, locals)
18
+ ERB.new(File.read(template), trim_mode: "-").result_with_hash(**locals)
19
+ end
20
+
21
+ def template_files(directory)
22
+ root = File.join(ROOT, directory)
23
+ return {} unless Dir.exist?(root)
24
+
25
+ Dir.glob(File.join(root, "**", "*.tt"), File::FNM_DOTMATCH).sort.to_h do |file|
26
+ [file.delete_prefix("#{root}/"), file]
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SinatraToTheMoon
4
+ VERSION = "0.2.0"
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sinatra_to_the_moon/cli"
4
+ require "sinatra_to_the_moon/generator"
5
+ require "sinatra_to_the_moon/version"
6
+
7
+ module SinatraToTheMoon
8
+ end
@@ -0,0 +1,7 @@
1
+ module SinatraToTheMoon
2
+ VERSION: String
3
+
4
+ module Generator
5
+ def self.generate: (name: String, ?profile: String, ?destination: String, ?force: bool, ?dry_run: bool) -> Array[String]
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra_to_the_moon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - joaoGabriel55
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: thor
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.3'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.3'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '13.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '13.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rspec
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.13'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.13'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rubocop
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.75'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.75'
68
+ - !ruby/object:Gem::Dependency
69
+ name: why-classes
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0.2'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.2'
82
+ description: A small CLI for generating modular Sinatra web apps, REST APIs, and GraphQL
83
+ APIs.
84
+ executables:
85
+ - flyme
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - CHANGELOG.md
90
+ - CODE_OF_CONDUCT.md
91
+ - LICENSE.txt
92
+ - README.md
93
+ - bin/console
94
+ - bin/flyme
95
+ - bin/setup
96
+ - lib/sinatra_to_the_moon.rb
97
+ - lib/sinatra_to_the_moon/cli.rb
98
+ - lib/sinatra_to_the_moon/error.rb
99
+ - lib/sinatra_to_the_moon/generator.rb
100
+ - lib/sinatra_to_the_moon/project.rb
101
+ - lib/sinatra_to_the_moon/templates.rb
102
+ - lib/sinatra_to_the_moon/templates/api/Gemfile.tt
103
+ - lib/sinatra_to_the_moon/templates/api/app.rb.tt
104
+ - lib/sinatra_to_the_moon/templates/api/app/http/json.rb.tt
105
+ - lib/sinatra_to_the_moon/templates/api/app/routes/users.rb.tt
106
+ - lib/sinatra_to_the_moon/templates/api/spec/requests/users_spec.rb.tt
107
+ - lib/sinatra_to_the_moon/templates/base/Gemfile.tt
108
+ - lib/sinatra_to_the_moon/templates/base/README.md.tt
109
+ - lib/sinatra_to_the_moon/templates/base/Rakefile.tt
110
+ - lib/sinatra_to_the_moon/templates/base/app.rb.tt
111
+ - lib/sinatra_to_the_moon/templates/base/app/routes/health.rb.tt
112
+ - lib/sinatra_to_the_moon/templates/base/config.ru.tt
113
+ - lib/sinatra_to_the_moon/templates/base/spec/requests/health_spec.rb.tt
114
+ - lib/sinatra_to_the_moon/templates/base/spec/spec_helper.rb.tt
115
+ - lib/sinatra_to_the_moon/templates/graphql/Gemfile.tt
116
+ - lib/sinatra_to_the_moon/templates/graphql/app.rb.tt
117
+ - lib/sinatra_to_the_moon/templates/graphql/app/graphql/schema.rb.tt
118
+ - lib/sinatra_to_the_moon/templates/graphql/app/http/json.rb.tt
119
+ - lib/sinatra_to_the_moon/templates/graphql/app/routes/graphql.rb.tt
120
+ - lib/sinatra_to_the_moon/templates/graphql/spec/requests/graphql_spec.rb.tt
121
+ - lib/sinatra_to_the_moon/templates/web/Gemfile.tt
122
+ - lib/sinatra_to_the_moon/templates/web/README.md.tt
123
+ - lib/sinatra_to_the_moon/templates/web/Rakefile.tt
124
+ - lib/sinatra_to_the_moon/templates/web/app.rb.tt
125
+ - lib/sinatra_to_the_moon/templates/web/app/controllers/home.rb.tt
126
+ - lib/sinatra_to_the_moon/templates/web/app/models/page.rb.tt
127
+ - lib/sinatra_to_the_moon/templates/web/assets/stylesheets/application.css.tt
128
+ - lib/sinatra_to_the_moon/templates/web/spec/requests/home_spec.rb.tt
129
+ - lib/sinatra_to_the_moon/templates/web/views/home.erb.tt
130
+ - lib/sinatra_to_the_moon/templates/web/views/layout.erb.tt
131
+ - lib/sinatra_to_the_moon/version.rb
132
+ - sig/sinatra_to_the_moon.rbs
133
+ homepage: https://github.com/joaoGabriel55/sinatra_to_the_moon
134
+ licenses:
135
+ - MIT
136
+ metadata:
137
+ homepage_uri: https://github.com/joaoGabriel55/sinatra_to_the_moon
138
+ source_code_uri: https://github.com/joaoGabriel55/sinatra_to_the_moon
139
+ changelog_uri: https://github.com/joaoGabriel55/sinatra_to_the_moon/blob/main/CHANGELOG.md
140
+ bug_tracker_uri: https://github.com/joaoGabriel55/sinatra_to_the_moon/issues
141
+ rubygems_mfa_required: 'true'
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: 3.2.0
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubygems_version: 3.6.7
157
+ specification_version: 4
158
+ summary: Generate concise, testable Sinatra applications
159
+ test_files: []