Class: TTY::Commands::New Private

Inherits:
TTY::Cmd show all
Includes:
Licenses
Defined in:
lib/tty/commands/new.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The ‘new` command

Constant Summary collapse

GEMSPEC_PATH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Pathname(__dir__).join('../../../tty.gemspec').realpath.to_s

Constants included from Licenses

Licenses::CUSTOM, Licenses::LICENSES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Licenses

#license_identifiers, #licenses

Methods inherited from TTY::Cmd

#command, #constantinize, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which

Methods included from PathHelpers

#name_from_path, #relative_path_from, #root_path, #within_root_path

Constructor Details

#initialize(app_path, options) ⇒ New

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of New.



39
40
41
42
43
44
45
46
47
48
# File 'lib/tty/commands/new.rb', line 39

def initialize(app_path, options)
  @app_path = relative_path_from(root_path, app_path)
  @app_name = name_from_path(app_path)
  @options  = options
  @pastel   = Pastel.new(enabled: !options['no-color'])

  @target_path = root_path.join(@app_path)
  @templater = Templater.new('new', @app_path)
  @runner = command(printer: :null)
end

Instance Attribute Details

#app_nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
# File 'lib/tty/commands/new.rb', line 27

def app_name
  @app_name
end

#app_pathObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
# File 'lib/tty/commands/new.rb', line 29

def app_path
  @app_path
end

#optionsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



32
33
34
# File 'lib/tty/commands/new.rb', line 32

def options
  @options
end

#target_pathObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



35
36
37
# File 'lib/tty/commands/new.rb', line 35

def target_path
  @target_path
end

#templatesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



37
38
39
# File 'lib/tty/commands/new.rb', line 37

def templates
  @templates
end

Instance Method Details

#authorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
61
62
63
64
65
66
# File 'lib/tty/commands/new.rb', line 58

def author
  if !Array(options['author']).empty?
    options['author'].join(', ')
  elsif !git_author.empty?
    git_author
  else
    'TODO: Write your name'
  end
end

#constantinized_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



76
77
78
# File 'lib/tty/commands/new.rb', line 76

def constantinized_name
  constantinize(app_name)
end

#execute(input: $stdin, output: $stdout) ⇒ Object

Execute the command



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/tty/commands/new.rb', line 110

def execute(input: $stdin, output: $stdout)
  output.puts "OPTS: #{options}" if options['debug']

  coc_opt  = options['coc'] ? '--coc' : '--no-coc'
  ext_opt  = options['ext'] ? '--ext' : '--no-ext'
  test_opt = options['test']
  command = [
    "bundle gem #{target_path}",
    '--no-mit',
    '--no-exe',
    coc_opt,
    ext_opt,
    "-t #{test_opt}"
  ].join(' ')

  install_info = []

  @runner.run(command) do |out, err|
    next unless out
    out.each_line do |line|
      if line =~ /^Initializing git/
        install_info << line.dup
        next
      elsif line =~ /^(Gem.*?was successfully created)/
        next
      end

      if !options['no-color']
        output.puts color_actions(line)
      else
        output.puts line
      end
    end
  end

  add_app_templates
  add_empty_directories
  add_required_libs_to_gemspec
  @templater.generate(template_context, file_options)
  make_executable
  output.puts install_info.join("\n") unless install_info.empty?

  output.puts "\n" + @pastel.green("Your teletype project has been created successfully.")
  output.puts "\n" + @pastel.green("Run \"teletype help\" for more commands.\n")
end

#file_optionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



92
93
94
95
96
97
# File 'lib/tty/commands/new.rb', line 92

def file_options
  opts = {}
  opts[:force] = true if options['force']
  opts[:color] = false if options['no-color']
  opts
end

#gemspec_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



99
100
101
# File 'lib/tty/commands/new.rb', line 99

def gemspec_name
  "#{app_name}.gemspec"
end

#gemspec_pathObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



103
104
105
# File 'lib/tty/commands/new.rb', line 103

def gemspec_path
  target_path.join("#{app_name}.gemspec").to_s
end

#git_authorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
# File 'lib/tty/commands/new.rb', line 54

def git_author
  git_exist? ? `git config user.name`.chomp : ''
end

#git_exist?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


50
51
52
# File 'lib/tty/commands/new.rb', line 50

def git_exist?
  exec_exist?('git')
end

#namespaced_pathObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



68
69
70
# File 'lib/tty/commands/new.rb', line 68

def namespaced_path
  app_name.tr('-', '/')
end

#template_contextObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tty/commands/new.rb', line 80

def template_context
  opts = OpenStruct.new
  opts[:app_name] = app_name
  opts[:author]   = author
  opts[:namespaced_path]  = namespaced_path
  opts[:underscored_name] = underscored_name
  opts[:constantinized_name] = constantinized_name
  opts[:constantinized_parts] = constantinized_name.split('::')
  opts[:indent] = '  ' * constantinized_name.split('::').size
  opts
end

#underscored_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



72
73
74
# File 'lib/tty/commands/new.rb', line 72

def underscored_name
  app_name.tr('-', '_')
end