Class: Mortar::Command::Projects

Inherits:
Base
  • Object
show all
Defined in:
lib/mortar/command/projects.rb

Overview

manage projects (create, register, clone, delete, set_remote)

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

#api, #ask_public, #config_parameters, #get_error_message_context, #git, #initialize, #initialize_embedded_project, #luigi_parameters, namespace, #pig_parameters, #project, #register_api_call, #register_do, #register_project, #spark_script_arguments, #validate_project_name, #validate_project_structure

Methods included from Helpers

#action, #ask, #confirm, #copy_if_not_present_at_dest, #default_host, #deprecate, #display, #display_header, #display_object, #display_row, #display_table, #display_with_indent, #download_to_file, #ensure_dir_exists, #error, error_with_failure, error_with_failure=, extended, extended_into, #format_bytes, #format_date, #format_with_bang, #full_host, #get_terminal_environment, #home_directory, #host, #hprint, #hputs, included, included_into, #installed_with_omnibus?, #json_decode, #json_encode, #line_formatter, #longest, #output_with_bang, #pending_github_team_state_message, #quantify, #redisplay, #retry_on_exception, #running_on_a_mac?, #running_on_windows?, #set_buffer, #shell, #spinner, #status, #string_distance, #styled_array, #styled_error, #styled_hash, #styled_header, #suggestion, #test_name, #ticking, #time_ago, #truncate, #warning, #with_tty, #write_to_file

Constructor Details

This class inherits a constructor from Mortar::Command::Base

Instance Method Details

#cloneObject

projects:clone PROJECTNAME

Used when you want to clone an existing Mortar project into the current directory.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/mortar/command/projects.rb', line 194

def clone
  name = shift_argument
  unless name
    error("Usage: mortar projects:clone PROJECT\nMust specify PROJECT.")
  end
  validate_arguments!
  validate_github_username
  projects = api.get_projects().body["projects"]
  project = projects.find{|p| p['name'] == name}
  unless project
    error("No project named: #{name} exists.  Your valid projects are:\n#{projects.collect{ |x| x["name"]}.join("\n")}")
  end

  project_dir = File.join(Dir.pwd, project['name'])
  unless !File.exists?(project_dir)
    error("Can't clone project: #{project['name']} since directory with that name already exists.")
  end

  git.clone(project['git_url'], project['name'])

  display "\nYour project is ready for use.  Type 'mortar help' to see the commands you can perform on the project.\n\n"
end

#createObject

projects:create PROJECTNAME

Used when you want to start a new Mortar project using Mortar generated code.

–embedded # Create a Mortar project that is not its own git repo. Your code will still be synced with a git repo in the cloud. –public # Register a public project, which can be viewed and forked by anyone.



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
119
120
121
# File 'lib/mortar/command/projects.rb', line 92

def create
  name = shift_argument
  unless name
    error("Usage: mortar projects:create PROJECTNAME\nMust specify PROJECTNAME")
  end

  args = [name,]
  is_public = false 
  if options[:public]
    is_public= true
    ask_public(is_public)
  end 
  validate_project_name(name)
  validate_github_username

  project_id = register_api_call(name,is_public) 
  Mortar::Command::run("generate:project", [name])
  FileUtils.cd(name)
  is_embedded = false
  if options[:embedded]
    is_embedded = true
    register_do(name, is_public, is_embedded, project_id)
  else
    git.git_init
    git.git("add .")
    git.git("commit -m \"Mortar project scaffolding\"")      
    register_do(name, is_public, is_embedded, project_id)
    display "NOTE: You'll need to change to the new directory to use your project:\n    cd #{name}\n\n"
  end
end

#deleteObject

projects:delete PROJECTNAME

Delete the Mortar project PROJECTNAME.



51
52
53
54
55
56
57
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/mortar/command/projects.rb', line 51

def delete
  name = shift_argument
  unless name
    error("Usage: mortar projects:delete PROJECTNAME\nMust specify PROJECTNAME.")
  end
  validate_arguments!
  projects = api.get_projects().body['projects']
  project_id = nil
  if projects.any?
    projects.each do |project|
      if project['name'] == name
        project_id = project['project_id']
      end
    end
  end

  if project_id.nil?
    display "\nNo project with name: #{name}"
  else
    # delete embedded project mirror if one exists
    mirror_dir = "#{git.mortar_mirrors_dir()}/#{name}"
    if File.directory? mirror_dir
      FileUtils.rm_r mirror_dir
    end

    # delete Mortar remote
    action("Sending request to delete project: #{name}") do
      api.delete_project(project_id).body['project_id']
    end
    display "\nYour project has been deleted."
  end
  
end

#forkObject

projects:fork GIT_URL PROJECT_NAME

Used when you want to fork an existing Git repository into your own Mortar project.

–public # Register a public project, which can be viewed and forked by anyone.



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
253
254
# File 'lib/mortar/command/projects.rb', line 224

def fork
  git_url = shift_argument
  name = shift_argument
  unless git_url and name
    error("Usage: mortar projects:fork GIT_URL PROJECT\nMust specify GIT_URL and PROJECT.")
  end
  validate_arguments!
  validate_project_name(name)
  validate_github_username

  if git.has_dot_git?
    begin
      error("Currently in git repo.  You can not fork a new project inside of an existing git repository.")
    rescue Mortar::Command::CommandFailed => cf
      error("Currently in git repo.  You can not fork a new project inside of an existing git repository.")
    end
  end
  is_public = options[:public]
  ask_public(is_public)
  git.clone(git_url, name, git.fork_base_remote_name)
  Dir.chdir(name)
  # register a nil project id because it hasn't been created yet
  register_project(name, is_public, nil) do |project_result|
    git.remote_add("mortar", project_result['git_url'])
    git.push_master
    # We want the default remote to be the Mortar managed repo.
    git.git("fetch --all")
    git.set_upstream('mortar/master')
    display "Your project is ready for use.  Type 'mortar help' to see the commands you can perform on the project.\n\n"
  end
end

#indexObject

projects

Display the available set of Mortar projects.



30
31
32
33
34
35
36
37
38
39
# File 'lib/mortar/command/projects.rb', line 30

def index
  validate_arguments!
  projects = api.get_projects().body["projects"]
  if projects.any?
    styled_header("projects")
    styled_array(projects.collect{ |x| x["name"] })
  else
    display("You have no projects.")
  end
end

#listObject

projects:list

Display the available set of Mortar projects.



44
45
46
# File 'lib/mortar/command/projects.rb', line 44

def list
  index
end

#registerObject

projects:register PROJECTNAME

Used when you want to start a new Mortar project using your existing code in the current directory.

–embedded # Register code that is not its own git repo as a Mortar project. Your code will still be synced with a git repo in the cloud. –public # Register a public project, which can be viewed and forked by anyone.



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/mortar/command/projects.rb', line 131

def register
  name = shift_argument
  unless name
    error("Usage: mortar projects:register PROJECT\nMust specify PROJECT.")
  end
  validate_arguments!
  ask_public(options[:public])
  #nil is non existant project_id because it hasn't been posted yet
  register_do(name, options[:public], options[:embedded], nil) 
  
end

#set_remoteObject

projects:set_remote PROJECTNAME

Used after you checkout code for an existing Mortar project from a non-Mortar git repository.

Adds a remote to your local git repository to the Mortar git repository. For example if a co-worker creates a Mortar project from an internal repository you would clone the internal repository and then after cloning call mortar projects:set_remote.

–embedded # make this a embedded project tied to the specified remote



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/mortar/command/projects.rb', line 154

def set_remote
  project_name = shift_argument

  unless project_name
    error("Usage: mortar projects:set_remote PROJECT\nMust specify PROJECT.")
  end

  unless options[:embedded]
    unless git.has_dot_git?
      error("Can only set the remote for an existing git project.  Please run:\n\ngit init\ngit add .\ngit commit -a -m \"first commit\"\n\nto initialize your project in git.")
    end

    if git.remotes(git_organization).include?("mortar")
      display("The remote has already been set for project: #{project_name}")
      return
    end
  end

  projects = api.get_projects().body["projects"]
  project = projects.find { |p| p['name'] == project_name}
  unless project
    error("No project named: #{project_name} exists. You can create this project using:\n\n mortar projects:create")
  end

  if options[:embedded]
    File.open(".mortar-project-remote", "w") do |f|
      f.puts project["git_url"]
    end
    git.sync_embedded_project(project, embedded_project_user_branch, git_organization)
  else
    git.remote_add("mortar", project["git_url"])
  end

  display("Successfully added the mortar remote to the #{project_name} project")

end