class Nouhin::CLI

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/nouhin.rb, line 18
def initialize(*args)
  super(*args)
  @dot_nouhin_path = dot_nouhin_path
  return @can_start = false unless @dot_nouhin_path
  @root_path = File.dirname(@dot_nouhin_path)
  @index_file_path = @dot_nouhin_path + "/nouhin_files.index"
  @ignore_file_path = @dot_nouhin_path + "/nouhinignore"
  @repository_file_path = @dot_nouhin_path + "/repository.tar.gz"
  return @can_start = true
end

Public Instance Methods

add(file) click to toggle source
# File lib/nouhin/commands/add.rb, line 4
def add(file)
  can_start?
  path = file_check(file)
  File.open(@index_file_path,"a"){|f| f.puts path}
  puts "[ #{file} ] を納品対象としてマークしました."
  puts "取り消しする場合は [ reset FILE ] オプションを実行してください."
end
add_all_to_git() click to toggle source
# File lib/nouhin/commands/add_all_to_git.rb, line 4
def add_all_to_git
  can_start?
  File.foreach(@index_file_path) do |file|
    `git add #{file.chomp}`
    puts "#{file.chomp} を git add しました."
  end
end
checkout(file) click to toggle source
# File lib/nouhin/commands/checkout.rb, line 4
def checkout(file)
  can_start?
  path = fpath(file)
  puts "[ #{path} ] を修正前の状態に復元します."
  puts "修正した内容は取り消されます."
  puts "よろしいですか?( y/n )"
  #
  (puts "停止します."; exit) unless gets.chomp == "y"
  path.gsub!(/#{@root_path}/, ".")
  puts `tar -C #{@root_path} -zxvf #{@repository_file_path} #{path}`
  puts "[ #{path} ] を修正前の状態に復元しました."
end
checkout_all() click to toggle source
# File lib/nouhin/commands/checkout_all.rb, line 4
def checkout_all
  can_start?
  puts "作業領域にあるすべてのファイルを修正前の状態に復元します."
  puts "作業領域で修正した内容はすべて取り消されます."
  puts "よろしいですか?( y/n )"
  #
  (puts "停止します."; exit) unless gets.chomp == "y"
  puts `tar -C #{@root_path} -zxvf #{@repository_file_path}`
  puts "作業領域にあるすべてのファイルを修正前の状態に復元しました."
end
commit(file) click to toggle source
# File lib/nouhin/commands/commit.rb, line 4
def commit(file)
  can_start?
  path = File.expand_path(file)
  basename = File.basename(path)
  basename << ".gz" unless basename =~ /\.gz$/
  basename.gsub!(/gz$/,"tar.gz") unless basename =~ /\.tar\.gz$/
  dirname = File.dirname(path)
  puts "[ #{dirname}/ ] に [ #{basename} ] を作成します."
  puts "よろしいですか?( y/n )"
  #
  (puts "停止します."; exit) unless gets.chomp == "y"
  files = File.read(@index_file_path)
  files.gsub!(/^#{dirname}/,".")
  puts `tar -C #{@root_path} -zcvf #{basename} #{files.split.uniq.join(" ")} #{exclude_options(@ignore_file_path)}`
  puts "[ #{dirname}/#{basename} ] を作成しました."
  puts "中身を確認する場合は [ extract FILE --test ] オプションを実行してください."
end
diff(file) click to toggle source
# File lib/nouhin/commands/diff.rb, line 4
def diff(file)
  can_start?
  path = file_check(file)
  archive_file = path.gsub(/#{@root_path}/, ".")
  Dir.mktmpdir("nouhin") do |dir|
    `tar -C #{dir} -zxvf #{@repository_file_path} #{archive_file}`
    archive_file_path = archive_file.gsub(/^\./, "#{dir}")
    puts `diff -uwB #{archive_file_path} #{file}`
  end
end
diff_all() click to toggle source
# File lib/nouhin/commands/diff_all.rb, line 4
def diff_all
  can_start?
  File.foreach(@index_file_path) do |file|
    #invoke :diff, [file.chomp]
    diff(file.chomp)
  end
end
extract(file) click to toggle source
# File lib/nouhin/commands/extract.rb, line 6
def extract(file)
  path = file_check(file)
  # --test が指定されていたらアーカイブの内容を一覧表示して終了
  (puts `tar -ztvf #{path}`; exit) if options.test?
  #
  base = options[:dir] || "."
  puts `tar -C #{base} -ztvf #{path}`
  puts "現在のディレクトリ [ ./ ] を基点として、上記のファイルを展開します."
  puts "よろしいですか?( y/n )"
  #
  (puts "停止します."; exit) unless gets.chomp == "y"
  puts `tar -zxvf #{path}`
end
init() click to toggle source
# File lib/nouhin/commands/init.rb, line 5
def init
  nouhin_dir = ".nouhin"
  base = options[:dir] || "."
  dot_nouhin_path = base + "/" + nouhin_dir
  if init_target?("Dir", dot_nouhin_path, "処理を続けますか?") then
    FileUtils.rm_r(dot_nouhin_path) if Dir.exist?(dot_nouhin_path)
    Dir.mkdir(dot_nouhin_path) unless Dir.exist?(dot_nouhin_path)
    puts "[ #{base} ] に [ #{nouhin_dir} ] ディレクトリを作成しました."
  else
    puts "停止します."; exit
  end

  ignore_file = "nouhinignore"
  ignore_file_path = dot_nouhin_path + "/" + ignore_file
  template_file_path = "#{File.dirname(__FILE__)}/template_ignore"
  FileUtils.cp(template_file_path, ignore_file_path)
  #puts "イグノアファイル [ #{ignore_file_path} ] を初期作成しました."

  index_file = "nouhin_files.index"
  index_file_path = dot_nouhin_path + "/" + index_file
  File.open(index_file_path,"w"){|f| f = nil}
  #puts "インデックスファイル [ #{index_file_path} ] を初期作成しました."

  repository_file = "repository.tar.gz"
  repository_file_path = dot_nouhin_path + "/" + repository_file
  `tar -zcvf #{repository_file_path} #{base} #{exclude_options(ignore_file_path)} --exclude ".nouhin"`
  #puts "バックアップファイル [ #{repository_file_path} ] を初期作成しました."
end
init_target?(klass,path,msg) click to toggle source
# File lib/nouhin/commands/init.rb, line 35
def init_target?(klass,path,msg)
  ret = "y"
  if Object.const_get(klass).exist?(path) then
    puts "すでに #{File.basename(path)} が存在します."
    puts "#{msg} ( y/n )"
    ret = gets.chomp
  end
  return ret == "y" ? true : false
end
reset(file) click to toggle source
# File lib/nouhin/commands/reset.rb, line 4
def reset(file)
  can_start?
  path = fpath(file)
  files = File.read(@index_file_path).split
  files.delete(path)
  File.open(@index_file_path,"w") do |f|
    files.each{|file| f.puts file }
  end
  puts "[ #{file} ] は納品対象から外れました."
end
status() click to toggle source
# File lib/nouhin/commands/status.rb, line 4
def status
  can_start?
  File.foreach(@index_file_path){|f| puts f}
end

Private Instance Methods

can_start?() click to toggle source

実行場所が作業ツリーの範囲内でなければ停止する

# File lib/nouhin.rb, line 31
def can_start?
  (puts "ここは作業領域ではありません.停止します."; exit) unless @can_start
end
dot_nouhin_path() click to toggle source

.nouhin ディレクトリを現在のディレクトリからさかのぼって探す

# File lib/nouhin.rb, line 52
def dot_nouhin_path
  Dir.pwd.split("/").count.times do |n|
    path = "../"*n + ".nouhin/"
    return File.expand_path(path) if Dir.exist?(path)
  end
  return false
end
exclude_options(ignore_file_path) click to toggle source

ignore ファイルから tar の exclude オプションを生成する

# File lib/nouhin.rb, line 36
def exclude_options(ignore_file_path)
  ar = File.read(ignore_file_path)
  ar = ar.split("\n")
  ar.map! do |r|
    r.gsub!(/#.*$/,""); r.gsub!(/^\s*?$/,"")
    r.gsub!(/^\//,"./"); r.gsub!(/\/$/,"/*")
    r
  end
  ar =  ar.delete_if{|r|r==""}
  ar.map! do |r|
    "--exclude '" + r + "'"
  end
  return ar.join(" ")
end
file_check(file) click to toggle source
# File lib/nouhin.rb, line 75
def file_check(file)
  path = fpath(file)
  (puts "ファイルが存在しません."; exit) if !File.exist?(path)
  return path
end
fpath(file) click to toggle source

対象ファイル存在チェック

# File lib/nouhin.rb, line 71
def fpath(file)
  File.expand_path(file)
end
gets() click to toggle source

モンキーパッチ

# File lib/nouhin.rb, line 66
def gets
  return options[:force] ? "y"  : $stdin.gets
end
puts(msg) click to toggle source

–force が指定されていたらメッセージインターフェイス抑制

Calls superclass method
# File lib/nouhin.rb, line 61
def puts(msg)
  super unless options[:force]
end