create-ruby-app 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +8 -1
- data/lib/create_ruby_app.rb +5 -1
- data/lib/create_ruby_app/actions/create_directories.rb +18 -0
- data/lib/create_ruby_app/actions/generate_files.rb +83 -0
- data/lib/create_ruby_app/actions/install_gems.rb +11 -0
- data/lib/create_ruby_app/actions/make_script_executable.rb +13 -0
- data/lib/create_ruby_app/actions/null_action.rb +9 -0
- data/lib/create_ruby_app/app.rb +9 -13
- data/lib/create_ruby_app/cli.rb +12 -3
- data/lib/create_ruby_app/templates/spec_helper.erb +2 -0
- data/lib/create_ruby_app/version.rb +1 -1
- data/spec/lib/create_ruby_app/actions/create_directories_spec.rb +18 -0
- data/spec/lib/create_ruby_app/actions/generate_files_spec.rb +136 -0
- data/spec/lib/create_ruby_app/actions/install_gems_spec.rb +18 -0
- data/spec/lib/create_ruby_app/actions/make_script_executable_spec.rb +20 -0
- data/spec/lib/create_ruby_app/app_spec.rb +1 -1
- data/spec/spec_helper.rb +2 -0
- metadata +16 -5
- data/lib/create_ruby_app/file_generator.rb +0 -88
- data/spec/lib/create_ruby_app/file_generator_spec.rb +0 -138
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff8d6584b0b8947926a85ec594f77b14b2a2cd20c1257fa84a9eb340c8031082
|
4
|
+
data.tar.gz: 1d70581a447648a128dd208840c8faa709a1e7b7bfe51780cb3f4e0457716569
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af116d9f492b893d3bc020800330c4e84d76f13f69e0112286aa0170511ced20bc81e177732a3813f158acb5448115c1d0bd4ddcf752f2cdc0f13a753cb0d428
|
7
|
+
data.tar.gz: 459e38e43ad058ba50b916b57d7f8262507821a85db7b4b26e05135522ccfe08d9a9849868552021d9f8ed18f7f13f726a3bd62fc9f382464d00cbd99146eade
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,13 +1,20 @@
|
|
1
1
|
# Create Ruby App
|
2
2
|
[![Build Status](https://travis-ci.org/majjoha/create-ruby-app.svg)](https://travis-ci.org/majjoha/create-ruby-app)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/create-ruby-app.svg)](http://badge.fury.io/rb/create-ruby-app)
|
3
4
|
|
4
5
|
`create-ruby-app` is an opinionated tool for scaffolding Ruby applications
|
5
6
|
effortlessly inspired by [Create React
|
6
7
|
App](https://github.com/facebook/create-react-app). It generates only the
|
7
8
|
essentials needed to start working.
|
8
9
|
|
10
|
+
It specifically targets non-Rails applications. For Ruby on Rails apps, it might
|
11
|
+
be worth looking into [Rails Application
|
12
|
+
Templates](https://guides.rubyonrails.org/rails_application_templates.html)
|
13
|
+
instead, and if you are building a gem, please take a look at the `bundle gem`
|
14
|
+
command.
|
15
|
+
|
9
16
|
## Requirements
|
10
|
-
* Ruby (version 2.6.
|
17
|
+
* Ruby (version 2.6.2 or newer).
|
11
18
|
|
12
19
|
## Installation
|
13
20
|
```
|
data/lib/create_ruby_app.rb
CHANGED
@@ -2,8 +2,12 @@
|
|
2
2
|
|
3
3
|
require_relative "create_ruby_app/app"
|
4
4
|
require_relative "create_ruby_app/cli"
|
5
|
-
require_relative "create_ruby_app/file_generator"
|
6
5
|
require_relative "create_ruby_app/version"
|
6
|
+
require_relative "create_ruby_app/actions/create_directories"
|
7
|
+
require_relative "create_ruby_app/actions/generate_files"
|
8
|
+
require_relative "create_ruby_app/actions/install_gems"
|
9
|
+
require_relative "create_ruby_app/actions/make_script_executable"
|
10
|
+
require_relative "create_ruby_app/actions/null_action"
|
7
11
|
|
8
12
|
module CreateRubyApp
|
9
13
|
class CreateRubyApp
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "fileutils"
|
4
|
+
require "pathname"
|
5
|
+
|
6
|
+
module CreateRubyApp
|
7
|
+
module Actions
|
8
|
+
class CreateDirectories
|
9
|
+
def self.call(app)
|
10
|
+
[
|
11
|
+
Pathname.new("#{app.name}/bin"),
|
12
|
+
Pathname.new("#{app.name}/lib/#{app.name}"),
|
13
|
+
Pathname.new("#{app.name}/spec/lib/#{app.name}")
|
14
|
+
].each(&FileUtils.method(:mkdir_p))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "erb"
|
4
|
+
require "pathname"
|
5
|
+
|
6
|
+
module CreateRubyApp
|
7
|
+
module Actions
|
8
|
+
class GenerateFiles
|
9
|
+
def initialize(app)
|
10
|
+
@app = app
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.call(app)
|
14
|
+
new(app).call
|
15
|
+
end
|
16
|
+
|
17
|
+
def call
|
18
|
+
generate_files
|
19
|
+
end
|
20
|
+
|
21
|
+
def script_file
|
22
|
+
generate_file(file: "script_file.erb", locals: {})
|
23
|
+
end
|
24
|
+
|
25
|
+
def lib_file
|
26
|
+
generate_file(file: "lib_file.erb", locals: { app: app.classify_name })
|
27
|
+
end
|
28
|
+
|
29
|
+
def spec_helper_file
|
30
|
+
generate_file(file: "spec_helper.erb", locals: { app: app.name })
|
31
|
+
end
|
32
|
+
|
33
|
+
def ruby_version_file
|
34
|
+
generate_file(
|
35
|
+
file: "ruby-version.erb",
|
36
|
+
locals: { version: app.version }
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
def gemfile
|
41
|
+
generate_file(file: "Gemfile.erb", locals: { gems: app.gems.sort })
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
attr_reader :app
|
47
|
+
|
48
|
+
def generate_files
|
49
|
+
files.each {|path, content| create_file(path: path, content: content) }
|
50
|
+
end
|
51
|
+
|
52
|
+
def create_file(path:, content:)
|
53
|
+
File.write("#{app.name}/#{path}", content)
|
54
|
+
end
|
55
|
+
|
56
|
+
def generate_file(file:, locals: [])
|
57
|
+
ERB
|
58
|
+
.new(read_file(file), trim_mode: TRIM_MODE)
|
59
|
+
.result_with_hash(locals: locals)
|
60
|
+
end
|
61
|
+
|
62
|
+
def read_file(file)
|
63
|
+
Pathname.new(__FILE__)
|
64
|
+
.dirname
|
65
|
+
.join("#{TEMPLATES_DIR}/#{file}")
|
66
|
+
.read
|
67
|
+
end
|
68
|
+
|
69
|
+
def files
|
70
|
+
{
|
71
|
+
"bin/#{app.name}" => script_file,
|
72
|
+
"lib/#{app.name}.rb" => lib_file,
|
73
|
+
"spec/spec_helper.rb" => spec_helper_file,
|
74
|
+
".ruby-version" => ruby_version_file,
|
75
|
+
"Gemfile" => gemfile
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
TRIM_MODE = "<>"
|
80
|
+
TEMPLATES_DIR = "../templates"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
data/lib/create_ruby_app/app.rb
CHANGED
@@ -17,30 +17,26 @@ module CreateRubyApp
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def run!
|
20
|
-
with_logger("Creating directories...",
|
21
|
-
with_logger("Generating files...",
|
22
|
-
with_logger("Making script executable...",
|
23
|
-
with_logger("Installing gems...",
|
24
|
-
with_logger("Happy hacking!")
|
20
|
+
with_logger("Creating directories...", Actions::CreateDirectories)
|
21
|
+
with_logger("Generating files...", Actions::GenerateFiles)
|
22
|
+
with_logger("Making script executable...", Actions::MakeScriptExecutable)
|
23
|
+
with_logger("Installing gems...", Actions::InstallGems)
|
24
|
+
with_logger("Happy hacking!", Actions::NullAction)
|
25
25
|
end
|
26
26
|
|
27
27
|
def classify_name
|
28
28
|
name.split("_").collect(&:capitalize).join
|
29
29
|
end
|
30
30
|
|
31
|
-
|
31
|
+
attr_reader :name, :gems, :version, :logger
|
32
32
|
|
33
|
-
RUBY_VERSION = "ruby-2.6.
|
33
|
+
RUBY_VERSION = "ruby-2.6.2"
|
34
34
|
|
35
35
|
private
|
36
36
|
|
37
|
-
def with_logger(text,
|
37
|
+
def with_logger(text, action)
|
38
38
|
logger.info(text)
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
def file_generator
|
43
|
-
@file_generator ||= FileGenerator.new(app: self)
|
39
|
+
action.call(self)
|
44
40
|
end
|
45
41
|
end
|
46
42
|
end
|
data/lib/create_ruby_app/cli.rb
CHANGED
@@ -23,9 +23,18 @@ module CreateRubyApp
|
|
23
23
|
\x5$ create-ruby-app new -g sinatra,sequel -r ruby-2.6.0 web_app
|
24
24
|
\x5$ create-ruby-app new --ruby jruby-2.9.6.0 my_app
|
25
25
|
DESC
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
method_option(
|
27
|
+
:ruby,
|
28
|
+
aliases: "-r",
|
29
|
+
desc: "Specify which Ruby version to use for the project",
|
30
|
+
default: App::RUBY_VERSION
|
31
|
+
)
|
32
|
+
method_option(
|
33
|
+
:gems,
|
34
|
+
aliases: "-g",
|
35
|
+
desc: "Specify which gems to add to the project",
|
36
|
+
default: ""
|
37
|
+
)
|
29
38
|
def new(name)
|
30
39
|
App.new(
|
31
40
|
name: replace_dashes_with_underscores(name),
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../../../spec_helper"
|
4
|
+
|
5
|
+
RSpec.describe CreateRubyApp::Actions::CreateDirectories do
|
6
|
+
describe ".call" do
|
7
|
+
let(:app) { instance_double("app", name: "foo_bar") }
|
8
|
+
let(:action) { described_class }
|
9
|
+
|
10
|
+
it "creates the directories" do
|
11
|
+
allow(FileUtils).to receive(:mkdir_p)
|
12
|
+
|
13
|
+
action.call(app)
|
14
|
+
|
15
|
+
expect(FileUtils).to have_received(:mkdir_p).exactly(3).times
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../../../spec_helper"
|
4
|
+
|
5
|
+
RSpec.describe CreateRubyApp::Actions::GenerateFiles do
|
6
|
+
describe "#generate_gemfile" do
|
7
|
+
context "when no additional gems are added" do
|
8
|
+
let(:app) { instance_double("app", gems: []) }
|
9
|
+
let(:action) { described_class.new(app) }
|
10
|
+
let(:gemfile) do
|
11
|
+
<<~GEMFILE
|
12
|
+
# frozen_string_literal: true
|
13
|
+
|
14
|
+
source "https://rubygems.org"
|
15
|
+
|
16
|
+
group :test, :development do
|
17
|
+
gem "rspec"
|
18
|
+
end
|
19
|
+
GEMFILE
|
20
|
+
end
|
21
|
+
|
22
|
+
it "produces a Gemfile without any additional gems" do
|
23
|
+
expect(action.gemfile).to eq(gemfile)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when additional gems are added" do
|
28
|
+
let(:app) { instance_double("app", gems: %w[sinatra sqlite]) }
|
29
|
+
let(:action) { described_class.new(app) }
|
30
|
+
let(:gemfile) do
|
31
|
+
<<~GEMFILE
|
32
|
+
# frozen_string_literal: true
|
33
|
+
|
34
|
+
source "https://rubygems.org"
|
35
|
+
|
36
|
+
group :test, :development do
|
37
|
+
gem "rspec"
|
38
|
+
end
|
39
|
+
|
40
|
+
gem "sinatra"
|
41
|
+
gem "sqlite"
|
42
|
+
GEMFILE
|
43
|
+
end
|
44
|
+
|
45
|
+
it "produces a Gemfile with the gems" do
|
46
|
+
expect(action.gemfile).to eq(gemfile)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "#generate_ruby_version_file" do
|
52
|
+
context "when no version is specified" do
|
53
|
+
let(:app) do
|
54
|
+
instance_double("app", version: CreateRubyApp::App::RUBY_VERSION)
|
55
|
+
end
|
56
|
+
let(:action) { described_class.new(app) }
|
57
|
+
|
58
|
+
it { expect(action.ruby_version_file).to eq("ruby-2.6.2") }
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when a version is specified" do
|
62
|
+
let(:app) { instance_double("app", version: "ruby-2.5.0") }
|
63
|
+
let(:action) { described_class.new(app) }
|
64
|
+
|
65
|
+
it { expect(action.ruby_version_file).to eq("ruby-2.5.0") }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "#generate_lib_file" do
|
70
|
+
context "when generating a lib file" do
|
71
|
+
let(:app) { instance_double("app") }
|
72
|
+
let(:action) { described_class.new(app) }
|
73
|
+
let(:lib_file) do
|
74
|
+
<<~LIB_FILE
|
75
|
+
# frozen_string_literal: true
|
76
|
+
|
77
|
+
module ThisIsAnApp
|
78
|
+
class ThisIsAnApp
|
79
|
+
end
|
80
|
+
end
|
81
|
+
LIB_FILE
|
82
|
+
end
|
83
|
+
|
84
|
+
it "fills out a lib file with the correct module and class name" do
|
85
|
+
allow(app).to receive(:classify_name).and_return("ThisIsAnApp")
|
86
|
+
|
87
|
+
expect(action.lib_file).to eq(lib_file)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe "#generate_spec_helper_file" do
|
93
|
+
context "when generating a spec helper file" do
|
94
|
+
let(:app) { instance_double("app", name: "this_is_an_app") }
|
95
|
+
let(:action) { described_class.new(app) }
|
96
|
+
let(:spec_helper) do
|
97
|
+
<<~SPEC_HELPER
|
98
|
+
# frozen_string_literal: true
|
99
|
+
|
100
|
+
require "rspec"
|
101
|
+
require_relative "../lib/this_is_an_app"
|
102
|
+
|
103
|
+
RSpec.configure do |config|
|
104
|
+
config.expect_with(:rspec) do |c|
|
105
|
+
c.syntax = :expect
|
106
|
+
end
|
107
|
+
|
108
|
+
config.disable_monkey_patching!
|
109
|
+
end
|
110
|
+
SPEC_HELPER
|
111
|
+
end
|
112
|
+
|
113
|
+
it "fills out the file and references the app" do
|
114
|
+
expect(action.spec_helper_file).to eq(spec_helper)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "#generate_script_file" do
|
120
|
+
context "when generating an executable script file" do
|
121
|
+
let(:app) { instance_double("app") }
|
122
|
+
let(:action) { described_class.new(app) }
|
123
|
+
let(:script_file) do
|
124
|
+
<<~SCRIPT_FILE
|
125
|
+
#!/usr/bin/env ruby
|
126
|
+
|
127
|
+
# frozen_string_literal: true
|
128
|
+
SCRIPT_FILE
|
129
|
+
end
|
130
|
+
|
131
|
+
it "fills out a default script file template" do
|
132
|
+
expect(action.script_file).to eq(script_file)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../../../spec_helper"
|
4
|
+
|
5
|
+
RSpec.describe CreateRubyApp::Actions::InstallGems do
|
6
|
+
describe ".call" do
|
7
|
+
let(:app) { instance_double("app", name: "foo_bar") }
|
8
|
+
let(:action) { described_class }
|
9
|
+
|
10
|
+
it "installs the gems" do
|
11
|
+
allow(Dir).to receive(:chdir).with("foo_bar")
|
12
|
+
|
13
|
+
action.call(app)
|
14
|
+
|
15
|
+
expect(Dir).to have_received(:chdir).with("foo_bar")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../../../spec_helper"
|
4
|
+
|
5
|
+
RSpec.describe CreateRubyApp::Actions::MakeScriptExecutable do
|
6
|
+
describe ".call" do
|
7
|
+
let(:app) { instance_double("app", name: "foo_bar") }
|
8
|
+
let(:action) { described_class }
|
9
|
+
|
10
|
+
it "creates the directories" do
|
11
|
+
allow(FileUtils).to receive(:chmod).with("+x", "foo_bar/bin/foo_bar")
|
12
|
+
|
13
|
+
action.call(app)
|
14
|
+
|
15
|
+
expect(FileUtils).to(
|
16
|
+
have_received(:chmod).with("+x", "foo_bar/bin/foo_bar")
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: create-ruby-app
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mathias Jean Johansen
|
@@ -61,21 +61,29 @@ executables:
|
|
61
61
|
extensions: []
|
62
62
|
extra_rdoc_files: []
|
63
63
|
files:
|
64
|
+
- CHANGELOG.md
|
64
65
|
- LICENSE
|
65
66
|
- README.md
|
66
67
|
- bin/create-ruby-app
|
67
68
|
- lib/create_ruby_app.rb
|
69
|
+
- lib/create_ruby_app/actions/create_directories.rb
|
70
|
+
- lib/create_ruby_app/actions/generate_files.rb
|
71
|
+
- lib/create_ruby_app/actions/install_gems.rb
|
72
|
+
- lib/create_ruby_app/actions/make_script_executable.rb
|
73
|
+
- lib/create_ruby_app/actions/null_action.rb
|
68
74
|
- lib/create_ruby_app/app.rb
|
69
75
|
- lib/create_ruby_app/cli.rb
|
70
|
-
- lib/create_ruby_app/file_generator.rb
|
71
76
|
- lib/create_ruby_app/templates/Gemfile.erb
|
72
77
|
- lib/create_ruby_app/templates/lib_file.erb
|
73
78
|
- lib/create_ruby_app/templates/ruby-version.erb
|
74
79
|
- lib/create_ruby_app/templates/script_file.erb
|
75
80
|
- lib/create_ruby_app/templates/spec_helper.erb
|
76
81
|
- lib/create_ruby_app/version.rb
|
82
|
+
- spec/lib/create_ruby_app/actions/create_directories_spec.rb
|
83
|
+
- spec/lib/create_ruby_app/actions/generate_files_spec.rb
|
84
|
+
- spec/lib/create_ruby_app/actions/install_gems_spec.rb
|
85
|
+
- spec/lib/create_ruby_app/actions/make_script_executable_spec.rb
|
77
86
|
- spec/lib/create_ruby_app/app_spec.rb
|
78
|
-
- spec/lib/create_ruby_app/file_generator_spec.rb
|
79
87
|
- spec/spec_helper.rb
|
80
88
|
homepage: https://github.com/majjoha/create-ruby-app
|
81
89
|
licenses:
|
@@ -96,11 +104,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
104
|
- !ruby/object:Gem::Version
|
97
105
|
version: '0'
|
98
106
|
requirements: []
|
99
|
-
rubygems_version: 3.0.
|
107
|
+
rubygems_version: 3.0.3
|
100
108
|
signing_key:
|
101
109
|
specification_version: 4
|
102
110
|
summary: Scaffold Ruby applications effortlessly
|
103
111
|
test_files:
|
104
112
|
- spec/spec_helper.rb
|
105
|
-
- spec/lib/create_ruby_app/
|
113
|
+
- spec/lib/create_ruby_app/actions/make_script_executable_spec.rb
|
114
|
+
- spec/lib/create_ruby_app/actions/create_directories_spec.rb
|
115
|
+
- spec/lib/create_ruby_app/actions/generate_files_spec.rb
|
116
|
+
- spec/lib/create_ruby_app/actions/install_gems_spec.rb
|
106
117
|
- spec/lib/create_ruby_app/app_spec.rb
|
@@ -1,88 +0,0 @@
|
|
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
|
@@ -1,138 +0,0 @@
|
|
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
|