Class: Project
- Inherits:
-
Object
- Object
- Project
- Defined in:
- lib/gito/project.rb
Instance Method Summary collapse
- #change_directory ⇒ Object
-
#clone(is_temp_folder = false, shell_copy = true) ⇒ Object
CLONE THE REPOSITORY.
- #destination ⇒ Object
- #go_inside_and_run(command) ⇒ Object
-
#initialize(url) ⇒ Project
constructor
A new instance of Project.
- #install_dependencies ⇒ Object
- #open_editor(app) ⇒ Object
- #open_folder ⇒ Object
- #retrieve_cloneable_url ⇒ Object
- #sanitize_url(url) ⇒ Object
Constructor Details
#initialize(url) ⇒ Project
Returns a new instance of Project.
9 10 11 12 13 14 |
# File 'lib/gito/project.rb', line 9 def initialize(url) @base_url = sanitize_url(url) @destination_dir = nil @destination = destination @detector_json_path = 'https://raw.githubusercontent.com/cesarferreira/gito/master/detector.json' end |
Instance Method Details
#change_directory ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gito/project.rb', line 53 def change_directory # TODO aparently this doesn't work because ruby forks the terminal process and can't communicate with his parent # temp_script_name = './temp.sh' # AppUtils::execute 'echo "cd '+@destination+'" > ' + temp_script_name # AppUtils::execute '. '+temp_script_name # AppUtils::execute 'rm -rf ' + temp_script_name short_path = @destination_dir.to_s.gsub(Dir.home, '~') puts "-------------------------------------------" puts "Please change directory" puts "cd #{short_path.yellow}" puts "-------------------------------------------" end |
#clone(is_temp_folder = false, shell_copy = true) ⇒ Object
CLONE THE REPOSITORY
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/gito/project.rb', line 99 def clone(is_temp_folder=false, shell_copy=true) url = retrieve_cloneable_url unless is_temp_folder prefix = Dir.pwd + '/' else prefix = Dir.tmpdir + '/gito/' end @destination_dir = prefix + "#{@destination}" if File.directory?(@destination_dir) puts "The folder #{@destination_dir.green} is not empty..." go_inside_and_run "git reset --hard HEAD" go_inside_and_run "git pull" else shell_copy_string = shell_copy ? '--depth 1' : '' AppUtils.execute("git clone #{shell_copy_string} --recursive #{url} #{@destination_dir}") end @destination_dir end |
#destination ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/gito/project.rb', line 35 def destination stripped_url = @base_url.gsub('.git', '') stripped_url = stripped_url.gsub('.git', '') stripped_url = stripped_url.gsub('[email protected]:', '') stripped_url = stripped_url.gsub('https://github.com/', '') stripped_url.gsub('http://github.com/', '') if stripped_url.start_with?('http') stripped_url = stripped_url.split('/').last(2).join('/') end if stripped_url.include?(':') && stripped_url.start_with?('git@') stripped_url = stripped_url.split(':').last end stripped_url.gsub('/','-') end |
#go_inside_and_run(command) ⇒ Object
132 133 134 135 136 |
# File 'lib/gito/project.rb', line 132 def go_inside_and_run(command) Dir.chdir(@destination_dir) do AppUtils::execute command, false end end |
#install_dependencies ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/gito/project.rb', line 68 def install_dependencies file = open(@detector_json_path) {|f| f.read } types = JSON.parse(file) chosen = nil Dir.chdir(@destination_dir) types.each do |item| if File.exists? (item['file_requirement']) puts "#{item['type']} detected...".yellow go_inside_and_run item['installation_command'] end end end |
#open_editor(app) ⇒ Object
122 123 124 125 |
# File 'lib/gito/project.rb', line 122 def open_editor(app) puts "Opening editor...".yellow go_inside_and_run "#{app} ." end |
#open_folder ⇒ Object
127 128 129 130 |
# File 'lib/gito/project.rb', line 127 def open_folder puts 'Opening folder...'.yellow go_inside_and_run 'open .' end |
#retrieve_cloneable_url ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/gito/project.rb', line 83 def retrieve_cloneable_url starts_with_git = @base_url.split(//).first(4).join.eql? 'git@' ends_with_git = @base_url.split(//).last(4).join.eql? '.git' # ends with git but doesnt start with git return @base_url if ends_with_git && !starts_with_git # ends with git but doesnt start with git return "#{@base_url}.git" if !ends_with_git && !starts_with_git @base_url end |
#sanitize_url(url) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/gito/project.rb', line 16 def sanitize_url(url) url = url.split('?').first url.chop! if url.end_with? '/' is_valid_url = url =~ /\A#{URI::regexp(['http', 'https'])}\z/ || url.include?('git@') unless is_valid_url valid_shortcut = url.split('/').size == 2 if valid_shortcut url = 'https://github.com/' + url else url = nil end end url end |