Class: Milkode::Cdstk

Inherits:
Object
  • Object
show all
Defined in:
lib/milkode/cdstk/cdstk.rb

Constant Summary collapse

CONFIG_LJUST =

Parameters

20
NAME =
'Name'
TOTAL =
'Total'

Instance Method Summary collapse

Constructor Details

#initialize(io = $stdout, db_dir = ".") ⇒ Cdstk

Display parameter for ‘milk config’



37
38
39
40
41
42
43
44
# File 'lib/milkode/cdstk/cdstk.rb', line 37

def initialize(io = $stdout, db_dir = ".")
  @db_dir = db_dir
  @out = io
  # @out = $stdout # 強制出力
  @is_display_info = false     # alert_info の表示
  clear_count
  @yaml = YamlFileWrapper.load_if(@db_dir)
end

Instance Method Details

#add(dirs, options) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/milkode/cdstk/cdstk.rb', line 119

def add(dirs, options)
  update_display_info(options)

  if options[:from_file]
    dirs = File.read(options[:from_file]).split("\n")
  end

  print_result do
    # データベースを開く
    db_open

    # メイン処理
    begin
      dirs.each do |v|
        # コンテンツを読み込める形に変換
        dir = convert_content(v, options)

        # YAMLに追加
        package = Package.create(dir, options[:ignore])
        skip_add = options[:from_file] && @yaml.find_name(package.name)
        add_yaml(package) unless skip_add

        # オプション設定
        is_update_with_git_pull   = git_protocol?(options, v)
        is_update_with_svn_update = svn_protocol?(options, v)
        set_yaml_options(package, options, is_update_with_git_pull, is_update_with_svn_update)

        # アップデート
        unless options[:empty]
          if skip_add
            update_package_in(package, options)
          else
            update_dir_in(dir)
          end
        end
      end
    rescue AddError => e
      error_alert(e.message)
      return
    rescue ConvertError => e
      return
    end
  end
end

#add_dir(dir, no_yaml = false) ⇒ Object



202
203
204
205
206
# File 'lib/milkode/cdstk/cdstk.rb', line 202

def add_dir(dir, no_yaml = false)
  add_yaml(Package.create(dir)) unless no_yaml
  db_open
  update_dir_in(dir)
end

#add_yaml(package) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/milkode/cdstk/cdstk.rb', line 208

def add_yaml(package)
  # Already exist package
  if @yaml.find_name(package.name)
    raise AddError, "package named '#{package.name}' already exist."
  end

  # File not exist
  unless File.exist?(package.directory)
    raise AddError, "not found '#{package.directory}'."
  end

  # Save yaml
  @yaml.add(package)
  @yaml.save

  # Sync yaml -> db
  db_open
  @grndb.yaml_sync(@yaml.contents)
end

#assert_compatibleObject



65
66
67
# File 'lib/milkode/cdstk/cdstk.rb', line 65

def assert_compatible
  db_open
end

#breakdown_detail(records, name_width) ⇒ Object



921
922
923
924
925
926
# File 'lib/milkode/cdstk/cdstk.rb', line 921

def breakdown_detail(records, name_width)
  sorted_plangs(records).map {|name, count|
    percent = (count.to_f / records.size * 100).to_i
    sprintf("%-#{name_width}s  %5d  %3d%%", name, count, percent)
  }.join("\n")
end

#breakdown_shorten(records) ⇒ Object



895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
# File 'lib/milkode/cdstk/cdstk.rb', line 895

def breakdown_shorten(records)
  plangs = sorted_plangs(records)

  plangs = plangs.reduce([]) {|array, plang|
    name, count = plang
    percent = calc_percent(count, records.size)

    if percent == 0
      if array.empty? || array[-1][0] != 'other'
        array << ['other', count]
      else
        array[-1][1] += count
      end
    else
      array << plang
    end

    array
  }
  
  plangs.map {|name, count|
    percent = calc_percent(count, records.size)
    "#{name}:#{count}(#{percent}%)"
  }.join(', ')
end

#calc_percent(count, size) ⇒ Object



891
892
893
# File 'lib/milkode/cdstk/cdstk.rb', line 891

def calc_percent(count, size)
  (count.to_f / size * 100).to_i
end

#check_integrityObject



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/milkode/cdstk/cdstk.rb', line 483

def check_integrity
  db_open

  @out.puts "Check integrity ..."

  yaml = @yaml.contents.map {|p| p.name}.sort
  grndb = @grndb.packages.map {|p| p.name}.sort

  d = yaml.diff grndb

  unless d[0].empty?
    @out.puts "'milkode.yaml' has #{d[0]}, but 'PackageTable' don't have."
  end

  unless d[1].empty?
    @out.puts "'PackageTable' has #{d[1]}, but 'milkode.yaml' don't have."
  end

  if d[0].empty? && d[1].empty?
    @out.puts 'ok'
  end
end

#cleanup(args, options) ⇒ Object



531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/milkode/cdstk/cdstk.rb', line 531

def cleanup(args, options)
  update_display_info(options)

  if (options[:all])
    cleanup_all(options)
  elsif (options[:packages])
    cleanup_packages
  else
    print_result do
      db_open
      args.each do |arg|
        package = @yaml.find_name(arg) || @yaml.find_dir(arg)

        unless package
          path = File.expand_path(arg)
          package = @yaml.package_root(path) if File.exist?(path)
        end
        
        if (package)
          @documents.cleanup_package_name(package.name) # TODO: Support ignore_checker
        else
          @out.puts "Not found package '#{arg}'."
          return
        end
      end
    end
  end
end

#cleanup_all(options) ⇒ Object



580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/milkode/cdstk/cdstk.rb', line 580

def cleanup_all(options)
  if (options[:force] or yes_or_no("cleanup contents? (yes/no)"))
    print_result do
      # Remove non exist packages
      cleanup_packages_in
      
      # @todo Remove ignore files
      @documents.cleanup do |record|
        alert("rm_record", record.path)
        @file_count += 1
      end
    end
  end
end

#cleanup_packagesObject

Remove non exist pakcages



561
562
563
564
565
# File 'lib/milkode/cdstk/cdstk.rb', line 561

def cleanup_packages
  print_result do
    cleanup_packages_in
  end
end

#cleanup_packages_inObject



567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/milkode/cdstk/cdstk.rb', line 567

def cleanup_packages_in
    @yaml.contents.find_all {|v| !File.exist? v.directory }.each do |p|
      @yaml.remove(p)
      alert("rm_package", p.directory)
      @package_count += 1
    end
    @yaml.save

    db_open

    @grndb.yaml_sync(@yaml.contents)
end

#clear_countObject



46
47
48
49
50
51
52
# File 'lib/milkode/cdstk/cdstk.rb', line 46

def clear_count
  @package_count = 0
  @file_count = 0
  @add_count = 0
  @update_count = 0
  @start_time = Time.now
end

#config(args, options) ⇒ Object



1040
1041
1042
1043
1044
1045
1046
# File 'lib/milkode/cdstk/cdstk.rb', line 1040

def config(args, options)
  if args.empty?
    config_print
  else
    config_set(args, options)
  end
end

#config_printObject



1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
# File 'lib/milkode/cdstk/cdstk.rb', line 1048

def config_print
  package = find_package_current_dir

  return if package.nil?

  @out.puts "Ignore:"
  package.ignore.each do |v|
    @out.puts "  #{v}"
  end

  @out.puts "Options:"
  package.options.each do |key, value|
    @out.puts "  #{(key.to_s + ':').ljust(CONFIG_LJUST)} #{value}"
  end
end

#config_set(args, options) ⇒ Object



1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
# File 'lib/milkode/cdstk/cdstk.rb', line 1064

def config_set(args, options)
  package = find_package_current_dir
  return if package.nil?

  opt = package.options

  if options[:delete]
    opt.delete(args[0].to_sym)
  else
    if args.size == 2
      opt[args[0].to_sym] = config_to_value(args[1])
    else
      @out.puts("[usage] milk config KEY VALUE")
    end
  end
  
  package.set_options(opt)

  @yaml.save
end

#config_to_value(v) ⇒ Object

config_to_value(‘true’) #=> true config_to_value(‘false’) #=> false config_to_value(‘abc’) #=> ‘abc’



1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
# File 'lib/milkode/cdstk/cdstk.rb', line 1088

def config_to_value(v)
  case v
  when 'true'
    true
  when 'false'
    false
  else
    v
  end
end

#convert_content(src, options) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/milkode/cdstk/cdstk.rb', line 228

def convert_content(src, options)
  # httpファイルならダウンロード
  begin
    src = download_file(src, options)
  rescue AddError => e
    raise e
  rescue => e
    error_alert("download failure '#{src}'.")
    raise e                 # そのまま上に持ち上げてスタックトレース表示
  end
  
  # アーカイブファイルなら展開
  begin
    src = extract_file(src)
  rescue => e
    error_alert("extract failure '#{src}'.")
    raise e                 # そのまま上に持ち上げてスタックトレース表示
  end

  # 絶対パスに変換
  File.expand_path(src)
end

#dir(args, options) ⇒ Object



653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# File 'lib/milkode/cdstk/cdstk.rb', line 653

def dir(args, options)
  if args.empty?
    path = File.expand_path('.')
    package = @yaml.package_root(path)

    if (package)
      @out.print package.directory + (options[:top] ? "" : "\n")
    else
      # Use mcd.
      @out.print "Not registered." + (options[:top] ? "" : "\n")
    end
  else
    match_p = @yaml.contents.find_all do |p|
      args.all? {|k| p.name.include? k }
    end

    dirs = match_p.map{|v|v.directory}.reverse

    if options[:top]
      unless (dirs.empty?)
        @out.print dirs[0]
      else
        @out.print ""
      end
    else
      @out.puts dirs
    end
  end
end

#download_file(src, options) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/milkode/cdstk/cdstk.rb', line 264

def download_file(src, options)
  if git_protocol?(options, src)
    git_clone_in(src, options)
  elsif svn_protocol?(options, src)
    svn_clone_in(src, options)
  elsif src =~ /^https?:/
    download_file_in(src)
  else
    raise AddError, "'--name' option is not available in local directory." if options[:name]
    src
  end
end

#download_file_in(url) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/milkode/cdstk/cdstk.rb', line 277

def download_file_in(url)
  alert("download", "#{url}")
  
  dst_dir = File.join(@db_dir, "packages/http")
  FileUtils.mkdir_p dst_dir

  filename = File.join(dst_dir, File.basename(url))
  
  open(url) do |src|
    open(filename, "wb") do |dst|
      dst.write(src.read)
    end
  end

  filename
end

#dumpObject



635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'lib/milkode/cdstk/cdstk.rb', line 635

def dump
  db_open

  @documents.each do |grnrcd|
    record = DocumentRecord.new grnrcd
    
    @out.puts record.inspect
    @out.puts "path : #{record.path}"
    @out.puts "shortpath : #{record.shortpath}"
    @out.puts "suffix : #{record.suffix}"
    @out.puts "timestamp : #{record.timestamp.strftime('%Y/%m/%d %H:%M:%S')}"
    @out.puts "content :", record.content ? record.content[0..64] : nil
    @out.puts
  end

  @out.puts "total #{@documents.size} record."
end

#extract_file(src) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/milkode/cdstk/cdstk.rb', line 251

def extract_file(src)
  ext = File.extname(src);
  
  case ext
  when '.zip', '.xpi'
    alert("extract", "#{src}")
    zip_dir = File.join(@db_dir, "packages/#{ext.sub(".", "")}")
    result = File.join(zip_dir, Util.zip_extract(src, zip_dir))
  else
    src
  end
end

#fav(args, options) ⇒ Object



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/milkode/cdstk/cdstk.rb', line 436

def fav(args, options)
  db_open

  if (options[:sync_yaml])
    is_dirty = false

    @yaml.contents.each do |package|
      db_fav = @grndb.packages.fav?(package.name)
      if package.fav? != db_fav
        @out.puts "#{package.name} : #{package.fav?} -> #{db_fav}"
        package.set_fav(db_fav)
        is_dirty = true
      end
    end

    @yaml.save if is_dirty

  elsif (args.empty?)
    @out.puts @grndb.packages.favs.map{|r| r.name}
  else
    is_dirty = false

    args.each do |v|
      # database
      time = options[:delete] ? Time.at(0) : Time.now

      if @grndb.packages.touch_if(v, :favtime, time)
        # milkode_yaml
        package = @yaml.find_name(v)
        dst = package.options
        unless options[:delete]
          dst[:fav] = true
        else
          dst.delete(:fav)
        end
        package.set_options(dst)
        @yaml.update(package)
        is_dirty = true
      else
        @out.puts "Not found package '#{v}'"
      end
    end

    @yaml.save if is_dirty
  end
end

#files(args, options) ⇒ Object



1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
# File 'lib/milkode/cdstk/cdstk.rb', line 1017

def files(args, options)
  packages = options[:all] ? @yaml.contents : find_packages(args)
  return if (packages.empty?)

  db_open

  basedir = File.expand_path('.') if options[:relative]
  
  packages.each do |package|
    package_records(package.name).each do |record|
      if options[:relative]
        @out.puts Util.relative_path(record.path, basedir)
      else
        if options[:all]
          @out.puts record.path
        else
          @out.puts record.restpath
        end
      end
    end
  end
end

#find_package_current_dirObject



941
942
943
944
945
946
# File 'lib/milkode/cdstk/cdstk.rb', line 941

def find_package_current_dir
  dir = File.expand_path('.')
  package = @yaml.package_root(dir)
  @out.puts "Not registered '#{dir}'." if package.nil?
  package
end

#find_packages(args) ⇒ Object

引数が指定されている時は名前にマッチするパッケージを、未指定の時は現在位置から見つける



929
930
931
932
933
934
935
936
937
938
939
# File 'lib/milkode/cdstk/cdstk.rb', line 929

def find_packages(args)
  unless args.empty?
    args.map do |v|
      r = @yaml.find_name(v)
      @out.puts "Not found package '#{v}'." if r.nil?
      r
    end
  else
    [find_package_current_dir].compact
  end
end

#git_clone_in(url, options) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/milkode/cdstk/cdstk.rb', line 294

def git_clone_in(url, options)
  dst_dir  = File.join(@db_dir, "packages/git")
  name     = options[:name] || File.basename(url).sub(/\.git\Z/, "")
  filename = File.join(dst_dir, name)

  unless File.exist?(filename)
    # git output progress to stderr.
    # `git clone #{url} #{filename} 2>&1`

    # with output
    if options[:branch_name]
      alert("git", "#{url} (#{options[:branch_name]})")
      system("git clone #{url} #{filename} -b #{options[:branch_name]}")
    else
      alert("git", url)
      system("git clone #{url} #{filename}")
    end
  end

  filename
end

#ignore(args, options) ⇒ Object



948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
# File 'lib/milkode/cdstk/cdstk.rb', line 948

def ignore(args, options)
  if options[:global]
    if args.size > 0
      @yaml.set_global_gitignore(args[0])
      @yaml.save
      @out.puts "Set '#{args[0]}'"
    else
      if @yaml.global_gitignore
        @out.puts @yaml.global_gitignore
      end
    end

    return
  end

  current_dir = File.expand_path('.')

  if (options[:package])
    package = @yaml.find_name(options[:package])
    raise IgnoreError, "Not found package '#{options[:package]}'." unless package
  else
    package = @yaml.package_root(current_dir)
    raise IgnoreError, "Not a package dir: '#{current_dir}'" unless package
  end

  if options[:dry_run]
    # Test mode
    db_open
    @is_display_info = true
    @is_silent = true
    update_dir_in(package.directory)
  elsif options[:delete_all]
    # Delete all
    package.set_ignore([])
    @yaml.update(package)
    @yaml.save
  elsif args.empty?
    # Display ignore settting
    @out.puts package.ignore
  else
    # Add or Delete
    if options[:package]
      add_ignore = args.map {|v| v.sub(/^.\//, "") }
    else
      path = Util.relative_path(File.expand_path('.'), package.directory).to_s
      add_ignore = args.map {|v| File.join(path, v).sub(/^.\//, "") }
    end

    ignore = package.ignore

    if options[:delete]
      ignore -= add_ignore          
    else
      ignore += add_ignore
    end
    ignore.uniq!
    package.set_ignore(ignore)
    
    @yaml.update(package)
    @yaml.save

    if options[:delete]
      @out.puts add_ignore.map{|v| "Delete : #{v}"}
    else
      @out.puts add_ignore.map{|v| "Add : #{v}"}
    end
  end
end

#info(args, options) ⇒ Object



741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'lib/milkode/cdstk/cdstk.rb', line 741

def info(args, options)
  db_open

  if options[:all]
    info_format(@yaml.contents, select_format(options, :table))
    milkode_info
    return
  end

  packages = find_packages(args)
  packages.compact!
  return if (packages.empty?)

  info_format(packages, select_format(options, :detail))
end

#info_format(packages, format) ⇒ Object



766
767
768
769
770
771
772
773
774
775
776
777
# File 'lib/milkode/cdstk/cdstk.rb', line 766

def info_format(packages, format)
  case format
  when :detail
    info_format_detail(packages)
  when :table
    info_format_table(packages)        
  when :breakdown
    info_format_breakdown(packages)        
  when :unknown
    info_format_unknown(packages)
  end
end

#info_format_breakdown(packages) ⇒ Object



815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
# File 'lib/milkode/cdstk/cdstk.rb', line 815

def info_format_breakdown(packages)
  packages.each_with_index do |package, index|
    records = package_records(package.name)
    plangs = sorted_plangs(records)

    # column1's width
    plang_names = plangs.map{|v| v[0]}
    max = (plang_names + [package.name, TOTAL]).map{|name| name.length}.max
    
    @out.puts if index != 0
  @out.puts <<EOF.chomp
#{package.name.ljust(max)}  files  rate
#{'=' * max}=============
#{breakdown_detail(package_records(package.name), max)}
#{'-' * max}-------------
#{sprintf("%-#{max}s  %5d  %3d%%", TOTAL, records.size, 100)}
EOF
  end
end

#info_format_detail(packages) ⇒ Object



779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
# File 'lib/milkode/cdstk/cdstk.rb', line 779

def info_format_detail(packages)
  packages.each_with_index do |package, index|
    records = package_records(package.name)

    r = []
    r.push "Name:      #{package.name}"
    r.push "Ignore:    #{package.ignore}" unless package.ignore.empty?
    r.push "Options:   #{package.options}" unless package.options.empty?
    r.push "Records:   #{records.size}"
    r.push "Breakdown: #{breakdown_shorten(records)}"
    r.push "Linecount: #{linecount_total(records)}"
    r.push ""

    @out.puts if index != 0
    @out.puts r.join("\n")
  end
end

#info_format_table(packages) ⇒ Object



800
801
802
803
804
805
806
807
808
809
810
811
812
813
# File 'lib/milkode/cdstk/cdstk.rb', line 800

def info_format_table(packages)
  max = packages.map{|p| p.name.length}.max
  max = NAME.length  if max < NAME.length

  @out.puts <<EOF.chomp
#{NAME.ljust(max)}     Records   Linecount
#{'=' * max}========================
EOF

  packages.each do |package|
    records = package_records(package.name)
    @out.puts "#{package.name.ljust(max)}  #{records.size.to_s.rjust(10)}  #{linecount_total(records).to_s.rjust(10)}"
  end
end

#info_format_unknown(packages) ⇒ Object



835
836
837
838
839
840
841
842
843
844
# File 'lib/milkode/cdstk/cdstk.rb', line 835

def info_format_unknown(packages)
  packages.each_with_index do |package, index|
    @out.puts if index != 0

    package_records(package.name).each do |record|
      path = record.restpath
      @out.puts path if PlangDetector.new(path).unknown?
    end
  end
end

#init(options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/milkode/cdstk/cdstk.rb', line 54

def init(options)
  if Dir.emptydir?(@db_dir)
    @yaml = YamlFileWrapper.create(@db_dir)
    @out.puts "create     : #{yaml_file}"
    db_create
    setdb([@db_dir], {}) if (options[:setdb])
  else
    @out.puts "Can't create milkode db (Because not empty in #{db_dir_expand})"
  end
end

#linecount_total(records) ⇒ Object



850
851
852
853
854
855
856
857
858
859
860
861
862
863
# File 'lib/milkode/cdstk/cdstk.rb', line 850

def linecount_total(records)
  records.reduce(0) do |total, record|
    begin
      unless record.content.nil?
        total + record.content.count($/) + 1
      else
        total
      end
    rescue ArgumentError
      warning_alert("invalid byte sequence : #{record.path}")
      total
    end
  end
end

#list(args, options) ⇒ Object



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/milkode/cdstk/cdstk.rb', line 400

def list(args, options)
  if options[:check]
    check_integrity
    return
  end
  
  match_p = @yaml.contents.find_all do |p|
    args.all? {|k| p.name.include? k }
  end

  max = match_p.map{|p| p.name.length}.max

  str = match_p.sort_by {|p|
    p.name
  }.map {|p|
    h = File.exist?(p.directory) ? '' : '? '
    if (options[:verbose])
      "#{(h + p.name).ljust(max+2)} #{p.directory}"
    elsif (options[:directory])
      "#{p.directory}"
    else
      "#{h}#{p.name}"
    end
  }.join("\n")

  @out.puts str

  if !options[:directory] && Util.pipe?($stdout)
    if args.empty?
      milkode_info
    else
      list_info(match_p) unless match_p.empty?
    end
  end
end

#mcd(options) ⇒ Object



701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# File 'lib/milkode/cdstk/cdstk.rb', line 701

def mcd(options)
  if options[:shell] != 'cygwin'
    @out.print <<EOF
# Copy to '.bashrc'.
mcd() {
local args="$1 $2 $3 $4 $5 $6 $7 $8 $9"
local dir=`milk dir --top $args`

if [ "$dir" = "" ]; then
    echo "fatal: Not found package: $1 $2 $3 $4 $5 $6 $7 $8 $9"
elif [ "$dir" = "Not registered." ]; then
    echo "fatal: Not a package dir: `pwd`"
else
    cd $dir
    pwd
fi
}
EOF
  end

  if options[:shell] != 'sh'
    @out.print <<EOF
# For Cygwin.
mcd() {
local args="$1 $2 $3 $4 $5 $6 $7 $8 $9"
local dir=`milk.bat dir --top $args`

if [ "$dir" = "" ]; then
    echo "fatal: Not found package: $1 $2 $3 $4 $5 $6 $7 $8 $9"
elif [ "$dir" = "Not registered." ]; then
    echo "fatal: Not a package dir: `pwd`"
else
    cd $dir
    pwd
fi
}
EOF
  end
end

#package_records(name) ⇒ Object



846
847
848
# File 'lib/milkode/cdstk/cdstk.rb', line 846

def package_records(name)
  @documents.search(:strict_packages => [name])
end

#pwd(options) ⇒ Object



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/milkode/cdstk/cdstk.rb', line 506

def pwd(options)
  dir = options[:default] ? Dbdir.default_dir : db_dir_expand
  
  if File.exist? dir
    if options[:default]
      @out.puts dir
    else
      package = @yaml.package_root(File.expand_path('.'))

      name = ""
      if package
        name = package.name
      elsif Dbdir.dbdir?
        name = 'On database'
      else
        name = 'Not package dir'
      end
      
      @out.puts "#{name} in #{dir}"
    end
  else
    @out.puts "Not found db in #{Dir.pwd}"
  end
end

#rebuild(args, options) ⇒ Object



595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
# File 'lib/milkode/cdstk/cdstk.rb', line 595

def rebuild(args, options)
  update_display_info(options)
  
  if (options[:all])
    db_delete
    db_create
    @grndb.yaml_sync(@yaml.contents)
    update_all({})
  else
    if (args.empty?)
      path = File.expand_path('.')
      package = @yaml.package_root(path)

      if (package)
        print_result do
          db_open
          remove_dir(package.directory, true)
          add_dir(package.directory, true)
        end
      else
        @out.puts "Not registered. '#{path}'."
      end
    else
      print_result do
        args.each do |name|
          package = @yaml.find_name(name)
          if (package)
            db_open
            remove_dir(package.directory, true)
            add_dir(package.directory, true)
          else
            @out.puts "Not found package '#{name}'."
            return
          end
        end
      end
    end
  end
end

#remove(args, options) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/milkode/cdstk/cdstk.rb', line 355

def remove(args, options)
  update_display_info(options)
  
  if (options[:all])
    remove_all
  else
    print_result do
      db_open
      args.each do |arg|
        package = @yaml.find_name(arg) || @yaml.find_dir(arg)

        unless package
          path = File.expand_path(arg)
          package = @yaml.package_root(path) if File.exist?(path)
        end
        
        if (package)
          remove_dir(package.directory)                
        else
          @out.puts "Not found package '#{arg}'."
          return
        end
      end
    end
  end
end

#remove_allObject



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/milkode/cdstk/cdstk.rb', line 329

def remove_all
  print_result do
    list([], {:verbose => true})
    
    if yes_or_no("Remove #{@yaml.contents.size} contents? (yes/no)")
      db_open

      # documents
      @documents.remove_all do |record|
        alert_info("rm_record", record.path)
        @file_count += 1
      end

      # packages
      @grndb.packages.remove_all
      
      # yaml
      @package_count += @yaml.contents.size
      @yaml.remove_all
      @yaml.save
    else
      return
    end
  end
end

#select_format(options, default_value) ⇒ Object



757
758
759
760
761
762
763
764
# File 'lib/milkode/cdstk/cdstk.rb', line 757

def select_format(options, default_value)
  format = default_value
  format = :detail     if options[:detail]
  format = :table      if options[:table]
  format = :breakdown  if options[:breakdown]
  format = :unknown    if options[:unknown]
  format
end

#set_yaml_options(package, options, is_update_with_git_pull, is_update_with_svn_update) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/milkode/cdstk/cdstk.rb', line 164

def set_yaml_options(package, options, is_update_with_git_pull, is_update_with_svn_update)
  is_dirty = false
  
  if options[:no_auto_ignore]
    dst = package.options
    dst[:no_auto_ignore] = true
    package.set_options(dst)
    is_dirty = true
  end

  # ローカルディレクトリの名前変更は後回し
  # if options[:name]
  #   dst = package.options
  #   dst[:name] = src[:name]
  #   package.set_options(dst)
  #   is_dirty = true
  # end

  if is_update_with_git_pull
    dst = package.options
    dst[:update_with_git_pull] = is_update_with_git_pull
    package.set_options(dst)
    is_dirty = true
  end

  if is_update_with_svn_update
    dst = package.options
    dst[:update_with_svn_update] = is_update_with_svn_update
    package.set_options(dst)
    is_dirty = true
  end

  if is_dirty
    @yaml.update(package)
    @yaml.save
  end
end

#setdb(args, options) ⇒ Object



683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/milkode/cdstk/cdstk.rb', line 683

def setdb(args, options)
  if (options[:reset])
    CdstkCommand.setdb_reset
    @out.puts "Reset default db\n  remove:      #{Dbdir.milkode_db_dir}\n  default_db:  #{Dbdir.default_dir}"
  elsif (args.empty?)
    @out.puts Dbdir.default_dir
  else
    dbpath = args.first
    path = File.expand_path(dbpath)
    begin
      CdstkCommand.setdb_set path
      @out.puts "Set default db #{path}."
    rescue CdstkCommand::NotExistDatabase
      @out.puts "fatal: '#{path}' is not database."
    end
  end
end

#sorted_plangs(records) ⇒ Object



865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
# File 'lib/milkode/cdstk/cdstk.rb', line 865

def sorted_plangs(records)
  total = {}
  
  records.each do |record|
    lang = PlangDetector.new(record.restpath)

    if total[lang.name]
      total[lang.name] += 1
    else
      total[lang.name] = 1
    end
  end

  total.map {|name, count|
    [name, count]
  }.sort {|a, b|
    if (a[0] == PlangDetector::UNKNOWN)
      -1
    elsif (b[0] == PlangDetector::UNKNOWN)
      1
    else
      a[1] <=> b[1]
    end
  }.reverse
end

#svn_clone_in(url, options) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/milkode/cdstk/cdstk.rb', line 316

def svn_clone_in(url, options)
  alert("svn", url)

  dst_dir  = File.join(@db_dir, "packages/svn")
  name     = options[:name] || File.basename(url)
  filename = File.join(dst_dir, name)

  # with output
  system("svn checkout #{url} #{filename}")

  filename
end

#update(args, options) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/milkode/cdstk/cdstk.rb', line 79

def update(args, options)
  update_display_info(options)

  if (options[:all])
    update_all(options)
  else
    if (args.empty?)
      path = File.expand_path('.')
      package = @yaml.package_root(path)

      if (package)
        print_result do
          db_open
          update_package_in(package, options)
        end
      else
        @out.puts "Not registered. If you want to add, 'milk add #{path}'."
      end
    else
      print_result do
        db_open
        args.each do |name|
          package = @yaml.find_name(name)
          if (package)
            update_package_in(package, options)
          else
            @out.puts "Not found package '#{name}'."
            return
          end
        end
      end
    end
  end
end

#update_all(options) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/milkode/cdstk/cdstk.rb', line 69

def update_all(options)
  print_result do 
    db_open

    @yaml.contents.each do |package|
      update_package_in(package, options)
    end
  end
end

#update_for_grep(dir) ⇒ Object



114
115
116
117
# File 'lib/milkode/cdstk/cdstk.rb', line 114

def update_for_grep(dir)
  db_open
  update_dir_in(dir)
end

#yes_or_no(msg) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/milkode/cdstk/cdstk.rb', line 382

def yes_or_no(msg)
  if ($no_readline)
    @out.puts "Pass Cdstk#yes_or_no, because fail \"require 'readline'\"."
    return true
  end
    
  @out.puts msg
  while buf = Readline.readline("> ")
    case buf
    when 'yes'
      return true
    when 'no'
      break
    end
  end
  return false
end