Skip to content

Commit

Permalink
Basic lifecycle support for installing preexisting files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarnette committed Jun 1, 2008
1 parent 899a5a2 commit ac5ec70
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
6 changes: 6 additions & 0 deletions lib/basis/errors.rb
Expand Up @@ -13,6 +13,12 @@ def initialize(directory)
end
end

class FileAlreadyExists < Reportable
def initialize(file)
super("#{file} already exists")
end
end

class UnknownScheme < Reportable
def initialize(url)
super("Unknown URL scheme: #{url}")
Expand Down
4 changes: 1 addition & 3 deletions lib/basis/installer.rb
Expand Up @@ -8,14 +8,13 @@

module Basis
class Installer
attr_reader :source, :target
attr_reader :lifecycle, :source, :target

def initialize(source, target)
@source = Pathname.new(source).freeze
@target = Pathname.new(target).freeze

raise Basis::DirectoryNotFound.new(@source) unless @source.directory?
raise Basis::DirectoryAlreadyExists.new(@target) if @target.exist?

@lifecycle = create_or_load_lifecycle
end
Expand All @@ -36,7 +35,6 @@ def install(context={})

if @lifecycle.install?(targetpath)
targetpath.dirname.mkpath

@lifecycle.installing(targetpath)

if erb?(sourcepath)
Expand Down
10 changes: 7 additions & 3 deletions lib/basis/installer/lifecycle.rb
Expand Up @@ -7,11 +7,15 @@ def initialize(installer)
@installer = installer
end

def install?(path); true end
def install?(file)
true # FIXME: user prompt on overwrite, etc
end

def installing(path); end
def installing(file)
end

def installed(path); end
def installed(file)
end
end
end
end
19 changes: 12 additions & 7 deletions spec/basis/installer_spec.rb
Expand Up @@ -96,13 +96,18 @@
(@target + "copy.txt").must be_exist
end

it "prompts before overwriting files that already exist"
# def test_initialize_complains_about_targets_that_already_exist
# assert_raise(Basis::DirectoryAlreadyExists) do
# Basis::Installer.new(@static, FIXTURES_PATH)
# end
# end

it "pings the lifecycle on files that already exist" do
@installer.install

def (@installer.lifecycle).install?(file)
raise Basis::FileAlreadyExists.new(file) if file.exist?
true
end

(@target + "monkeys.txt").must be_exist
lambda { @installer.install }.must raise_error(Basis::FileAlreadyExists)
end

def valid_context
{ :greeting => "hai!", :foo => Struct.new(:bar).new("baz") }
end
Expand Down

0 comments on commit ac5ec70

Please sign in to comment.