sublinit 1.5.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: f91ca9d50e3f8858e19750ba56fb523e7b8f85848897fa5f36d1f21517348278
4
+ data.tar.gz: 31b7aca6b581e0551434e73918ccf9d72874863c1b9de5c16da962cbf1289acb
5
+ SHA512:
6
+ metadata.gz: 45e354ddfe55355285178f7721c7e57a6442b5dc1e8631f4467e99d0ee82d4799da251340e7912c247ccbb781377bb056e892e1406bf7250d4f3d5ff6c29bace
7
+ data.tar.gz: 429dc9822d91ba683b8419dbeedae29f1d098f8b710717b9d510d6caf54bbf39732c570f9cf334ed138a341c96fbdecd33ab818ca1403c6378a8705ab28c1bf8
data/bin/sublinit ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'sublinit'
5
+
6
+ SublInit::Thor::CLI.start(ARGV)
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SublInit
4
+ module CLI
5
+ module Git
6
+ class << self
7
+ def init
8
+ git('init')
9
+ end
10
+
11
+ def add_remote(name, adress)
12
+ git('remote', 'add', name, adress)
13
+ end
14
+
15
+ private
16
+
17
+ def git(*command)
18
+ SublInit::CLI.exec(:git, *command)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SublInit
4
+ module CLI
5
+ module IO
6
+ POSITIVE_ANWSER_REGEXP = /^(Y|y|yes)$/i.freeze
7
+ COLOR_CODE_MAPPING = {
8
+ white: 0,
9
+ red: 31,
10
+ green: 32,
11
+ yellow: 33,
12
+ purple: 34,
13
+ pink: 35,
14
+ blue: 36
15
+ }.freeze
16
+
17
+ class << self
18
+ def ask_boolean(message, options = {})
19
+ ask("#{message} (Y/n)", **options) do |answer|
20
+ answer.match?(POSITIVE_ANWSER_REGEXP)
21
+ end
22
+ end
23
+
24
+ def ask(question, options = {})
25
+ say(question, **options)
26
+
27
+ say('Answer: ', newline: false)
28
+ answer = $stdin.gets.strip
29
+
30
+ block_given? ? yield(answer) : answer
31
+ end
32
+
33
+ def say(message, color: :white, newline: true)
34
+ color_code = COLOR_CODE_MAPPING.fetch(color, 0)
35
+ message = "\e[#{color_code}m#{message}\e[0m"
36
+
37
+ newline ? puts(message) : print(message)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SublInit
4
+ module CLI
5
+ module RVM
6
+ class << self
7
+ def list
8
+ rvm('list', 'rubies', 'strings').split("\n")
9
+ end
10
+
11
+ def use(ruby_version)
12
+ rvm('use', ruby_version)
13
+ end
14
+
15
+ def create_gemset(gemset_name)
16
+ rvm('gemset', 'create', gemset_name)
17
+ end
18
+
19
+ private
20
+
21
+ def rvm(*command, status_code: false)
22
+ SublInit::CLI.exec(:rvm, *command, status_code: status_code)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SublInit
4
+ module CLI
5
+ module Sublime
6
+ module Text
7
+ class << self
8
+ def open(project)
9
+ subl(project)
10
+ end
11
+
12
+ private
13
+
14
+ def subl(*command)
15
+ SublInit::CLI.exec(:subl, *command)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'sublime/text'
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'cli/io'
4
+ require_relative 'cli/git'
5
+ require_relative 'cli/rvm'
6
+ require_relative 'cli/sublime'
7
+
8
+ module SublInit
9
+ module CLI
10
+ class << self
11
+ def exec(command, *options, status_code: false)
12
+ full_command = "#{command} #{options.join(' ')}"
13
+
14
+ SublInit::CLI::IO.say('[CLI] ', color: :pink, newline: false)
15
+ SublInit::CLI::IO.say("Executing '#{full_command}'", color: :blue)
16
+
17
+ status_code ? system(full_command) : `#{full_command}`.strip
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SublInit
4
+ class Context
5
+ def initialize(hash)
6
+ hash.each do |key, value|
7
+ instance_variable_set("@#{key}", value)
8
+ end
9
+ end
10
+
11
+ def get_binding
12
+ binding
13
+ end
14
+
15
+ def method_missing(method_name, *arguments, &block)
16
+ instance_variable_name = "@#{method_name}"
17
+ return unless defined?(instance_variable_name)
18
+
19
+ instance_variable_get(instance_variable_name)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class String
4
+ def underscore
5
+ self.gsub(/::/, '/').
6
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
7
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
8
+ tr("-", "_").
9
+ downcase
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SublInit
4
+ class Error < StandardError; end
5
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SublInit
4
+ module Project
5
+ module Files
6
+ class Base
7
+ def self.create!(...)
8
+ new(...).create!
9
+ end
10
+
11
+ def create!
12
+ raise SublInit::Project::Files::AlreadyExists, filename if file_exists?
13
+
14
+ File.write(filename, default_content)
15
+ filename
16
+ end
17
+
18
+ private
19
+
20
+ def build_full_filename(filename, extension)
21
+ [filename, extension].join('.')
22
+ end
23
+
24
+ def file_exists?
25
+ File.file?(filename)
26
+ end
27
+
28
+ def filename
29
+ raise NotImplementedError
30
+ end
31
+
32
+ def default_content
33
+ raise NotImplementedError
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SublInit
4
+ module Project
5
+ module Files
6
+ module Ruby
7
+ class VersionsConf < SublInit::Project::Files::Templated
8
+ FILENAME = '.versions.conf'
9
+
10
+ def filename
11
+ FILENAME
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module SublInit
6
+ module Project
7
+ module Files
8
+ class SublimeProject < SublInit::Project::Files::Base
9
+ FILE_EXTENSION = 'sublime-project'
10
+
11
+ FolderEntry = Struct.new(:name, :path, keyword_init: true) do
12
+ def to_entry
13
+ to_h.compact
14
+ end
15
+ end
16
+
17
+ attr_reader :project_name, :additional_folder_entries
18
+
19
+ def initialize(project_name:, additional_folders: [])
20
+ @project_name = project_name
21
+ @additional_folder_entries = build_additional_folder_entries(additional_folders)
22
+ end
23
+
24
+ private
25
+
26
+ def filename
27
+ build_full_filename(project_name, FILE_EXTENSION)
28
+ end
29
+
30
+ def default_content
31
+ JSON.pretty_generate(build_project_hash)
32
+ end
33
+
34
+ def build_project_hash
35
+ {
36
+ folders: build_folders_array,
37
+ settings: build_settings_hash
38
+ }
39
+ end
40
+
41
+ def build_folders_array
42
+ [
43
+ FolderEntry.new(name: nil, path: '.').to_entry,
44
+ *build_additional_folders_array
45
+ ]
46
+ end
47
+
48
+ def build_additional_folders_array
49
+ additional_folder_entries.map(&:to_entry)
50
+ end
51
+
52
+ def build_settings_hash
53
+ {
54
+ tab_size: 2
55
+ }
56
+ end
57
+
58
+ def build_additional_folder_entries(additional_folders)
59
+ additional_folders.map do |folder_data|
60
+ FolderEntry.new(**folder_data)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'erb'
4
+
5
+ module SublInit
6
+ module Project
7
+ module Files
8
+ class Templated < Base
9
+ TEMPLATES_FOLDER = 'templates'
10
+
11
+ attr_reader :options
12
+
13
+ def initialize(options)
14
+ @options = options
15
+ end
16
+
17
+ private
18
+
19
+ def default_content
20
+ context = SublInit::Context.new(options).get_binding
21
+ ERB.new(template_file_content).result(context)
22
+ end
23
+
24
+ def template_file_content
25
+ File.new(template_path).read
26
+ end
27
+
28
+ def template_path
29
+ File.join(
30
+ SublInit.root,
31
+ TEMPLATES_FOLDER,
32
+ template_filename
33
+ )
34
+ end
35
+
36
+ def template_filename
37
+ self.class.name.split('::').last(2).join('/').underscore
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'files/base'
4
+ require_relative 'files/templated'
5
+
6
+ require_relative 'files/sublime_project'
7
+ require_relative 'files/ruby/versions_conf'
8
+
9
+ module SublInit
10
+ module Project
11
+ module Files
12
+ class AlreadyExists < SublInit::Error
13
+ def initialize(filename)
14
+ super("#{filename} already exists")
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SublInit
4
+ module Project
5
+ module RVM
6
+ class Gemset
7
+ attr_reader :ruby_version, :gemset_name
8
+
9
+ def initialize(ruby_version, gemset_name)
10
+ @ruby_version = ruby_version
11
+ @gemset_name = gemset_name
12
+ end
13
+
14
+ def path
15
+ File.join('~', '.rvm', 'gems', ruby_installation_name, 'gems')
16
+ end
17
+
18
+ private
19
+
20
+ def ruby_installation_name
21
+ "ruby-#{ruby_version}@#{gemset_name}"
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rvm/gemset'
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'project/files'
4
+ require_relative 'project/rvm'
@@ -0,0 +1,2 @@
1
+ ruby=ruby-<%= ruby_version %>
2
+ <% if ruby_gemset %>ruby-gemset=<%= ruby_gemset %><% end %>
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SublInit
4
+ module Thor
5
+ module Ruby
6
+ extend SublInit::Thor::Support
7
+
8
+ commands do
9
+ option :new, type: :boolean, default: false
10
+ option :name, aliases: '-n', required: true
11
+ option :gemset, aliases: '-g'
12
+ option :open, aliases: '-o', type: :boolean, default: true
13
+ option :origin, aliases: '-or'
14
+ desc 'ruby [NEW] [GEMSET] [OPEN] [ORIGIN]', 'Initialize a new Ruby Sublime Text project'
15
+ def ruby
16
+ ruby_version = RUBY_VERSION
17
+
18
+ project_name = options[:name]
19
+ ruby_gemset = options[:gemset]
20
+
21
+ within project_name, if: options[:new] do
22
+ # 1. Create RVM gemset
23
+ operation "Creating #{ruby_gemset} gemset for Ruby #{ruby_version}", if: ruby_gemset do
24
+ SublInit::CLI::RVM.create_gemset(ruby_gemset)
25
+ end
26
+
27
+ # 2. Create versions.conf
28
+ operation 'Creating .versions.conf file' do
29
+ SublInit::Project::Files::Ruby::VersionsConf.create!(
30
+ ruby_version: ruby_version, ruby_gemset: ruby_gemset
31
+ )
32
+ end
33
+
34
+ # 3. Create Sublime Text project file
35
+ sublime_project = operation "Creating #{project_name}.sublime-project" do
36
+ gemset_folder_path = SublInit::Project::RVM::Gemset.new(ruby_version, ruby_gemset).path
37
+
38
+ SublInit::Project::Files::SublimeProject.create!(
39
+ project_name: project_name,
40
+ additional_folders: [
41
+ {
42
+ name: 'Gems',
43
+ path: gemset_folder_path
44
+ }
45
+ ]
46
+ )
47
+ end
48
+
49
+ # 4. Initialize Git repository
50
+ operation 'Initializing git repository' do
51
+ SublInit::CLI::Git.init
52
+ end
53
+
54
+ # 5. Add remote
55
+ operation "Adding #{options[:origin]} as origin git remote", if: options[:origin] do
56
+ SublInit::CLI::Git.add_remote(:origin, options[:origin])
57
+ end
58
+
59
+ # 6. Open Sublime Text Project
60
+ operation "Opening #{sublime_project} in Sublime Text", if: (sublime_project && options[:open]) do
61
+ SublInit::CLI::Sublime::Text.open(sublime_project)
62
+ end
63
+
64
+ say("Done! Your project path is #{Dir.pwd}", color: :green)
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SublInit
4
+ module Thor
5
+ module Support
6
+ def self.extended(base)
7
+ base.define_singleton_method(:included) do |thor|
8
+ thor.class_eval(&@_commands_block)
9
+ end
10
+ end
11
+
12
+ def commands(&commands_definition)
13
+ @_commands_block = commands_definition
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+
5
+ require_relative 'thor/support'
6
+ require_relative 'thor/ruby'
7
+
8
+ module SublInit
9
+ module Thor
10
+ class CLI < ::Thor
11
+ namespace :sublinit
12
+ default_command :ruby
13
+
14
+ # Supported projects languages
15
+ include SublInit::Thor::Ruby
16
+
17
+ def initialize(args = [], local_options = {}, config = {})
18
+ super
19
+ @operations_count = 0
20
+ end
21
+
22
+ no_commands do
23
+ def invoke(task, args: [], options: {})
24
+ super(task, args, options)
25
+ end
26
+
27
+ def say(message, options = {})
28
+ SublInit::CLI::IO.say('[THOR] ', color: :pink, newline: false)
29
+ SublInit::CLI::IO.say(message, **{ color: :yellow }.merge(options))
30
+ end
31
+
32
+ def operation(description, options = {})
33
+ return unless options.fetch(:if, true)
34
+
35
+ @operations_count += 1
36
+ say("#{@operations_count}. #{description}...")
37
+
38
+ yield
39
+ rescue SublInit::Error => e
40
+ say(e.message, color: :red)
41
+ nil
42
+ end
43
+
44
+ def execute(command, *options, status_code: false)
45
+ SublInit::CLI.exec(command, options, status_code: status_code)
46
+ end
47
+
48
+ def within(directory, options = {}, &block)
49
+ return block.call unless options.fetch(:if, true)
50
+
51
+ Dir.mkdir(directory) unless File.directory?(directory)
52
+ Dir.chdir(directory, &block)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SublInit
4
+ VERSION = '1.5.0'
5
+ end
data/lib/sublinit.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sublinit/version'
4
+
5
+ require 'sublinit/core_ext/string'
6
+
7
+ require 'sublinit/errors'
8
+ require 'sublinit/thor'
9
+ require 'sublinit/cli'
10
+ require 'sublinit/context'
11
+ require 'sublinit/project'
12
+
13
+ module SublInit
14
+ module_function
15
+
16
+ def root
17
+ File.join(__dir__, 'sublinit')
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sublinit
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Egor Iskrenkov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-05-19 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: 1.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.0
27
+ description:
28
+ email:
29
+ - egor@iskrenkov.me
30
+ executables:
31
+ - sublinit
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - bin/sublinit
36
+ - lib/sublinit.rb
37
+ - lib/sublinit/cli.rb
38
+ - lib/sublinit/cli/git.rb
39
+ - lib/sublinit/cli/io.rb
40
+ - lib/sublinit/cli/rvm.rb
41
+ - lib/sublinit/cli/sublime.rb
42
+ - lib/sublinit/cli/sublime/text.rb
43
+ - lib/sublinit/context.rb
44
+ - lib/sublinit/core_ext/string.rb
45
+ - lib/sublinit/errors.rb
46
+ - lib/sublinit/project.rb
47
+ - lib/sublinit/project/files.rb
48
+ - lib/sublinit/project/files/base.rb
49
+ - lib/sublinit/project/files/ruby/versions_conf.rb
50
+ - lib/sublinit/project/files/sublime_project.rb
51
+ - lib/sublinit/project/files/templated.rb
52
+ - lib/sublinit/project/rvm.rb
53
+ - lib/sublinit/project/rvm/gemset.rb
54
+ - lib/sublinit/templates/ruby/versions_conf
55
+ - lib/sublinit/thor.rb
56
+ - lib/sublinit/thor/ruby.rb
57
+ - lib/sublinit/thor/support.rb
58
+ - lib/sublinit/version.rb
59
+ homepage: https://github.com/eiskrenkov/sublinit
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 2.4.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.3.7
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Sublime Text CLI for new projects initialization
82
+ test_files: []