Class: Marv::Project::Create

Inherits:
Object
  • Object
show all
Defined in:
lib/marv/project/create.rb

Instance Method Summary collapse

Constructor Details

#initialize(task, dir) ⇒ Create

Initialize project creator



6
7
8
9
10
11
12
13
14
15
# File 'lib/marv/project/create.rb', line 6

def initialize(task, dir)
  @task = task
  @dir = dir
  @path = project_path
  @global = Marv::Global.new(task)
  @options = project_options
  @layout = layout_path

  create_project
end

Instance Method Details

#ask_author_detailsObject

Ask author details



48
49
50
51
52
53
54
55
56
57
# File 'lib/marv/project/create.rb', line 48

def ask_author_details
  options = {}

  options[:author] = @task.ask_input "Enter project author:", :default => @global.config[:author]
  options[:author_uri] = @task.ask_input "Enter project author URI:", :default => @global.config[:author_uri]
  options[:license_name] = @task.ask_input "Enter project license name:", :default => @global.config[:license_name]
  options[:license_uri] = @task.ask_input "Enter project license URI:", :default => @global.config[:license_uri]

  return options
end

#ask_builtin_project_layoutObject

Ask builtin project layout



78
79
80
81
82
83
84
85
# File 'lib/marv/project/create.rb', line 78

def ask_builtin_project_layout
  options = {}

  options[:local_layout] = false
  options[:layout] = @task.ask_input "Which layout do you want to use?", :limited_to => ["theme", "plugin"], :default => "theme"

  return options
end

#ask_project_layoutObject

Ask project layout



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/marv/project/create.rb', line 60

def ask_project_layout
  options = {}

  if @global.layouts.empty?
    options.merge!(ask_builtin_project_layout)
  else
    if @task.said_yes?("Do you want to use a local layout?")
      options[:local_layout] = true
      options[:layout] = @task.ask_input "Which layout do you want to use?", :limited_to => @global.layouts
    else
      options.merge!(ask_builtin_project_layout)
    end
  end

  return options
end

#config_templateObject

Project config template



99
100
101
# File 'lib/marv/project/create.rb', line 99

def config_template
  ::File.expand_path(::File.join(Marv.root, 'layouts', 'config', 'project.rb'))
end

#create_config_fileObject

Create config file



123
124
125
126
127
128
# File 'lib/marv/project/create.rb', line 123

def create_config_file
  @project = Marv::Project::Project.new(@task, @path, @options)

  config_rb = ::File.join(@path, 'config.rb')
  @global.template config_template, config_rb, @project.context unless ::File.exists?(config_rb)
end

#create_projectObject

Create a new project



144
145
146
147
148
149
150
# File 'lib/marv/project/create.rb', line 144

def create_project
  @task.say_empty(2)

  create_project_dirs
  create_config_file
  parse_layout_files
end

#create_project_dirsObject

Create project directories



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/marv/project/create.rb', line 104

def create_project_dirs
  ::Dir.glob(::File.join(@layout, '**', '*')).each do |dir|
    if ::File.directory?(dir)
      # Get source and target files
      source_dir = dir.gsub(@layout, '')
      target_dir = ::File.join(@path, 'source', source_dir)

      @task.empty_directory target_dir unless ::File.directory?(target_dir)
    end
  end

  # Create .wacth dir
  @task.shell.mute do
    watch_dir = ::File.join(@path, '.watch', 'build')
    @task.empty_directory watch_dir unless ::File.directory?(watch_dir)
  end
end

#layout_pathObject

Choosen layout path



88
89
90
91
92
93
94
95
96
# File 'lib/marv/project/create.rb', line 88

def layout_path
  layout = ::File.expand_path(::File.join(Marv.root, 'layouts', @options[:layout]))

  if @options[:local_layout]
    layout = ::File.join(@global.layouts_path, @options[:layout])
  end

  return layout
end

#parse_layout_filesObject

Parse layout files in project dir



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/marv/project/create.rb', line 131

def parse_layout_files
  ::Dir.glob(::File.join(@layout, '**', '*')).each do |file|
    unless ::File.directory?(file)
      # Get source and target files
      source_file = file.gsub(@layout, '')
      target_file = ::File.join(@path, 'source', source_file)
      # Parse template file
      @global.template file, target_file, @project.context
    end
  end
end

#project_optionsObject

Ask for project details



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/marv/project/create.rb', line 23

def project_options
  # Check if project exists and abort
  if ::File.directory?(@path)
    @task.say_error "Project already exists", nil, false
    abort
  end

  @task.say_info "This will create a new project."
  @task.say_warning "Please enter project details below."

  # Get project options
  options = {}

  options[:name] = @task.ask_input "Enter project name:", :default => @global.config.fetch(:name, @dir)
  options[:uri] = @task.ask_input "Enter project URI:", :default => @global.config[:uri]
  options[:version] = @task.ask_input "Enter project version:", :default => @global.config.fetch(:version, '0.1.0')
  options[:description] = @task.ask_input "Enter project description:", :default => @global.config.fetch(:description, 'Created with Marv')

  options.merge!(ask_author_details)
  options.merge!(ask_project_layout)

  return options
end

#project_pathObject

Project path



18
19
20
# File 'lib/marv/project/create.rb', line 18

def project_path
  ::File.expand_path(@dir)
end