Class: ProjectCreator

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/ramaze/tool/project_creator.rb

Constant Summary collapse

PROTO =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ ProjectCreator

Returns a new instance of ProjectCreator.



12
13
14
# File 'lib/ramaze/tool/project_creator.rb', line 12

def initialize(name, options = {})
  @name, @options = name, options
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/ramaze/tool/project_creator.rb', line 10

def name
  @name
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/ramaze/tool/project_creator.rb', line 10

def options
  @options
end

Instance Method Details

#amend?Boolean

Returns:

  • (Boolean)


98
# File 'lib/ramaze/tool/project_creator.rb', line 98

def amend?; options[:amend] end

#copy(from, to) ⇒ Object



68
69
70
71
72
73
# File 'lib/ramaze/tool/project_creator.rb', line 68

def copy(from, to)
  return unless copy_check(to)
  puts "copy(%p, %p)" % [from, to]
  FileUtils.cp(from, to)
  post_process(to)
end

#copy_check(to) ⇒ Object



75
76
77
78
79
80
# File 'lib/ramaze/tool/project_creator.rb', line 75

def copy_check(to)
  exists = File.file?(to)
  return if exists and amend?
  return if exists and not force?
  return true
end

#createObject



38
39
40
41
42
43
44
# File 'lib/ramaze/tool/project_creator.rb', line 38

def create
  got_proto?

  puts "Found proto at: %p, proceeding...\n\n" % proto
  mkdir(relate('/')) if create_root?
  proceed
end

#create_root?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/ramaze/tool/project_creator.rb', line 27

def create_root?
  return true unless File.directory?(target)
  return true if amend? or force?
  fatal "%p is a directory, choose different project name or use --amend/--force" % target
end

#eachObject



106
107
108
# File 'lib/ramaze/tool/project_creator.rb', line 106

def each
  Dir["#{proto}/**/*"].each{|path| yield(path) }
end

#fatal(message) ⇒ Object



101
102
103
104
# File 'lib/ramaze/tool/project_creator.rb', line 101

def fatal(message)
  warn message
  exit 1
end

#force?Boolean

Returns:

  • (Boolean)


99
# File 'lib/ramaze/tool/project_creator.rb', line 99

def force?; options[:force] end

#got_proto?Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/ramaze/tool/project_creator.rb', line 33

def got_proto?
  return true if File.directory?(proto)
  fatal "Cannot create, %p doesn't exist, use --proto or create the proto directory" % proto
end

#mkdir(dir) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/ramaze/tool/project_creator.rb', line 60

def mkdir(dir)
  exists = File.directory?(dir)
  return if exists and amend?
  return if exists and not force?
  puts "mkdir(%p)" % dir
  FileUtils.mkdir_p(dir)
end

#post_process(file) ⇒ Object

Think about a useful way to process the generated files it should be possible to substitute some things like the project name in the configuration



86
87
88
89
90
91
92
# File 'lib/ramaze/tool/project_creator.rb', line 86

def post_process(file)
  return
  source = File.read(file)
  File.open(file, 'w+') do |io|
    io.write(source.gsub('$project', "'#@name'"))
  end
end

#proceedObject



46
47
48
49
50
# File 'lib/ramaze/tool/project_creator.rb', line 46

def proceed
  files, directories = partition{|path| File.file?(path) }
  proceed_directories(directories)
  proceed_files(files)
end

#proceed_directories(dirs) ⇒ Object



56
57
58
# File 'lib/ramaze/tool/project_creator.rb', line 56

def proceed_directories(dirs)
  dirs.each{|dir| mkdir(relate(dir)) }
end

#proceed_files(files) ⇒ Object



52
53
54
# File 'lib/ramaze/tool/project_creator.rb', line 52

def proceed_files(files)
  files.each{|file| copy(file, relate(file)) }
end

#protoObject



20
21
22
23
24
25
# File 'lib/ramaze/tool/project_creator.rb', line 20

def proto
  PROTO.map!{|pr| File.expand_path(pr) }
  proto = options[:proto] ||= PROTO.find{|f| File.directory?(f) }
  layout = options[:layout] ||= '/'
  File.expand_path(File.join(proto, layout))
end

#relate(path) ⇒ Object



94
95
96
# File 'lib/ramaze/tool/project_creator.rb', line 94

def relate(path)
  File.join(target, path.to_s.sub(proto, ''))
end

#targetObject



16
17
18
# File 'lib/ramaze/tool/project_creator.rb', line 16

def target
  File.expand_path(name)
end