Class: FileSystem::ProfileHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/atk/file_system.rb

Instance Method Summary collapse

Constructor Details

#initialize(unqiue_id) ⇒ ProfileHelper

Returns a new instance of ProfileHelper.



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/atk/file_system.rb', line 415

def initialize(unqiue_id)
    function_def = "ProfileHelper.new(unqiue_id)"
    if unqiue_id =~ /\n/
        raise "            \n            \n            Inside the \#{function_def.color_as :code}\n            the unqiue_id contains a newline (\\\\n)\n            \n            unqiue_id: \#{\"\#{unqiue_id}\".inspect}\n            \n            Sadly newlines are not allowed in the unqiue_id due to how they are searched for.\n            Please provide a unqiue_id that doesn't have newlines.\n        HEREDOC\n    end\n    if \"\#{unqiue_id}\".size < 5 \n        raise <<-HEREDOC.remove_indent\n            \n            \n            Inside the \#{function_def.color_as :code}\n            the unqiue_id is: \#{\"\#{unqiue_id}\".inspect}\n            \n            That's not even 5 characters. Come on man, there's going to be problems if the unqiue_id isn't unqiue\n            generate a random number (once), then put the name of the service at the front of that random number\n        HEREDOC\n    end\n    @unqiue_id = unqiue_id\nend\n".remove_indent

Instance Method Details

#add_to_bash_profile(code) ⇒ Object



450
451
452
# File 'lib/atk/file_system.rb', line 450

def add_to_bash_profile(code)
    uniquely_append(code, HOME/".bash_profile", bash_comment_out)
end

#add_to_bash_rc(code) ⇒ Object



458
459
460
# File 'lib/atk/file_system.rb', line 458

def add_to_bash_rc(code)
    uniquely_append(code, HOME/".bashrc", bash_comment_out)
end

#add_to_zsh_profile(code) ⇒ Object



454
455
456
# File 'lib/atk/file_system.rb', line 454

def add_to_zsh_profile(code)
    uniquely_append(code, HOME/".zprofile", bash_comment_out)
end

#add_to_zsh_rc(code) ⇒ Object



462
463
464
# File 'lib/atk/file_system.rb', line 462

def add_to_zsh_rc(code)
    uniquely_append(code, HOME/".zshrc", bash_comment_out)
end

#bash_comment_outObject



444
445
446
447
448
# File 'lib/atk/file_system.rb', line 444

def bash_comment_out
    ->(code) do
        "### #{code}"
    end
end

#uniquely_append(string_to_add, location_of_file, comment_out_line) ⇒ Object



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/atk/file_system.rb', line 466

def uniquely_append(string_to_add, location_of_file, comment_out_line)
    _UNQIUE_HELPER = 'fj03498hglkasjdgoghu2904' # dont change this, its a 1-time randomly generated string
    final_string = "\n"
    final_string += comment_out_line["start of ID: #{@unqiue_id} #{_UNQIUE_HELPER}"] + "\n"
    final_string += comment_out_line["NOTE! if you remove this, remove the whole thing (don't leave a dangling start/end comment)"] + "\n"
    final_string += string_to_add + "\n"
    final_string += comment_out_line["end of ID: #{@unqiue_id} #{_UNQIUE_HELPER}"]
    
    # open the existing file if there is one
    file = FS.read(location_of_file) || ""
    # remove any previous versions
    file.gsub!(/### start of ID: (.+) #{_UNQIUE_HELPER}[\s\S]*### end of ID: \1 #{_UNQIUE_HELPER}/) do |match|
        if $1 == @unqiue_id
            ""
        else
            match
        end
    end
    # append the the new code at the bottom (highest priority)
    file += final_string
    # overwrite the file
    FS.write(file, to: location_of_file)
end