Class: ProjectCreator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/autumn/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/autumn/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/autumn/tool/project_creator.rb', line 10

def name
  @name
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#amend?Boolean

Returns:

  • (Boolean)


97
# File 'lib/autumn/tool/project_creator.rb', line 97

def amend?; options[:amend] end

#copy(from, to) ⇒ Object



68
69
70
71
72
73
# File 'lib/autumn/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/autumn/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/autumn/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/autumn/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



105
106
107
# File 'lib/autumn/tool/project_creator.rb', line 105

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

#fatal(message) ⇒ Object



100
101
102
103
# File 'lib/autumn/tool/project_creator.rb', line 100

def fatal(message)
  warn message
  exit 1
end

#force?Boolean

Returns:

  • (Boolean)


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

def force?; options[:force] end

#got_proto?Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/autumn/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/autumn/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
# File 'lib/autumn/tool/project_creator.rb', line 86

def post_process(file)
  source = File.read(file)
  File.open(file, 'w+') do |io|
    io.puts source.gsub('$${project}', @name)
  end
end

#proceedObject



46
47
48
49
50
# File 'lib/autumn/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/autumn/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/autumn/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/autumn/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



93
94
95
# File 'lib/autumn/tool/project_creator.rb', line 93

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

#targetObject



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

def target
  File.expand_path(name)
end