homeconf 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0c665a0c79b5e74a56181634140175956bf93f51ff32df7ddb9aed81a7917890
4
+ data.tar.gz: 14e23e0a0b332f2afdf3d2d9c6db63cbe2336394fe4ede5349b2e90e1a98a781
5
+ SHA512:
6
+ metadata.gz: a6fa7e2c13a83c7aeddb0a01279dff85219c2f9fa5a4fbbe43d562d10b25b1e55d80a963b3d21522a54538aa5e500dda8a81ed8b7821aae24023c7a09753faed
7
+ data.tar.gz: d037c435518d1e6af58341887519bc7975c0e0f8b756637635b8d90bb733c8382415a723f51db59a78f3319d050434002301420c8f42d3f3e9402182659cffa7
data/HEADER ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2014-2023 Mark Lundquist
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/NOTICE ADDED
@@ -0,0 +1,2 @@
1
+ Homeconf
2
+ Copyright 2014-2023 Mark Lundquist
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # homeconf
2
+
3
+ **homeconf** is a Ruby utility to create and manage your home directory files and configuration in a single, portable,
4
+ version controllable directory.
5
+
6
+ ----------
7
+
8
+ ## Installation
9
+
10
+ **homeconf**'s installation is pretty standard:
11
+
12
+ ```sh
13
+ $ gem install homeconf
14
+ ```
15
+
16
+ If you'd rather install homeconf using `bundler`, add a line for it in your `Gemfile` (but set the `require` option
17
+ to `false`, as it is a standalone tool):
18
+
19
+ ```rb
20
+ gem 'homeconf', require: false
21
+ ```
22
+
23
+ ----------
24
+
25
+ ## Quickstart
26
+
27
+ Just type `homeconf` to see your homeconf directory and if it's initialized.
28
+
29
+ ```
30
+ $ cd
31
+ $ homeconf
32
+ ```
33
+
34
+ ### Create homeconf directory
35
+
36
+ Create your homeconf directory and start adding files and directories.
37
+
38
+ ```
39
+ $ cd
40
+ $ homeconf --create
41
+ ```
42
+
43
+ ### Initialize to create symlinks
44
+
45
+ You can add files and directories to your homeconf directory, then initialize to create symlinks from your home
46
+ directory.
47
+ ```
48
+ $ echo "echo 'hello world'" > ~/homeconf/hello.sh
49
+ $ mkdir ~/homeconf/my_scripts
50
+ $ homeconf --init
51
+ ```
52
+
53
+ ### Add files and directories
54
+ Move existing files and directories into homeconf directory. Homeconf will create the symlink from your home directory.
55
+
56
+ ```
57
+ $ homeconf --add .zshrc
58
+ $ homeconf --add bin
59
+ ```
60
+
61
+ ### See configuration
62
+ Run with verbose to see homeconf files and directories, and whether they're linked.
63
+ ```
64
+ $ homeconf --verbose
65
+ ```
data/bin/homeconf ADDED
@@ -0,0 +1,133 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('../lib', __dir__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+
7
+ require 'getoptlong'
8
+ require 'homeconf'
9
+
10
+ @this = File.basename $PROGRAM_NAME
11
+
12
+ def usage(retval = 0)
13
+ puts <<~USAGE
14
+ Usage: #{@this} [OPTION]...
15
+
16
+ Options:
17
+ -a, --add Add filepath. Moves filepath into homeconf, then creates a link to it.
18
+ -c, --create Create homeconf directory, if it doesn't exist.
19
+ -d, --directory Homeconf directory. Default: #{Homeconf::DEFAULT_DIRECTORY}
20
+ -i, --initialize Initialize homeconf. Creates links to dot files and directories from homeconf to home.
21
+
22
+ -h, --help show help
23
+ -v, --verbose verbose output
24
+ -V, --version show version
25
+ USAGE
26
+ exit retval
27
+ end
28
+
29
+ def print_version
30
+ header = File.realdirpath(File.join(File.dirname(__dir__), 'HEADER'))
31
+ puts <<~VERSION
32
+ #{@this} #{Homeconf::VERSION}
33
+ #{File.read(header)}
34
+ VERSION
35
+ exit 0
36
+ end
37
+
38
+ def error(msg)
39
+ puts "#{@this}: #{msg}"
40
+ end
41
+
42
+ def print_config(homeconf)
43
+ puts 'homeconf dirs:'
44
+ print_linked_table(homeconf, homeconf.list_homeconf_dirs)
45
+ puts 'homeconf files:'
46
+ print_linked_table(homeconf, homeconf.list_homeconf_files)
47
+ end
48
+
49
+ def print_linked_table(homeconf, filepaths)
50
+ longest = 1
51
+ filepaths.each do |filepath|
52
+ basename = File.basename(filepath)
53
+ length = basename.length
54
+ longest = length if length > longest
55
+ end
56
+
57
+ filepaths.each do |filepath|
58
+ basename = File.basename(filepath)
59
+ puts " #{basename}#{' ' * (longest - basename.length + 2)}#{homeconf.linked?(filepath) ? 'linked' : 'unlinked'}"
60
+ end
61
+ end
62
+
63
+ # Handle options
64
+ add_filepaths = Set.new
65
+ homeconf_dir = Homeconf::DEFAULT_DIRECTORY
66
+ create = false
67
+ init = false
68
+ verbose = false
69
+
70
+ begin
71
+ opts = GetoptLong.new(
72
+ ['--add', '-a', GetoptLong::REQUIRED_ARGUMENT],
73
+ ['--create', '-c', GetoptLong::NO_ARGUMENT],
74
+ ['--directory', '-d', GetoptLong::REQUIRED_ARGUMENT],
75
+ ['--initialize', '-i', GetoptLong::NO_ARGUMENT],
76
+ ['--help', '-h', GetoptLong::NO_ARGUMENT],
77
+ ['--verbose', '-v', GetoptLong::NO_ARGUMENT],
78
+ ['--version', '-V', GetoptLong::NO_ARGUMENT]
79
+ )
80
+ opts.quiet = true # suppress GetoptLong stderr messages
81
+
82
+ opts.each do |opt, arg|
83
+ case opt
84
+ when '--add'
85
+ add_filepaths.add arg
86
+ when '--create'
87
+ create = true
88
+ when '--directory'
89
+ homeconf_dir = arg
90
+ when '--initialize'
91
+ init = true
92
+ when '--help'
93
+ usage
94
+ exit 0
95
+ when '--verbose'
96
+ verbose = true
97
+ when '--version'
98
+ print_version
99
+ else
100
+ puts "other passed: #{opt}"
101
+ end
102
+ end
103
+ rescue GetoptLong::MissingArgument => e
104
+ error(e)
105
+ usage 1
106
+ rescue StandardError => e
107
+ puts "#{e.class}: #{e}" if verbose
108
+ error(e)
109
+ exit 1
110
+ end
111
+
112
+ begin
113
+ homeconf = Homeconf::Homeconf.new(homeconf_dir, verbose: verbose)
114
+ homeconf.create if create
115
+ add_filepaths.each do |add_filepath|
116
+ puts add_filepath
117
+ Dir.glob(add_filepath, File::FNM_DOTMATCH).reject {|f| f == '.' || f == '..' }.each do |filepath|
118
+ homeconf.add filepath
119
+ end
120
+ end
121
+ homeconf.init if init
122
+
123
+ if homeconf.initialized?
124
+ puts "#{@this} initialized. #{homeconf.directory}"
125
+ else
126
+ puts "#{@this} not initialized. #{homeconf.directory}"
127
+ end
128
+ print_config(homeconf) if verbose
129
+ rescue StandardError => e
130
+ puts "#{e.class}: #{e}" if verbose
131
+ error(e)
132
+ exit 1
133
+ end
@@ -0,0 +1,22 @@
1
+ # homeconf ignore file
2
+ # Files and directories in the homeconf directory matching an ignore pattern won't by symlinked from HOME
3
+ # File glob patterns are supported.
4
+ #
5
+ # Examples:
6
+ # .git
7
+ # private_dir
8
+ # *~
9
+ # *.iml
10
+ #
11
+ # vcs
12
+ .git
13
+ .svn
14
+ .hg
15
+ .cvs
16
+
17
+ # IntelliJ
18
+ *.iml
19
+ /.idea
20
+
21
+ # emacs
22
+ *~
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Homeconf
4
+ # {HomeconfDirNotFound} is raised to indicate that no {Homeconf} directory was not found
5
+ class HomeconfDirNotFound < StandardError
6
+ end
7
+
8
+ # {InvalidHomeconfDir} is raised to indicate that the homeconf directory is not a valid directory
9
+ class InvalidHomeconfDir < StandardError
10
+ end
11
+
12
+ # {FileExistsError} is raised to indicate that file or directory already exists
13
+ class FileExistsError < StandardError
14
+ end
15
+
16
+ # {FileNotFoundError} is raised to indicate that file or directory was not found
17
+ class FileNotFoundError < StandardError
18
+ end
19
+
20
+ # {SymlinkError} is raised to indicate that target file or directory is a symlink
21
+ class SymlinkError < StandardError
22
+ end
23
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+ require_relative 'errors'
5
+
6
+ module Homeconf
7
+ IGNORE_FILE = '.homeconfignore'
8
+
9
+ # Common methods for finding files.
10
+ # @api private
11
+ module FileFinder
12
+ def self.homeconf_dirs(homeconf_dir)
13
+ homeconf_dir = realpath(homeconf_dir)
14
+ ignores = ignores(homeconf_dir)
15
+ homeconf_dir.children
16
+ .select(&:directory?)
17
+ .reject { |p| ignores.include?(p.to_s) }
18
+ .map(&:to_s)
19
+ .sort
20
+ end
21
+
22
+ def self.homeconf_files(homeconf_dir)
23
+ homeconf_dir = realpath(homeconf_dir)
24
+ ignores = ignores(homeconf_dir)
25
+ homeconf_dir.children
26
+ .select(&:file?)
27
+ .reject { |p| ignores.include?(p.to_s) }
28
+ .map(&:to_s)
29
+ .sort
30
+ end
31
+
32
+ def self.ignores(filepath)
33
+ homeconf_dir = realpath(filepath)
34
+ ignore_file = homeconf_dir.join(IGNORE_FILE)
35
+ ignores = Set.new
36
+
37
+ return ignores.to_a unless File.file?(ignore_file) && File.readable?(ignore_file)
38
+
39
+ ignores.add ignore_file.to_s # implicitly ignore IGNORE_FILE
40
+
41
+ ignore_globs = File.readlines(ignore_file, chomp: true).grep_v(/^\s*$/).grep_v(/^\s*#/)
42
+ ignore_globs.each do |g|
43
+ Dir.glob(homeconf_dir.join(g).to_s, File::FNM_DOTMATCH).each { |f| ignores.add(f) }
44
+ end
45
+
46
+ ignores
47
+ end
48
+
49
+ def self.realpath(filepath)
50
+ begin
51
+ realpath = Pathname.new(filepath).expand_path.realpath
52
+ rescue Errno::ENOENT
53
+ raise InvalidHomeconfDir.new("Does not exist. #{filepath}")
54
+ rescue => e
55
+ raise e
56
+ end
57
+ raise InvalidHomeconfDir.new("Not a directory. #{filepath}") unless realpath.directory?
58
+ realpath
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,196 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require 'etc'
5
+ require 'fileutils'
6
+ require 'pathname'
7
+ require_relative 'errors'
8
+ require_relative 'file_finder'
9
+ require_relative 'pathname'
10
+
11
+ module Homeconf
12
+ DEFAULT_DIRECTORY_NAME = 'homeconf'
13
+ DEFAULT_DIRECTORY = File.join(Dir.home, DEFAULT_DIRECTORY_NAME).freeze
14
+ DEFAULT_HOMECONFIGNORE_FILE = File.join(__dir__,'default_homeconfignore')
15
+
16
+ class Homeconf
17
+ attr_reader :directory, :homedir
18
+
19
+ @verbose = false
20
+
21
+ def initialize(directory = DEFAULT_DIRECTORY, verbose: false)
22
+ @verbose = verbose
23
+ @homedir = Dir.home
24
+
25
+ # Parent directory must exist
26
+ @directory = File.realdirpath(File.expand_path(directory))
27
+
28
+ if @directory.eql? @homedir
29
+ raise InvalidHomeconfDir, "cannot use directory '#{@directory}': #{$USER} home directory cannot be used."
30
+ end
31
+ end
32
+
33
+ def create
34
+ mkdir_unless_exists @directory
35
+ ignore_file = File.join(@directory, IGNORE_FILE)
36
+ return if File.exist?(ignore_file)
37
+
38
+ description = File.read(DEFAULT_HOMECONFIGNORE_FILE)
39
+ File.write(ignore_file, description)
40
+ end
41
+
42
+ def add(filepath)
43
+ expand_path = File.expand_path(filepath)
44
+ raise FileNotFoundError, "cannot add '#{expand_path}': No such file or directory" unless File.exist?(expand_path)
45
+
46
+ if File.symlink?(expand_path)
47
+ raise SymlinkError,
48
+ "cannot add '#{expand_path}': It is a symlink, not a file or directory"
49
+ end
50
+
51
+ target = File.join(@directory, File.basename(expand_path))
52
+
53
+ raise FileExistsError, "cannot add '#{expand_path}', '#{target}' already exists" if File.exist?(target)
54
+
55
+ move(expand_path)
56
+ link(target)
57
+ end
58
+
59
+ def init
60
+ validate
61
+ unlinked_dirs.each do |f|
62
+ link(f)
63
+ end
64
+ unlinked_files.each do |f|
65
+ link(f)
66
+ end
67
+ end
68
+
69
+ def validate
70
+ validate_dir @directory
71
+ end
72
+
73
+ def create_directory(directory)
74
+ mkdir_unless_exists(File.realdirpath(directory))
75
+ end
76
+
77
+ # Returns the homeconf files that are not ignored and not symlinked from the home directory.
78
+ def unlinked_files
79
+ unlinked_files = []
80
+ FileFinder.homeconf_files(@directory).each do |file|
81
+ unlinked_files.push file unless linked? file
82
+ end
83
+ unlinked_files.sort
84
+ end
85
+
86
+ # Returns the homeconf directories that are not ignored and not symlinked from the home directory.
87
+ def unlinked_dirs
88
+ unlinked_dirs = []
89
+ FileFinder.homeconf_dirs(@directory).each do |dir|
90
+ unlinked_dirs.push dir unless linked? dir
91
+ end
92
+ unlinked_dirs.sort
93
+ end
94
+
95
+ # Returns whether the homeconf is initialized. Homeconf is initialized when all files and directories in the
96
+ # homeconf directory that are not ignored and symlinked from the home directory.
97
+ def initialized?
98
+ validate_dir @directory
99
+ unlinked_dirs.empty? && unlinked_files.empty?
100
+ rescue StandardError => e
101
+ puts "Error: #{e}" if @verbose
102
+ false
103
+ end
104
+
105
+ def validate_dir(directory)
106
+ raise HomeconfDirNotFound, "No such directory. #{directory}" unless File.exist? directory
107
+ raise InvalidHomeconfDir, "Not a directory. #{directory}" unless File.directory? directory
108
+ raise InvalidHomeconfDir, "Directory not writable. #{directory}" unless File.writable? directory
109
+ true
110
+ end
111
+
112
+ def list_homeconf_dirs
113
+ FileFinder.homeconf_dirs @directory
114
+ end
115
+
116
+ def list_homeconf_files
117
+ FileFinder.homeconf_files @directory
118
+ end
119
+
120
+ def linked?(filepath)
121
+ basename = File.basename(filepath)
122
+ link = File.join(Dir.home, basename)
123
+ return false unless File.symlink?(link)
124
+
125
+ expected_link_target = File.join(@directory, basename)
126
+ link_target = File.readlink(link)
127
+ link_target = File.join(@homedir, link_target) unless Pathname.new(link_target).absolute?
128
+
129
+ link_target = File.realpath(link_target)
130
+ expected_link_target.eql?(link_target)
131
+ end
132
+
133
+ def move(path)
134
+ expand_path = File.expand_path(path)
135
+ unless File.exist?(expand_path)
136
+ raise FileNotFoundError,
137
+ "cannot move '#{expand_path}', No such file or directory."
138
+ end
139
+ if File.symlink?(expand_path)
140
+ raise SymlinkError,
141
+ "cannot move '#{expand_path}': It is a symlink, not a file or directory"
142
+ end
143
+
144
+ target = File.join(@directory, File.basename(expand_path))
145
+ raise FileExistsError, "cannot move '#{target}', File or directory exists." if File.exist?(target)
146
+
147
+ FileUtils.mv(expand_path, target)
148
+ end
149
+
150
+ def link(filepath)
151
+ target = Pathname.new(filepath).expand_path(@directory)
152
+
153
+ unless target.path_prefix?(@directory)
154
+ raise SymlinkError,
155
+ "cannot link '#{target}', not in homeconf directory: '#{@directory}'"
156
+ end
157
+ raise FileNotFoundError, "cannot link '#{target}', No such file or directory." unless File.exist?(target)
158
+
159
+ return if FileFinder.ignores(@directory).include? target.to_s
160
+
161
+ relative_target = target.relative_path_from(@homedir)
162
+ link_path = File.join(@homedir, File.basename(target))
163
+
164
+ # move existing file to backup
165
+ backup_file_if_exist(link_path)
166
+
167
+ puts "link: #{link_path} -> #{relative_target}" if @verbose
168
+ File.symlink(relative_target, link_path)
169
+ end
170
+
171
+ private
172
+
173
+ def backup_file_if_exist(filepath)
174
+ return unless File.exist? filepath
175
+
176
+ bk_file = "#{filepath}#{bk_timestamp}"
177
+ puts "#{filepath} file exists, moving to #{bk_file}" if @verbose
178
+ FileUtils.mv(filepath, bk_file)
179
+ end
180
+
181
+ def bk_timestamp
182
+ DateTime.now.strftime('.bk-%Y-%m-%d_%H-%M-%S')
183
+ end
184
+
185
+ def mkdir_unless_exists(directory, permissions = 0o755)
186
+ unless Dir.exist? directory
187
+ Dir.mkdir directory, permissions
188
+ puts "Created directory. #{directory}" if @verbose
189
+ end
190
+ rescue Errno::ENOENT
191
+ raise HomeconfDirNotFound, "No such directory. #{File.dirname directory}"
192
+ rescue Errno::ENOTDIR
193
+ raise InvalidHomeconfDir, "Not a directory. #{File.dirname directory}"
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Pathname mix-ins for homeconf.
4
+ #
5
+ class Pathname
6
+ # Returns whether +self+ has the path prefix of +path_prefix+
7
+ #
8
+ def path_prefix?(path_prefix)
9
+ path_parts = expand_path.parent.to_s.split(File::SEPARATOR)
10
+ prefix_parts = File.expand_path(path_prefix).split(File::SEPARATOR)
11
+
12
+ return false if path_parts.size < prefix_parts.size
13
+
14
+ prefix_parts.each_with_index do |part, index|
15
+ return false unless path_parts[index].eql? part
16
+ end
17
+
18
+ true
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Homeconf
4
+ VERSION = '0.1.1'
5
+ end
data/lib/homeconf.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+ require_relative 'homeconf/homeconf'
5
+ require_relative 'homeconf/pathname'
6
+ require_relative 'homeconf/version'
7
+
8
+ module Homeconf
9
+ class << self
10
+ attr_accessor :logger
11
+ end
12
+
13
+ self.logger = Logger.new($stderr)
14
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: homeconf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Mark Lundquist
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faker
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.1
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 3.1.1
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: parser
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 3.2.1
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.2.0
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 3.2.1
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 3.2.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: rspec
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '3.12'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 3.0.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.12'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 3.0.0
73
+ - !ruby/object:Gem::Dependency
74
+ name: rspec-parameterized
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 1.0.0
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 1.0.0
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.0.0
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 1.0.0
93
+ description: " Homeconf is a Ruby program to create and manage a single directory
94
+ for all home directory configuration.\n"
95
+ email:
96
+ - homeconf@epicmaven.com
97
+ executables:
98
+ - homeconf
99
+ extensions: []
100
+ extra_rdoc_files:
101
+ - HEADER
102
+ - LICENSE
103
+ - NOTICE
104
+ - README.md
105
+ files:
106
+ - HEADER
107
+ - LICENSE
108
+ - NOTICE
109
+ - README.md
110
+ - bin/homeconf
111
+ - lib/homeconf.rb
112
+ - lib/homeconf/default_homeconfignore
113
+ - lib/homeconf/errors.rb
114
+ - lib/homeconf/file_finder.rb
115
+ - lib/homeconf/homeconf.rb
116
+ - lib/homeconf/pathname.rb
117
+ - lib/homeconf/version.rb
118
+ homepage: https://github.com/EpicMaven/homeconf
119
+ licenses:
120
+ - Apache-2.0
121
+ metadata:
122
+ homepage_uri: https://github.com/EpicMaven/homeconf
123
+ bug_tracker_uri: https://github.com/EpicMaven/homeconf/issues
124
+ source_code_uri: https://github.com/EpicMaven/homeconf/tree/v0.1.1
125
+ documentation_uri: https://rubydoc.info/gems/EpicMaven/0.1.1
126
+ post_install_message:
127
+ rdoc_options:
128
+ - "--main"
129
+ - README.md
130
+ - "--title=homeconf"
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: 2.6.0
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubygems_version: 3.4.8
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Homeconf manages portable, version controlled home directory configuration.
148
+ test_files: []