temp 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,25 +10,17 @@ You can install Temp with `gem install temp`.
10
10
 
11
11
  ### Templates
12
12
 
13
- By default, Temp will look for templates in `~/.temp`. A template can have a
14
- `.tempignore` file that contains a newline-separated list of globs to specify
15
- files that should be ignored.
13
+ By default, Temp will look for templates in `~/.temp`, but you can specify a
14
+ different template directory with the `~/.tempconf` file.
15
+
16
+ Templates can contain Tempfiles that specify the name, description, files to
17
+ ignore, etc. for a template.
16
18
 
17
19
  ### Project
18
20
 
19
21
  To create a project from a template, use `temp project_dir template_name`. Temp
20
- will create a project directory and copy all the files from a template of the
21
- given name (excluding the ignored files, of course).
22
-
23
- ## Later
24
-
25
- Temp isn't very useful at the moment but I do have some features planned for
26
- later:
27
-
28
- - A DSL for the templates
29
- - Passing options to the DSL so that, for example, a file can be ignored if the
30
- user specifies a certain option on the command line
31
- - Allow ERB to be used in files
22
+ will create a project directory and copy the files from a template of the given
23
+ name.
32
24
 
33
25
  ## License
34
26
 
data/bin/temp CHANGED
@@ -4,5 +4,4 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
 
5
5
  require 'temp'
6
6
 
7
- runner = Temp::Runner.new(:args => ARGV, :conf_file => '~/.tempconfig')
8
- runner.start!
7
+ Temp::Runner.start!(ARGV)
@@ -1,3 +1,7 @@
1
- require 'temp/copier'
1
+ require 'temp/config'
2
+ require 'temp/tempfile'
3
+ require 'temp/template'
4
+ require 'temp/project'
5
+ require 'temp/exceptions'
2
6
  require 'temp/version'
3
7
  require 'temp/runner'
@@ -0,0 +1,37 @@
1
+ require 'yaml'
2
+
3
+ module Temp
4
+
5
+ # A Config object loads the Temp configuration file and provides access to its
6
+ # values for other parts of Temp.
7
+ class Config
8
+
9
+ DEFAULT_CONF_FILE = File.expand_path('~/.tempconf')
10
+ DEFAULT_TEMP_DIR = File.expand_path('~/.temp')
11
+
12
+ attr_reader :file, :vals, :template_dir
13
+ attr_accessor :template_options
14
+
15
+ # Load a configuration file and set the configuration variables to the
16
+ # values in the file. If not given a filename, load the default
17
+ # configuration file. If the configuration file doesn't exist, use default
18
+ # configuration values. Also accepts template options to make it easier to
19
+ # pass to tempfiles.
20
+ def initialize(file = DEFAULT_CONF_FILE, template_options = {})
21
+ @file = file
22
+ @template_options = template_options
23
+
24
+ if File.file? @file
25
+ @vals = YAML::load_file(@file)
26
+ else
27
+ @vals = {
28
+ :template_dir => DEFAULT_TEMP_DIR
29
+ }
30
+ end
31
+
32
+ @template_dir = File.expand_path(@vals[:template_dir])
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,10 @@
1
+ module Temp
2
+
3
+ module Exceptions
4
+
5
+ class ProjectExistsError < StandardError; end
6
+ class TemplateNonExistentError < StandardError; end
7
+
8
+ end
9
+
10
+ end
@@ -0,0 +1,54 @@
1
+ require 'fileutils'
2
+ require 'erb'
3
+
4
+ module Temp
5
+
6
+ # A Project object represents a project and provides a method for copying the
7
+ # files from a template to create a project.
8
+ class Project
9
+
10
+ attr_reader :name, :path, :template
11
+
12
+ # Initialize all the required information for creating the project and make
13
+ # sure the project doesn't already exist
14
+ def initialize(path, template)
15
+ @path = File.expand_path(path)
16
+ @template = template
17
+ if File.file? @path
18
+ raise Temp::Exceptions::ProjectExistsError
19
+ elsif File.directory? @path
20
+ @name = @template.filename
21
+ @path = File.join(@path, @name)
22
+ raise Temp::Exceptions::ProjectExistsError if File.exist? @path
23
+ else
24
+ @name = File.basename(@path)
25
+ end
26
+ end
27
+
28
+ # Create the project
29
+ def create
30
+ if File.file? @template.path
31
+ FileUtils.cp(@template.path, @path)
32
+ else
33
+ FileUtils.mkdir_p(path)
34
+ (@template.files - @template.tempfile.ignore_files).each do |file|
35
+ t_file = File.join(@template.path, file)
36
+ p_file = File.join(@path, file)
37
+ if File.file? t_file
38
+ if @template.tempfile.erb_files.include? file
39
+ renderer = ERB.new(File.read(t_file))
40
+ File.open(p_file, 'w') { |f|
41
+ f.write renderer.result(@template.tempfile.get_binding) }
42
+ else
43
+ FileUtils.cp(t_file, p_file)
44
+ end
45
+ else
46
+ Dir.mkdir(p_file)
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -1,43 +1,62 @@
1
- require 'yaml'
2
1
  require 'optparse'
3
2
 
4
3
  module Temp
5
4
 
6
- class Runner
5
+ # The Runner module contains methods for Temp's command line interface.
6
+ module Runner
7
7
 
8
- attr_reader :conf_file, :conf, :args
8
+ # Run Temp as a command line program
9
+ def self.start!(args)
10
+ conf = Temp::Config.new(Temp::Config::DEFAULT_CONF_FILE)
11
+ options = {}
9
12
 
10
- def initialize(options = {})
11
- @conf_file = File.expand_path(options[:conf_file])
12
- @conf = { :template_dir => '~/.temp' }
13
- @args = options[:args] || {}
14
-
15
- if @conf_file && File.file?(@conf_file)
16
- @conf = YAML::load_file(@conf_file)
17
- else
18
- File.open(@conf_file, 'w') { |f| f.write(@conf.to_yaml) }
19
- end
20
- end
21
-
22
- def start!
23
13
  OptionParser.new do |o|
24
14
  o.banner = 'Usage: temp [options] [project] [template]'
25
15
 
26
16
  o.on('-v', '--version', 'Show version information') do
27
17
  puts "Temp #{Temp::VERSION}"
18
+ exit
28
19
  end
29
- end.parse!(@args)
30
20
 
31
- c = Temp::Copier.new(:template_dir => @conf[:template_dir])
21
+ o.on('-l', '--list [name]', 'List all templates') do |name|
22
+ if name
23
+ t = Temp::Template.new(name, conf)
24
+ puts "#{t.name} - #{t.desc}"
25
+ else
26
+ Temp::Template.all(conf).each do |t|
27
+ puts "#{t.name} - #{t.desc}"
28
+ end
29
+ end
30
+ exit
31
+ end
32
+
33
+ o.on('-t', '--template-options [opts]',
34
+ 'Pass comma-separated option flags to the template') do |opts|
35
+ opts.split(',').each { |o| options[o.to_sym] = true }
36
+ end
37
+ end.parse!(args)
38
+ conf.template_options = options
39
+
32
40
  if ARGV.size == 0
33
- raise 'no project directory or template name specified'
41
+ puts 'No template specified.'
34
42
  elsif ARGV.size == 1
35
- c.create_project(ARGV[0])
43
+ template = Temp::Template.new(ARGV[0], conf)
44
+ Temp::Project.new(Dir.pwd, template).create
36
45
  else
37
- c.create_project(ARGV[0], ARGV[1])
46
+ template = Temp::Template.new(ARGV[1], conf)
47
+ Temp::Project.new(ARGV[0], template).create
38
48
  end
39
49
  rescue => e
40
- puts e.message
50
+ case e.exception
51
+ when Temp::Exceptions::ProjectExistsError
52
+ puts 'Cannot create project because file exists there.'
53
+ when Temp::Exceptions::TemplateNonExistentError
54
+ puts 'The specified template does not exist.'
55
+ when OptionParser::InvalidOption
56
+ puts e.message
57
+ else
58
+ raise e
59
+ end
41
60
  end
42
61
 
43
62
  end
@@ -0,0 +1,51 @@
1
+ module Temp
2
+
3
+ # A Tempfile object acts as a sandbox for the Tempfile DSL.
4
+ class Tempfile
5
+
6
+ attr_reader :options, :info, :ignore_files, :erb_files
7
+
8
+ # Load a Tempfile in the specified directory. Options can be passed so that
9
+ # they can be accessed from the Tempfile.
10
+ def initialize(dir, options = {})
11
+ @dir = File.expand_path(dir)
12
+ @file = File.join(@dir, 'Tempfile')
13
+ @options = options
14
+ @info = {}
15
+ @ignore_files = []
16
+ @erb_files = []
17
+ instance_eval File.read(@file) if File.file? @file
18
+ end
19
+
20
+ # Public method to get the object's binding
21
+ def get_binding
22
+ binding
23
+ end
24
+
25
+ # Set the template name
26
+ def name(str)
27
+ info[:name] = str
28
+ end
29
+
30
+ # Set the template description
31
+ def desc(str)
32
+ info[:desc] = str
33
+ end
34
+
35
+ # Add files to the ignore list by expanding the given glob(s)
36
+ def ignore(*files)
37
+ @ignore_files |= files.map do |file|
38
+ Dir.glob(File.join(@dir, file)).map { |f| f.sub(@dir + '/', '') }
39
+ end.flatten
40
+ end
41
+
42
+ # Add files to the ERB list by expanding the given glob(s)
43
+ def use_erb(*files)
44
+ @erb_files |= files.map do |file|
45
+ Dir.glob(File.join(@dir, file)).map { |f| f.sub(@dir + '/', '') }
46
+ end.flatten
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,40 @@
1
+ require 'fileutils'
2
+
3
+ module Temp
4
+
5
+ # A Template object represents a template and provides information about it.
6
+ class Template
7
+
8
+ attr_reader :conf, :tempfile, :path, :filename, :name, :desc, :files
9
+
10
+ # Return an array of all templates in the template directory
11
+ def self.all(conf)
12
+ Dir.entries(conf.template_dir).map do |t|
13
+ unless t == '.' || t == '..'
14
+ Temp::Template.new(t, conf)
15
+ end
16
+ end.compact
17
+ end
18
+
19
+ # Load a template
20
+ def initialize(name, conf)
21
+ @conf = conf
22
+ @path = File.join(@conf.template_dir, name)
23
+ @filename = name
24
+ raise Temp::Exceptions::TemplateNonExistentError unless File.exist? @path
25
+
26
+ @tempfile = Temp::Tempfile.new(@path, @conf.template_options)
27
+ @name = @tempfile.info[:name] || name
28
+ @desc = @tempfile.info[:desc] || ''
29
+ @files = Template.find_files(@path) - @tempfile.ignore_files
30
+ end
31
+
32
+ # Find all of the files in a path
33
+ def self.find_files(path)
34
+ path = File.expand_path(path)
35
+ Dir.glob(File.join(path, '**/*')).map { |f| f.sub(path + '/', '') }
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -1,3 +1,3 @@
1
1
  module Temp
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,3 +1,36 @@
1
1
  require 'fileutils'
2
2
  require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib',
3
3
  'temp.rb'))
4
+
5
+ # Create a template and return a hash with all the template's information
6
+ def create_template(path, name, desc, files, ignore, erb)
7
+ path = File.expand_path(path)
8
+ Dir.mkdir(path)
9
+
10
+ files.each do |file|
11
+ file = File.join(path, file)
12
+ if file =~ /.*\..*/
13
+ File.open(file, 'w') { |f| f.write("Hello, World!\n") }
14
+ else
15
+ Dir.mkdir(file)
16
+ end
17
+ end
18
+
19
+ tempfile = "name '#{name}'\ndesc '#{desc}'\n\n"
20
+ ignore.each { |f| tempfile << "ignore '#{f}'\n" }
21
+ erb.each { |f| tempfile << "use_erb '#{f}'\n" }
22
+ tempfile << "\n@foo = 'Foo'\n"
23
+ File.open(File.join(path, 'Tempfile'), 'w') { |f| f.write(tempfile) }
24
+
25
+ {
26
+ :path => path,
27
+ :filename => File.basename(path),
28
+ :name => name,
29
+ :desc => desc,
30
+ :files => (['Tempfile'] + files).sort,
31
+ :ignore => ignore.map { |f| Dir.glob(File.join(path, f)) }.flatten.
32
+ map { |f| f.sub(path + '/', '') },
33
+ :erb => erb.map { |f| Dir.glob(File.join(path, f)) }.flatten.
34
+ map { |f| f.sub(path + '/', '') },
35
+ }
36
+ end
@@ -5,140 +5,236 @@ describe Temp do
5
5
  before :each do
6
6
  @dir = File.expand_path(File.dirname(__FILE__))
7
7
  @test_dir = File.join(@dir, 'test')
8
- @temp_dir = File.join(@dir, 'template')
9
- FileUtils.mkdir(@test_dir)
8
+ @template_dir = File.join(@test_dir, 'template')
9
+
10
+ FileUtils.mkdir_p(@template_dir)
11
+ File.open(File.join(@test_dir, 'foo.txt'), 'w') { |f|
12
+ f.write "Hello, World!\n" }
13
+
14
+ @conf_file = File.join(@dir, 'tempconf.yml')
15
+ @conf = Temp::Config.new(@conf_file)
16
+
17
+ @template1_info = create_template(File.join(@template_dir, 'template1'),
18
+ 'Template 1',
19
+ 'A test template.',
20
+ ['hello.txt',
21
+ 'ignore.txt',
22
+ 'foo',
23
+ 'foo/bar',
24
+ 'foo/bar/baz.txt'],
25
+ ['**/*ignore*'],
26
+ ['hello.txt'])
27
+ @template1_hello_erb = 'Hello, <%= @foo %>!'
28
+ @template1_hello = 'Hello, Foo!'
29
+ File.open(File.join(@template1_info[:path], 'hello.txt'), 'w') { |f|
30
+ f.write @template1_hello_erb }
31
+ @template2_info = { :path => File.join(@template_dir, 'template2.txt'),
32
+ :name => 'template2.txt' }
33
+ File.open(@template2_info[:path], 'w') { |f| f.write("Hello, World!\n") }
34
+ @template1 = Temp::Template.new(@template1_info[:filename], @conf)
35
+ @template2 = Temp::Template.new(@template2_info[:name], @conf)
10
36
  end
11
37
 
12
38
  after :each do
13
39
  FileUtils.rm_r(@test_dir)
14
40
  end
15
41
 
16
- describe Temp::Runner do
42
+ describe Temp::Config do
17
43
 
18
- describe 'configuration' do
44
+ it 'should load the config file' do
45
+ Temp::Config.new(@conf_file)
46
+ end
47
+
48
+ it 'should have the default template directory' do
49
+ conf = Temp::Config.new('hopefully-nonexistent-file.txt')
50
+ conf.template_dir.should == Temp::Config::DEFAULT_TEMP_DIR
51
+ end
52
+
53
+ it 'should have the template directory specified in the config file' do
54
+ conf = Temp::Config.new(@conf_file)
55
+ conf.template_dir.should == @template_dir
56
+ end
57
+
58
+ end
59
+
60
+ describe Temp::Tempfile do
61
+
62
+ describe 'template1 tempfile' do
19
63
 
20
64
  before :each do
21
- @conf_file1 = File.join(@dir, 'tempconf.yml')
22
- @conf_file2 = File.join(@test_dir, '.tempconf')
65
+ @tempfile = Temp::Tempfile.new(@template1_info[:path])
66
+ end
67
+
68
+ it 'should have the correct name' do
69
+ @tempfile.info[:name].should == @template1_info[:name]
70
+ end
71
+
72
+ it 'should have the correct description' do
73
+ @tempfile.info[:desc].should == @template1_info[:desc]
23
74
  end
24
75
 
25
- it 'should load a configuration file' do
26
- runner = Temp::Runner.new(:conf_file => @conf_file1)
27
- runner.conf[:test].should == 'test'
76
+ it 'should ignore certain files' do
77
+ @tempfile.ignore_files.sort.should == @template1_info[:ignore]
28
78
  end
29
79
 
30
- it 'should create a configuration file if it is nonexistent' do
31
- runner = Temp::Runner.new(:conf_file => @conf_file2)
32
- File.file?(@conf_file2)
80
+ it 'should use ERB on some files' do
81
+ @tempfile.erb_files.should == @template1_info[:erb]
33
82
  end
34
83
 
35
84
  end
36
85
 
37
86
  end
38
87
 
39
- describe Temp::Copier do
88
+ describe Temp::Template do
40
89
 
41
- before :each do
42
- @copier = Temp::Copier.new(:template_dir => @temp_dir)
43
- @template1_name = 'template1'
44
- @template1 = File.join(@temp_dir, @template1_name)
45
- @template2_name = 'template.txt'
46
- @template2 = File.join(@temp_dir, @template2_name)
90
+ it 'should return all templates' do
91
+ Temp::Template.all(@conf).map { |t| t.name }.should ==
92
+ [@template1_info[:name], @template2_info[:name]]
47
93
  end
48
94
 
49
- describe 'finding files' do
95
+ describe 'load template1' do
50
96
 
51
97
  before :each do
52
- @files = Dir.glob(@template1 + '/**/*').map { |f|
53
- f.sub(@template1 + '/', '') }.sort
54
- @ignore = ['.tempignore'] | Dir.glob(File.read(File.join(@template1,
55
- '.tempignore')).split(?\n)).map {
56
- |f| File.expand_path(f).sub(@template1 + '/', '') }.sort
98
+ @template = Temp::Template.new(@template1_info[:filename], @conf)
57
99
  end
58
100
 
59
- it 'should find all files in a template' do
60
- @copier.find_files(@template1_name).sort.should == @files.sort
101
+ it 'should have correct name' do
102
+ @template.name.should == @template1_info[:name]
61
103
  end
62
104
 
63
- it 'should find the correct files to ignore from reading .tempignore' do
64
- @copier.read_tempignore(@template1_name).sort.should == @ignore
105
+ it 'should have correct description' do
106
+ @template.desc.should == @template1_info[:desc]
65
107
  end
66
108
 
67
- it 'should find all files in a template except for files to ignore' do
68
- @copier.find_files(@template1_name, @ignore).sort.should == @files -
69
- @ignore
109
+ it 'should have the correct files' do
110
+ @template.files.sort.should == @template1_info[:files] -
111
+ @template1_info[:ignore]
70
112
  end
71
113
 
72
114
  end
73
115
 
74
- describe 'project creation' do
116
+ describe 'failure' do
75
117
 
76
- before :each do
77
- @project1_dir = File.join(@test_dir, 'project')
78
- @project2_dir = File.join(@test_dir, @template1_name)
79
- @project1_file = File.join(@test_dir, 'project.txt')
80
- @project2_file = File.join(@test_dir, @template2_name)
118
+ it 'should raise an exception when given a non-existing template name' do
119
+ lambda do
120
+ Temp::Template.new('foo', @conf)
121
+ end.should raise_error Temp::Exceptions::TemplateNonExistentError
81
122
  end
82
123
 
124
+ end
125
+
126
+ end
127
+
128
+ describe Temp::Project do
129
+
130
+ before :each do
131
+ @conf = Temp::Config.new(@conf_file)
132
+ end
133
+
134
+ describe 'initialization' do
135
+
83
136
  describe 'success' do
84
137
 
85
- it 'should create a project directory' do
86
- @copier.create_project(@project1_dir, @template1_name)
87
- File.exist?(@project1_dir).should be_true
138
+ describe 'template1 with non-existent path' do
139
+
140
+ before :each do
141
+ @name = 'foo'
142
+ @path = File.join(@test_dir, @name)
143
+ @proj = Temp::Project.new(@path, @template1_info)
144
+ end
145
+
146
+ it 'should have the correct name' do
147
+ @proj.name.should == @name
148
+ end
149
+
150
+ it 'should have the correct path' do
151
+ @proj.path.should == @path
152
+ end
153
+
154
+ end
155
+
156
+ describe 'template1 with existing path' do
157
+
158
+ before :each do
159
+ @name = @template1_info[:filename]
160
+ @path = File.join(@test_dir, @name)
161
+ @proj = Temp::Project.new(@test_dir, @template1)
162
+ end
163
+
164
+ it 'should have the correct name' do
165
+ @proj.name.should == @name
166
+ end
167
+
168
+ it 'should have the correct path' do
169
+ @proj.path.should == @path
170
+ end
171
+
88
172
  end
89
173
 
90
- it 'should create a project directory, naming it after the template' do
91
- pwd = Dir.pwd
92
- Dir.chdir(@test_dir)
93
- @copier.create_project(@template1_name)
94
- File.exist?(@project2_dir).should be_true
95
- Dir.chdir(pwd)
174
+ end
175
+
176
+ describe 'failure' do
177
+
178
+ describe 'template1 with existing file as path' do
179
+
180
+ it 'should raise an exception' do
181
+ lambda do
182
+ Temp::Project.new(File.join(@test_dir, 'foo.txt'),
183
+ @template1_info)
184
+ end.should raise_error Temp::Exceptions::ProjectExistsError
185
+ end
186
+
96
187
  end
97
188
 
98
- it 'should copy all files from a template' do
99
- files = @copier.find_files(@template1_name,
100
- @copier.read_tempignore(@template1_name))
101
- @copier.create_project(@project1_dir, @template1_name)
102
- Dir.glob(File.join(@project1_dir, '**/*')).map { |f|
103
- f.sub(@project1_dir + '/', '') }.sort.should == files.sort
189
+ describe 'template1 with existing path and template in path' do
190
+
191
+ it 'should raise an exception' do
192
+ Dir.mkdir(File.join(@test_dir, @template1_info[:filename]))
193
+ lambda do
194
+ Temp::Project.new(@test_dir, @template1)
195
+ end.should raise_error Temp::Exceptions::ProjectExistsError
196
+ end
197
+
104
198
  end
105
199
 
106
- it 'should copy all files from a template to a project named after it' do
107
- pwd = Dir.pwd
108
- Dir.chdir(@test_dir)
109
- files = @copier.find_files(@template1_name,
110
- @copier.read_tempignore(@template1_name))
111
- @copier.create_project(@template1_name)
112
- Dir.glob(File.join(@project2_dir, '**/*')).map { |f|
113
- f.sub(@project2_dir + '/', '') }.sort.should == files.sort
114
- Dir.chdir(pwd)
200
+ end
201
+
202
+ end
203
+
204
+ describe 'creation' do
205
+
206
+ describe 'template1' do
207
+
208
+ before :each do
209
+ @proj_dir = File.join(@test_dir, 'project1')
210
+ @proj = Temp::Project.new(@proj_dir, @template1)
115
211
  end
116
212
 
117
- it 'should copy a single file template' do
118
- @copier.create_project(@project1_file, @template2_name)
119
- File.file?(@project1_file).should be_true
213
+ it 'should have copied the correct files' do
214
+ @proj.create
215
+ Dir.glob(File.join(@proj_dir, '**/*')).map { |f|
216
+ f.sub(@proj_dir + '/', '') }.sort.should ==
217
+ @template1_info[:files] - @template1_info[:ignore]
120
218
  end
121
219
 
122
- it 'should copy a single file template named after the template' do
123
- @copier.create_project(@project2_file, @template2_name)
124
- File.file?(@project2_file).should be_true
220
+ it 'should use ERB on a file' do
221
+ @proj.create
222
+ File.read(File.join(@proj_dir, 'hello.txt')).should ==
223
+ @template1_hello
125
224
  end
126
225
 
127
226
  end
128
227
 
129
- describe 'failure' do
228
+ describe 'template2' do
130
229
 
131
- it 'should raise an exception if the project to create already exists' do
132
- FileUtils.mkdir(@project1_dir)
133
- lambda do
134
- @copier.create_project(@project1_dir, @template1_name)
135
- end.should raise_error
230
+ before :each do
231
+ @proj_file = File.join(@test_dir, 'project.txt')
232
+ @proj = Temp::Project.new(@proj_file, @template2)
136
233
  end
137
234
 
138
- it 'should raise an exception if the template does not exist' do
139
- lambda do
140
- @copier.create_project(@project1_dir, 'fake_template')
141
- end.should raise_error
235
+ it 'should have copied the template file' do
236
+ @proj.create
237
+ File.file?(@proj_file).should be_true
142
238
  end
143
239
 
144
240
  end
@@ -1,3 +1,2 @@
1
1
  ---
2
- :template_dir: ~/.temp
3
- :test: test
2
+ :template_dir: spec/test/template
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Austin Gatchell
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-07-25 00:00:00 -05:00
17
+ date: 2011-10-09 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -48,18 +48,16 @@ files:
48
48
  - Readme.mdown
49
49
  - bin/temp
50
50
  - lib/temp.rb
51
- - lib/temp/copier.rb
51
+ - lib/temp/config.rb
52
+ - lib/temp/exceptions.rb
53
+ - lib/temp/project.rb
52
54
  - lib/temp/runner.rb
55
+ - lib/temp/tempfile.rb
56
+ - lib/temp/template.rb
53
57
  - lib/temp/version.rb
54
58
  - spec/spec_helper.rb
55
59
  - spec/temp_spec.rb
56
60
  - spec/tempconf.yml
57
- - spec/template/template.txt
58
- - spec/template/template1/.tempignore
59
- - spec/template/template1/foo/bar/baz.txt
60
- - spec/template/template1/foo/bar/ignore-me.txt
61
- - spec/template/template1/hello.txt
62
- - spec/template/template1/ignore-me.txt
63
61
  - temp.gemspec
64
62
  has_rdoc: true
65
63
  homepage: https://github.com/ausgat/temp
@@ -97,9 +95,3 @@ test_files:
97
95
  - spec/spec_helper.rb
98
96
  - spec/temp_spec.rb
99
97
  - spec/tempconf.yml
100
- - spec/template/template.txt
101
- - spec/template/template1/.tempignore
102
- - spec/template/template1/foo/bar/baz.txt
103
- - spec/template/template1/foo/bar/ignore-me.txt
104
- - spec/template/template1/hello.txt
105
- - spec/template/template1/ignore-me.txt
@@ -1,76 +0,0 @@
1
- require 'fileutils'
2
-
3
- module Temp
4
-
5
- # A Copier object provides various methods for creating a project from a
6
- # template. It can also be given a path to the directory where templates can
7
- # be found.
8
- class Copier
9
-
10
- attr_reader :options
11
-
12
- def initialize(options = {})
13
- @options = options
14
- @template_dir = File.expand_path(options[:template_dir] || '~/.temp')
15
- end
16
-
17
- # Creates a new project from a template. If a path is supplied, the
18
- # project's name will be the last item in the path. Otherwise, it will have
19
- # the same name as the template and be created in the current working
20
- # directory.
21
- def create_project(project = nil, template)
22
- project = File.expand_path(project || template)
23
- template_path = File.expand_path(File.join(@template_dir, template))
24
-
25
- raise 'project already exists' if File.exist? project
26
- raise 'template does not exist' unless File.exist? template_path
27
-
28
- ignore = read_tempignore(template)
29
- files = find_files(template, ignore)
30
-
31
- if File.file? template_path
32
- FileUtils.cp(template_path, project)
33
- else
34
- FileUtils.mkdir(project)
35
- files.each do |file|
36
- p = File.join(project, file)
37
- t = File.join(template_path, file)
38
-
39
- if File.directory? t
40
- FileUtils.mkdir(p)
41
- else
42
- FileUtils.cp(t, p)
43
- end
44
- end
45
- end
46
- end
47
-
48
- # Returns an array of all files in a template, optionally ignoring all files
49
- # in ignore.
50
- def find_files(template, ignore = [])
51
- template = File.expand_path(File.join(@template_dir, template))
52
- Dir.glob(File.join(template, '**/*')).map do |file|
53
- file.sub(template + '/', '')
54
- end - ignore
55
- end
56
-
57
- # Returns an array of files in a template to ignore.
58
- def read_tempignore(template)
59
- template = File.expand_path(File.join(@template_dir, template))
60
- tempignore = File.join(template, '.tempignore')
61
- files = ['.tempignore']
62
-
63
- if File.exist? tempignore
64
- files |= File.read(tempignore).split(?\n).map do |line|
65
- Dir.glob(File.join(template, line)).map do |file|
66
- file.sub(template + '/', '')
67
- end
68
- end.flatten
69
- end
70
-
71
- files
72
- end
73
-
74
- end
75
-
76
- end
@@ -1 +0,0 @@
1
- I am a template.
@@ -1 +0,0 @@
1
- **/ignore-me.txt
@@ -1 +0,0 @@
1
- foobar
@@ -1 +0,0 @@
1
- I should be ignored.
@@ -1 +0,0 @@
1
- Hello, World!
@@ -1 +0,0 @@
1
- I should be ignored.