-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerateProject.rb
99 lines (83 loc) · 3.11 KB
/
generateProject.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/ruby -wKU
require 'trollop'
require 'fileutils'
GENVERSION = '0.0.1'
REPO_CORESTR = 'https://github.com/Sunstrike/LibCorestrike.git'
REPO_CCC = 'https://github.com/Quiddity-Modding/CodeChickenCore.git'
# Parse flags
opts = Trollop::options do
opt :modname, 'Mod name (human)', :type => :string, :short => 'n'
opt :modid, 'Mod ID', :type => :string, :short => 'i'
opt :package, 'Package ID', :type => :string
opt :codechickencore, 'Add CodeChickenCore submodule (creates Git repo; requires Git on path)', :default => false, :short => 'c'
opt :libcorestrike, 'Add LibCorestrike submodule (creates Git repo; requires Git on path)', :default => false, :short => 'l'
version GENVERSION
end
if opts[:modname] == nil || opts[:modid] == nil || opts[:package] == nil
puts 'Parameters not specified! See --help for flags.'
exit -1
end
opts[:date] = DateTime.now.strftime('%k:%M %Z (%-d/%-m/%Y)')
opts[:genversion] = GENVERSION
# Directories and pre-clean
FileUtils.rm_r 'output' if Dir.exists? 'output'
outSrc = "output#{File::SEPARATOR}src#{File::SEPARATOR}#{opts[:package].gsub(/\./, File::SEPARATOR)}"
resSrc = "output#{File::SEPARATOR}resources"
libSrc = outSrc + File::SEPARATOR + 'lib'
confSrc = outSrc + File::SEPARATOR + 'configuration'
FileUtils.mkdir_p libSrc
FileUtils.mkdir_p confSrc
FileUtils.mkdir_p resSrc
# Assemble filters
filters = {
'@MODNAME@' => opts[:modname],
'@MODID@' => opts[:modid],
'@PACKAGE@' => opts[:package],
'@PACKAGE_START@' => opts[:package][/[a-z]+/],
'@DATE@' => opts[:date],
'@GENVERSION@' => opts[:genversion]
}
puts "Filters: #{filters}\n\n"
# Get code from project
files = {}
Dir.glob(File.join('template', '**', '*'), File::FNM_DOTMATCH) { |file|
next if File.directory? file
next if file == '.' || file == '..' # We need DOTMATCH to catch .gitignore, so we ignore . and ..
files[file] = IO.read file
}
puts 'Copying and filtering from template:'
files.each { |file, content|
# Get target fname
f = file.gsub /template/, 'output'
f = f.gsub /src/, 'src' + File::SEPARATOR + opts[:package].gsub(/\./, File::SEPARATOR)
f = f.gsub /@MODNAME@/, opts[:modid]
# Filter file contents
filters.each { |key, target|
content.gsub! /#{key}/, target
}
# Write file
File.open(f, 'w') { |f|
f.write(content)
}
puts "#{file} => #{f}"
}
# Git support (submodules)
need_git = opts[:codechickencore] || opts[:libcorestrike]
puts "\nBuild git repo: #{if !need_git; 'Not needed.' end}"
if need_git
Dir.chdir 'output'
system 'git', 'init'
system 'git', 'add', '.'
system 'git', 'commit', '-m', '(Skeleton) Initial commit.'
if opts[:codechickencore]
puts "\nAdding submodule CodeChickenCore:"
system 'git', 'submodule', 'add', REPO_CCC, 'CodeChickenCore'
end
if opts[:libcorestrike]
puts "\nAdding submodule LibCorestrike:"
system 'git', 'submodule', 'add', REPO_CORESTR, 'LibCorestrike'
end
puts "\nCommitting submodules:"
system 'git', 'commit', '-m', '(Skeleton) Commit submodules.'
end
puts "\nFINISHED: See output directory for completed repo."