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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

def constantinized_name
  constantinize(app_name)
end

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

Execute the command



109
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
# File 'lib/tty/commands/new.rb', line 109

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.rstrip.dup
        next
      elsif line =~ /^(Gem.*?was successfully created)/
        next
      end

      if !options['no-color']
        output.puts color_actions(line.rstrip)
      else
        output.puts line.rstrip
      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.print "\n" + @pastel.green("Your teletype project has been created successfully.\n")
  output.print "\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.



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

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.



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

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.



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

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.



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

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)


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

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.



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

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.



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

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.



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

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