middleman/middleman-cli/lib/middleman-cli/init.rb

130 lines
3.5 KiB
Ruby
Raw Normal View History

2011-12-29 07:52:51 +01:00
# CLI Module
2011-12-21 21:13:28 +01:00
module Middleman::Cli
2011-12-29 07:52:51 +01:00
# A thor task for creating new projects
2015-01-04 21:23:35 +01:00
class Init < Thor::Group
include Thor::Actions
2015-01-04 21:23:35 +01:00
2016-01-19 23:55:47 +01:00
GIT_CMD = 'git'.freeze
check_unknown_options!
2015-01-04 21:23:35 +01:00
argument :target, type: :string, default: '.'
2015-01-04 21:23:35 +01:00
class_option 'template',
aliases: '-T',
default: 'middleman/middleman-templates-default',
desc: 'Use a project template'
# Do not run bundle install
2015-01-04 21:23:35 +01:00
class_option 'skip-bundle',
type: :boolean,
aliases: '-B',
default: false,
desc: 'Skip bundle install'
2011-12-29 07:52:51 +01:00
# The init task
2015-01-04 21:23:35 +01:00
def init
2015-01-04 22:06:14 +01:00
require 'fileutils'
require 'tmpdir'
2016-01-19 23:55:47 +01:00
unless git_present?
msg = 'You need to install the git command line tool to initialize a new project. '
msg << "For help installing git, please refer to GitHub's tutorial at https://help.github.com/articles/set-up-git"
say msg, :red
exit 1
end
2014-08-15 05:34:31 +02:00
repo_path, repo_branch = if shortname?(options[:template])
require 'open-uri'
require 'json'
2015-12-10 22:05:57 +01:00
api = 'https://directory.middlemanapp.com/api'
uri = ::URI.parse("#{api}/#{options[:template]}.json")
begin
data = ::JSON.parse(uri.read)
data['links']['github']
2014-08-15 05:34:31 +02:00
data['links']['github'].split('#')
rescue ::OpenURI::HTTPError
2015-01-04 21:23:35 +01:00
say "Template `#{options[:template]}` not found in Middleman Directory."
say 'Did you mean to use a full `user/repo` path?'
exit 1
end
else
2014-08-15 05:34:31 +02:00
repo_name, repo_branch = options[:template].split('#')
[repository_path(repo_name), repo_branch]
end
2015-01-04 22:06:14 +01:00
dir = Dir.mktmpdir
2015-02-07 22:38:29 +01:00
2015-01-04 22:06:14 +01:00
begin
branch_cmd = repo_branch ? "-b #{repo_branch} " : ''
2014-08-15 05:34:31 +02:00
git_path = "#{branch_cmd}#{repo_path}"
run("#{GIT_CMD} clone --depth 1 #{branch_cmd}#{repo_path} #{dir}")
2016-01-19 23:55:47 +01:00
unless $?.success?
say "Git clone command failed. Make sure git repository exists: #{git_path}", :red
exit 1
end
2015-01-04 21:23:35 +01:00
inside(target) do
thorfile = File.join(dir, 'Thorfile')
if File.exist?(thorfile)
::Thor::Util.load_thorfile(thorfile)
2015-01-04 21:23:35 +01:00
invoke 'middleman:generator'
else
source_paths << dir
directory dir, '.', exclude_pattern: /\.git\/|\.gitignore$/
end
2015-01-04 21:23:35 +01:00
run('bundle install') unless ENV['TEST'] || options[:'skip-bundle']
end
2015-01-04 22:06:14 +01:00
ensure
FileUtils.remove_entry(dir) if File.directory?(dir)
end
end
protected
# Copied from Bundler
def git_present?
return @git_present if defined?(@git_present)
2016-01-19 23:55:47 +01:00
@git_present = which(GIT_CMD) || which('git.exe')
end
# Copied from Bundler
def which(executable)
if File.file?(executable) && File.executable?(executable)
executable
elsif ENV['PATH']
path = ENV['PATH'].split(File::PATH_SEPARATOR).find do |p|
abs_path = File.join(p, executable)
File.file?(abs_path) && File.executable?(abs_path)
end
path && File.expand_path(executable, path)
end
end
def shortname?(repo)
repo.split('/').length == 1
end
def repository_path(repo)
2015-02-13 10:08:09 +01:00
repo.include?('://') || repo.include?('git@') ? repo : "git://github.com/#{repo}.git"
end
2015-01-04 21:23:35 +01:00
# Add to CLI
Base.register(self, 'init', 'init TARGET [options]', 'Create new project at TARGET')
2015-01-04 21:23:35 +01:00
# Map "i", "new" and "n" to "init"
Base.map(
'i' => 'init',
'new' => 'init',
'n' => 'init'
)
end
end