Class: ChromeSSB

Inherits:
Object
  • Object
show all
Defined in:
lib/chrome_ssb.rb,
lib/chrome_ssb/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ChromeSSB

Returns a new instance of ChromeSSB.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/chrome_ssb.rb', line 11

def initialize(args={})
  @compatible = !(/darwin/ =~ RUBY_PLATFORM).nil?

  ## Require Mac OS X for SSB .app structure
  raise '`chrome_ssb` is only compatible with Mac OS X' unless @compatible

  ## Set Default Values
  self.name = 'Untitled SSB'
  self.location = 'http://www.google.com/'
  self.icon = '.'
  self.path = '.'
  self.chrome_path = `mdfind "kMDItemCFBundleIdentifier == 'com.google.Chrome'" | head -n 1`.strip
  self.overwrite = false

  ## Process passed args
  args.each { |k,v| send("#{k}=",v) if @params.include? k.to_sym }
end

Class Method Details

.compatible?Boolean

System Compatible

Returns:

  • (Boolean)


143
144
145
# File 'lib/chrome_ssb.rb', line 143

def self.compatible?
  !!@compatible
end

.create(args = {}) ⇒ Object

Static Create Shortcut



138
139
140
# File 'lib/chrome_ssb.rb', line 138

def self.create(args={})
  ChromeSSB.new(args).create!
end

Instance Method Details

#cbObject

Path Expansion Setters



125
126
127
128
129
130
131
# File 'lib/chrome_ssb.rb', line 125

%i(icon path chrome_path).each do |cb|
  class_eval <<-RUBY_EVAL, __FILE__, __LINE__+1
    def #{cb}=(#{cb})
      @#{cb} = File.expand_path #{cb}
    end
  RUBY_EVAL
end

#create!Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
113
114
115
116
117
# File 'lib/chrome_ssb.rb', line 35

def create!
  app_path = File.join(path, name+'.app')
  contents_path = File.join(app_path, 'Contents')
  chrome_link = File.join(contents_path, 'MacOS', 'Chrome')
  exec_path = File.join(contents_path, 'MacOS', name)
  icon_path = File.join(contents_path, 'Resources', 'icon.icns')
  locale_path = File.join(contents_path, 'Resources', 'en.lproj', 'InfoPlist.strings')
  plist_path = File.join(contents_path, 'Info.plist')
  versions_path = File.join(contents_path, 'Versions')

  chrome_executable = File.join(chrome_path, 'Contents', 'MacOS', 'Google Chrome')
  chrome_versions = File.join(chrome_path, 'Contents', 'Versions')

  ## Check that Google Chrome exists
  raise ArgumentError, "Google Chrome was not found at #{chrome_path}. Try again passing its path" unless File.file? chrome_executable

  ## Check if app exists
  if File.directory? app_path
    if overwrite?
      FileUtils.rm_rf app_path
    else
      raise ArgumentError, "App name is already taken"
    end
  end

  ## Create Initial Directories
  [File.dirname(locale_path), File.dirname(exec_path), File.join(contents_path,'Profile')].each do |dir|
    FileUtils.mkdir_p dir
  end

  ## Icon
  download_icon! unless File.file? icon
  if File.file? icon
    if (File.extname icon || '') == '.icns'
      FileUtils.cp icon, icon_path
    else
      system %(sips -s format tiff "#{icon}" --out "#{icon_path}.tiff" --resampleWidth 128 >& /dev/null)
      system %(tiff2icns -noLarge "#{icon_path}.tiff" >& /dev/null)
    end
  else
    raise ArgumentError, "Icon could not be found or downloaded for #{location}"
  end

  ## Chrome Symlink
  FileUtils.ln_s chrome_executable, chrome_link

  ## Chrome Versions
  FileUtils.ln_s chrome_versions, versions_path

  ## Chrome Wrapper
  IO.write exec_path, <<-WRAPPER.gsub(/^\s+/m, '')
    #!/bin/bash
    ABSPATH=\$(cd "\$(dirname "\$0")"; pwd)
    /bin/cp "\$ABSPATH/Chrome" "\$ABSPATH/#{name} Chrome"
    exec "\$ABSPATH/#{name} Chrome" --app="#{location}" --user-data-dir="\$ABSPATH/../Profile" "\$@"
  WRAPPER
  system %(/bin/chmod +x "#{exec_path}" >& /dev/null)

  ## Info.plist
  IO.write plist_path, <<-PLIST.gsub(/^\s+/m, '')
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
      <dict>
        <key>CFBundleExecutable</key>
          <string>#{name}</string>
        <key>CFBundleName</key>
          <string>#{name}</string>
        <key>CFBundleIconFile</key>
          <string>icon.icns</string>
        <key>NSHighResolutionCapable</key>
          <string>True</string>
      </dict>
    </plist>
  PLIST

  ## Localization
  IO.write locale_path, <<-LOCALE.gsub(/^\s+/m, '')
    CFBundleDisplayName = "#{name}";
    CFBundleName = "#{name}";
  LOCALE

end

#download_icon!Object



29
30
31
32
33
# File 'lib/chrome_ssb.rb', line 29

def download_icon!
  tmpfile = Tempfile.new('icon')
  system %(curl -o "#{tmpfile.path}" "http://g.etfv.co/#{location}" >& /dev/null)
  self.icon = tmpfile.path
end

#name=(name) ⇒ Object

Name Filtered Setter



120
121
122
# File 'lib/chrome_ssb.rb', line 120

def name=(name)
  @name = name.gsub /[^\w\-\.\s]/, ''
end

#overwrite?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/chrome_ssb.rb', line 133

def overwrite?
  !!overwrite
end