temp 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.mdown CHANGED
@@ -4,7 +4,7 @@ Temp is a simple command line utility for creating projects from templates.
4
4
 
5
5
  ## Installation
6
6
 
7
- You can install Temp with `gem install temp`
7
+ You can install Temp with `gem install temp`.
8
8
 
9
9
  ## Usage
10
10
 
data/bin/temp CHANGED
@@ -4,4 +4,5 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
 
5
5
  require 'temp'
6
6
 
7
- Temp::Runner.start
7
+ runner = Temp::Runner.new(:args => ARGV, :conf_file => '~/.tempconfig')
8
+ runner.start!
data/lib/temp/copier.rb CHANGED
@@ -2,7 +2,7 @@ require 'fileutils'
2
2
 
3
3
  module Temp
4
4
 
5
- # A Copier object provides various functions for creating a project from a
5
+ # A Copier object provides various methods for creating a project from a
6
6
  # template. It can also be given a path to the directory where templates can
7
7
  # be found.
8
8
  class Copier
@@ -14,9 +14,12 @@ module Temp
14
14
  @template_dir = File.expand_path(options[:template_dir] || '~/.temp')
15
15
  end
16
16
 
17
- # Creates a new project with the given path from the given template name.
18
- def create_project(project, template)
19
- project = File.expand_path(project)
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)
20
23
  template_path = File.expand_path(File.join(@template_dir, template))
21
24
 
22
25
  raise 'project already exists' if File.exist? project
@@ -25,15 +28,19 @@ module Temp
25
28
  ignore = read_tempignore(template)
26
29
  files = find_files(template, ignore)
27
30
 
28
- FileUtils.mkdir(project)
29
- files.each do |file|
30
- p = File.join(project, file)
31
- t = File.join(template_path, file)
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)
32
38
 
33
- if File.directory? t
34
- FileUtils.mkdir(p)
35
- else
36
- FileUtils.cp(t, p)
39
+ if File.directory? t
40
+ FileUtils.mkdir(p)
41
+ else
42
+ FileUtils.cp(t, p)
43
+ end
37
44
  end
38
45
  end
39
46
  end
data/lib/temp/runner.rb CHANGED
@@ -1,32 +1,43 @@
1
+ require 'yaml'
1
2
  require 'optparse'
2
3
 
3
4
  module Temp
4
5
 
5
6
  class Runner
6
7
 
7
- def self.start
8
- options = {}
9
-
10
- begin
11
- OptionParser.new do |o|
12
- o.banner = 'Usage: temp [options] [project] [template]'
13
-
14
- o.on('-v', '--version', 'Show version information') do
15
- puts "Temp #{Temp::VERSION}"
16
- end
17
- end.parse!
18
-
19
- c = Temp::Copier.new
20
- if ARGV.size == 0
21
- raise 'no project directory or template name specified'
22
- elsif ARGV.size == 1
23
- raise 'no template name specified'
24
- else
25
- c.create_project(ARGV[0], ARGV[1])
8
+ attr_reader :conf_file, :conf, :args
9
+
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
+ OptionParser.new do |o|
24
+ o.banner = 'Usage: temp [options] [project] [template]'
25
+
26
+ o.on('-v', '--version', 'Show version information') do
27
+ puts "Temp #{Temp::VERSION}"
26
28
  end
27
- rescue => e
28
- puts e.message
29
+ end.parse!(@args)
30
+
31
+ c = Temp::Copier.new(:template_dir => @conf[:template_dir])
32
+ if ARGV.size == 0
33
+ raise 'no project directory or template name specified'
34
+ elsif ARGV.size == 1
35
+ c.create_project(ARGV[0])
36
+ else
37
+ c.create_project(ARGV[0], ARGV[1])
29
38
  end
39
+ rescue => e
40
+ puts e.message
30
41
  end
31
42
 
32
43
  end
data/lib/temp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Temp
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/spec/temp_spec.rb CHANGED
@@ -1,82 +1,146 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
- describe Temp::Copier do
3
+ describe Temp do
4
4
 
5
5
  before :each do
6
6
  @dir = File.expand_path(File.dirname(__FILE__))
7
7
  @test_dir = File.join(@dir, 'test')
8
8
  @temp_dir = File.join(@dir, 'template')
9
- @template1_name = 'template1'
10
- @template1 = File.join(@temp_dir, @template1_name)
11
9
  FileUtils.mkdir(@test_dir)
12
- @copier = Temp::Copier.new(:template_dir => @temp_dir)
13
10
  end
14
11
 
15
12
  after :each do
16
13
  FileUtils.rm_r(@test_dir)
17
14
  end
18
15
 
19
- describe 'finding files' do
16
+ describe Temp::Runner do
20
17
 
21
- before :each do
22
- @files = Dir.glob(@template1 + '/**/*').map { |f|
23
- f.sub(@template1 + '/', '') }.sort
24
- @ignore = ['.tempignore'] | Dir.glob(File.read(File.join(@template1,
25
- '.tempignore')).split(?\n)).map {
26
- |f| File.expand_path(f).sub(@template1 + '/', '') }.sort
27
- end
18
+ describe 'configuration' do
28
19
 
29
- it 'should find all files in a template' do
30
- @copier.find_files(@template1_name).sort.should == @files.sort
31
- end
20
+ before :each do
21
+ @conf_file1 = File.join(@dir, 'tempconf.yml')
22
+ @conf_file2 = File.join(@test_dir, '.tempconf')
23
+ end
32
24
 
33
- it 'should find the correct files to ignore from reading .tempignore' do
34
- @copier.read_tempignore(@template1_name).sort.should == @ignore
35
- end
25
+ it 'should load a configuration file' do
26
+ runner = Temp::Runner.new(:conf_file => @conf_file1)
27
+ runner.conf[:test].should == 'test'
28
+ end
29
+
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)
33
+ end
36
34
 
37
- it 'should find all files in a template except for files to ignore' do
38
- @copier.find_files(@template1_name, @ignore).sort.should == @files -
39
- @ignore
40
35
  end
41
36
 
42
37
  end
43
38
 
44
- describe 'project creation' do
39
+ describe Temp::Copier do
45
40
 
46
41
  before :each do
47
- @project_dir = File.join(@test_dir, 'project')
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)
48
47
  end
49
48
 
50
- describe 'success' do
49
+ describe 'finding files' do
50
+
51
+ 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
57
+ end
58
+
59
+ it 'should find all files in a template' do
60
+ @copier.find_files(@template1_name).sort.should == @files.sort
61
+ end
51
62
 
52
- it 'should create a project directory' do
53
- @copier.create_project(@project_dir, @template1_name)
54
- File.exist?(@project_dir).should be_true
63
+ it 'should find the correct files to ignore from reading .tempignore' do
64
+ @copier.read_tempignore(@template1_name).sort.should == @ignore
55
65
  end
56
66
 
57
- it 'should copy all files from a template' do
58
- files = @copier.find_files(@template1_name,
59
- @copier.read_tempignore(@template1_name))
60
- @copier.create_project(@project_dir, @template1_name)
61
- Dir.glob(File.join(@project_dir, '**/*')).map { |f|
62
- f.sub(@project_dir + '/', '') }.sort.should == files.sort
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
63
70
  end
64
71
 
65
72
  end
66
73
 
67
- describe 'failure' do
74
+ describe 'project creation' do
68
75
 
69
- it 'should raise an exception if the project to create already exists' do
70
- FileUtils.mkdir(@project_dir)
71
- lambda do
72
- @copier.create_project(@project_dir, @template1_name)
73
- end.should raise_error
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)
74
81
  end
75
82
 
76
- it 'should raise an exception if the template does not exist' do
77
- lambda do
78
- @copier.create_project(@project_dir, 'fake_template')
79
- end.should raise_error
83
+ describe 'success' do
84
+
85
+ it 'should create a project directory' do
86
+ @copier.create_project(@project1_dir, @template1_name)
87
+ File.exist?(@project1_dir).should be_true
88
+ end
89
+
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)
96
+ end
97
+
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
104
+ end
105
+
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)
115
+ end
116
+
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
120
+ end
121
+
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
125
+ end
126
+
127
+ end
128
+
129
+ describe 'failure' do
130
+
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
136
+ end
137
+
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
142
+ end
143
+
80
144
  end
81
145
 
82
146
  end
data/spec/tempconf.yml ADDED
@@ -0,0 +1,3 @@
1
+ ---
2
+ :template_dir: ~/.temp
3
+ :test: test
@@ -0,0 +1 @@
1
+ I am a template.
data/temp.gemspec CHANGED
@@ -9,9 +9,9 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Austin Gatchell"]
10
10
  s.email = ["austin@ausgat.com"]
11
11
  s.homepage = "https://github.com/ausgat/temp"
12
- s.summary = %q{A small project creation utility.}
13
- s.description = %q{Temp is a simple command line utility for creating \
14
- projects from templates.}
12
+ s.summary = 'A small project creation utility.'
13
+ s.description = 'Temp is a simple command line utility for creating ' \
14
+ 'projects from templates.'
15
15
  s.license = 'MIT'
16
16
 
17
17
  s.files = `git ls-files`.split("\n")
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
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-23 00:00:00 -05:00
17
+ date: 2011-07-25 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -30,9 +30,7 @@ dependencies:
30
30
  version: "0"
31
31
  type: :development
32
32
  version_requirements: *id001
33
- description: |-
34
- Temp is a simple command line utility for creating \
35
- projects from templates.
33
+ description: Temp is a simple command line utility for creating projects from templates.
36
34
  email:
37
35
  - austin@ausgat.com
38
36
  executables:
@@ -55,6 +53,8 @@ files:
55
53
  - lib/temp/version.rb
56
54
  - spec/spec_helper.rb
57
55
  - spec/temp_spec.rb
56
+ - spec/tempconf.yml
57
+ - spec/template/template.txt
58
58
  - spec/template/template1/.tempignore
59
59
  - spec/template/template1/foo/bar/baz.txt
60
60
  - spec/template/template1/foo/bar/ignore-me.txt
@@ -96,6 +96,8 @@ summary: A small project creation utility.
96
96
  test_files:
97
97
  - spec/spec_helper.rb
98
98
  - spec/temp_spec.rb
99
+ - spec/tempconf.yml
100
+ - spec/template/template.txt
99
101
  - spec/template/template1/.tempignore
100
102
  - spec/template/template1/foo/bar/baz.txt
101
103
  - spec/template/template1/foo/bar/ignore-me.txt