Class: NoTBJ::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/no_tbj/core.rb

Instance Method Summary collapse

Instance Method Details

#add_bundler_scriptObject



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
155
156
157
158
159
160
161
162
# File 'lib/no_tbj/core.rb', line 130

def add_bundler_script
  bundler_ruby = "    #!/usr/bin/env ruby\n    #\n    # This file was generated by RubyGems.\n    #\n    # The application 'bundler' is installed as part of a gem, and\n    # this file is here to facilitate running it.\n    #\n\n    require 'rubygems'\n\n    version = \">= 0.a\"\n\n    str = ARGV.first\n    if str\n      str = str.b[/\\A_(.*)_\\z/, 1]\n      if str and Gem::Version.correct?(str)\n        version = str\n        ARGV.shift\n      end\n    end\n\n    if Gem.respond_to?(:activate_bin_path)\n    load Gem.activate_bin_path('bundler', '!name!', version)\n    else\n    gem \"bundler\", version\n    load Gem.bin_path(\"bundler\", \"!name!\", version)\n    end\n  RUBY\n  File.write(File.join(RbConfig::CONFIG[\"bindir\"], \"bundler\"), bundler_ruby.sub(\"!name!\", \"bundler\"))\n  File.write(File.join(RbConfig::CONFIG[\"bindir\"], \"bundle\"), bundler_ruby.sub(\"!name!\", \"bundle\"))\nend\n"

#installObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/no_tbj/core.rb', line 30

def install
  files = { skipped_exist: [], skipped_nobat: [], skipped_exception: [], installed: [] }
  add_bundler_script unless File.exist?(File.join(RbConfig::CONFIG["bindir"], "bundler"))
  force_level = ARGV.count("-f")
  verbose_level = ARGV.count("-v")
  bindirs.each do |bindir|
    Dir.glob(File.join(bindir, "*")).each do |file|
      next if File.directory?(file)  # No directory
      next if File.fnmatch?("*.*", File.basename(file))  # We only want ruby file
      next if file == "no_tbj"  # Ignore no_tbj itself

      if File.exist?(file + ".exe") && force_level < 1
        next files[:skipped_exist] << file
      end
      unless [
        File.exist?(file + ".bat"), File.exist?(file + ".cmd"),
      ].any?
        files[:skipped_nobat] << file
        puts "Could not find bat file for #{file}".minfo
        next
      end

      begin
        if File.read(Dir.glob(file + ".{bat,cmd}")[0]).include?("../../../../../bin/")
          FileUtils.cp(exepath_gemlib, file + ".exe")
        else
          FileUtils.cp(exepath, file + ".exe")
        end
      rescue => e
        if verbose_level > 0
          puts "Failed to copy {#{exepath}} to {#{file}.exe}.".red
          puts (verbose_level > 1 ? e.full_message : e.message).lines.map { |l| "  " + l }.join
        end
        files[:skipped_exception] << file
      else
        files[:installed] << file
      end
    end
  end
  files = files.map { |k, v| [k, v.map { |f| File.basename(f) }] }.to_h
  puts "Installed for {#{files[:installed].length}} files.".green
  puts "Skipped {#{files[:skipped_exist].length}} files because they already exist, use {-f} to overwrite.".minfo unless files[:skipped_exist].empty?
  puts "Skipped {#{files[:skipped_nobat].length}} files because they don't have a bat or cmd file.".minfo unless files[:skipped_nobat].empty?
  unless files[:skipped_exception].empty?
    puts "Skipped {#{files[:skipped_exception].length}} files because of an exception.".warn
    puts "This usually occurs that the executable is already installed and running.".minfo
  end
end

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/no_tbj/core.rb', line 10

def run
  if ARGV.empty?
    puts "No arguments given.".red
    puts "Please specify a command, {no_tbj help} for more information.".minfo
    exit
  end
  case ARGV[0]
  when "install"
    install
  when "uninstall"
    uninstall
  when "help"
    show_help
  when "info"
    show_info
  else
    puts "Unrecognized command.".red
  end
end

#show_helpObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/no_tbj/core.rb', line 105

def show_help
  puts "no_tbj lets you ignore {`Terminate Batch Job (Y/N)`} dialogs.".green
  puts "It installs executable to your Ruby's executable directory."
  puts "{install:}".green
  puts "  no_tbj install".info
  puts "  You can specify {-f} to overwrite existing files.".minfo
  puts "  You can specify {-v} to show verbose output.".minfo
  puts "{uninstall:}".green
  puts "  no_tbj uninstall".info
  puts "  You can specify {-v} to show verbose output.".minfo
  puts "{info:}".green
  puts "  no_tbj info".info
  puts "  Shows some information.".minfo
end

#show_infoObject



120
121
122
123
124
125
126
127
128
# File 'lib/no_tbj/core.rb', line 120

def show_info
  puts "no_tbj version: {#{NoTBJ::VERSION}}".minfo
  puts "Bindirs: {#{bindirs.join("}, {")}}".minfo
  puts "Executables: {#{tbj_executables.length}}".minfo
  puts "Executables path: {#{exepath}}".minfo
  puts ""
  puts "GitHub: {https://github.com/sevenc-nanashi/no_tbj}".minfo
  puts "Copyright (c) 2021 {sevenc-nanashi}.".info
end

#uninstallObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/no_tbj/core.rb', line 79

def uninstall
  files = { skipped_notnotbj: [], skipped_exception: [], uninstalled: [] }
  verbose_level = ARGV.count("-v")
  tbj_executables.each do |file|
    next if File.directory?(file)
    begin
      FileUtils.rm(file)
    rescue => e
      if verbose_level > 0
        puts "Failed to remove {#{file}}.".red
        puts (verbose_level > 1 ? e.full_message : e.message).lines.map { |l| "  " + l }.join
      end
      files[:skipped_exception] << file
    else
      files[:uninstalled] << file
    end
  end

  files = files.map { |k, v| [k, v.map { |f| File.basename(f) }] }.to_h
  puts "Uninstalled {#{files[:uninstalled].length}} files.".success
  unless files[:skipped_exception].empty?
    puts "Skipped {#{files[:skipped_exception].length}} files because of an exception.".minfo
    puts "This usually occurs that the executable is running.".minfo
  end
end