Permalink
Browse files

comp impl erbt system

  • Loading branch information...
akiradeveloper committed Apr 15, 2012
1 parent 800b810 commit 6d57f6b7bad19f4b3797ba796a40787b882a83a5
Showing with 104 additions and 30 deletions.
  1. +8 −8 configure.rb
  2. +2 −3 install.sh
  3. +11 −4 install/common.rb
  4. +32 −13 install/erbt/erbt.rb
  5. +1 −0 install/erbt/test/.gitignore
  6. +46 −0 install/erbt/test/Rakefile
  7. +2 −0 install/erbt/test/src
  8. +1 −1 install/system.rb
  9. +1 −1 install/user.rb
View
@@ -1,12 +1,12 @@
USER_COPIES = {
- ".zshrc.local" => "~/",
- ".wgetrc" => "~/",
- ".curlrc" => "~/",
- ".xsession" => "~/",
- ".gitconfig" => "~/",
- ".screenrc" => "~/",
- ".tmux.conf" => "~/",
- ".ssh/config" => "~/.ssh"
+ ".zshrc.local" => ENV["HOME"],
+ ".wgetrc" => ENV["HOME"],
+ ".curlrc" => ENV["HOME"],
+ ".xsession" => ENV["HOME"],
+ ".gitconfig" => ENV["HOME"],
+ ".screenrc" => ENV["HOME"],
+ ".tmux.conf" => ENV["HOME"],
+ ".ssh/config" => "#{ENV["HOME"]}/.ssh"
}
SYSTEM_COPIES = {
View
@@ -1,5 +1,4 @@
#!/bin/sh
-cd install
-ruby user.rb
-sudo ruby system.rb
+ruby install/user.rb
+sudo ruby install/system.rb
View
@@ -1,6 +1,13 @@
-require "../configure"
-require File.dirname(__FILE__) + "/erbt"
+require File.dirname(__FILE__) + "/../configure"
+require File.dirname(__FILE__) + "/erbt/erbt"
-def process(cpmap)
- p cpmap
+def process(map)
+ cp_logger = ERB::Template::Logger.new("Cp")
+ update_logger = ERB::Template::Logger.new("Update")
+ map.each do |src, dest|
+ ERB::Template::Cp.process(src, dest, cp_logger)
+ ERB::Template::Update.process(dest + "/" + File.basename(src), update_logger)
+ end
+ cp_logger.show
+ update_logger.show
end
View
@@ -3,10 +3,11 @@
module ERB::Template
Dirname = "/.erbt"
def self.directory(dir)
- if Dir.exist?(dir)
- puts "the directory is already exists."
+ if File.exist?(dir)
+ puts "the directory #{dir} is already exists."
return
end
+ puts "create directory #{dir}"
system "mkdir -p #{dir}"
end
def self.tdir(f)
@@ -20,13 +21,20 @@ class Logger
SUCCESS = 0
FAILURE = 1
TAGS = [SUCCESS, FAILURE]
- def initialize
+ def initialize(title)
+ @title = title
@m = {}
end
def log(f, tag, message)
@m[f] = {:tag => tag, :message => message}
end
+
+ def __show(map)
+ map.each do |f, e|
+ print "#{f} (#{e[:message]})\n"
+ end
+ end
def show
success = {}
@@ -35,53 +43,64 @@ def show
@m.each do |f, e|
xs[e[:tag]][f] = e
end
+ puts ""
+ puts "******* Logging of #{@title} *******"
puts "[success]"
- # impl
+ __show(success)
puts "[failure]"
- # impl
+ __show(failure)
end
end
class Cp
- def process(src, dest, logger)
+ def self.process(src, dest, logger, force=false)
unless File.file?(src)
- msg = "the source #{dest} is not a file."
+ msg = "the source #{src} is not a file."
logger.log(src, ERB::Template::Logger::FAILURE, msg)
return
end
unless File.directory?(dest)
msg = "the destination #{dest} is not a dir."
+ logger.log(src, ERB::Template::Logger::FAILURE, msg)
return
end
tdir = dest + ERB::Template::Dirname
- directory(tdir)
+ ERB::Template.directory(tdir)
tf = tdir + "/" + File.basename(src)
- if File.exist?(tf)
- msg = "the template file (for #{src}) already exists."
+ if !force and File.exist?(tf)
+ msg = "the template file for #{src} already exists."
logger.log(src, ERB::Template::Logger::FAILURE, msg)
return
end
+ # TODO if the content is the same, do not copy.
system "cp #{src} #{tf}"
logger.log(src, ERB::Template::Logger::SUCCESS, "")
return
end
end
class Update
- def process(f, logger)
+ def self.process(f, logger, force=false)
tf = ERB::Template.tf(f)
unless File.exist?(tf)
msg = "template file does not exist."
logger.log(f, ERB::Template::Logger::FAILURE, msg)
return
end
content = `erb #{tf}`
- if content == File.read(f)
+ if !force and File.exist?(f)
+ msg = "the file already exists. never update."
+ logger.log(f, ERB::Template::Logger::FAILURE, msg)
+ return
+ end
+ if File.exist?(f) and content == File.read(f)
msg = "the file does not need to be updated."
logger.log(f, ERB::Template::Logger::FAILURE, msg)
return
end
- system "echo #{content} > #{f}"
+ File.open(f, 'w') do |fd|
+ fd.write(content)
+ end
logger.log(f, ERB::Template::Logger::SUCCESS, "")
return
end
@@ -0,0 +1 @@
+dest/*
View
@@ -0,0 +1,46 @@
+require File.dirname(__FILE__) + "/" + "../erbt"
+
+include ERB::Template
+
+FAKE_LOGGER = Logger.new("Fake")
+
+task :clean do
+ `rm -rf dest`
+ `mkdir dest`
+end
+
+task :t1 => [:clean] do
+ logger = Logger.new("Cp")
+ Cp.process('src', 'src', logger)
+ Cp.process('dest', 'src', logger)
+ logger.show
+end
+
+task :t2 => [:clean] do
+ logger = Logger.new("Cp")
+ Cp.process('src', 'dest', logger)
+ logger.show
+ Cp.process('src', 'dest', logger)
+ logger.show
+end
+
+task :t3 => [:clean] do
+ logger = Logger.new("Update")
+ Update.process('dest/null', logger)
+ logger.show
+end
+
+task :t4 => [:clean] do
+ Cp.process('src', 'dest', FAKE_LOGGER)
+ sh "tree -a"
+ logger = Logger.new("Update")
+ Update.process('dest/src', logger)
+ logger.show
+ sh "cat dest/src"
+ Update.process('dest/src', logger)
+ logger.show
+ sh "tree -a"
+end
+
+task :test => [:t1, :t2, :t3, :t4]
+
View
@@ -0,0 +1,2 @@
+<% name = "akira" %>
+You are <%=name%>developer!
View
@@ -1,3 +1,3 @@
-require File.dirname(__FILE__) + "/common"
+require File.expand_path(File.dirname(__FILE__)) + "/common"
process(SYSTEM_COPIES)
View
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + "/common"
+require File.expand_path(File.dirname(__FILE__)) + "/common"
[".vimrc", ".vim", ".zsh/.zshenv"].each do |f|
system "ln -s dotfiles/#{f} $HOME"

0 comments on commit 6d57f6b

Please sign in to comment.