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.



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

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

Instance Method Details

#add_to_bash_profile(code) ⇒ Object



499
500
501
# File 'lib/atk/file_system.rb', line 499

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

#add_to_bash_rc(code) ⇒ Object



507
508
509
# File 'lib/atk/file_system.rb', line 507

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

#add_to_zsh_profile(code) ⇒ Object



503
504
505
# File 'lib/atk/file_system.rb', line 503

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

#add_to_zsh_rc(code) ⇒ Object



511
512
513
# File 'lib/atk/file_system.rb', line 511

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

#bash_comment_outObject



493
494
495
496
497
# File 'lib/atk/file_system.rb', line 493

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

#uniquely_append(string_to_add, location_of_file, comment_out_line) ⇒ Object



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/atk/file_system.rb', line 515

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