Skip to content

Commit 02f4502

Browse files
committed
Allow initialization of an existing directory
1 parent 2010085 commit 02f4502

File tree

4 files changed

+65
-15
lines changed

4 files changed

+65
-15
lines changed

.rspec

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--color
2+
--format documentation
3+
--tty
4+
--order random
5+
--require 'spec_helper'

README.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ This gem provides a set of [`Rake`](https://github.com/ruby/rake) tasks to creat
3030

3131
# TODO
3232

33+
1. Allow initializing the current directory
3334
1. `revealing doctor` to analyze tools
3435
1. Target folders mirror source, so that we don't risk duplicates
3536
1. Read desired versions of dependencies from a YAML file (with sensible defaults coming from this project)

lib/revealing/cli.rb

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
11
require 'thor'
2+
require 'pathname'
23

34
module Revealing
45
class CLI < Thor
56
desc 'init DIRECTORY', "initialize a new revealing project in DIRECTORY."
67
def init(directory)
78
project_directory = Pathname(directory)
8-
project_directory.mkdir
9+
project_directory.mkdir unless project_directory.exist?
910

10-
templates_directory = Pathname(__dir__) / '../../templates'
11-
FileUtils.cp_r(templates_directory / 'init/.', project_directory)
11+
templates_directory = Pathname(__dir__) / '../../templates/init'
12+
13+
# FileUtils.cp_r overwrites the target if it exists, but we want to preserve it
14+
# and print information about what happened
15+
source_target_pairs(templates_directory, project_directory).each do |src, target|
16+
if target.exist?
17+
warn "#{target} exists; skipping"
18+
else
19+
target.dirname.mkpath
20+
FileUtils.cp(src, target)
21+
puts "#{target} created"
22+
end
23+
end
24+
end
25+
26+
private
27+
28+
def source_target_pairs(src_dir, target_dir)
29+
src_dir
30+
.glob('**/*')
31+
.select(&:file?)
32+
.map{|e| [e, target_dir / e.relative_path_from(src_dir)]}
1233
end
1334
end
1435
end

spec/system/init_spec.rb

+35-12
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,48 @@
44

55
describe 'init', type: 'aruba' do
66
let(:revealing_init) { "#{aruba.root_directory}/exe/revealing init" }
7+
let(:cwd) { Pathname(aruba.config.working_directory) }
8+
let(:project_directory) { cwd / 'shiny-new-presentation' }
9+
let(:project_files) { %w[src/index.markdown Rakefile Gemfile README.markdown].map { |e| project_directory / e } }
710

8-
context 'with a new project directory' do
9-
let(:project_directory) { 'shiny-new-presentation' }
10-
let(:cwd) { Pathname(aruba.config.working_directory) }
11-
12-
before do
13-
run_command "#{revealing_init} #{project_directory}"
11+
shared_examples 'a new project directory' do
12+
it 'creates the required files in the new project directory' do
13+
run_command "#{revealing_init} #{project_directory.basename}"
1414
expect(last_command_started).to be_successfully_executed
15+
expect(project_files).to all(exist)
1516
end
17+
end
1618

19+
context 'with a new project directory' do
1720
it 'creates the new project directory' do
18-
expect(cwd / project_directory).to be_directory
21+
run_command "#{revealing_init} #{project_directory.basename}"
22+
expect(last_command_started).to be_successfully_executed
23+
expect(project_directory).to be_directory
1924
end
2025

21-
it 'creates the required files in the new project directory' do
22-
expect(cwd / project_directory / 'src/index.markdown').to exist
23-
expect(cwd / project_directory / 'Rakefile').to exist
24-
expect(cwd / project_directory / 'Gemfile').to exist
25-
expect(cwd / project_directory / 'README.markdown').to exist
26+
it_behaves_like('a new project directory')
27+
end
28+
29+
context 'with an existing (empty) project directory' do
30+
before do
31+
project_directory.mkdir
32+
expect(project_directory).to be_directory
33+
end
34+
35+
it_behaves_like('a new project directory')
36+
37+
context 'the README already exists' do
38+
let(:readme) { (project_directory / 'README.markdown') }
39+
40+
before do
41+
readme.write('fooo')
42+
end
43+
44+
it 'does not override an existing file' do
45+
run_command "#{revealing_init} #{project_directory.basename}"
46+
expect(last_command_started).to be_successfully_executed
47+
expect(readme.read).to eq('fooo')
48+
end
2649
end
2750
end
2851
end

0 commit comments

Comments
 (0)