nrb 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +2 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +12 -1093
- data/README.md +79 -101
- data/Rakefile +5 -5
- data/bin/console +2 -10
- data/lib/nrb/cli.rb +11 -5
- data/lib/nrb/commands/base/base.rb +6 -5
- data/lib/nrb/commands/base/inside.rb +4 -1
- data/lib/nrb/commands/base/resource.rb +3 -1
- data/lib/nrb/commands/concerns/resource_generator.rb +3 -1
- data/lib/nrb/commands/concerns/script_generator.rb +59 -0
- data/lib/nrb/commands/console.rb +1 -0
- data/lib/nrb/commands/destroy.rb +5 -4
- data/lib/nrb/commands/generate.rb +14 -7
- data/lib/nrb/commands/script.rb +11 -48
- data/lib/nrb/commands/starter.rb +1 -0
- data/lib/nrb/errors.rb +7 -2
- data/lib/nrb/templates/config/boot.rb.tt +2 -2
- data/lib/nrb/templates/config/nrb.rb.tt +2 -2
- data/lib/nrb/utils.rb +14 -0
- data/lib/nrb/version.rb +1 -1
- data/lib/nrb.rb +9 -9
- data/nrb.gemspec +2 -2
- metadata +19 -17
data/README.md
CHANGED
@@ -1,138 +1,121 @@
|
|
1
|
-
# Ninja Ruby -
|
2
|
-
|
3
|
-
[![Build Status](https://travis-ci.org/shuriu/nrb.svg?branch=master)](https://travis-ci.org/shuriu/nrb) [![Coverage Status](https://coveralls.io/repos/github/shuriu/nrb/badge.svg?branch=master)](https://coveralls.io/github/shuriu/nrb?branch=master) [![Code Climate](https://codeclimate.com/github/shuriu/nrb/badges/gpa.svg)](https://codeclimate.com/github/shuriu/nrb) [![Dependency Status](https://gemnasium.com/shuriu/nrb.svg)](https://gemnasium.com/shuriu/nrb)
|
1
|
+
# `nrb` - Ninja Ruby scripts - [![Gem Version](https://badge.fury.io/rb/nrb.svg)](https://badge.fury.io/rb/nrb)
|
4
2
|
|
5
3
|
Ninja Ruby scripts with easy persistence for your experimenting needs.
|
6
4
|
|
7
|
-
|
5
|
+
Do you want to work on some idea that's more than a script but you don't want to generate a fully fleged Rails app?
|
8
6
|
|
9
|
-
|
7
|
+
**`nrb`** generates a simple scaffold for your script. Complete with autoloading, custom configurations and persistence. Just like a mini rails.
|
10
8
|
|
11
|
-
|
9
|
+
## Getting Started
|
12
10
|
|
13
|
-
|
11
|
+
1. First install the gem:
|
14
12
|
|
15
|
-
|
13
|
+
$ gem install nrb
|
16
14
|
|
17
|
-
|
15
|
+
2. Create your new Ninja Ruy project:
|
18
16
|
|
19
|
-
|
17
|
+
$ nrb new my-project
|
20
18
|
|
21
|
-
|
19
|
+
`my-project` is your project name.
|
22
20
|
|
23
|
-
|
21
|
+
3. Change the directory to `my-project` and start hacking:
|
24
22
|
|
25
|
-
|
23
|
+
$ cd my-project
|
26
24
|
|
27
|
-
|
25
|
+
├── config
|
26
|
+
│ ├── boot.rb
|
27
|
+
│ └── nrb.rb
|
28
|
+
├── db
|
29
|
+
│ └── config.yml
|
30
|
+
├── models
|
31
|
+
├── services
|
32
|
+
├── Gemfile
|
33
|
+
├── my-project.rb
|
34
|
+
├── Rakefile
|
35
|
+
└── README.md
|
28
36
|
|
29
|
-
|
37
|
+
4. Your main file is `my-project.rb`. Add your ideas here. You can also add more gems to the `Gemfile` and they will be automatically required.
|
30
38
|
|
31
|
-
|
39
|
+
You can also change the `config/nrb.rb` file to add custom configurations.
|
32
40
|
|
33
|
-
|
41
|
+
Also, everything you add to `models` and `services` will be required automatically.
|
34
42
|
|
35
|
-
|
43
|
+
5. To run your script:
|
36
44
|
|
37
|
-
|
45
|
+
$ nrb start
|
38
46
|
|
39
|
-
|
47
|
+
And to start the console with your script loaded:
|
40
48
|
|
41
|
-
|
49
|
+
$ nrb console
|
42
50
|
|
43
|
-
##
|
51
|
+
## Models
|
44
52
|
|
45
|
-
|
46
|
-
$ nrb help
|
47
|
-
Commands:
|
48
|
-
nrb [c]onsole # Jump into a Pry console with your project loaded. NOTE: Available inside a NinjaRuby project
|
49
|
-
nrb [d]estroy <resource> <name> # Destroy a generated resource. NOTE: Available inside a NinjaRuby project
|
50
|
-
nrb [g]enerate <resource> <name> # Generate a resource (models, services). NOTE: Available inside a NinjaRuby project
|
51
|
-
nrb [s]tart # Require the main file. NOTE: Available inside a NinjaRuby project
|
52
|
-
nrb help [COMMAND] # Describe available commands or one specific command
|
53
|
-
nrb new <name-or-path> [OPTIONS] # Creates a Ninja Ruby Script at the given path
|
54
|
-
```
|
53
|
+
You can enjoy all the goodness `ActiveRecord` provides, outside Rails, and without the bolierplate needed. The commands are almost the same as in Rails.
|
55
54
|
|
56
|
-
Create
|
57
|
-
|
58
|
-
```sh
|
59
|
-
$ nrb help new
|
60
|
-
Usage:
|
61
|
-
nrb new <name-or-path> [OPTIONS]
|
62
|
-
|
63
|
-
Options:
|
64
|
-
-r, [--init-repo], [--no-init-repo] # Initialize a repository at the target location
|
65
|
-
# Default: true
|
66
|
-
-b, [--bundle-install], [--no-bundle-install] # Run bundle install after generating the skeleton
|
67
|
-
# Default: true
|
68
|
-
Creates a Ninja Ruby Script at the given path
|
69
|
-
|
70
|
-
$ nrb new user_importer
|
71
|
-
create user_importer/.gitignore
|
72
|
-
create user_importer/README.md
|
73
|
-
create user_importer/Gemfile
|
74
|
-
create user_importer/Rakefile
|
75
|
-
create user_importer/config/nrb.rb
|
76
|
-
create user_importer/models/.keep
|
77
|
-
create user_importer/services/.keep
|
78
|
-
create user_importer/config/boot.rb
|
79
|
-
create user_importer/db/config.yml
|
80
|
-
create user_importer/user_importer.rb
|
81
|
-
$ cd user_importer
|
82
|
-
|
83
|
-
$ rake db:create
|
84
|
-
|
85
|
-
$ nrb g model user first_name last_name email
|
86
|
-
create models/user.rb
|
87
|
-
run rake db:new_migration name=create_users options='first_name last_name email' from "."
|
88
|
-
create db/migrate/20160309074029_create_users.rb
|
89
|
-
|
90
|
-
$ rake db:migrate
|
91
|
-
|
92
|
-
$ nrb console
|
93
|
-
|
94
|
-
[1] pry(main)> User.create(first_name: 'Gandalf', last_name: 'The Grey', email: 'gandalf@example.com')
|
95
|
-
D, [2016-03-09T09:44:12.434679 #21739] DEBUG -- : (0.1ms) begin transaction
|
96
|
-
D, [2016-03-09T09:44:12.440635 #21739] DEBUG -- : SQL (0.5ms) INSERT INTO "users" ("first_name", "last_name", "email") VALUES (?, ?, ?) [["first_name", "Gandalf"], ["last_name", "The Grey"], ["email", "gandalf@example.com"]]
|
97
|
-
D, [2016-03-09T09:44:12.444638 #21739] DEBUG -- : (3.6ms) commit transaction
|
98
|
-
=> #<User:0x005582ec4153d8 id: 1, first_name: "Gandalf", last_name: "The Grey", email: "gandalf@example.com">
|
99
|
-
|
100
|
-
[2] pry(main)> User.all
|
101
|
-
D, [2016-03-09T09:45:11.252371 #21739] DEBUG -- : User Load (0.2ms) SELECT "users".* FROM "users"
|
102
|
-
=> [#<User:0x005582ed31c3e0 id: 1, first_name: "Gandalf", last_name: "The Grey", email: "gandalf@example.com">]
|
55
|
+
1. Create the database:
|
103
56
|
|
104
|
-
|
57
|
+
$ rake db:create
|
58
|
+
|
59
|
+
2. Create a model:
|
60
|
+
|
61
|
+
$ nrb g model user username email
|
62
|
+
|
63
|
+
db/migrate
|
64
|
+
└── 20160311091641_create_users.rb
|
65
|
+
models
|
66
|
+
└── user.rb
|
67
|
+
|
68
|
+
|
69
|
+
You can then edit the migration and the model as you please.
|
70
|
+
|
71
|
+
3. Finally run the migration!
|
72
|
+
|
73
|
+
$ rake db:migrate
|
74
|
+
|
75
|
+
## Services
|
76
|
+
|
77
|
+
These are just simple classes with the added benefit of being automatically Required in your main script file (the `my-project.rb` from the examples above).
|
78
|
+
|
79
|
+
To add a service:
|
80
|
+
|
81
|
+
$ nrb g service user_service
|
82
|
+
|
83
|
+
services
|
84
|
+
└── user_service.rb
|
105
85
|
|
106
86
|
## Configuration
|
107
87
|
|
108
|
-
|
88
|
+
You can add or change configurations inside `config/nrb.rb`:
|
109
89
|
|
110
90
|
```ruby
|
111
91
|
Nrb.configure do |config|
|
112
92
|
# Root of the script folder
|
113
93
|
config.root = File.expand_path('..', __dir__)
|
114
94
|
|
115
|
-
# Default
|
116
|
-
# config.
|
95
|
+
# Default directories to autoload
|
96
|
+
# config.autoload = %w(models services)
|
97
|
+
|
98
|
+
# My custom config
|
99
|
+
config.my_custom_config = :foo
|
117
100
|
end
|
118
101
|
```
|
119
102
|
|
120
|
-
|
121
|
-
|
122
|
-
```yaml
|
123
|
-
development:
|
124
|
-
adapter: sqlite3
|
125
|
-
pool: 5
|
126
|
-
timeout: 5000
|
127
|
-
database: db/new_script.sqlite3
|
128
|
-
```
|
103
|
+
You can access them by simply calling: `Nrb.config.my_custom_config`.
|
129
104
|
|
130
105
|
## Roadmap
|
131
106
|
|
132
|
-
- [
|
133
|
-
- [ ]
|
134
|
-
- [ ]
|
135
|
-
- [ ]
|
107
|
+
- [ ] Add generator for tests.
|
108
|
+
- [ ] Add support for custom bundler groups.
|
109
|
+
- [ ] Add ability to change the persistence adapter. Currently `sqlite` is the default.
|
110
|
+
- [ ] Add ability to skip persistence altogether.
|
111
|
+
|
112
|
+
## Contributing
|
113
|
+
|
114
|
+
General feedback, bug reports and pull requests are more than welcome!
|
115
|
+
|
116
|
+
## Status
|
117
|
+
|
118
|
+
[![Build Status](https://travis-ci.org/shuriu/nrb.svg?branch=master)](https://travis-ci.org/shuriu/nrb) [![Coverage Status](https://coveralls.io/repos/github/shuriu/nrb/badge.svg?branch=master)](https://coveralls.io/github/shuriu/nrb?branch=master) [![Code Climate](https://codeclimate.com/github/shuriu/nrb/badges/gpa.svg)](https://codeclimate.com/github/shuriu/nrb) [![Dependency Status](https://gemnasium.com/shuriu/nrb.svg)](https://gemnasium.com/shuriu/nrb)
|
136
119
|
|
137
120
|
## Development
|
138
121
|
|
@@ -140,11 +123,6 @@ Run `rake test` to run the tests. You can also run `bin/console` for an interact
|
|
140
123
|
|
141
124
|
To install this gem onto your local machine, run `bundle exec rake install`.
|
142
125
|
|
143
|
-
## Contributing
|
144
|
-
|
145
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/shuriu/nrb.
|
146
|
-
|
147
126
|
## License
|
148
127
|
|
149
128
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
150
|
-
|
data/Rakefile
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
3
|
|
4
4
|
Rake::TestTask.new(:test) do |t|
|
5
|
-
t.libs <<
|
6
|
-
t.libs <<
|
5
|
+
t.libs << 'test'
|
6
|
+
t.libs << 'lib'
|
7
7
|
t.test_files = FileList['test/**/*_test.rb']
|
8
8
|
end
|
9
9
|
|
10
|
-
task :
|
10
|
+
task default: :test
|
data/bin/console
CHANGED
@@ -1,14 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'nrb'
|
5
5
|
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
require "pry"
|
11
6
|
Pry.start
|
12
|
-
|
13
|
-
# require "irb"
|
14
|
-
# IRB.start
|
data/lib/nrb/cli.rb
CHANGED
@@ -4,28 +4,34 @@ require 'thor'
|
|
4
4
|
require 'nrb/commands/all'
|
5
5
|
|
6
6
|
module Nrb
|
7
|
+
# Command line interface entry point.
|
7
8
|
class CLI < Thor
|
8
9
|
class_option :verbose, type: :boolean, default: true,
|
9
10
|
desc: 'Verbose mode.',
|
10
11
|
aliases: '-v'
|
11
12
|
|
12
|
-
register Nrb::Commands::Script, 'new',
|
13
|
+
register Nrb::Commands::Script, 'new',
|
14
|
+
'new <name-or-path> [OPTIONS]',
|
13
15
|
Nrb::Commands::Script.desc
|
14
16
|
tasks['new'].options = Nrb::Commands::Script.class_options
|
15
17
|
|
16
|
-
register Nrb::Commands::Starter, 'start',
|
18
|
+
register Nrb::Commands::Starter, 'start',
|
19
|
+
'[s]tart',
|
17
20
|
Nrb::Commands::Starter.desc
|
18
21
|
map 's' => :start
|
19
22
|
|
20
|
-
register Nrb::Commands::Console, 'console',
|
23
|
+
register Nrb::Commands::Console, 'console',
|
24
|
+
'[c]onsole',
|
21
25
|
Nrb::Commands::Console.desc
|
22
26
|
map 'c' => :console
|
23
27
|
|
24
|
-
register Nrb::Commands::Generate, 'generate',
|
28
|
+
register Nrb::Commands::Generate, 'generate',
|
29
|
+
'[g]enerate <resource> <name>',
|
25
30
|
Nrb::Commands::Generate.desc
|
26
31
|
map 'g' => :generate
|
27
32
|
|
28
|
-
register Nrb::Commands::Destroy, 'destroy',
|
33
|
+
register Nrb::Commands::Destroy, 'destroy',
|
34
|
+
'[d]estroy <resource> <name>',
|
29
35
|
Nrb::Commands::Destroy.desc
|
30
36
|
map 'd' => :destroy
|
31
37
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
module Nrb
|
2
2
|
module Commands
|
3
|
+
# This class is the base class for other thor groups.
|
3
4
|
class Base < Thor::Group
|
4
5
|
include Thor::Actions
|
5
6
|
|
@@ -18,12 +19,12 @@ module Nrb
|
|
18
19
|
private
|
19
20
|
|
20
21
|
def require_main_file
|
21
|
-
|
22
|
-
|
22
|
+
root = Nrb.root
|
23
|
+
assumed_file_name = "#{root.split('/').last}.rb"
|
24
|
+
assumed_file_path = File.join(root, assumed_file_name)
|
25
|
+
return false unless File.exist?(assumed_file_path)
|
23
26
|
|
24
|
-
|
25
|
-
require File.join(Nrb.root, assumed_file_name)
|
26
|
-
end
|
27
|
+
require assumed_file_path
|
27
28
|
end
|
28
29
|
end
|
29
30
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module Nrb
|
2
2
|
module Commands
|
3
|
+
# This class fires a task that checks if the following command
|
4
|
+
# is done inside the root of the project.
|
3
5
|
class Inside < Commands::Base
|
4
6
|
def ensure_inside_root
|
5
7
|
return true if Nrb.inside?
|
@@ -12,7 +14,8 @@ module Nrb
|
|
12
14
|
command = self.class.to_s.split('::').last
|
13
15
|
|
14
16
|
fail Nrb::OutsideRootError,
|
15
|
-
|
17
|
+
'You need to be inside a NinjaRuby folder' \
|
18
|
+
"to run the #{command} command."
|
16
19
|
end
|
17
20
|
end
|
18
21
|
end
|
@@ -1,10 +1,12 @@
|
|
1
1
|
module Nrb
|
2
2
|
module Commands
|
3
|
+
# This class fires a task that checks if the following command
|
4
|
+
# is done on the correct resource type.
|
3
5
|
class Resource < Commands::Inside
|
4
6
|
include ResourceGenerator
|
5
7
|
|
6
8
|
def self.valid_resources
|
7
|
-
arguments.find { |
|
9
|
+
arguments.find { |arg| arg.name == 'resource' }.enum
|
8
10
|
end
|
9
11
|
|
10
12
|
def ensure_valid_resource
|
@@ -2,6 +2,8 @@ require 'active_support/concern'
|
|
2
2
|
|
3
3
|
module Nrb
|
4
4
|
module Commands
|
5
|
+
# This module adds the :resource, and the :name to resource generator
|
6
|
+
# commands (generate or destroy).
|
5
7
|
module ResourceGenerator
|
6
8
|
extend ActiveSupport::Concern
|
7
9
|
|
@@ -9,7 +11,7 @@ module Nrb
|
|
9
11
|
argument :resource, type: :string, required: true,
|
10
12
|
desc: 'resource type',
|
11
13
|
banner: 'resource',
|
12
|
-
enum: Nrb.
|
14
|
+
enum: Nrb.resources.map(&:singularize)
|
13
15
|
|
14
16
|
argument :name, type: :string, required: true,
|
15
17
|
desc: 'name of the resource',
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module Nrb
|
4
|
+
module Commands
|
5
|
+
# This module adds the :path_or_folder_name required argument,
|
6
|
+
# and some helper flags like: --init-repo, --bundle-install, --local.
|
7
|
+
module ScriptGenerator
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
argument :path_or_folder_name, type: :string, required: true,
|
12
|
+
desc: 'The name of the project, or the path.'
|
13
|
+
|
14
|
+
class_option :init_repo, default: true, type: :boolean,
|
15
|
+
desc: 'Initialize a repository at the target location.',
|
16
|
+
aliases: '-r'
|
17
|
+
|
18
|
+
class_option :bundle_install, default: false, type: :boolean,
|
19
|
+
desc: 'Run bundle install after generating the skeleton.',
|
20
|
+
aliases: '-b'
|
21
|
+
|
22
|
+
class_option :local, default: false, type: :boolean,
|
23
|
+
desc: 'Add local path of the gem when ' \
|
24
|
+
'generating the Gemfile. Useful for testing.'
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def try_loud_command(command)
|
30
|
+
opts[:verbose] ? run(command) : silent_command(command)
|
31
|
+
end
|
32
|
+
|
33
|
+
def silent_command(command)
|
34
|
+
Nrb::Utils.silently { run command }
|
35
|
+
end
|
36
|
+
|
37
|
+
def target(final = nil)
|
38
|
+
File.join(File.expand_path(path_or_folder_name), final.to_s)
|
39
|
+
end
|
40
|
+
|
41
|
+
def name
|
42
|
+
File.basename(path_or_folder_name)
|
43
|
+
end
|
44
|
+
|
45
|
+
def nrb_gem
|
46
|
+
text = "gem 'nrb', '#{Nrb::VERSION}'"
|
47
|
+
add_local_path(text) if options[:local]
|
48
|
+
text
|
49
|
+
end
|
50
|
+
|
51
|
+
def add_local_path(text)
|
52
|
+
local_gem_path = Pathname.new(File.expand_path('../../..', __dir__))
|
53
|
+
target_path = Pathname.new(target)
|
54
|
+
relative_path = local_gem_path.relative_path_from(target_path)
|
55
|
+
text << ", path: '#{relative_path}'"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/nrb/commands/console.rb
CHANGED
data/lib/nrb/commands/destroy.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require 'nrb/commands/concerns/resource_generator'
|
2
|
-
|
3
1
|
module Nrb
|
4
2
|
module Commands
|
3
|
+
# This command destroys a resource, and if it is a model, it also destroys
|
4
|
+
# the correspnding create migration file.
|
5
5
|
class Destroy < Commands::Resource
|
6
6
|
desc 'Destroy a generated resource.'
|
7
7
|
|
@@ -11,8 +11,9 @@ module Nrb
|
|
11
11
|
|
12
12
|
def destroy_migration
|
13
13
|
return false unless resource == 'model'
|
14
|
-
|
15
|
-
|
14
|
+
prefix = 'db/migrate/*_create_'
|
15
|
+
migration_file = Dir["#{prefix}#{name.underscore.pluralize}.rb"].first
|
16
|
+
remove_file(migration_file, opts)
|
16
17
|
end
|
17
18
|
end
|
18
19
|
end
|
@@ -1,26 +1,33 @@
|
|
1
1
|
module Nrb
|
2
2
|
module Commands
|
3
|
+
# This command generates a resource, and if it is a model, it also
|
4
|
+
# generates the correspnding create migration file.
|
3
5
|
class Generate < Commands::Resource
|
4
6
|
desc "Generate a resource (#{valid_resources.join(', ')})."
|
5
7
|
|
6
8
|
def generate_resource
|
7
9
|
template "templates/#{resource}.rb.tt", target("#{name.underscore}.rb"),
|
8
|
-
opts.merge(
|
10
|
+
opts.merge(name: name.camelize)
|
9
11
|
end
|
10
12
|
|
11
13
|
def generate_table
|
12
14
|
return false unless resource == 'model'
|
13
15
|
|
14
|
-
migration_name = "create_#{name.underscore.pluralize}"
|
15
|
-
rake_options = args.join(' ')
|
16
|
-
|
17
16
|
inside Nrb.root, opts do
|
18
|
-
Nrb.silently
|
19
|
-
|
20
|
-
opts
|
17
|
+
Nrb::Utils.silently do
|
18
|
+
generate_table_file
|
21
19
|
end
|
22
20
|
end
|
23
21
|
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def generate_table_file
|
26
|
+
migration_name = "create_#{name.underscore.pluralize}"
|
27
|
+
rake_options = args.join(' ')
|
28
|
+
command = 'bundle exec rake db:new_migration'
|
29
|
+
run "#{command} name=#{migration_name} options='#{rake_options}'", opts
|
30
|
+
end
|
24
31
|
end
|
25
32
|
end
|
26
33
|
end
|
data/lib/nrb/commands/script.rb
CHANGED
@@ -1,23 +1,13 @@
|
|
1
1
|
require 'pathname'
|
2
|
+
require 'nrb/commands/concerns/script_generator'
|
2
3
|
|
3
4
|
module Nrb
|
4
5
|
module Commands
|
6
|
+
# This command generates a new script scaffold.
|
5
7
|
class Script < Commands::Base
|
6
|
-
|
7
|
-
|
8
|
-
argument :path_or_folder_name, type: :string, required: true,
|
9
|
-
desc: 'The name of the project, or the path.'
|
10
|
-
|
11
|
-
class_option :init_repo, default: true, type: :boolean,
|
12
|
-
desc: 'Initialize a repository at the target location.',
|
13
|
-
aliases: '-r'
|
14
|
-
|
15
|
-
class_option :bundle_install, default: false, type: :boolean,
|
16
|
-
desc: 'Run bundle install after generating the skeleton.',
|
17
|
-
aliases: '-b'
|
8
|
+
include ScriptGenerator
|
18
9
|
|
19
|
-
|
20
|
-
desc: 'Add local path of the gem when generating the Gemfile. Useful for testing.'
|
10
|
+
desc 'Creates a Ninja Ruby Script at the given path.'
|
21
11
|
|
22
12
|
def gitignore
|
23
13
|
template 'templates/.gitignore.tt', target('.gitignore'), opts
|
@@ -25,12 +15,12 @@ module Nrb
|
|
25
15
|
|
26
16
|
def readme
|
27
17
|
template 'templates/README.md.tt', target('README.md'),
|
28
|
-
opts.merge(
|
18
|
+
opts.merge(title: name, version: Nrb::VERSION)
|
29
19
|
end
|
30
20
|
|
31
21
|
def gemfile
|
32
22
|
template 'templates/Gemfile.tt', target('Gemfile'),
|
33
|
-
opts.merge(
|
23
|
+
opts.merge(nrb_gem: nrb_gem)
|
34
24
|
end
|
35
25
|
|
36
26
|
def rakefile
|
@@ -39,11 +29,11 @@ module Nrb
|
|
39
29
|
|
40
30
|
def config_nrb
|
41
31
|
template 'templates/config/nrb.rb.tt', target('config/nrb.rb'),
|
42
|
-
opts.merge(
|
32
|
+
opts.merge(resources: Nrb.resources)
|
43
33
|
end
|
44
34
|
|
45
35
|
def resources
|
46
|
-
Nrb.
|
36
|
+
Nrb.resources.each do |dir|
|
47
37
|
create_file target("#{dir}/.keep"), opts
|
48
38
|
end
|
49
39
|
end
|
@@ -54,7 +44,7 @@ module Nrb
|
|
54
44
|
|
55
45
|
def db_config
|
56
46
|
template 'templates/db/config.yml.tt', target('db/config.yml'),
|
57
|
-
opts.merge(
|
47
|
+
opts.merge(db: name)
|
58
48
|
end
|
59
49
|
|
60
50
|
def script_file
|
@@ -65,9 +55,7 @@ module Nrb
|
|
65
55
|
return unless options[:init_repo]
|
66
56
|
|
67
57
|
inside target, opts do
|
68
|
-
|
69
|
-
run 'git init'
|
70
|
-
end
|
58
|
+
try_loud_command('git init')
|
71
59
|
end
|
72
60
|
end
|
73
61
|
|
@@ -76,35 +64,10 @@ module Nrb
|
|
76
64
|
|
77
65
|
inside target, opts do
|
78
66
|
Bundler.with_clean_env do
|
79
|
-
|
80
|
-
run 'bundle install'
|
81
|
-
end
|
67
|
+
try_loud_command('bundle install')
|
82
68
|
end
|
83
69
|
end
|
84
70
|
end
|
85
|
-
|
86
|
-
private
|
87
|
-
|
88
|
-
def nrb_gem
|
89
|
-
text = "gem 'nrb', '#{Nrb::VERSION}'"
|
90
|
-
|
91
|
-
if options[:local]
|
92
|
-
local_gem_path = Pathname.new(File.expand_path('../../..', __dir__))
|
93
|
-
target_path = Pathname.new(target)
|
94
|
-
relative_path = local_gem_path.relative_path_from(target_path)
|
95
|
-
text << ", path: '#{relative_path}'"
|
96
|
-
end
|
97
|
-
|
98
|
-
text
|
99
|
-
end
|
100
|
-
|
101
|
-
def target(final = nil)
|
102
|
-
File.join(File.expand_path(path_or_folder_name), final.to_s)
|
103
|
-
end
|
104
|
-
|
105
|
-
def name
|
106
|
-
File.basename(path_or_folder_name)
|
107
|
-
end
|
108
71
|
end
|
109
72
|
end
|
110
73
|
end
|
data/lib/nrb/commands/starter.rb
CHANGED
data/lib/nrb/errors.rb
CHANGED
@@ -1,4 +1,9 @@
|
|
1
1
|
module Nrb
|
2
|
-
|
3
|
-
|
2
|
+
# Raised when a command that needs to be run inside a project root,
|
3
|
+
# is actually ran outside.
|
4
|
+
class OutsideRootError < Thor::Error; end
|
5
|
+
|
6
|
+
# Raised when a command that needs to generate or destroy a resource,
|
7
|
+
# needs a name, and the name supplied is wrong.
|
8
|
+
class InvalidResourceError < Thor::Error; end
|
4
9
|
end
|
@@ -19,7 +19,7 @@ ActiveRecord::Base.configurations = YAML.load_file('db/config.yml')
|
|
19
19
|
ActiveRecord::Base.establish_connection(:development)
|
20
20
|
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
21
21
|
|
22
|
-
# Finally require files inside
|
23
|
-
Nrb.config.
|
22
|
+
# Finally require files inside resources
|
23
|
+
Nrb.config.autoload.each do |dir|
|
24
24
|
Dir[File.join(dir, '*.rb')].each { |f| require(f) }
|
25
25
|
end
|
@@ -2,6 +2,6 @@ Nrb.configure do |config|
|
|
2
2
|
# Root of the script folder
|
3
3
|
config.root = File.expand_path('..', __dir__)
|
4
4
|
|
5
|
-
# Default
|
6
|
-
# config.
|
5
|
+
# Default directories to autoload
|
6
|
+
# config.autoload = %w(<%= config[:resources].join(' ') %>)
|
7
7
|
end
|