spell_craft 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51e6df509e88a2fdb1cffa70963e78efed803d5fc53b74b17da22ec24cbdb630
4
- data.tar.gz: cc4a063114b4ed4ef4e8da045ecc48d205ff4fc5561c44f5626427c2f0f4183c
3
+ metadata.gz: cfb94192846d3309c224e6512046f4031c66198d903f3745adce62ded95aeeb9
4
+ data.tar.gz: 27820fe9ab3ecf5d985c4232e7d780584fc001a71ad9290f7d839e09d1a36e6a
5
5
  SHA512:
6
- metadata.gz: 9374f712bcafab23eefdbeca6b81b69ed9d2633bfe016d35537692f66ba40123194b334430afc8e74dde872d1023c0df950757262a2cabc474ebfe16ca49b6b3
7
- data.tar.gz: 5071f7bfb76234778d933ba02c00d62694c60d1680e5f73c39886a52915199a3515cd38480de099ba873cda342aee8f8e9f52deca3f011c4f87b3adabdb0d379
6
+ metadata.gz: 7b6d6a5bd7bc8a8e42fd8da1e9fadeb38ae579197ca63c8489aef379c62d57ff1c591037d0c01cbca42093211e45462ab5afb038f2b2b415b5bd441d003cd5f6
7
+ data.tar.gz: 0e03842cfb8946170a0fef437eb78edab24c4ebc7d1e2e3e3fbff4eb70a28159a2b704ff89f6db10de42c12d3c02fb7c796e91e49c2642cb20580e46d199c693
data/README.md CHANGED
@@ -11,6 +11,11 @@ In the future, this project should enable configuration of [dry-rb](https://dry-
11
11
  ## Commands
12
12
 
13
13
  - [ ] `add`: A branch to add configurations
14
- - [ ] `repl`: This command should prompt to choose from IRB or pry and add correct configuration to `bin/console`, for pry it should install common extensions like pry reload and pry rescue.
15
- - [ ] `autoload`: This command should add the configuration necessary to use zeitwerk on the current project.
14
+ - [ ] `repl`: This command should prompt to choose from IRB or pry and add correct configuration to `bin/console`, for pry it should install common extensions like pry reload and pry rescue. `(PS: This should support rails)`
15
+ - [x] `autoload`: This command should add the configuration necessary to use zeitwerk on the current project.
16
+ - [ ] `type_checking`: This command should add the configuration necessary to use sorbet on the current project. `(PS: This should support rails)`
16
17
  - [ ] `remove`: A branch to remove configurations
18
+
19
+ ## Future plans
20
+
21
+ - Add support for third party generators to be plugged in?
@@ -4,7 +4,7 @@
4
4
  require 'zeitwerk'
5
5
  require 'pathname'
6
6
 
7
- root = Pathname('.')
7
+ root = Pathname('/Users/cherryramatis/Repos/spell_craft')
8
8
 
9
9
  loader = Zeitwerk::Loader.new
10
10
 
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ require 'async'
5
+
6
+ module Cmd
7
+ module Add
8
+ class Autoload < Dry::CLI::Command
9
+ extend T::Sig
10
+
11
+ desc 'Allow adding configuration for Autoloading and autorequiring on the current application, being either plain zeitwerk or dry-system + zeitwerk'
12
+
13
+ option :dry_system, type: :boolean, default: false,
14
+ desc: 'Enable the code generator to assume dry-system + zeitwerk configuration'
15
+
16
+ ZEITWERK_TEMPLATE = <<~CONTENT
17
+ # frozen_string_literal: true
18
+
19
+ require 'zeitwerk'
20
+ require 'pathname'
21
+
22
+ root = Pathname('.')
23
+
24
+ loader = Zeitwerk::Loader.new
25
+
26
+ loader.push_dir(root.join('lib').realpath)
27
+ loader.push_dir(root.join('config').realpath)
28
+
29
+ loader.setup
30
+ CONTENT
31
+
32
+ DRY_SYSTEM_TEMPLATE = <<~CONTENT
33
+ # frozen_string_literal: true
34
+
35
+ require 'dry/system'
36
+
37
+ # Application configure all the paths to be autoloaded and auto injectable.
38
+ class Application < Dry::System::Container
39
+ configure do |config|
40
+ config.root = Pathname('.')
41
+
42
+ config.component_dirs.loader = Dry::System::Loader::Autoloading
43
+
44
+ config.component_dirs.add 'lib'
45
+ config.component_dirs.add 'config'
46
+ end
47
+ end
48
+
49
+ loader = Zeitwerk::Loader.new
50
+ loader.push_dir(Application.config.root.join('lib').realpath)
51
+ loader.push_dir(Application.config.root.join('config').realpath)
52
+ loader.setup
53
+ CONTENT
54
+
55
+ class DepsType < T::Enum
56
+ enums do
57
+ DRY_SYSTEM = new
58
+ ZEITWERK = new
59
+ end
60
+ end
61
+
62
+ sig { params(dry_system: T::Boolean).void }
63
+ def call(dry_system:)
64
+ return puts 'Could not locate Gemfile' unless Utils::Gem.gemfile_exist?
65
+
66
+ Utils::FileSystem.create_directory_if_not_exist('config')
67
+
68
+ if dry_system
69
+ File.write('config/application.rb', DRY_SYSTEM_TEMPLATE)
70
+
71
+ install_deps(DepsType::DRY_SYSTEM)
72
+ end
73
+
74
+ return if dry_system
75
+
76
+ File.write('config/application.rb', ZEITWERK_TEMPLATE)
77
+
78
+ install_deps(DepsType::ZEITWERK)
79
+ end
80
+
81
+ private
82
+
83
+ sig { params(type: DepsType).void }
84
+ def install_deps(type)
85
+ if Utils::Gem.exists?('dry-system') || Utils::Gem.exists?('zeitwerk') || Utils::Gem.exists?('dry-auto_inject')
86
+ return
87
+ end
88
+
89
+ pkgs = T.let(['zeitwerk'], T::Array[String])
90
+
91
+ pkgs.push('dry-system', 'dry-auto_inject') if type == DepsType::DRY_SYSTEM
92
+
93
+ Async do
94
+ pkgs.each do |pkg|
95
+ system("bundle add #{pkg}")
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
data/lib/cmd/add/repl.rb CHANGED
@@ -1,9 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
  # typed: true
3
3
 
4
+ require 'dry/monads/all'
5
+ require 'async'
6
+
4
7
  module Cmd
5
8
  module Add
6
9
  class Repl < Dry::CLI::Command
10
+ include Dry::Monads
7
11
  extend T::Sig
8
12
 
9
13
  desc 'Allow adding configuration for REPL support on the current application, being either pry or irb'
@@ -13,17 +17,80 @@ module Cmd
13
17
  option :pry, type: :boolean, default: false,
14
18
  desc: 'Enable the code generator to assume Pry as the REPL of choice'
15
19
 
20
+ IRB_TEMPLATE = <<~CONTENT
21
+ #!/usr/bin/env ruby
22
+
23
+ # frozen_string_literal: true
24
+
25
+ require 'irb'
26
+ IRB.start
27
+ CONTENT
28
+
29
+ PRY_TEMPLATE = <<~CONTENT
30
+ #!/usr/bin/env ruby
31
+
32
+ require 'pry-reload'
33
+ require 'pry'
34
+ Pry.start
35
+ CONTENT
36
+
37
+ class ConfigType < T::Enum
38
+ enums do
39
+ IRB = new
40
+ PRY = new
41
+ end
42
+ end
43
+
44
+ sig { params(irb: T::Boolean, pry: T::Boolean).void }
16
45
  def call(irb:, pry:)
17
- install_deps if pry
46
+ if pry
47
+ response = install_deps
48
+
49
+ if response.failure?
50
+ # TODO: make this better with red message maybe.
51
+ puts response.failure[:message]
52
+ return
53
+ end
54
+ end
18
55
 
19
- puts 'Hello world'
56
+ write_config(type: ConfigType::IRB) if irb
57
+ write_config(type: ConfigType::PRY) if pry
20
58
  end
21
59
 
22
60
  private
23
61
 
24
- sig { void }
62
+ sig { params(type: ConfigType).void }
63
+ def write_config(type:)
64
+ case type
65
+ when ConfigType::IRB
66
+ Utils::FileSystem.create_directory_if_not_exist('bin')
67
+
68
+ File.write('bin/console', IRB_TEMPLATE)
69
+ when ConfigType::PRY
70
+ Utils::FileSystem.create_directory_if_not_exist('bin')
71
+
72
+ File.write('bin/console', PRY_TEMPLATE)
73
+
74
+ File.chmod(0o755, 'bin/console')
75
+ end
76
+ end
77
+
78
+ sig { returns(T.any(Result::Success, Result::Failure)) }
25
79
  def install_deps
26
- puts 'installing deps'
80
+ return Result::Failure.new({ message: 'Could not locate Gemfile' }) unless Utils::Gem.gemfile_exist?
81
+
82
+ unless Utils::Shell.binary_exists?('bundle')
83
+ return Result::Failure.new({ message: 'Could not locate bundle gem' })
84
+ end
85
+
86
+ return Result::Success.new(true) if Utils::Gem.exists?('pry') || Utils::Gem.exists?('pry-reload')
87
+
88
+ Async do
89
+ system('bundle add pry')
90
+ system('bundle add pry-reload')
91
+ end
92
+
93
+ Result::Success.new(true)
27
94
  end
28
95
  end
29
96
  end
data/lib/cmd/cli.rb CHANGED
@@ -8,6 +8,7 @@ module Cmd
8
8
  register 'version', Version, aliases: ['v', '-v', '--version']
9
9
  register 'add', aliases: ['a'] do |prefix|
10
10
  prefix.register 'repl', Add::Repl
11
+ prefix.register 'autoload', Add::Autoload
11
12
  end
12
13
  end
13
14
  end
data/lib/cmd/version.rb CHANGED
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
  # typed: true
3
3
 
4
+ require 'rubygems'
5
+
4
6
  module Cmd
5
7
  class Version < Dry::CLI::Command
6
8
  desc 'Print version'
7
9
 
8
10
  def call
9
- puts '0.0.1'
11
+ puts Gem::Specification.load('spell_craft.gemspec').version
10
12
  end
11
13
  end
12
14
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ module Utils
5
+ class FileSystem
6
+ extend T::Sig
7
+
8
+ sig { params(name: String).returns(T::Boolean) }
9
+ def self.create_directory_if_not_exist(name)
10
+ return true if File.directory?(name)
11
+
12
+ begin
13
+ Dir.mkdir(name)
14
+ true
15
+ rescue SystemCallError
16
+ false
17
+ end
18
+ end
19
+ end
20
+ end
data/lib/utils/gem.rb ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ module Utils
5
+ class Gem
6
+ extend T::Sig
7
+
8
+ sig { params(name: String).returns(T::Boolean) }
9
+ def self.exists?(name)
10
+ gemfile_content = File.read('Gemfile')
11
+
12
+ pattern = /gem\s+['"]#{name}['"]/
13
+ !gemfile_content.scan(pattern).empty?
14
+ end
15
+
16
+ sig { returns(T::Boolean) }
17
+ def self.gemfile_exist?
18
+ File.exist?('Gemfile')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ module Utils
5
+ class Shell
6
+ extend T::Sig
7
+
8
+ sig { params(binary: String).returns(T::Boolean) }
9
+ def self.binary_exists?(binary)
10
+ File.executable?(File.join(ENV.fetch('PATH', '').split(':'), binary))
11
+ path_dirs = ENV.fetch('PATH', '').split(File::PATH_SEPARATOR)
12
+
13
+ !path_dirs.find do |dir|
14
+ File.exist?(File.join(dir, binary)) && !File.directory?(File.join(dir, binary))
15
+ end.nil?
16
+ end
17
+ end
18
+ end
data/spell_craft.gemspec CHANGED
@@ -8,7 +8,7 @@ $LOAD_PATH.unshift(config) unless $LOAD_PATH.include?(config)
8
8
 
9
9
  Gem::Specification.new do |spec|
10
10
  spec.name = 'spell_craft'
11
- spec.version = '0.1.1'
11
+ spec.version = '0.1.3'
12
12
  spec.summary = 'Yet another code generator to make you type less stuff'
13
13
  spec.description = 'Yet another code generator to make you type less stuff'
14
14
  spec.authors = ['Cherry Ramatis']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spell_craft
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cherry Ramatis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-19 00:00:00.000000000 Z
11
+ date: 2023-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-cli
@@ -49,9 +49,13 @@ files:
49
49
  - README.md
50
50
  - bin/spell_craft
51
51
  - config/application.rb
52
+ - lib/cmd/add/autoload.rb
52
53
  - lib/cmd/add/repl.rb
53
54
  - lib/cmd/cli.rb
54
55
  - lib/cmd/version.rb
56
+ - lib/utils/file_system.rb
57
+ - lib/utils/gem.rb
58
+ - lib/utils/shell.rb
55
59
  - spell_craft.gemspec
56
60
  homepage: https://github.com/cherryramatisdev/spell_craft.git
57
61
  licenses: