Class: CDNGet::Main

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script = nil) ⇒ Main

Returns a new instance of Main.



655
656
657
# File 'lib/cdnget.rb', line 655

def initialize(script=nil)
  @script = script || File.basename($0)
end

Class Method Details

.main(args = nil) ⇒ Object



690
691
692
693
694
695
696
697
698
# File 'lib/cdnget.rb', line 690

def self.main(args=nil)
  args ||= ARGV
  s = self.new().run(*args)
  puts s if s
  exit 0
rescue CommandError => ex
  $stderr.puts ex.message
  exit 1
end

Instance Method Details

#do_download_library(cdn_code, library, version, basedir) ⇒ Object



836
837
838
839
840
841
# File 'lib/cdnget.rb', line 836

def do_download_library(cdn_code, library, version, basedir)
  cdn = find_cdn(cdn_code)
  version = cdn.latest_version(library) if version == 'latest'
  cdn.download(library, version, basedir, quiet: @quiet)
  return nil
end

#do_find_library(cdn_code, library) ⇒ Object



784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/cdnget.rb', line 784

def do_find_library(cdn_code, library)
  cdn = find_cdn(cdn_code)
  d = cdn.find(library)
  buf = []
  if @quiet
    d[:versions].each do |ver|
      buf << "#{ver}\n"
    end unless empty?(d[:versions])
  else
    buf << "name:     #{d[:name]}\n"
    buf << "desc:     #{d[:desc]}\n"    unless empty?(d[:desc])
    buf << "tags:     #{d[:tags]}\n"    unless empty?(d[:tags])
    buf << "site:     #{d[:site]}\n"    unless empty?(d[:site])
    buf << "info:     #{d[:info]}\n"    unless empty?(d[:info])
    buf << "license:  #{d[:license]}\n" unless empty?(d[:license])
    buf << "snippet: |\n" << d[:snippet].gsub(/^/, '    ') unless empty?(d[:snippet])
    buf << "versions:\n"
    d[:versions].each do |ver|
      buf << "  - #{ver}\n"
    end unless empty?(d[:versions])
  end
  return buf.join()
end

#do_get_library(cdn_code, library, version) ⇒ Object



808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
# File 'lib/cdnget.rb', line 808

def do_get_library(cdn_code, library, version)
  cdn = find_cdn(cdn_code)
  version = cdn.latest_version(library) if version == 'latest'
  d = cdn.get(library, version)
  buf = []
  if @quiet
    d[:urls].each do |url|
      buf << "#{url}\n"
    end if d[:urls]
  else
    buf << "name:     #{d[:name]}\n"
    buf << "version:  #{d[:version]}\n"
    buf << "desc:     #{d[:desc]}\n"    unless empty?(d[:desc])
    buf << "tags:     #{d[:tags]}\n"    unless empty?(d[:tags])
    buf << "site:     #{d[:site]}\n"    unless empty?(d[:site])
    buf << "info:     #{d[:info]}\n"    unless empty?(d[:info])
    buf << "npmpkg:   #{d[:npmpkg]}\n"  unless empty?(d[:npmpkg])
    buf << "default:  #{d[:default]}\n" unless empty?(d[:default])
    buf << "license:  #{d[:license]}\n" unless empty?(d[:license])
    buf << "snippet: |\n" << d[:snippet].gsub(/^/, '    ') unless empty?(d[:snippet])
    buf << "urls:\n" unless empty?(d[:urls])
    d[:urls].each do |url|
      buf << "  - #{url}\n"
    end unless empty?(d[:urls])
  end
  return buf.join()
end

#do_list_cdnsObject



767
768
769
770
# File 'lib/cdnget.rb', line 767

def do_list_cdns()
  return CLASSES.map {|c| "#{c::CODE}\n" }.join() if @quiet
  return CLASSES.map {|c| "%-10s  # %s\n" % [c::CODE, c::SITE_URL] }.join()
end

#do_list_libraries(cdn_code) ⇒ Object



772
773
774
775
776
777
# File 'lib/cdnget.rb', line 772

def do_list_libraries(cdn_code)
  cdn = find_cdn(cdn_code)
  list = cdn.list()  or
    raise CommandError.new("#{cdn_code}: cannot list libraries; please specify pattern such as 'jquery*'.")
  return render_list(list)
end

#do_search_libraries(cdn_code, pattern) ⇒ Object



779
780
781
782
# File 'lib/cdnget.rb', line 779

def do_search_libraries(cdn_code, pattern)
  cdn = find_cdn(cdn_code)
  return render_list(cdn.search(pattern))
end

#find_cdn(cdn_code) ⇒ Object



756
757
758
759
760
# File 'lib/cdnget.rb', line 756

def find_cdn(cdn_code)
  klass = CLASSES.find {|c| c::CODE == cdn_code }  or
    raise CommandError.new("#{cdn_code}: no such CDN.")
  return klass.new(debug_mode: @debug_mode)
end

#help_messageObject



659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'lib/cdnget.rb', line 659

def help_message()
  script = @script
  return "\#{script}  -- download files from public CDN (cdnjs/jsdelivr/unpkg/google)\n\nUsage: \#{script} [<options>] [<CDN> [<library> [<version> [<directory>]]]]\n\nOptions:\n  -h, --help        : help\n  -v, --version     : version\n  -q, --quiet       : minimal output\n  --debug       : (debug mode)\n\nExample:\n  $ \#{script}                              # list public CDN names\n  $ \#{script} [-q] cdnjs                   # list libraries (except jsdelivr/unpkg)\n  $ \#{script} [-q] cdnjs 'jquery*'         # search libraries\n  $ \#{script} [-q] cdnjs jquery            # list versions\n  $ \#{script} [-q] cdnjs jquery latest     # show latest version\n  $ \#{script} [-q] cdnjs jquery 2.2.0      # list files\n  $ mkdir -p static/lib                    # create a directory\n  $ \#{script} [-q] cdnjs jquery 2.2.0 static/lib  # download files\n  static/lib/jquery/2.2.0/jquery.js ... Done (258,388 byte)\n  static/lib/jquery/2.2.0/jquery.min.js ... Done (85,589 byte)\n  static/lib/jquery/2.2.0/jquery.min.map ... Done (129,544 byte)\n  $ ls static/lib/jquery/2.2.0\n  jquery.js       jquery.min.js   jquery.min.map\n\n"
end

#parse_cmdopts(cmdargs, short_opts, long_opts) ⇒ Object



729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
# File 'lib/cdnget.rb', line 729

def parse_cmdopts(cmdargs, short_opts, long_opts)
  cmdopts = {}
  while cmdargs[0] && cmdargs[0].start_with?('-')
    cmdarg = cmdargs.shift
    if cmdarg == '--'
      break
    elsif cmdarg.start_with?('--')
      cmdarg =~ /\A--(\w[-\w]+)(=.*?)?/  or
        raise CommandError.new("#{cmdarg}: invalid command option.")
      name  = $1
      value = $2 ? $2[1..-1] : true
      long_opts.include?(name)  or
        raise CommandError.new("#{cmdarg}: unknown command option.")
      cmdopts[name] = value
    elsif cmdarg.start_with?('-')
      cmdarg[1..-1].each_char do |c|
        short_opts.include?(c)  or
          raise CommandError.new("-#{c}: unknown command option.")
        cmdopts[c] = true
      end
    else
      raise "unreachable"
    end
  end
  return cmdopts
end

#render_list(list) ⇒ Object



762
763
764
765
# File 'lib/cdnget.rb', line 762

def render_list(list)
  return list.collect {|d| "#{d[:name]}\n" }.join() if @quiet
  return list.collect {|d| "%-20s  # %s\n" % [d[:name], d[:desc]] }.join()
end

#run(*args) ⇒ Object



700
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
# File 'lib/cdnget.rb', line 700

def run(*args)
  cmdopts = parse_cmdopts(args, "hvq", ["help", "version", "quiet", "debug"])
  return help_message() if cmdopts['h'] || cmdopts['help']
  return RELEASE + "\n" if cmdopts['v'] || cmdopts['version']
  @quiet = cmdopts['quiet'] || cmdopts['q']
  @debug_mode = cmdopts['debug']
  case args.length
  when 0
    return do_list_cdns()
  when 1
    cdn_code = args[0]
    return do_list_libraries(cdn_code)
  when 2
    cdn_code, library = args
    return library.include?('*') \
           ? do_search_libraries(cdn_code, library) \
           : do_find_library(cdn_code, library)
  when 3
    cdn_code, library, version = args
    return do_get_library(cdn_code, library, version)
  when 4
    cdn_code, library, version, basedir = args
    do_download_library(cdn_code, library, version, basedir)
    return ""
  else
    raise CommandError.new("'#{args[4]}': Too many arguments.")
  end
end