mac_app_sync 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1717db820f1c9e2db8c533c7e021e2f05d5ac49767b7dd2dd3c2d8a3d5202f3e
4
+ data.tar.gz: ad833b483a788eb5a349b8b80a26fa3ceba40298b4948b446b143d0eb51026c3
5
+ SHA512:
6
+ metadata.gz: 10f35cf3e51c518c544bbfb7bb8160d61fab4efdc3da9548f771257f851349f323ce75daee53b263bead250ebfaa1694268b9b31dbb668aa3c874e6903a89bf7
7
+ data.tar.gz: 179e85fce3ed7ad65126e44368564db4036889a5a5199b6d7c9dcbbe78bc80d00f1c8e738d22647b49278050f88f30f1b6de5b278ff5b06d99243c37c54da4cd
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at matthew.wean@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mac_app_sync.gemspec
6
+ gemspec
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mac_app_sync (0.1.0)
5
+ CFPropertyList
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ CFPropertyList (3.0.0)
11
+ coderay (1.1.2)
12
+ diff-lcs (1.3)
13
+ method_source (0.9.0)
14
+ pry (0.11.3)
15
+ coderay (~> 1.1.0)
16
+ method_source (~> 0.9.0)
17
+ rake (10.5.0)
18
+ rspec (3.8.0)
19
+ rspec-core (~> 3.8.0)
20
+ rspec-expectations (~> 3.8.0)
21
+ rspec-mocks (~> 3.8.0)
22
+ rspec-core (3.8.0)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-expectations (3.8.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-mocks (3.8.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-support (3.8.0)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ bundler (~> 1.16)
37
+ mac_app_sync!
38
+ pry
39
+ rake (~> 10.0)
40
+ rspec (~> 3.0)
41
+
42
+ BUNDLED WITH
43
+ 1.16.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Matt Wean
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # MacAppSync
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'mac_app_sync'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mac_app_sync
18
+
19
+ ## Usage
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mwean/mac_app_sync. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
34
+
35
+ ## Code of Conduct
36
+
37
+ Everyone interacting in the MacAppSync project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mwean/mac_app_sync/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mac_app_sync"
5
+
6
+ require "pry"
7
+ Pry.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ keys:
2
+ - NSNavLastRootDirectory
3
+ - NSNavPanelExpandedSizeForOpenMode
4
+ - NSNavPanelExpandedSizeForSaveMode
5
+ - NSNavPanelExpandedStateForSaveMode
6
+ - PMSparkleWrapper Status
7
+ - RawCameraSupportVersion
8
+ - SUEnableAutomaticChecks
9
+ - SUFeedURL
10
+ - SUHasLaunchedBefore
11
+ - SULastCheckTime
12
+ - SULastProfileSubmissionDate
13
+ - SUScheduledCheckInterval
14
+ - SUSendProfileInfo
15
+ - SUUpdateRelaunchingMarker
16
+ - WebKitDefaultFontSize
17
+ - WebKitStandardFont
18
+
19
+ patterns:
20
+ - ^Hockey
21
+ - ^MASPreferences
22
+ - ^NSSplitView Subview
23
+ - ^NSStatusItem Preferred Position
24
+ - ^NSWindow Frame
25
+ - ^BITApplication
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "yaml"
4
+ require "mac_app_sync"
5
+
6
+ command = ARGV[0]
7
+ config_path = File.expand_path(ENV.fetch("MAC_APP_SYNC_CONFIG"))
8
+ config = YAML.load_file(config_path)
9
+
10
+ case command
11
+ when "backup"
12
+ MacAppSync::Backup.run(config)
13
+ when "restore"
14
+ MacAppSync::Restore.run(config)
15
+ else
16
+ puts "Unknown command #{command}"
17
+ end
@@ -0,0 +1,18 @@
1
+ require "pathname"
2
+
3
+ require "cfpropertylist"
4
+
5
+ require "mac_app_sync/version"
6
+ require "mac_app_sync/persistence"
7
+ require "mac_app_sync/defaults"
8
+
9
+ require "mac_app_sync/backup"
10
+ require "mac_app_sync/restore"
11
+
12
+ module MacAppSync
13
+ module_function
14
+
15
+ def gem_root
16
+ @gem_root ||= Pathname.new("..").expand_path(__dir__)
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module MacAppSync
2
+ class Backup
3
+ class << self
4
+ def run(config)
5
+ Defaults::Save.run(config)
6
+ write_backup_stamp
7
+ end
8
+
9
+ private
10
+
11
+ def write_backup_stamp
12
+ platform_info = `ioreg -rd1 -c IOPlatformExpertDevice`
13
+ machine_id = platform_info.match(/IOPlatformUUID"\s+=\s+"(\w{8}-\w{4}-\w{4}-\w{4}-\w*)/)[1]
14
+ Persistence.write("last_backup", machine: machine_id, time: Time.now)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "defaults/store"
2
+ require_relative "defaults/filter"
3
+ require_relative "defaults/plist_converter"
4
+ require_relative "defaults/comparator"
5
+ require_relative "defaults/updater"
6
+ require_relative "defaults/save"
7
+ require_relative "defaults/restore"
8
+
9
+ module MacAppSync
10
+ module Defaults
11
+ end
12
+ end
@@ -0,0 +1,43 @@
1
+ module MacAppSync
2
+ module Defaults
3
+ class Comparator
4
+ class << self
5
+ def different?(a, b)
6
+ different_type?(a, b) || different_value?(a, b)
7
+ end
8
+
9
+ private
10
+
11
+ def different_type?(a, b)
12
+ a.class != b.class
13
+ end
14
+
15
+ def different_value?(a, b)
16
+ if a.is_a?(CFPropertyList::CFDictionary)
17
+ different_size?(a.value, b.value) ||
18
+ different_hash_values?(a.value, b.value)
19
+ elsif a.is_a?(CFPropertyList::CFArray)
20
+ different_size?(a.value, b.value) ||
21
+ different_items?(a.value, b.value)
22
+ else
23
+ a.value != b.value
24
+ end
25
+ end
26
+
27
+ def different_size?(a, b)
28
+ a.size != b.size
29
+ end
30
+
31
+ def different_items?(a, b)
32
+ a.any? do |a_item|
33
+ b.all? { |b_item| different?(a_item, b_item) }
34
+ end
35
+ end
36
+
37
+ def different_hash_values?(a, b)
38
+ a.any? { |a_key, a_item| different?(a_item, b[a_key]) }
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,75 @@
1
+ require "yaml"
2
+
3
+ module MacAppSync
4
+ module Defaults
5
+ class Filter
6
+ class << self
7
+ def filter(data, opts)
8
+ user_filter(global_filter(data), opts)
9
+ end
10
+
11
+ private
12
+
13
+ def global_filter(data)
14
+ data.reject do |key, _value|
15
+ global_exclude_keys.include?(key) ||
16
+ global_exclude_patterns.any? { |pattern| key =~ pattern }
17
+ end
18
+ end
19
+
20
+ def user_filter(data, opts)
21
+ return data if opts.empty?
22
+
23
+ should_exclude = build_filter(opts)
24
+ data.reject { |key, _| should_exclude.call(key) }
25
+ end
26
+
27
+ def build_filter(opts)
28
+ if opts["only"]
29
+ include_filter(opts)
30
+ else
31
+ exclude_filter(opts)
32
+ end
33
+ end
34
+
35
+ def include_filter(opts)
36
+ only = wrap(opts["only"]).to_set
37
+ ->(key) { !only.include?(key) }
38
+ end
39
+
40
+ def exclude_filter(opts)
41
+ excludes = wrap(opts["exclude"]).to_set
42
+ exclude_patterns = wrap(opts["exclude_patterns"]).map { |pattern| Regexp.new(pattern) }
43
+
44
+ lambda do |key|
45
+ excludes.include?(key) || exclude_patterns.any? { |pattern| key =~ pattern }
46
+ end
47
+ end
48
+
49
+ def wrap(object)
50
+ if object.nil?
51
+ []
52
+ elsif object.respond_to?(:to_ary)
53
+ object.to_ary || [object]
54
+ else
55
+ [object]
56
+ end
57
+ end
58
+
59
+ def global_exclude_keys
60
+ @global_exclude_keys ||= global_exclusions.fetch("keys").to_set
61
+ end
62
+
63
+ def global_exclude_patterns
64
+ @global_exclude_patterns ||= global_exclusions.fetch("patterns").map do |pattern|
65
+ Regexp.new(pattern)
66
+ end
67
+ end
68
+
69
+ def global_exclusions
70
+ @global_exclusions ||= YAML.load_file(MacAppSync.gem_root.join("config", "global_defaults_exclusions.yml"))
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,78 @@
1
+ module MacAppSync
2
+ module Defaults
3
+ class PlistConverter
4
+ TYPE_MAPPINGS = {
5
+ CFPropertyList::CFDate => :date,
6
+ CFPropertyList::CFBoolean => :boolean,
7
+ CFPropertyList::CFInteger => :integer,
8
+ CFPropertyList::CFReal => :float,
9
+ CFPropertyList::CFString => :string,
10
+ CFPropertyList::CFUid => :cfuid,
11
+ CFPropertyList::CFData => :data,
12
+ CFPropertyList::CFDictionary => :dict,
13
+ CFPropertyList::CFArray => :array
14
+ }
15
+
16
+ class << self
17
+ def to_ruby(plist_data)
18
+ convert_plist_dict(plist_data)
19
+ end
20
+
21
+ def to_plist(value_data)
22
+ build_plist_value(value_data.fetch("type"), value_data.fetch("value"))
23
+ end
24
+
25
+ private
26
+
27
+ def convert_plist_value(container)
28
+ case container
29
+ when CFPropertyList::CFArray
30
+ convert_plist_simple(convert_plist_array(container), :array)
31
+ when CFPropertyList::CFDictionary
32
+ convert_plist_simple(convert_plist_dict(container), :dict)
33
+ when CFPropertyList::CFData
34
+ convert_plist_simple(container.encoded_value, :data)
35
+ else
36
+ convert_plist_simple(container.value, TYPE_MAPPINGS.fetch(container.class))
37
+ end
38
+ end
39
+
40
+ def build_plist_value(type, value)
41
+ plist_class = TYPE_MAPPINGS.invert.fetch(type.to_sym)
42
+ converted_value = convert_ruby_value(type, value)
43
+
44
+ plist_class.new(converted_value)
45
+ end
46
+
47
+ def convert_ruby_value(type, value)
48
+ case type
49
+ when "array"
50
+ value.map { |value_data| to_plist(value_data) }
51
+ when "dict"
52
+ value.map { |key, value_data| [key, to_plist(value_data)] }.to_h
53
+ when "date"
54
+ Time.parse(value)
55
+ else
56
+ value
57
+ end
58
+ end
59
+
60
+ def convert_plist_array(container)
61
+ container.value.map { |element| convert_plist_value(element) }
62
+ end
63
+
64
+ def convert_plist_dict(container)
65
+ ruby_values = container.value.map { |key, element| [key, convert_plist_value(element)] }
66
+ ruby_values.sort_by { |key, _| key }.to_h
67
+ end
68
+
69
+ def convert_plist_simple(value, type)
70
+ {
71
+ "value" => value,
72
+ "type" => type
73
+ }
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,36 @@
1
+ module MacAppSync
2
+ module Defaults
3
+ class Restore
4
+ def self.run(_config)
5
+ new.run
6
+ end
7
+
8
+ def run
9
+ update_defaults
10
+ write_updates
11
+ end
12
+
13
+ private
14
+
15
+ def update_defaults
16
+ persistence.each_file do |domain, key, content|
17
+ stores[domain].set(key, content)
18
+ end
19
+ end
20
+
21
+ def write_updates
22
+ stores.each do |domain, store|
23
+ Updater.update(domain, store)
24
+ end
25
+ end
26
+
27
+ def persistence
28
+ @persistence ||= Persistence.new("defaults")
29
+ end
30
+
31
+ def stores
32
+ @stores ||= Hash.new { |hash, domain| hash[domain] = Store.new(domain) }
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,65 @@
1
+ module MacAppSync
2
+ module Defaults
3
+ class Save
4
+ attr_reader :domain_configs
5
+
6
+ def self.run(config)
7
+ new(config).run
8
+ end
9
+
10
+ def initialize(config)
11
+ @domain_configs = parse_config(config)
12
+ end
13
+
14
+ def run
15
+ return if domain_configs.empty?
16
+
17
+ persistence.reset!
18
+ save_domains
19
+ end
20
+
21
+ private
22
+
23
+ def parse_config(config)
24
+ config.fetch("defaults", []).map do |domain_config|
25
+ if domain_config.is_a?(String)
26
+ [domain_config, {}]
27
+ else
28
+ domain_config.to_a.first
29
+ end
30
+ end
31
+ end
32
+
33
+ def save_domains
34
+ domain_configs.each do |domain, opts|
35
+ save_domain(domain, opts)
36
+ end
37
+ end
38
+
39
+ def save_domain(domain, opts)
40
+ data = values_to_save(domain, opts)
41
+
42
+ return if data.empty?
43
+
44
+ persist(domain, data)
45
+ end
46
+
47
+ def values_to_save(domain, opts)
48
+ all_values = Store.new(domain).values
49
+ Filter.filter(all_values, opts)
50
+ end
51
+
52
+ def persist(domain, data)
53
+ persistence.in_dir(domain) do
54
+ data.each do |key, value|
55
+ persistence.write(key, value)
56
+ end
57
+ end
58
+ end
59
+
60
+ def persistence
61
+ @persistence ||= Persistence.new("defaults")
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,51 @@
1
+ require "pathname"
2
+
3
+ module MacAppSync
4
+ module Defaults
5
+ class Store
6
+ attr_reader :domain
7
+
8
+ def initialize(domain)
9
+ @domain = domain
10
+ end
11
+
12
+ def values
13
+ PlistConverter.to_ruby(plist.value)
14
+ end
15
+
16
+ def to_binary
17
+ plist.to_str(CFPropertyList::List::FORMAT_XML)
18
+ end
19
+
20
+ def plist
21
+ @plist ||= CFPropertyList::List.new(file: plist_path)
22
+ end
23
+
24
+ def set(key, value)
25
+ new_value = PlistConverter.to_plist(value)
26
+ current_value = plist.value.value[key]
27
+
28
+ if Comparator.different?(current_value, new_value)
29
+ plist.value.value[key] = new_value
30
+ end
31
+ end
32
+
33
+ def plist_path
34
+ @plist_path ||= prefs_file_path || container_file_path
35
+ end
36
+
37
+ private
38
+
39
+ def prefs_file_path
40
+ prefs_file = Pathname.new("~/Library/Preferences/#{domain}.plist").expand_path
41
+ prefs_file.to_s if prefs_file.exist?
42
+ end
43
+
44
+ def container_file_path
45
+ container_path = "~/Library/Containers/#{domain}/Data/Library/Preferences/#{domain}.plist"
46
+ container_file = Pathname.new(container_path).expand_path
47
+ container_file.to_s if container_file.exist?
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,31 @@
1
+ require "tempfile"
2
+
3
+ module MacAppSync
4
+ module Defaults
5
+ class Updater
6
+ class << self
7
+ def update(domain, store)
8
+ with_tempfile(store.to_binary) do |file|
9
+ if system("plutil -lint #{file.path}")
10
+ system("defaults import #{store.plist_path} #{file.path}")
11
+ else
12
+ raise "Invalid plist for #{domain}"
13
+ end
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def with_tempfile(content)
20
+ Tempfile.create("plist") do |file|
21
+ file.write(content)
22
+ File.chmod(0644, file)
23
+ file.rewind
24
+
25
+ yield file
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,61 @@
1
+ require "pathname"
2
+ require "json"
3
+
4
+ module MacAppSync
5
+ class Persistence
6
+ SAVE_PATH = Pathname.new("~/.app_prefs").expand_path
7
+
8
+ PATH_REPLACEMENTS = {
9
+ "/" => "%-",
10
+ ":" => "%.",
11
+ "%" => "%%"
12
+ }
13
+
14
+ def self.write(name, content, path = SAVE_PATH)
15
+ path.join(name).write(JSON.pretty_generate(content))
16
+ end
17
+
18
+ def initialize(namespace)
19
+ @root_path = SAVE_PATH.join(namespace)
20
+ end
21
+
22
+ def reset!
23
+ @root_path.rmtree if @root_path.exist?
24
+ @root_path.mkpath
25
+ end
26
+
27
+ def in_dir(name)
28
+ @save_path = @root_path.join(escape_path(name))
29
+ @save_path.mkpath
30
+
31
+ yield
32
+
33
+ @save_path = @root_path
34
+ end
35
+
36
+ def write(name, content)
37
+ self.class.write(escape_path(name), content, @save_path)
38
+ end
39
+
40
+ def each_file
41
+ @root_path.each_child do |group|
42
+ group.each_child do |file|
43
+ content = JSON.parse(file.read)
44
+
45
+ yield group.basename.to_s, file.basename.to_s, content
46
+ end
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def escape_path(str)
53
+ pattern = "[#{PATH_REPLACEMENTS.keys.join}]"
54
+ str.gsub(Regexp.new(pattern), PATH_REPLACEMENTS)
55
+ end
56
+
57
+ def unescape_path(str)
58
+ str.gsub(/%./, PATH_REPLACEMENTS.invert)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,9 @@
1
+ module MacAppSync
2
+ class Restore
3
+ class << self
4
+ def run(config)
5
+ Defaults::Restore.run(config)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module MacAppSync
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "mac_app_sync/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mac_app_sync"
8
+ spec.version = MacAppSync::VERSION
9
+ spec.authors = ["Matt Wean"]
10
+ spec.email = ["matthew.wean@gmail.com"]
11
+
12
+ spec.summary = "A CLI tool to back up and restore Mac app settings"
13
+ spec.homepage = "https://github.com/mwean/mac_app_sync"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "pry"
28
+
29
+ spec.add_dependency "CFPropertyList"
30
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mac_app_sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Wean
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: CFPropertyList
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - matthew.wean@gmail.com
86
+ executables:
87
+ - mac_app_sync
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - config/global_defaults_exclusions.yml
102
+ - exe/mac_app_sync
103
+ - lib/mac_app_sync.rb
104
+ - lib/mac_app_sync/backup.rb
105
+ - lib/mac_app_sync/defaults.rb
106
+ - lib/mac_app_sync/defaults/comparator.rb
107
+ - lib/mac_app_sync/defaults/filter.rb
108
+ - lib/mac_app_sync/defaults/plist_converter.rb
109
+ - lib/mac_app_sync/defaults/restore.rb
110
+ - lib/mac_app_sync/defaults/save.rb
111
+ - lib/mac_app_sync/defaults/store.rb
112
+ - lib/mac_app_sync/defaults/updater.rb
113
+ - lib/mac_app_sync/persistence.rb
114
+ - lib/mac_app_sync/restore.rb
115
+ - lib/mac_app_sync/version.rb
116
+ - mac_app_sync.gemspec
117
+ homepage: https://github.com/mwean/mac_app_sync
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.7.7
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: A CLI tool to back up and restore Mac app settings
141
+ test_files: []