lib/mortar/command/projects.rb in mortar-0.13.2 vs lib/mortar/command/projects.rb in mortar-0.13.3

- old
+ new

@@ -86,27 +86,31 @@ name = shift_argument unless name error("Usage: mortar projects:create PROJECTNAME\nMust specify PROJECTNAME") end - Mortar::Command::run("generate:project", [name]) - FileUtils.cd(name) args = [name,] + is_public = false if options[:public] - args.push('--public') + is_public= true end - + validate_project_name(name) + project_id = register_api_call(name,is_public) + # is_public is created for clarity in other sections of code + Mortar::Command::run("generate:project", [name]) + FileUtils.cd(name) + is_embedded = false if options[:embedded] - args.push("--embedded") - Mortar::Command::run("projects:register", args) + 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\"") - Mortar::Command::run("projects:register", args) + 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 alias_command "new", "projects:create" @@ -121,56 +125,17 @@ name = shift_argument unless name error("Usage: mortar projects:register PROJECT\nMust specify PROJECT.") end validate_arguments! - - if options[:public] - unless confirm("Public projects allow anyone to view and fork the code in this project\'s repository. Are you sure? (y/n)") - error("Mortar project was not registered") - end - is_private = false - else - is_private = true - end - - if options[:embedded] - validate_project_name(name) - validate_project_structure() - - register_project(name, is_private) do |project_result| - initialize_embedded_project(project_result) - end - else - unless git.has_dot_git? - # check if we're in the parent directory - if File.exists? name - error("mortar projects:register must be run from within the project directory.\nPlease \"cd #{name}\" and rerun this command.") - else - error("No git repository found in the current directory.\nTo register a project that is not its own git repository, use the --embedded option.\nIf you do want this project to be its own git repository, please initialize git in this directory, and then rerun the register command.\nTo initialize your project in git, use:\n\ngit init\ngit add .\ngit commit -a -m \"first commit\"") - end - end - - validate_project_name(name) - - unless git.remotes(git_organization).empty? - begin - error("Currently in project: #{project.name}. You can not register a new project inside of an existing mortar project.") - rescue Mortar::Command::CommandFailed => cf - error("Currently in an existing Mortar project. You can not register a new project inside of an existing mortar project.") - end - end - - register_project(name, is_private) do |project_result| - git.remote_add("mortar", project_result['git_url']) - git.push_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 + # nil is non existant project_id because it hasn't been posted yet + register_do(name, options[:public], options[:embedded], nil) + end alias_command "register", "projects:register" + # 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 @@ -237,6 +202,54 @@ 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 + + + # projects:fork GIT_URL PROJECT_NAME + # + # Used when you want to fork an existing Github repository into your own Mortar project. + # + # --public # Register a public project, which can be viewed and forked by anyone. + # + 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) + + 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 + + if options[:public] + unless confirm("Public projects allow anyone to view and fork the code in this project\'s repository. Are you sure? (y/n)") + error("Mortar project was not registered") + end + is_public = true + else + is_public = false + end + + git.clone(git_url, name, "base") + 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.git("branch --set-upstream-to 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 + alias_command "fork", "projects:fork" + end