Class: Middleman::Cli::Init

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
middleman-cli/lib/middleman-cli/init.rb

Overview

A thor task for creating new projects

Constant Summary collapse

GIT_CMD =
'git'

Instance Method Summary collapse

Instance Method Details

#initObject

The init task


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
84
85
86
87
88
# File 'middleman-cli/lib/middleman-cli/init.rb', line 32

def init
  require 'fileutils'
  require 'tmpdir'

  unless git_present?
    msg_array = [
      'You need to install the git command line tool to initialize a new project. ',
      "For help installing git, please refer to GitHub's tutorial at https://help.github.com/articles/set-up-git"
    ]
    say msg_array.join, :red
    exit 1
  end

  repo_path, repo_branch = if shortname?(options[:template])
                             require 'open-uri'
                             require 'json'

                             api = 'https://directory.middlemanapp.com/api'
                             uri = ::URI.parse("#{api}/#{options[:template]}.json")

                             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

  begin
    dir = is_local_dir ? repo_path : clone_repository(repo_path, repo_branch)

    inside(target) do
      thorfile = File.join(dir, 'Thorfile')

      if File.exist?(thorfile)
        ::Thor::Util.load_thorfile(thorfile)

        invoke 'middleman:generator'
      else
        source_paths << dir
        directory dir, '.', exclude_pattern: %r{\.git/|\.gitignore$}
      end

      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 !is_local_dir && File.directory?(dir)
  end
end