public
Description: An MLB.com plugin for Plex Media Server
Homepage:
Clone URL: git://github.com/rfletcher/plex-mlb.git
Click here to lend your support to: plex-mlb and make a donation at www.pledgie.com !
plex-mlb / Rakefile
100644 171 lines (143 sloc) 5.248 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
118
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
163
164
165
166
167
168
169
170
171
require 'rake'
require 'rake/clean'
require 'rake/packagetask'
require "tempfile"
require 'yaml'
 
# base paths
PLEXMLB_ROOT = File.expand_path( File.dirname( __FILE__ ) )
PLEXMLB_SRC_DIR = File.join( PLEXMLB_ROOT, 'src' )
PLEXMLB_LIB_DIR = File.join( PLEXMLB_ROOT, 'lib' )
PLEXMLB_DIST_DIR = File.join( PLEXMLB_ROOT, 'dist' )
 
# paths used by the :install task
PLEX_SUPPORT_DIR = File.expand_path( '~/Library/Application Support/Plex Media Server' )
PLEX_PLUGIN_DIR = File.join( PLEX_SUPPORT_DIR, 'Plug-ins' )
PLEX_SITE_CONFIG_DIR = File.join( PLEX_SUPPORT_DIR, 'Site Configurations' )
PLEX_PLUGIN_DATA_DIR = File.join( PLEX_SUPPORT_DIR, 'Plug-in Support' )
 
class File
  def self.binary?(name)
    fstat = stat(name)
    if !fstat.file?
      false
    else
      open(name) do |file|
        blk = file.read(fstat.blksize)
        blk.size == 0 || blk.count("^ -~", "^\r\n") / blk.size > 0.3 || blk.count("\x00") > 0
      end
    end
  end
end
 
def bundle_name config
  ( config['PLUGIN_BUNDLE_NAME'] ? config['PLUGIN_BUNDLE_NAME'] : config['PLUGIN_NAME'] ) + '.bundle'
end
 
def erb config, file
  temp = Tempfile.new( "erb" )
  File.open( file ).each_line do |line|
    temp << line.gsub(/<%=(.*?)%>/) do
      prop = $1.strip
      if value = config[prop]
        value
      else
        raise "couldn't find property `#{prop}' (in #{file})"
      end
    end
  end
  temp.close
 
  mv( temp.path, file, :verbose => false )
end
 
def load_config env=:release
  YAML.load_file( File.join( PLEXMLB_SRC_DIR, 'config.yml' ) )[env.to_s]
end
 
def rm_if_exists file
  rm_rf file if File.exists? file
end
 
def site_config_name config
  config['PLUGIN_ID'].split('.').last + '.xml'
end
 
config = load_config
 
PLEXMLB_PACKAGE_NAME = "#{config['PLUGIN_NAME']}-#{config['PLUGIN_VERSION']}".gsub " ", "_"
 
# files to blow away with a `rake clobber`
CLOBBER.include( PLEXMLB_DIST_DIR, "#{PLEXMLB_PACKAGE_NAME}.tar.gz" )
 
task :default => :dist
 
namespace :dist do
  desc 'Build a dev distribution.'
  task :development do
    config = load_config :development
    Rake::Task["dist:release"].execute
    touch File.join( PLEXMLB_DIST_DIR, bundle_name( config ), "development" )
  end
 
  desc 'Build a release distribution.'
  task :release do
    rm_if_exists File.join( PLEXMLB_DIST_DIR )
    bundle_dest = File.join( PLEXMLB_DIST_DIR, bundle_name( config ) )
    mkdir_p( bundle_dest )
    cp_r File.join( PLEXMLB_SRC_DIR, 'bundle' ), File.join( bundle_dest, 'Contents' )
    cp_r File.join( PLEXMLB_SRC_DIR, 'site configuration.xml' ), File.join( PLEXMLB_DIST_DIR, site_config_name( config ) )
    cp_r File.join( PLEXMLB_SRC_DIR, 'config.yml' ), File.join( bundle_dest, 'Contents', 'Code' )
    cp_r File.join( PLEXMLB_ROOT, 'README.rst' ), PLEXMLB_DIST_DIR
 
    # process files with erb
    FileList[ File.join( PLEXMLB_DIST_DIR, '**', '*' ) ].exclude().each do |file|
      erb config, file unless ( File.directory?( file ) || File.binary?( file ) )
    end
 
    cp_r PLEXMLB_LIB_DIR, File.join( bundle_dest, 'Contents', 'Libraries' )
  end
end
desc 'Alias for dist:release'
task :dist => 'dist:release'
 
desc 'Create a tarball, suitable for distribution'
task :package => 'dist:release' do
  Dir.chdir PLEXMLB_DIST_DIR do
    contents = Dir.glob( "*" )
    mkdir_p PLEXMLB_PACKAGE_NAME
    mv contents, PLEXMLB_PACKAGE_NAME
    system "tar czf #{PLEXMLB_PACKAGE_NAME}.tar.gz #{PLEXMLB_PACKAGE_NAME}"
    mv "#{PLEXMLB_PACKAGE_NAME}.tar.gz", PLEXMLB_ROOT
  end
end
 
namespace :install do
  desc 'Install a development version of the plugin'
  task :development => [ 'dist:development', :install ]
 
  desc 'Install a release version of the plugin'
  task :release => [ 'dist:release', :install ]
 
  task :install => :uninstall do
    cp_r File.join( PLEXMLB_DIST_DIR, bundle_name( config ) ), File.join( PLEX_PLUGIN_DIR, bundle_name( config ) )
    cp_r File.join( PLEXMLB_DIST_DIR, site_config_name( config ) ), File.join( PLEX_SITE_CONFIG_DIR, site_config_name( config ) )
  end
end
desc 'Alias for install:development'
task :install => 'install:development'
 
namespace :uninstall do
  desc 'Remove the installed bundle, but leave data behind.'
  task :soft do
    rm_if_exists File.join( PLEX_PLUGIN_DIR, bundle_name( config ) )
    rm_if_exists File.join( PLEX_SITE_CONFIG_DIR, site_config_name( config ) )
  end
 
  desc 'Remove the installed bundle and data.'
  task :hard => :soft do
    files = FileList[
      File.join( PLEX_PLUGIN_DATA_DIR, "*", "#{config['PLUGIN_ID']}.*" ),
      File.join( PLEX_PLUGIN_DATA_DIR, "*", "#{config['PLUGIN_ID']}" ),
    ]
    rm_rf files unless files.empty?
  end
end
desc 'Alias for uninstall:soft'
task :uninstall => 'uninstall:soft'
 
namespace :tail do
  logs = {
    :plex => [ 'Plex', File.expand_path( "~/Library/Logs/Plex.log" ) ],
    :plugin => [ 'the plugin', File.expand_path( "~/Library/Logs/PMS Plugin Logs/#{config['PLUGIN_ID']}.log" ) ]
  }
 
  def tail logs
    system "tail -f " << logs.collect { |log| "\"#{log}\"" }.join(' ')
  end
 
  logs.each do |k,v|
    desc "Tail #{v[0]}'s log file"
    task( k ) { tail [v[1]] }
  end
 
  desc 'Tail log files'
  task :all do
    tail logs.collect { |k,v| v[1] }
  end
end
desc 'Alias for tail:all'
task :tail => 'tail:all'