Show
Ignore:
Timestamp:
01/11/08 14:34:33 (5 years ago)
Author:
cho45
Message:

lang/ruby/userscripts_org/trunk/bin/userjs:

Implement install subcommand.
This is from http://svn.coderepos.org/share/lang/ruby/misc/linkuserjs.rb

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/userscripts_org/trunk/bin/userjs

    r4419 r4420  
    4949                                EOB 
    5050                        }, 
     51 
     52                        "install" => OptionParser.new { |opts| 
     53                                opts.banner = <<-EOB.gsub(/^\t+/, "") 
     54                                        Usage: #{NAME} install [opts] <filename> 
     55 
     56                                        Install <filename> script. 
     57                                EOB 
     58 
     59                                parser.separator "" 
     60 
     61                                parser.separator "Options:" 
     62                                parser.on('-l', '--symlink', "Replace installed file with symlink.") do 
     63                                        @symlink = true 
     64                                end 
     65                        }, 
     66 
    5167                } 
    5268 
     
    133149        end 
    134150 
     151        def cmd_install 
     152                abort @subparsers[@subcommand].help if @argv.empty? 
     153                file = Pathname.new(@argv.first).realpath 
     154                prev = scan 
     155                install_userjs(file) 
     156                installed = catch(:break) { 
     157                        loop do 
     158                                scan.each do |f,time| 
     159                                        if prev.key?(f) 
     160                                                throw :break, f unless prev[f] == time  
     161                                        else 
     162                                                throw :break, f 
     163                                        end 
     164                                end 
     165                                sleep 1 
     166                        end 
     167                } 
     168                puts "#{file} was installed as #{installed}." 
     169                puts "#{installed} will be replaced with symlink to #{file}." 
     170 
     171                FileUtils.symlink(file, installed, :force => true) 
     172                puts "done" 
     173        end 
     174 
    135175 
    136176        private 
     
    142182                })).login 
    143183        end 
     184 
     185        def profile_dir 
     186                home = Pathname.new ENV["HOME"] 
     187                home + "Library/Application Support/Firefox/Profiles" 
     188        end 
     189 
     190        def scan 
     191                Pathname.glob(profile_dir + "*/gm_scripts/**/*.user.js").inject({}) {|r,i| 
     192                        r[i] = i.mtime 
     193                        r 
     194                } 
     195        end 
     196 
     197        def install_userjs(path) 
     198                system("open", "-a", "Firefox", path.to_s) 
     199                puts "Complete the install on Firefox" 
     200        end 
    144201end 
    145202