Skip to content

Commit

Permalink
Add launchpad module and reorganize folders
Browse files Browse the repository at this point in the history
  • Loading branch information
lexun committed Jan 24, 2015
1 parent 2b7e699 commit 91b0c7a
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 96 deletions.
2 changes: 1 addition & 1 deletion lib/launchpad.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require 'launchpad/settings'
require 'launchpad/index'
require 'launchpad/window'
require 'launchpad/gui/application'
14 changes: 14 additions & 0 deletions lib/launchpad/gui/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'jrubyfx'

module Launchpad
# Main application which is only created once at launch
class Application < JRubyFX::Application
# Automatically called on {.launch}
def start(stage)
stage.title = 'Launchpad'
stage.width = 800
stage.height = 600
stage.show
end
end
end
Empty file.
Empty file added lib/launchpad/gui/fxml/.keep
Empty file.
138 changes: 70 additions & 68 deletions lib/launchpad/index.rb
Original file line number Diff line number Diff line change
@@ -1,90 +1,92 @@
require 'digest'
require 'open-uri'

# Provides a diff between local and remote file data by maintaining a cache
# with pathnames and md5 hex digests.
class Index
# @param [Hash] options
# @option options [String] :installation_path root installation directory.
# @option options [String] :local_index_path where the local index is cached.
# @option options [String] :remote_index_uri uri for the remote index.
def initialize(options = {})
@target_dir = Pathname.new options[:target_dir] || Config.installation_path
@local_index_path = options[:local_index_path] || Config.local_index_path
@remote_index_uri = options[:remote_index_uri] || Config.remote_index_uri
end
module Launchpad
# Provides a diff between local and remote file data by maintaining a cache
# with pathnames and md5 hex digests.
class Index
# @param [Hash] options
# @option options [String] :installation_path root installation directory.
# @option options [String] :local_index_path where the local index is saved.
# @option options [String] :remote_index_uri uri for the remote index.
def initialize(options = {})
@target_dir = Pathname.new options[:target_dir]
@local_index_path = options[:local_index_path]
@remote_index_uri = options[:remote_index_uri]
end

# @return [Array<String>]
# an array of files that are not present or do not match the remote.
def diff
(remote - local).map(&:first)
end
# @return [Array<String>]
# an array of files that are not present or do not match the remote.
def diff
(remote - local).map(&:first)
end

# @return [Array<Array<Pathname, String>>]
# an array of pathnames and their md5 digests for the local install.
def local
@local ||= parse_local
end
# @return [Array<Array<Pathname, String>>]
# an array of pathnames and their md5 digests for the local install.
def local
@local ||= parse_local
end

# @return [Array<Array<Pathname, String>>]
# an array of remote pathnames and their md5 digests from the patcher.
def remote
@remote ||= parse_remote
end
# @return [Array<Array<Pathname, String>>]
# an array of remote pathnames and their md5 digests from the patcher.
def remote
@remote ||= parse_remote
end

# Updates {#local} with a full scan of the installation directory.
# @return [self]
def scan
@local = []
recursive_scan
@local = sort local
save
end
# Updates {#local} with a full scan of the installation directory.
# @return [self]
def scan
@local = []
recursive_scan
@local = sort local
save
end

private
private

def parse(file)
file.read.split("\n").map do |line|
line.split(' | ').tap do |path, md5|
[Pathname.new(path), md5.chomp]
def parse(file)
file.read.split("\n").map do |line|
line.split(' | ').tap do |path, md5|
[Pathname.new(path), md5.chomp]
end
end
end
end

def parse_local
sort File.open(@local_index_path, 'r') { |file| parse file }
rescue Errno::ENOENT
[]
end
def parse_local
sort File.open(@local_index_path, 'r') { |file| parse file }
rescue Errno::ENOENT
[]
end

def parse_remote
sort Kernel.open(@remote_index_uri) { |file| parse file }
rescue Errno::ENOENT
[]
end
def parse_remote
sort Kernel.open(@remote_index_uri) { |file| parse file }
rescue Errno::ENOENT
[]
end

def recursive_scan(target = @target_dir)
target.each_child do |pathname|
if pathname.file?
@local << [pathname.to_s, Digest::MD5.file(pathname).to_s]
elsif pathname.directory?
recursive_scan pathname
def recursive_scan(target = @target_dir)
target.each_child do |pathname|
if pathname.file?
@local << [pathname.to_s, Digest::MD5.file(pathname).to_s]
elsif pathname.directory?
recursive_scan pathname
end
end
end
end

def save
file = File.open(@local_index_path, 'w')
def save
file = File.open(@local_index_path, 'w')

local.each do |data|
file.write "#{data[0]} | #{data[1]}\n"
end
local.each do |data|
file.write "#{data[0]} | #{data[1]}\n"
end

file.close
self
end
file.close
self
end

def sort(list)
list.empty? ? list : list.sort_by(&:first)
def sort(list)
list.empty? ? list : list.sort_by(&:first)
end
end
end
24 changes: 13 additions & 11 deletions lib/launchpad/settings.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# Provides default settings and allows user customization through creation of a
# yaml file for overrides
class Settings
DEFAULT = {
local_index_path: 'index',
remote_index_uri: 'http://patcher.cuemu.com/index'
}
module Launchpad
# Provides default settings and allows user customization through creation of
# a yaml file for overrides
class Settings
DEFAULT = {
local_index_path: 'index',
remote_index_uri: 'http://patcher.cuemu.com/index'
}

class << self
DEFAULT.each_key do |key|
define_method(key) do
DEFAULT[key]
class << self
DEFAULT.each_key do |key|
define_method(key) do
DEFAULT[key]
end
end
end
end
Expand Down
12 changes: 0 additions & 12 deletions lib/launchpad/window.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe Window do
describe Launchpad::Application do
describe '#start' do
let(:stage) { double 'stage' }

Expand Down
4 changes: 2 additions & 2 deletions spec/lib/launchpad/index_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'spec_helper'

describe Index do
describe Launchpad::Index do
subject { described_class.new args }

let(:target_dir) { Pathname.new 'spec/fixtures/test_dir' }
let(:target_dir) { 'spec/fixtures/test_dir' }
let(:local_index_path) { 'spec/fixtures/indexes/local' }
let(:remote_index_uri) { 'https://patch.cuemu.com/index' }

Expand Down
2 changes: 1 addition & 1 deletion spec/lib/launchpad/settings_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe Settings do
describe Launchpad::Settings do
describe 'accessing default values' do
describe '.local_index_path' do
specify { expect(described_class.local_index_path).to eq 'index' }
Expand Down

0 comments on commit 91b0c7a

Please sign in to comment.