From 6e72abba12adff352991456630b5859aafeecec0 Mon Sep 17 00:00:00 2001 From: Kelsey Judson Date: Fri, 19 Oct 2012 02:57:25 +1300 Subject: [PATCH] One more structural change (to GroupParser). --- lib/dotfile.rb | 6 +- lib/dotfile/configuration/group_parser.rb | 140 ++++++++++++++++++++++ lib/dotfile/group_parser.rb | 135 --------------------- spec/dotfile_group_parser_spec.rb | 10 +- spec/spec_helper.rb | 2 + 5 files changed, 151 insertions(+), 142 deletions(-) create mode 100644 lib/dotfile/configuration/group_parser.rb delete mode 100644 lib/dotfile/group_parser.rb diff --git a/lib/dotfile.rb b/lib/dotfile.rb index d7c1ec5..0136011 100644 --- a/lib/dotfile.rb +++ b/lib/dotfile.rb @@ -1,10 +1,10 @@ require 'dotfile/version' -require 'dotfile/configuration' -require 'dotfile/configuration/settings' -require 'dotfile/group_parser' require 'dotfile/base' require 'dotfile/static' require 'dotfile/template' +require 'dotfile/configuration' +require 'dotfile/configuration/settings' +require 'dotfile/configuration/group_parser' module Dotfile diff --git a/lib/dotfile/configuration/group_parser.rb b/lib/dotfile/configuration/group_parser.rb new file mode 100644 index 0000000..b1fdbfd --- /dev/null +++ b/lib/dotfile/configuration/group_parser.rb @@ -0,0 +1,140 @@ +# Parses the groups.conf file and returns definitions (as a hash) for each +# dotfile found in enabled groups. + +module Dotfile + class Configuration + class GroupParser + + attr_reader :config_file, :groups, :current_group, :dotfiles + + def initialize(config_file = Dotfile::GROUPS, + dotfile_path = Dotfile::DOTFILES, + groups = :all, + test = nil) + @config_file = File.new(config_file, 'r') + @dotfile_path = dotfile_path + @groups = groups + @current_group = '' + @dotfiles = [] + + parse unless test + end + + def parse + parse_file + convert_directories + end + + ### Parsing a line + + def parse_line(line) + return if ignore_line?(line) + line = strip_excess(line) + return if is_group_name?(line) + if included_group? + line = split_line(line) + line = build_paths(line) + { group: @current_group, + source: line[0], + destination: File.expand_path(line[1]) + } + end + end + + def ignore_line?(line) + return true if line.strip == '' + return true if line.strip =~ /^#/ + end + + def strip_excess(line) + line.split('#')[0].strip + end + + def is_group_name?(line) + if line =~ /^\[\w+\]$/ + @current_group = line.gsub(/\[|\]/, '') + return true + end + end + + def included_group? + return true if @groups == :all + @groups.include?(@current_group) + end + + def split_line(line) + line.split(/\s*,\s*/) + end + + def build_paths(line) + source = "#{@dotfile_path}/#{@current_group}/" + line[0] + destination = File.expand_path(line[1]) + [source, destination] + end + + ### Parsing a file + + def parse_file + @config_file.readlines.each do |line| + dotfile = parse_line(line) + @dotfiles << dotfile if dotfile + end + + @config_file.close + end + + ### Handling Directories + + def convert_directories + add_directory_contents + remove_directories + end + + def add_directory_contents + get_directories.each do |dir| + find_dotfiles(dir).each do |dotfile| + @dotfiles << dotfile + end + end + end + + def get_directories + @dotfiles.select do |dotfile| + File.directory?(dotfile[:source]) + end + end + + def find_dotfiles(directory) + dir_contents = add_recursively(directory[:source]) + + dir_contents.map! do |path| + # Destination for found templates is not explicitly set in groups.conf, + # so it is necessary to remove the suffix for template files here. + destination = path =~ /\.template$/ ? path.sub(/\.template$/, '') : path + + { group: directory[:group], + source: directory[:source] + '/' + path, + destination: directory[:destination] + '/' + destination + } + end + + dir_contents + end + + def add_recursively(path) + dir_contents = [] + + Dir.glob("#{path}/**/*") do |f| + dir_contents << f.sub(/^#{path}\//, '') unless File.directory?(f) + end + + dir_contents + end + + def remove_directories + @dotfiles -= get_directories + end + + end + end +end diff --git a/lib/dotfile/group_parser.rb b/lib/dotfile/group_parser.rb deleted file mode 100644 index de09fb9..0000000 --- a/lib/dotfile/group_parser.rb +++ /dev/null @@ -1,135 +0,0 @@ -module Dotfile - class GroupParser - - attr_reader :config_file, :groups, :current_group, :dotfiles - - def initialize(config_file = Dotfile::GROUPS, - dotfile_path = Dotfile::DOTFILES, - groups = :all, - test = nil) - @config_file = File.new(config_file, 'r') - @dotfile_path = dotfile_path - @groups = groups - @current_group = '' - @dotfiles = [] - - parse unless test - end - - def parse - parse_file - convert_directories - end - - ### Parsing a line - - def parse_line(line) - return if ignore_line?(line) - line = strip_excess(line) - return if is_group_name?(line) - if included_group? - line = split_line(line) - line = build_paths(line) - { group: @current_group, - source: line[0], - destination: File.expand_path(line[1]) - } - end - end - - def ignore_line?(line) - return true if line.strip == '' - return true if line.strip =~ /^#/ - end - - def strip_excess(line) - line.split('#')[0].strip - end - - def is_group_name?(line) - if line =~ /^\[\w+\]$/ - @current_group = line.gsub(/\[|\]/, '') - return true - end - end - - def included_group? - return true if @groups == :all - @groups.include?(@current_group) - end - - def split_line(line) - line.split(/\s*,\s*/) - end - - def build_paths(line) - source = "#{@dotfile_path}/#{@current_group}/" + line[0] - destination = File.expand_path(line[1]) - [source, destination] - end - - ### Parsing a file - - def parse_file - @config_file.readlines.each do |line| - dotfile = parse_line(line) - @dotfiles << dotfile if dotfile - end - - @config_file.close - end - - ### Handling Directories - - def convert_directories - add_directory_contents - remove_directories - end - - def add_directory_contents - get_directories.each do |dir| - find_dotfiles(dir).each do |dotfile| - @dotfiles << dotfile - end - end - end - - def get_directories - @dotfiles.select do |dotfile| - File.directory?(dotfile[:source]) - end - end - - def find_dotfiles(directory) - dir_contents = add_recursively(directory[:source]) - - dir_contents.map! do |path| - # Destination for found templates is not explicitly set in groups.conf, - # so it is necessary to remove the suffix for template files here. - destination = path =~ /\.template$/ ? path.sub(/\.template$/, '') : path - - { group: directory[:group], - source: directory[:source] + '/' + path, - destination: directory[:destination] + '/' + destination - } - end - - dir_contents - end - - def add_recursively(path) - dir_contents = [] - - Dir.glob("#{path}/**/*") do |f| - dir_contents << f.sub(/^#{path}\//, '') unless File.directory?(f) - end - - dir_contents - end - - def remove_directories - @dotfiles -= get_directories - end - - end -end diff --git a/spec/dotfile_group_parser_spec.rb b/spec/dotfile_group_parser_spec.rb index f913d0b..f79bffc 100644 --- a/spec/dotfile_group_parser_spec.rb +++ b/spec/dotfile_group_parser_spec.rb @@ -1,12 +1,14 @@ -require 'spec_helper.rb' -require 'dotfile/group_parser.rb' +require 'spec_helper' +require 'dotfile/configuration/group_parser' -describe Dotfile::GroupParser do +klass = Dotfile::Configuration::GroupParser + +describe klass do let(:config_file) { 'spec/examples/groups.conf' } let(:dotfile_path) { 'spec/examples/dotfiles' } let(:included) { %w{ vim zsh xorg } } - let(:group) { Dotfile::GroupParser.new(config_file, dotfile_path, included, :test) } + let(:group) { klass.new(config_file, dotfile_path, included, :test) } it 'reads examples/groups.conf' do File.exists?(config_file).should be_true diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 03b6411..4b641c5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,2 +1,4 @@ module Dotfile + class Configuration + end end