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.



461
462
463
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
# File 'lib/atk/file_system.rb', line 461

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



496
497
498
# File 'lib/atk/file_system.rb', line 496

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

#add_to_bash_rc(code) ⇒ Object



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

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

#add_to_zsh_profile(code) ⇒ Object



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

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

#add_to_zsh_rc(code) ⇒ Object



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

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

#bash_comment_outObject



490
491
492
493
494
# File 'lib/atk/file_system.rb', line 490

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

#uniquely_append(string_to_add, location_of_file, comment_out_line) ⇒ Object



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

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