Skip to content

Commit

Permalink
Fix bad files path in the local repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi Durieu committed Oct 12, 2020
1 parent d00fde1 commit d0e9505
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
20 changes: 16 additions & 4 deletions bin/dotrs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
require 'git'
require 'optparse'
require_relative '../lib/master_tree'
require_relative '../lib/version.rb'

EXIT_SUCCESS = 0
EXIT_FAILURE = 1

VERSION = '2.0.0'
REPO_PATH = File.join(Dir.home, '.dotfiles')

# Set up the options
Expand All @@ -29,7 +29,7 @@ optparse = OptionParser.new do |opts|
exit(EXIT_SUCCESS)
end
opts.on('--version', 'Display version number and exit') do
puts "dotrs #{VERSION}"
puts "dotrs #{Dotrs::VERSION}"
exit(EXIT_SUCCESS)
end
end
Expand All @@ -50,8 +50,10 @@ if ARGV.empty?
exit(EXIT_FAILURE)
end

# Get the command given by the user
cmd = ARGV[0].to_sym

# Handle the init command
if cmd == :init
begin
Git.clone(ARGV[1], REPO_PATH)
Expand All @@ -62,18 +64,28 @@ if cmd == :init
exit(EXIT_SUCCESS)
end

# Exit if REPO_PATH doesn't exist
unless Dir.exist?(REPO_PATH)
puts('dotrs: not initialized')
puts('Type `dotrs --help` for a list of available commands.')
exit(EXIT_FAILURE)
end

# Handle other commands
begin
mt = MasterTree.new(REPO_PATH)
mt = MasterTree.new(REPO_PATH, Dir.home)
repo = Git.open(REPO_PATH)
case cmd
when :add
ARGV[1..-1].each { |file| mt.add(file) }
ARGV[1..-1].each do |file|
begin
mt.add(file)
rescue AssertionError
puts("dotrs: invalid file '#{file}'.")
puts('The file can\'t be a symbolic link.')
exit(EXIT_FAILURE)
end
end
when :apply
mt.link_all
when :list
Expand Down
4 changes: 3 additions & 1 deletion dotrs.gemspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

require_relative 'lib/version.rb'

Gem::Specification.new do |spec|
spec.name = 'dotrs'
spec.version = '2.0.0'
spec.version = Dotrs::VERSION
spec.date = '2020-10-10'
spec.summary = 'Straighforward dotfiles management'
spec.description = 'Straighforward dotfiles management'
Expand Down
29 changes: 18 additions & 11 deletions lib/master_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
# If a file's original path is "/foo/bar/file" the new path of the same file,
# after being added to a MasterTree is "mastertree_root_directory/foo/bar/file".
#
# By default, the depth of path's reproduction is the root '/' directory; which
# means that the full path to the file is recreated in the MasterTree. One can
# specify the depth of path when creating a new MasterTree. The path of an added
# file will be reproduced only until the given directory is reached.
#
# In the following documentation, the terms "real" and "virtual" will be used
# to describe, respectively, the original path of a file, and its path once
# added in a MasterTree.
Expand All @@ -31,19 +36,22 @@
# # |-bar/
# # |-file
class MasterTree
# Internal: Returns the String asbolute path of the MasterTree.
attr_reader :path

# Internal: Initialize a new MasterTree whose root directory is the given
# empty directory.
#
# dir_name - The String root directory name for the new MasterTree. The
# directory must already exist. This parameter must not be null.
def initialize(dir_name)
# max_depth - The String directory name for the maximum depth each file's path
# will be reproduced. The directory must already exist. This
# paramater must not be null.
def initialize(dir_name, max_depth = '/')
Contract.check(!dir_name.nil? && Dir.exist?(dir_name),
"invalid directory: #{dir_name}")
Contract.check(!max_depth.nil? && Dir.exist?(max_depth),
"invalid maximum depth: #{max_depth}")

@path = File.absolute_path(dir_name)
@max_depth = max_depth
end

# Internal: Add a file to the MasterTree.
Expand Down Expand Up @@ -100,7 +108,10 @@ def remove(file_name)
# Returns an Array the true String paths of all the files in the MasterTree.
def list
result = []
each_child_rec(@path) { |file| result << file.delete_prefix(@path) }
each_child_rec(@path) do |file|
file_name = File.join(Dir.home, file.delete_prefix(@path))
result << file_name
end
result
end

Expand All @@ -124,7 +135,8 @@ def link_all
#
# Returns the String virtual path.
def virtual_path(real_path)
File.join(@path, File.absolute_path(real_path))
File.join(@path,
File.absolute_path(real_path).delete_prefix("#{@max_depth}/"))
end

# Internal: Get the real path in the file system corresponding to a virtual
Expand All @@ -138,11 +150,6 @@ def real_path(virtual_path)
end

def remove_empty_dirs(dir_name)
# mt_parent = File.dirname(virtual_path(file_name))
# while Dir.empty?(mt_parent)
# FileUtils.rm_r(mt_parent) if Dir.exist?(mt_parent)
# mt_parent = File.dirname(mt_parent)
# end
Dir.each_child(dir_name) do |entry|
next unless File.directory?(entry)

Expand Down
6 changes: 6 additions & 0 deletions lib/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

# The version of dotrs
module Dotrs
VERSION = '2.0.1'
end
15 changes: 7 additions & 8 deletions test/tc_master_tree.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# frozen_string_literal: true

$LOAD_PATH << '.'
$LOAD_PATH << '../lib/'

require 'test/unit'
require 'fileutils'
require 'master_tree'
require 'assertion_error'
require 'test_environment'
require_relative '../lib/master_tree'
require_relative '../lib/assertion_error'
require_relative 'test_environment'

# Internal: Unit test for the MasterTree class.
class TestMasterTree < Test::Unit::TestCase
Expand All @@ -24,6 +21,8 @@ def teardown
def test_pre_new
assert_raise(AssertionError) { MasterTree.new(nil) }
assert_raise(AssertionError) { MasterTree.new('invalid/directory') }
assert_raise(AssertionError) { MasterTree.new(MT_DIR, nil) }
assert_raise(AssertionError) { MasterTree.new(MT_DIR, 'invalid/directory') }
end

def test_new
Expand Down Expand Up @@ -54,7 +53,7 @@ def test_post_add
def test_pre_remove
mt = MasterTree.new(MT_DIR)
assert_raise(AssertionError) { mt.remove(nil) }
assert_raise(AssertionError) { mt.remove('inalid_file') }
assert_raise(AssertionError) { mt.remove('invalid_file') }
assert_raise(AssertionError) { mt.remove(FILES[:file_sa]) }
assert_raise(AssertionError) { mt.remove(FILES[:link_sa]) }
assert_raise(AssertionError) { mt.remove(FILES[:file_mt]) }
Expand All @@ -78,6 +77,6 @@ def test_list
list = mt.list
assert_not_nil(list)
assert_instance_of(Array, list)
assert(list.each { |file| !file.include?(MT_DIR) })
assert(list.none? { |file| file.include?(MT_DIR) })
end
end

0 comments on commit d0e9505

Please sign in to comment.