Class: TechbookHelper::Helper
- Inherits:
-
Object
- Object
- TechbookHelper::Helper
- Defined in:
- lib/techbook_helper.rb
Constant Summary collapse
- IMAGES_DIR =
'_i'
- INDEX_MD =
'index.md'
- GIT_KEEP =
'.keep'
Instance Method Summary collapse
- #add_git_remote ⇒ Object
- #build_chapter(name) ⇒ Object
- #build_template ⇒ Object
- #check_book_exist(book_name) ⇒ Object
- #clear_book_dir ⇒ Object
- #create ⇒ Object
- #create_root_chapters ⇒ Object
- #handler_arguments(arg) ⇒ Object
- #init_repository ⇒ Object
- #is_dir_used? ⇒ Boolean
- #print_finish_message ⇒ Object
- #print_help ⇒ Object
- #print_promt ⇒ Object
- #print_unknown_command(command) ⇒ Object
- #puts_for_next ⇒ Object
- #puts_line ⇒ Object
- #puts_ok ⇒ Object
- #say_hello ⇒ Object
- #too_many_arg_error ⇒ Object
- #yes_no_choice ⇒ Object
Instance Method Details
#add_git_remote ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/techbook_helper.rb', line 221 def add_git_remote puts 'Want to add remote url to this book?' if yes_no_choice puts_for_next puts 'Type git remote url:' #https:// print_promt while user_input = STDIN.gets.chomp case when user_input.nil? || user_input.size == 0 puts_for_next puts 'Do not play with me!' puts 'Enter valid url or go to exit!' print_promt when @reject_args.include?(user_input) puts_ok break when !user_input.start_with?('https://') puts_for_next puts 'Sorry. We need url start from https://' puts 'Please, try again' print_promt else Dir.chdir(@current_book_directory) do `git remote add origin #{user_input}` end puts 'Remote url added to git'.green break end end end end |
#build_chapter(name) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/techbook_helper.rb', line 147 def build_chapter(name) if @current_book_directory.nil? puts "Do not know which book to work with".red return end Dir.mkdir(File.join(@current_book_directory, name)) puts "Create chapter \"#{name}\"" sleep(0.4) File.open(File.join(@current_book_directory, name, INDEX_MD), 'w').close puts 'Index page' sleep(0.5) end |
#build_template ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/techbook_helper.rb', line 162 def build_template unless Dir.exist?(@current_book_directory) Dir.mkdir(@current_book_directory) puts 'Create root directory' sleep(0.4) end File.open(File.join(@current_book_directory, INDEX_MD), 'w').close puts 'Create Index page' sleep(0.4) Dir.mkdir(File.join(@current_book_directory, IMAGES_DIR)) File.open(File.join(@current_book_directory, IMAGES_DIR, GIT_KEEP), 'w').close sleep(0.4) puts 'Create images directory' puts_line puts 'Book template complete' puts_line sleep(0.5) end |
#check_book_exist(book_name) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/techbook_helper.rb', line 85 def check_book_exist(book_name) current_folder_name = File.basename(Dir.pwd) @current_book_name = book_name @current_book_directory = File.join(Dir.pwd, book_name) if current_folder_name == book_name puts 'Attention! Attention please!'.yellow puts 'Helper now inside directory with book name you are typed' puts 'Dow you want to clear existing files and create book inside current directory?'.red if yes_no_choice @current_book_directory = Dir.pwd clear_book_dir end end if Dir.exist?(book_name) puts 'Book with same name already exist in current place'.red puts 'Want to empty it and create new book?' if yes_no_choice clear_book_dir end end build_template puts 'Want to create top level chapters?' if yes_no_choice create_root_chapters end init_repository end |
#clear_book_dir ⇒ Object
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 |
# File 'lib/techbook_helper.rb', line 120 def clear_book_dir puts "Clear book #{@current_book_name}".red Dir["#{@current_book_directory}/*"].each do |file| if File.directory? file FileUtils.rm_rf file else File.delete file end end Dir["#{@current_book_directory}/.*"].each do |file| unless File.basename(file) == '.' || File.basename(file) == '..' if File.directory? file FileUtils.rm_rf file else File.delete file end end end 10.times{print '. '; sleep 0.3;} puts 'Done' sleep(0.4) puts_line end |
#create ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/techbook_helper.rb', line 58 def create puts 'Ok, lest do it.' puts 'Helper will create new book in current directory:' puts " #{Dir.pwd} ".blue.on_white puts_line puts 'What the name of new book?:' print_promt while user_input = STDIN.gets.chomp case when user_input.nil? || user_input.size == 0 puts 'Hmmm... Book without name at all? Are you kidding me?' puts "Please, give me something more intelligent" puts "If you want to cancel, just say 'cancel' or 'exit'" print_promt when @reject_args.include?(user_input) puts_ok break else check_book_exist user_input break end end end |
#create_root_chapters ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/techbook_helper.rb', line 181 def create_root_chapters puts 'Top level chapters creation' puts_line puts_for_next puts 'Enter chapter name (ex: "1 Specification", "2 Money and low")' print_promt while user_input = STDIN.gets.chomp case when user_input.nil? || user_input.size == 0 puts "Chapter with empty name? Are you kidding me?" puts "Please, give me something more intelligent" print_promt when @reject_args.include?(user_input) puts 'Build chapters complete' puts_line break else build_chapter user_input puts_for_next puts 'Enter chapter name' print_promt end end end |
#handler_arguments(arg) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/techbook_helper.rb', line 44 def handler_arguments(arg) case when @help_args.include?(arg) puts "Welcome to TechBook helper!".green puts 'cd path_to_your_books_library_or_book and lets go.' puts_line print_help when @create_args.include?(arg) create else print_unknown_command(arg) end end |
#init_repository ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/techbook_helper.rb', line 206 def init_repository puts 'Create git repository' Dir.chdir(@current_book_directory) do `git init` `git add .` `git commit -m "Build book #{@current_book_name} template"` end 10.times{print '. '; sleep 0.1;} puts 'Done' sleep(0.2) puts_line add_git_remote end |
#is_dir_used? ⇒ Boolean
254 255 256 257 258 |
# File 'lib/techbook_helper.rb', line 254 def is_dir_used? files_array = Dir['*'] return true unless files_array.empty? return false end |
#print_finish_message ⇒ Object
301 302 303 304 305 306 307 308 |
# File 'lib/techbook_helper.rb', line 301 def puts 'We are finish.' puts "Book '#{@current_book_name}' was created!" puts_line puts 'Add and edit .md pages, commit and push to remote to publish this book' puts 'Thanks' 1.times{puts ''} end |
#print_help ⇒ Object
260 261 262 263 264 265 266 267 |
# File 'lib/techbook_helper.rb', line 260 def print_help puts 'Command you can use:' puts '%-10s – %10s' % ['create'.green, 'Will create new book template in current directory'] puts '%-10s – %10s' % [' ', 'Aliases c, -c, --c, -create, --create'] # puts '%-10s – %10s' % ['check'.green, 'Go through dirs in current folder and check known problems'] # puts '%-10s – %10s' % [' ', 'Aliases ch, -ch, --ch, -check, --check'] puts_line end |
#print_promt ⇒ Object
310 311 312 |
# File 'lib/techbook_helper.rb', line 310 def print_promt print ':> '.green end |
#print_unknown_command(command) ⇒ Object
269 270 271 272 273 |
# File 'lib/techbook_helper.rb', line 269 def print_unknown_command(command) puts "Unknown command: #{command}" puts_line print_help end |
#puts_for_next ⇒ Object
314 315 316 |
# File 'lib/techbook_helper.rb', line 314 def puts_for_next puts 'For exit and continue type "next"'.yellow end |
#puts_line ⇒ Object
318 319 320 |
# File 'lib/techbook_helper.rb', line 318 def puts_line puts '--------------------------' end |
#puts_ok ⇒ Object
322 323 324 |
# File 'lib/techbook_helper.rb', line 322 def puts_ok puts 'I respect your choice' end |
#say_hello ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/techbook_helper.rb', line 20 def say_hello @help_args = ['-h', 'h', '--h', '-help', 'help', '--help'] @create_args = ['create', '-create', '--create', 'c', '-c', '--c'] # @check_args = ['check', '-check', '--check', 'ch', '-ch', '--ch'] @confirm_args = ['y', 'yes', 'ok', 'sure', 'Yes', 'No'] @reject_args = ['n', 'no', 'o no', 'do not', 'exit', 'close', 'cancel', 'next'] @current_book_name = nil @current_book_directory = nil print_help if ARGV.size == 0 if ARGV.size > 1 too_many_arg_error return end ARGV.each do |a| handler_arguments a end end |
#too_many_arg_error ⇒ Object
275 276 277 278 279 |
# File 'lib/techbook_helper.rb', line 275 def too_many_arg_error puts 'Too many arguments.'.red.on_black puts_line print_help end |
#yes_no_choice ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/techbook_helper.rb', line 281 def yes_no_choice print '(Yes/No) '.green print_promt answer = false while user_input = STDIN.gets.chomp case when @confirm_args.include?(user_input) answer = true break when @reject_args.include?(user_input) answer = false break else puts 'Please, answer the question – yes or no' print_promt end end answer end |