create-ruby-app 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 769d91354db227cdf9f8e82610666d2ba473e5f559e36708df0841850d1c777c
4
+ data.tar.gz: 28eb12ac49e3334ffa4757b74793ac3b9081115c75dc4ab6a45c5b75e2b9ccd2
5
+ SHA512:
6
+ metadata.gz: 4ca234dfc8161a2f6335905e8898e3c3697e9eb736aeb7b7cd5e8f2c240cb212232b690eeb1fb395ffb7ce5a195e5631d7dd56ff1bfabe32f1a07e51b09b8381
7
+ data.tar.gz: 0e26c8a4e941063b07d4bf8d64d147d0d462efb214b874ea5d81fca0a3bdc0b665d9cbb903372d0068585cab063f078924590cb53840e202b0b2ac872689bd83
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Mathias Jean Johansen
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,50 @@
1
+ # Create Ruby App
2
+ [![Build Status](https://travis-ci.org/majjoha/create-ruby-app.svg)](https://travis-ci.org/majjoha/create-ruby-app)
3
+
4
+ `create-ruby-app` is an opinionated tool for scaffolding Ruby applications
5
+ effortlessly inspired by [Create React
6
+ App](https://github.com/facebook/create-react-app). It generates only the
7
+ essentials needed to start working.
8
+
9
+ ## Requirements
10
+ * Ruby (version 2.6.0 or newer).
11
+
12
+ ## Installation
13
+ ```
14
+ gem install create-ruby-app
15
+ ```
16
+
17
+ ## Usage
18
+ ```
19
+ create-ruby-app new NAME [--ruby RUBY] [--gems GEMS]
20
+ ```
21
+
22
+ ### Example
23
+ ```
24
+ create-ruby-app new my-app --gems sinatra,sequel --ruby ruby-2.6.1
25
+ ```
26
+
27
+ This will generate the following project structure with
28
+ [Sinatra](http://sinatrarb.com) and [Sequel](http://sequel.jeremyevans.net)
29
+ added to the Gemfile.
30
+
31
+ ```
32
+ my_app
33
+ ├── bin/
34
+ │ └── my_app
35
+ ├── lib/
36
+ │ ├── my_app/
37
+ │ └── my_app.rb
38
+ ├── spec/
39
+ │ ├── lib/
40
+ │ │ └── my_app/
41
+ │ └── spec_helper.rb
42
+ ├── .ruby-version
43
+ └── Gemfile
44
+ ```
45
+
46
+ Once the project is generated, it will run `bundle install` so you can start
47
+ working.
48
+
49
+ ## License
50
+ See [LICENSE](https://github.com/majjoha/create-ruby-app/blob/master/LICENSE).
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require_relative "../lib/create_ruby_app"
6
+
7
+ CreateRubyApp::CLI.start
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logger"
4
+
5
+ module CreateRubyApp
6
+ class App
7
+ def initialize(
8
+ name: "app",
9
+ gems: [],
10
+ version: RUBY_VERSION,
11
+ logger: Logger.new(STDOUT)
12
+ )
13
+ @name = name
14
+ @gems = gems
15
+ @version = version
16
+ @logger = logger
17
+ end
18
+
19
+ def run!
20
+ with_logger("Creating directories...", :create_directories)
21
+ with_logger("Generating files...", :generate_files)
22
+ with_logger("Making script executable...", :make_script_executable)
23
+ with_logger("Installing gems...", :install_gems)
24
+ with_logger("Happy hacking!")
25
+ end
26
+
27
+ def classify_name
28
+ name.split("_").collect(&:capitalize).join
29
+ end
30
+
31
+ attr_accessor :name, :gems, :version, :logger
32
+
33
+ RUBY_VERSION = "ruby-2.6.0"
34
+
35
+ private
36
+
37
+ def with_logger(text, callable = false)
38
+ logger.info(text)
39
+ file_generator.method(callable).call if callable
40
+ end
41
+
42
+ def file_generator
43
+ @file_generator ||= FileGenerator.new(app: self)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+
5
+ module CreateRubyApp
6
+ class CLI < Thor
7
+ def self.exit_on_failure?
8
+ true
9
+ end
10
+
11
+ desc "version", "Print the current version and exit"
12
+ map %w[--version -v] => :version
13
+ def version
14
+ say VERSION
15
+ end
16
+
17
+ desc "new [NAME] [--ruby RUBY] [--gems GEMS]", "Generate new app"
18
+ long_desc <<~DESC
19
+ `create-ruby-app new NAME` will generate an app with the provided name.
20
+
21
+ Examples:
22
+ \x5$ create-ruby-app new my_app
23
+ \x5$ create-ruby-app new -g sinatra,sequel -r ruby-2.6.0 web_app
24
+ \x5$ create-ruby-app new --ruby jruby-2.9.6.0 my_app
25
+ DESC
26
+
27
+ method_option(:ruby, default: App::RUBY_VERSION, aliases: "-r")
28
+ method_option(:gems, default: "", aliases: "-g")
29
+ def new(name)
30
+ App.new(
31
+ name: replace_dashes_with_underscores(name),
32
+ gems: options[:gems].split(","),
33
+ version: options[:ruby]
34
+ ).run!
35
+ end
36
+
37
+ private
38
+
39
+ def replace_dashes_with_underscores(name)
40
+ name.tr("-", "_")
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "erb"
4
+ require "fileutils"
5
+ require "pathname"
6
+
7
+ module CreateRubyApp
8
+ class FileGenerator
9
+ def initialize(app: App.new)
10
+ @app = app
11
+ end
12
+
13
+ def generate_gemfile
14
+ generate_file(file: "Gemfile.erb", locals: { gems: app.gems.sort })
15
+ end
16
+
17
+ def generate_lib_file
18
+ generate_file(file: "lib_file.erb", locals: { app: app.classify_name })
19
+ end
20
+
21
+ def generate_ruby_version_file
22
+ generate_file(file: "ruby-version.erb", locals: { version: app.version })
23
+ end
24
+
25
+ def generate_script_file
26
+ generate_file(file: "script_file.erb", locals: {})
27
+ end
28
+
29
+ def generate_spec_helper_file
30
+ generate_file(file: "spec_helper.erb", locals: { app: app.name })
31
+ end
32
+
33
+ def create_directories
34
+ directories.each(&FileUtils.method(:mkdir_p))
35
+ end
36
+
37
+ def generate_files
38
+ files.each {|path, content| create_file(path: path, content: content) }
39
+ end
40
+
41
+ def make_script_executable
42
+ FileUtils.chmod("+x", "#{app.name}/bin/#{app.name}")
43
+ end
44
+
45
+ def install_gems
46
+ system("cd #{app.name} && bundle install")
47
+ end
48
+
49
+ private
50
+
51
+ attr_accessor :app
52
+
53
+ def generate_file(file:, locals: [])
54
+ ERB
55
+ .new(read_file(file), trim_mode: TRIM_MODE)
56
+ .result_with_hash(locals: locals)
57
+ end
58
+
59
+ def read_file(file)
60
+ Pathname.new(__FILE__).dirname.join("#{TEMPLATES_DIR}/#{file}").read
61
+ end
62
+
63
+ def create_file(path:, content:)
64
+ File.write("#{app.name}/#{path}", content)
65
+ end
66
+
67
+ def files
68
+ {
69
+ "bin/#{app.name}" => generate_script_file,
70
+ "lib/#{app.name}.rb" => generate_lib_file,
71
+ "spec/spec_helper.rb" => generate_spec_helper_file,
72
+ ".ruby-version" => generate_ruby_version_file,
73
+ "Gemfile" => generate_gemfile
74
+ }
75
+ end
76
+
77
+ def directories
78
+ [
79
+ Pathname.new("#{app.name}/bin"),
80
+ Pathname.new("#{app.name}/lib/#{app.name}"),
81
+ Pathname.new("#{app.name}/spec/lib/#{app.name}")
82
+ ]
83
+ end
84
+
85
+ TEMPLATES_DIR = "templates"
86
+ TRIM_MODE = "<>"
87
+ end
88
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ group :test, :development do
6
+ gem "rspec"
7
+ end
8
+ <% unless locals[:gems].empty? %>
9
+
10
+ <%= locals[:gems].map {|gem| "gem \"#{gem}\"" }.join("\n") %>
11
+
12
+ <% end %>
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= locals[:app] %>
4
+ class <%= locals[:app] %>
5
+ end
6
+ end
@@ -0,0 +1 @@
1
+ <%= locals[:version] %>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec"
4
+ require_relative "../lib/<%= locals[:app] %>"
5
+
6
+ RSpec.configure do |config|
7
+ config.expect_with(:rspec) do |c|
8
+ c.syntax = :expect
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CreateRubyApp
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "create_ruby_app/app"
4
+ require_relative "create_ruby_app/cli"
5
+ require_relative "create_ruby_app/file_generator"
6
+ require_relative "create_ruby_app/version"
7
+
8
+ module CreateRubyApp
9
+ class CreateRubyApp
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../spec_helper"
4
+
5
+ describe CreateRubyApp::App do
6
+ describe "#classify_name" do
7
+ context "when the name of the app contains underscores" do
8
+ let(:app) { described_class.new(name: "foo_bar_baz") }
9
+
10
+ it "removes the underscores and capitalizes each word" do
11
+ expect(app.classify_name).to eq("FooBarBaz")
12
+ end
13
+ end
14
+
15
+ context "when the name of the app contains no underscores" do
16
+ let(:app) { described_class.new(name: "foobar") }
17
+
18
+ it "capitalizes the name" do
19
+ expect(app.classify_name).to eq("Foobar")
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../spec_helper"
4
+
5
+ describe CreateRubyApp::FileGenerator do
6
+ describe "#generate_gemfile" do
7
+ context "when no additional gems are added" do
8
+ let(:file_generator) { described_class.new }
9
+
10
+ it "produces a Gemfile without any additional gems" do
11
+ expect(file_generator.generate_gemfile).to eq(
12
+ <<~GEMFILE
13
+ # frozen_string_literal: true
14
+
15
+ source "https://rubygems.org"
16
+
17
+ group :test, :development do
18
+ gem "rspec"
19
+ end
20
+ GEMFILE
21
+ )
22
+ end
23
+ end
24
+
25
+ context "when additional gems are added" do
26
+ let(:app) { instance_double("app") }
27
+ let(:file_generator) { described_class.new(app: app) }
28
+
29
+ it "produces a Gemfile with the gems" do
30
+ allow(app).to receive(:gems).and_return(%w[sinatra sqlite])
31
+
32
+ expect(file_generator.generate_gemfile).to eq(
33
+ <<~GEMFILE
34
+ # frozen_string_literal: true
35
+
36
+ source "https://rubygems.org"
37
+
38
+ group :test, :development do
39
+ gem "rspec"
40
+ end
41
+
42
+ gem "sinatra"
43
+ gem "sqlite"
44
+ GEMFILE
45
+ )
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "#generate_ruby_version_file" do
51
+ context "when no version is specified" do
52
+ let(:app) { instance_double("app") }
53
+ let(:file_generator) { described_class.new(app: app) }
54
+
55
+ it "uses version 2.6.0 as default" do
56
+ allow(app).to receive(:version).and_return("ruby-2.6.0")
57
+
58
+ expect(file_generator.generate_ruby_version_file).to eq("ruby-2.6.0")
59
+ end
60
+ end
61
+
62
+ context "when a version is specified" do
63
+ let(:app) { instance_double("app") }
64
+ let(:file_generator) { described_class.new(app: app) }
65
+
66
+ it "uses the version in the .ruby-version file" do
67
+ allow(app).to receive(:version).and_return("ruby-2.5.0")
68
+
69
+ expect(file_generator.generate_ruby_version_file).to eq("ruby-2.5.0")
70
+ end
71
+ end
72
+ end
73
+
74
+ describe "#generate_lib_file" do
75
+ context "when generating a lib file" do
76
+ let(:app) { instance_double("app") }
77
+ let(:file_generator) { described_class.new(app: app) }
78
+
79
+ it "fills out a lib file with the correct module and class name" do
80
+ allow(app).to receive(:name).and_return("this_is_an_app")
81
+ allow(app).to receive(:classify_name).and_return("ThisIsAnApp")
82
+
83
+ expect(file_generator.generate_lib_file).to eq(
84
+ <<~LIB_FILE
85
+ # frozen_string_literal: true
86
+
87
+ module ThisIsAnApp
88
+ class ThisIsAnApp
89
+ end
90
+ end
91
+ LIB_FILE
92
+ )
93
+ end
94
+ end
95
+ end
96
+
97
+ describe "#generate_spec_helper_file" do
98
+ context "when generating a spec helper file" do
99
+ let(:app) { instance_double("app") }
100
+ let(:file_generator) { described_class.new(app: app) }
101
+
102
+ it "fills out the file and references the app" do
103
+ allow(app).to receive(:name).and_return("this_is_an_app")
104
+
105
+ expect(file_generator.generate_spec_helper_file).to eq(
106
+ <<~SPEC_HELPER
107
+ # frozen_string_literal: true
108
+
109
+ require "rspec"
110
+ require_relative "../lib/this_is_an_app"
111
+
112
+ RSpec.configure do |config|
113
+ config.expect_with(:rspec) do |c|
114
+ c.syntax = :expect
115
+ end
116
+ end
117
+ SPEC_HELPER
118
+ )
119
+ end
120
+ end
121
+ end
122
+
123
+ describe "#generate_script_file" do
124
+ context "when generating an executable script file" do
125
+ let(:file_generator) { described_class.new }
126
+
127
+ it "fills out a default script file template" do
128
+ expect(file_generator.generate_script_file).to eq(
129
+ <<~SCRIPT_FILE
130
+ #!/usr/bin/env ruby
131
+
132
+ # frozen_string_literal: true
133
+ SCRIPT_FILE
134
+ )
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec"
4
+
5
+ require_relative "../lib/create_ruby_app"
6
+
7
+ RSpec.configure do |config|
8
+ config.expect_with(:rspec) do |c|
9
+ c.syntax = :expect
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: create-ruby-app
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mathias Jean Johansen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-03-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.20.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.20.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.8.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.8.0
55
+ description: |-
56
+ create-ruby-app is an opinionated tool for scaffolding Ruby
57
+ applications
58
+ email: mathias@mjj.io
59
+ executables:
60
+ - create-ruby-app
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - LICENSE
65
+ - README.md
66
+ - bin/create-ruby-app
67
+ - lib/create_ruby_app.rb
68
+ - lib/create_ruby_app/app.rb
69
+ - lib/create_ruby_app/cli.rb
70
+ - lib/create_ruby_app/file_generator.rb
71
+ - lib/create_ruby_app/templates/Gemfile.erb
72
+ - lib/create_ruby_app/templates/lib_file.erb
73
+ - lib/create_ruby_app/templates/ruby-version.erb
74
+ - lib/create_ruby_app/templates/script_file.erb
75
+ - lib/create_ruby_app/templates/spec_helper.erb
76
+ - lib/create_ruby_app/version.rb
77
+ - spec/lib/create_ruby_app/app_spec.rb
78
+ - spec/lib/create_ruby_app/file_generator_spec.rb
79
+ - spec/spec_helper.rb
80
+ homepage: https://github.com/majjoha/create-ruby-app
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubygems_version: 3.0.1
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Scaffold Ruby applications effortlessly
103
+ test_files:
104
+ - spec/spec_helper.rb
105
+ - spec/lib/create_ruby_app/file_generator_spec.rb
106
+ - spec/lib/create_ruby_app/app_spec.rb