dotpack 0.1.0 → 1.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d91e034bbf9fe2cbc60810e9d92802b9a898926b
4
- data.tar.gz: 668694ab480f5a3aed4b2779fca06e367e313fdd
3
+ metadata.gz: 539c18bfaa268973a4c4495a4a66b46df2ee2444
4
+ data.tar.gz: 43450e8fcbc340c1148a025e9ebd3033a1224267
5
5
  SHA512:
6
- metadata.gz: c487ae199c5e25fcabfb124ec0a1e47424f6c24fc21e7d250bc9322a6a6c8285a5f7abf47334560d61012978ca9677ea981cdfb5a651dc2ec60a6289fa4fde35
7
- data.tar.gz: eca911cac58a3d541a3a5c34a4e21e949093da7a53567d5dc31dafb31c20b29076a5ff34f5caa5c6915916762e63094a5249133e2c1b2232de7d1eeb0f1a0d29
6
+ metadata.gz: 2f74a38d9c30a5a09957e0f03a2289aca5b5c57cfcfe11ef1aa1d0a6e46fee45b6c117fc66ee528355b3405faa26226d255309abcdec639d9bfbc5c7964cdb81
7
+ data.tar.gz: 2b8f7f16dc328af5b52fb95e157b3c428bcb9b25edd76e1c31b367924073dec57ecca4d0c46c2ea3be4f5449dca7d4c6569ee6f4c3f8ee960799f1443fa9df36
@@ -1,10 +1,34 @@
1
1
  require 'thor'
2
2
  require 'dotpack/cli/flavor'
3
+ require 'dotpack/cli/repo'
4
+ require 'dotpack/package'
5
+ require 'dotpack/runner'
3
6
 
4
7
  module Dotpack
5
8
  module Cli
6
9
  class App < Thor
7
- register(Cli::Flavor, 'flavor', 'flavor <command>', 'Add, remove or list flavors.')
10
+ register(Cli::Flavor, 'flavor', 'flavor <command>', 'Add, remove or list flavors')
11
+ register(Cli::Repo, 'repo', 'repo <command>', 'Manage repositories')
12
+
13
+ desc "relink", "Create or recreate dotfiles and link them"
14
+ def relink
15
+ Runner.clear
16
+ runner = Runner.new
17
+ Package.all.each do |package|
18
+ runner.start package
19
+ end
20
+ end
21
+
22
+ desc "update", "Update the dotfiles from source"
23
+ def update
24
+ repos = Dotpack::Repo.all
25
+ relink if repos.map(&:update).any?
26
+ end
27
+
28
+ desc "clear", "Clear every symlink"
29
+ def clear
30
+ Runner.clear
31
+ end
8
32
  end
9
33
  end
10
34
  end
@@ -5,31 +5,29 @@ require 'dotpack/config'
5
5
  module Dotpack
6
6
  module Cli
7
7
  class Flavor < Thor
8
- desc('add [ARGS]', 'Add flavors.')
9
- def add(*args)
10
- args.each do |a|
11
- a = a.downcase
12
- next if Config.flavors.include? a
13
- Config.flavors << a
14
- puts "Added flavor: #{a}".bold
15
- end
8
+ desc('add FLAVOR [VALUE]', 'Add flavor.')
9
+ def add(f, value='')
10
+ f = f.downcase
11
+ Config.set_flavor(f, value)
16
12
  Config.save
17
13
  end
18
14
 
19
- desc('remove [ARGS]', 'Remove flavors.')
20
- def remove(*args)
21
- args.each do |a|
22
- a = a.downcase
23
- next unless Config.flavors.include? a
24
- Config.flavors.delete a
25
- puts "Removed flavor: #{a}".bold
26
- end
15
+ desc('remove FLAVOR', 'Remove flavor.')
16
+ def remove(f)
17
+ f = f.downcase
18
+ Config.remove_flavor f
27
19
  Config.save
28
20
  end
29
21
 
30
22
  desc('list', 'List used flavors.')
31
23
  def list
32
- puts Config.flavors
24
+ Config.flavors.each do |k, v|
25
+ if v == '' || v.nil?
26
+ puts k
27
+ else
28
+ puts "#{k} = #{v}"
29
+ end
30
+ end
33
31
  end
34
32
  end
35
33
  end
@@ -0,0 +1,38 @@
1
+ require 'thor'
2
+ require 'dotpack/repo'
3
+
4
+ module Dotpack
5
+ module Cli
6
+ class Repo < Thor
7
+ desc "list", "List installed repositories"
8
+ def list
9
+ ::Dotpack::Repo.all.each{|r| puts r.name}
10
+ end
11
+
12
+ desc "clone [ARG]", "Clone a new repository"
13
+ def clone(arg)
14
+ url = arg
15
+ name = arg.split('/').last
16
+ if name.include? ".git"
17
+ name[".git"] = ''
18
+ end
19
+ if arg =~ /\A[a-zA-Z0-9_-]+\/[\.a-zA-Z0-9_-]+\z/
20
+ url = "https://github.com/#{arg}.git"
21
+ name = arg.sub('/', '__')
22
+ end
23
+ Dir.chdir Config::BASE_DIR + '/repositories'
24
+ return if Dir.exist? name
25
+ system("git", "clone", url, name)
26
+ end
27
+
28
+ desc "remove [ARGS]", "Remove repositories"
29
+ def remove(*args)
30
+ args.each do |repo|
31
+ r = ::Dotpack::Repo.find_by_name repo
32
+ next if r.nil?
33
+ FileUtils.rm_rf r.path
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,16 +1,33 @@
1
1
  require 'fileutils'
2
+ require 'json'
2
3
 
3
4
  module Dotpack
4
5
  module Config
5
6
  BASE_DIR = Dir.home + '/.dotpack'
6
7
  FLAVORS_PATH = BASE_DIR + '/flavors'
7
8
 
8
- @flavors = []
9
+ @flavors = {}
9
10
  @flavors_cached = false
10
11
 
12
+ def self.flavor(f)
13
+ cache_flavors
14
+ @flavors[f]
15
+ end
16
+
17
+ def self.set_flavor(f, value='')
18
+ value = '' if value.nil?
19
+ cache_flavors
20
+ @flavors[f] = value
21
+ end
22
+
23
+ def self.remove_flavor(f)
24
+ cache_flavors
25
+ @flavors.delete f
26
+ end
27
+
11
28
  def self.flavors
12
29
  cache_flavors
13
- return @flavors
30
+ @flavors.clone
14
31
  end
15
32
 
16
33
  def self.save
@@ -20,7 +37,7 @@ module Dotpack
20
37
  def self.save_flavors
21
38
  return unless @flavors_cached
22
39
  File.open(FLAVORS_PATH, 'w') do |file|
23
- @flavors.each{|f| file.puts f}
40
+ file.puts JSON.unparse(@flavors)
24
41
  end
25
42
  end
26
43
 
@@ -30,9 +47,9 @@ module Dotpack
30
47
 
31
48
  FileUtils.mkdir_p BASE_DIR
32
49
  unless File.exist? FLAVORS_PATH
33
- FileUtils.touch FLAVORS_PATH
50
+ File.write FLAVORS_PATH, "{}"
34
51
  end
35
- @flavors = File.read(FLAVORS_PATH).split
52
+ @flavors = JSON.parse(File.read(FLAVORS_PATH))
36
53
  @flavors_cached = true
37
54
  end
38
55
  end
@@ -0,0 +1,23 @@
1
+ require 'dotpack/repo'
2
+
3
+ module Dotpack
4
+ class Package
5
+ attr_reader :path
6
+
7
+ @packages = nil
8
+
9
+ def self.all
10
+ return @packages unless @packages.nil?
11
+ repos = Repo.all
12
+ packages = repos.map{|x| Dir[x.path + "/*"]}.flatten
13
+ packages.select!{|x| File.exist? x + "/Dotpackfile"}
14
+ @packages = packages.map{|x| Package.new(x)}
15
+ @packages
16
+ end
17
+
18
+ private
19
+ def initialize(path)
20
+ @path = path
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,44 @@
1
+ require 'fileutils'
2
+ require 'dotpack/config'
3
+
4
+ module Dotpack
5
+ REPO_DIR = Config::BASE_DIR + '/repositories'
6
+
7
+ class Repo
8
+ attr_reader :path, :name
9
+
10
+ @repos = nil
11
+
12
+ def self.all
13
+ return @repos unless @repos.nil?
14
+ FileUtils.mkdir_p REPO_DIR
15
+ @repos = Dir[REPO_DIR + '/*'].map{|p| Repo.new(p)}
16
+ @repos
17
+ end
18
+
19
+ def self.find_by_name(name)
20
+ repos = all()
21
+ repos.each do |r|
22
+ return r if r.name == name
23
+ end
24
+ nil
25
+ end
26
+
27
+ def update
28
+ Dir.chdir @path
29
+ return false unless system "git fetch origin >/dev/null 2>&1"
30
+ commits = `git log HEAD..origin/master --oneline`
31
+ return false if commits == ""
32
+ last = commits.lines.last.split(' ').first
33
+ puts "Updated #{@name} to #{last}"
34
+ system "git pull -f origin master >/dev/null"
35
+ true
36
+ end
37
+
38
+ private
39
+ def initialize(path)
40
+ @path = path
41
+ @name = File.basename(path).sub(/(.*)__/, '\1/')
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,90 @@
1
+ require 'fileutils'
2
+ require 'erb'
3
+ require 'dotpack/config'
4
+
5
+ module Dotpack
6
+ class Runner
7
+ LOCAL_DIR = Config::BASE_DIR + '/local'
8
+
9
+ def self.clear
10
+ FileUtils.rm_rf(LOCAL_DIR)
11
+ if File.exist? Config::BASE_DIR + '/links'
12
+ File.foreach(Config::BASE_DIR + '/links') do |line|
13
+ line.chomp!
14
+ if File.exist?(line) || File.symlink?(line)
15
+ File.unlink(line)
16
+ end
17
+ end
18
+ File.unlink(Config::BASE_DIR + '/links')
19
+ end
20
+ end
21
+
22
+ def start(package)
23
+ @links = File.open(Config::BASE_DIR + '/links', "a")
24
+ pwd = Dir.pwd
25
+ Dir.chdir package.path
26
+ instance_eval File.read(package.path + "/Dotpackfile")
27
+ Dir.chdir pwd
28
+ @links.close
29
+ @links = nil
30
+ end
31
+
32
+ def pack(path)
33
+ return unless File.exist? path
34
+ target = path.split('/').map do |p|
35
+ if p[0] == '_'
36
+ '.' + p[1..-1]
37
+ else
38
+ p
39
+ end
40
+ end
41
+ target = target.join "/"
42
+ sym = Dir.home + '/' + target
43
+
44
+ if File.directory? path
45
+ FileUtils.mkdir_p sym
46
+ FileUtils.mkdir_p LOCAL_DIR + '/' + target
47
+ else
48
+ if path.include? '/'
49
+ pack(File.dirname path)
50
+ end
51
+ if File.exist?(sym) || File.symlink?(sym)
52
+ File.unlink sym
53
+ end
54
+ erb = ERB.new(File.read(path))
55
+ result = erb.result(binding)
56
+ File.write LOCAL_DIR + '/' + target, result
57
+ File.symlink(File.expand_path(LOCAL_DIR + '/' + target), sym)
58
+ @links.puts sym
59
+ end
60
+ end
61
+
62
+ def pack_recurse(path)
63
+ return unless File.exist? path
64
+ if File.directory? path
65
+ Dir[path + '/**/*'].each {|x| pack(x)}
66
+ else
67
+ pack(path)
68
+ end
69
+ end
70
+
71
+ def flavor?(f)
72
+ !!flavor(f)
73
+ end
74
+
75
+ def flavor(f)
76
+ f = f.to_s if f.is_a? Symbol
77
+ Config.flavor(f)
78
+ end
79
+
80
+ def flavor_present?(f)
81
+ return !!flavor!
82
+ end
83
+
84
+ def flavor!(f)
85
+ v = flavor(f)
86
+ return nil if v == ''
87
+ v
88
+ end
89
+ end
90
+ end
@@ -1,3 +1,3 @@
1
1
  module Dotpack
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotpack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nax
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-04 00:00:00.000000000 Z
11
+ date: 2016-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -79,7 +79,11 @@ files:
79
79
  - lib/dotpack/cli.rb
80
80
  - lib/dotpack/cli/app.rb
81
81
  - lib/dotpack/cli/flavor.rb
82
+ - lib/dotpack/cli/repo.rb
82
83
  - lib/dotpack/config.rb
84
+ - lib/dotpack/package.rb
85
+ - lib/dotpack/repo.rb
86
+ - lib/dotpack/runner.rb
83
87
  - lib/dotpack/version.rb
84
88
  homepage: https://github.com/Nax/dotpack
85
89
  licenses: