lib/middleman-cli/init.rb in middleman-cli-4.5.1 vs lib/middleman-cli/init.rb in middleman-cli-5.0.0.rc.1

- old
+ new

@@ -20,10 +20,14 @@ type: :boolean, aliases: '-B', default: false, desc: 'Skip bundle install' + class_option 'bundle-path', + type: :string, + desc: 'Use specified bundle path' + # The init task def init require 'fileutils' require 'tmpdir' @@ -33,43 +37,34 @@ say msg, :red exit 1 end repo_path, repo_branch = if shortname?(options[:template]) - require 'open-uri' - require 'json' + require 'open-uri' + require 'json' - api = 'https://directory.middlemanapp.com/api' - uri = ::URI.parse("#{api}/#{options[:template]}.json") + api = 'https://directory.middlemanapp.com/api' + uri = ::URI.parse("#{api}/#{options[:template]}.json") - begin - data = ::JSON.parse(uri.read) - data['links']['github'] - data['links']['github'].split('#') - rescue ::OpenURI::HTTPError - say "Template `#{options[:template]}` not found in Middleman Directory." - say 'Did you mean to use a full `user/repo` path?' - exit 1 - end - else - repo_name, repo_branch = options[:template].split('#') - [repository_path(repo_name), repo_branch] - end + begin + data = ::JSON.parse(uri.read) + is_local_dir = false + data['links']['github'].split('#') + rescue ::OpenURI::HTTPError + say "Template `#{options[:template]}` not found in Middleman Directory." + say 'Did you mean to use a full `user/repo` path?' + exit 1 + end + else + repo_name, repo_branch = options[:template].split('#') + repo_path, is_local_dir = repository_path(repo_name) + [repo_path, repo_branch] + end - dir = Dir.mktmpdir - begin - branch_cmd = repo_branch ? "-b #{repo_branch} " : '' + dir = is_local_dir ? repo_path : clone_repository(repo_path, repo_branch) - git_path = "#{branch_cmd}#{repo_path}" - run("#{GIT_CMD} clone --depth 1 #{branch_cmd}#{repo_path} #{dir}") - - unless $?.success? - say "Git clone command failed. Make sure git repository exists: #{git_path}", :red - exit 1 - end - inside(target) do thorfile = File.join(dir, 'Thorfile') if File.exist?(thorfile) ::Thor::Util.load_thorfile(thorfile) @@ -78,22 +73,24 @@ else source_paths << dir directory dir, '.', exclude_pattern: /\.git\/|\.gitignore$/ end - run('bundle install') unless ENV['TEST'] || options[:'skip-bundle'] + bundle_args = options[:'bundle-path'] ? " --path=#{options[:'bundle-path']}" : '' + run("bundle install#{bundle_args}") unless ENV['TEST'] || options[:'skip-bundle'] end ensure - FileUtils.remove_entry(dir) if File.directory?(dir) + FileUtils.remove_entry(dir) if !is_local_dir && File.directory?(dir) end end protected # Copied from Bundler def git_present? return @git_present if defined?(@git_present) + @git_present = which(GIT_CMD) || which('git.exe') end # Copied from Bundler def which(executable) @@ -110,11 +107,33 @@ def shortname?(repo) repo.split('/').length == 1 end - def repository_path(repo) - repo.include?('://') || repo.include?('git@') ? repo : "https://github.com/#{repo}.git" + def repository_path(repo_name) + if repo_name.include?('://') || /^[^@]+@[^:]+:.+/ =~ repo_name + repo_name + elsif (repo_path = Pathname(repo_name)).directory? && repo_path.absolute? + [repo_name, true] + else + "https://github.com/#{repo_name}.git" + end + end + + def clone_repository(repo_path, repo_branch) + dir = Dir.mktmpdir + + branch_cmd = repo_branch ? "-b #{repo_branch} " : '' + + git_path = "#{branch_cmd}#{repo_path}" + run("#{GIT_CMD} clone --depth 1 #{branch_cmd}#{repo_path} #{dir}") + + unless $CHILD_STATUS.success? + say "Git clone command failed. Make sure git repository exists: #{git_path}", :red + exit 1 + end + + dir end # Add to CLI Base.register(self, 'init', 'init TARGET [options]', 'Create new project at TARGET')