hgrc_generator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7276d16e56c73b9b3be8f22fa7bd5bf0b1ef864a
4
+ data.tar.gz: f3b9b79fd64002497f79fb0623acc8428a7fff5e
5
+ SHA512:
6
+ metadata.gz: d59e54a49f4c4c0db54954ab4faecbd13a6112cdbc01f78653a2aa4a6d825beb0ba8b4f0be614d2601b80a264d7bdc18d0298ec445fb260568d58c8a1cf36de6
7
+ data.tar.gz: dd06a76fe7fec4ea6008845d2b470cf2070357576cac7fbee48d9e27e06808ade995c3f7ac6931f0751a5ccbf9887b123af6943a05e7c5eabc4bc3432b824f02
@@ -0,0 +1,2 @@
1
+ E;WR� H��!$�Cd#�Lb�w�0�4#�������U���(݊�y�¿y9�j���k�L��`j1EF��ݯ^�V�J��8��
2
+
Binary file
@@ -0,0 +1,12 @@
1
+ # hgrc Generator
2
+
3
+ Creates a default hgrc file.
4
+
5
+ For more information:
6
+
7
+ $ hgrc_generator --help
8
+
9
+
10
+ ## Installation
11
+
12
+ $ gem install hgrc_generator
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path('../../lib/hgrc_generator', __FILE__)
3
+ HgrcGenerator.run ARGV
@@ -0,0 +1,10 @@
1
+ module HgrcGenerator
2
+ end
3
+
4
+ root = File.dirname(__FILE__)
5
+
6
+ require File.join(root, 'hgrc_generator', 'section')
7
+
8
+ Dir[File.join(root, 'hgrc_generator', '**', '*.rb')].each do |file|
9
+ require file
10
+ end
@@ -0,0 +1,99 @@
1
+ module HgrcGenerator
2
+ module AliasSection
3
+ extend self, Section
4
+
5
+ ALIAS_LIST = [
6
+ :lli,
7
+ :llo,
8
+ :lll,
9
+ :llh,
10
+ :rli,
11
+ :rlo,
12
+ :rll,
13
+ :rlh,
14
+ ]
15
+
16
+ def section_content
17
+ get_aliases
18
+ end
19
+
20
+ private
21
+
22
+ def get_aliases
23
+ aliases = []
24
+
25
+ aliases << unix_local_in
26
+ aliases << unix_local_out
27
+ aliases << unix_local_pull
28
+ aliases << unix_local_push
29
+ aliases << unix_remote_in
30
+ aliases << unix_remote_out
31
+ aliases << unix_remote_pull
32
+ aliases << unix_remote_push
33
+
34
+ check_aliases
35
+
36
+ aliases
37
+ end
38
+
39
+ def check_aliases
40
+ unused_aliases = ALIAS_LIST - @keys
41
+ unless unused_aliases.empty?
42
+ raise "The following aliases were not used: #{unused_aliases}"
43
+ end
44
+ end
45
+
46
+ def unix_local_in
47
+ commands = Path.get_local_in
48
+ unix_format_for :lli, commands, :in
49
+ end
50
+
51
+ def unix_local_out
52
+ commands = Path.get_local_out.reverse
53
+ unix_format_for :llo, commands, :out
54
+ end
55
+
56
+ def unix_local_pull
57
+ commands = Path.get_local_in
58
+ unix_format_for :lll, commands, :pull
59
+ end
60
+
61
+ def unix_local_push
62
+ commands = Path.get_local_out.reverse
63
+ unix_format_for :llh, commands, :push
64
+ end
65
+
66
+ def unix_remote_in
67
+ commands = Path.get_remote_in
68
+ unix_format_for :rli, commands, :in
69
+ end
70
+
71
+ def unix_remote_out
72
+ commands = Path.get_remote_out.reverse
73
+ unix_format_for :rlo, commands, :out
74
+ end
75
+
76
+ def unix_remote_pull
77
+ commands = Path.get_remote_in
78
+ unix_format_for :rll, commands, :pull
79
+ end
80
+
81
+ def unix_remote_push
82
+ commands = Path.get_remote_out.reverse
83
+ unix_format_for :rlh, commands, :push
84
+ end
85
+
86
+ def unix_format_for(key, commands, group)
87
+ @keys ||= []
88
+ raise "Alias '#{key}' was not expected" unless ALIAS_LIST.include?(key)
89
+ raise "Duplicate alias '#{key}'" if @keys.include?(key)
90
+ @keys << key
91
+
92
+ UnixFormatter.format key, commands.map(&group), padding
93
+ end
94
+
95
+ def padding
96
+ @padding ||= ALIAS_LIST.map(&:to_s).map(&:length).max
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,33 @@
1
+ module HgrcGenerator
2
+ module Content
3
+ extend self
4
+
5
+ def create(params)
6
+ @params = params
7
+ @content = []
8
+
9
+ leader
10
+ sections
11
+
12
+ return @content.join("\n").strip
13
+ end
14
+
15
+ private
16
+
17
+ def leader
18
+ mask = '# This file was created by version %s of %s at %s UTC'
19
+
20
+ @content << mask % [
21
+ HgrcGenerator::VERSION,
22
+ HgrcGenerator,
23
+ Time.now.utc.strftime('%F %T'),
24
+ ]
25
+ end
26
+
27
+ def sections
28
+ @content.concat PathsSection.content
29
+ @content << ''
30
+ @content.concat AliasSection.content
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+ module HgrcGenerator
2
+ extend self
3
+
4
+ def run(args)
5
+ Core.run args
6
+ end
7
+
8
+ module Core
9
+ extend self
10
+
11
+ def run(args)
12
+ params = Params.new(args)
13
+ if File.directory?('.hg')
14
+ if File.file?('.hg/hgrc') && !params.dry_run
15
+ puts 'hgrc already exists.'
16
+ else
17
+ contents = Content.create(params)
18
+ if params.dry_run
19
+ puts contents
20
+ else
21
+ File.write '.hg/hgrc', contents
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,49 @@
1
+ module HgrcGenerator
2
+ class Params
3
+ attr_reader :include_prefix, :branches, :dry_run, :help
4
+
5
+ HELP = <<-DOC
6
+ Usage: hgrc [--include-prefix] [--dry_run] BRANCH_LIST
7
+
8
+ Create an hgrc file for the currrent project.
9
+
10
+ The file will not be created if hgrc already exists in `./.hg`
11
+
12
+ BRANCH_LIST is a list of branches to push to when pushes are not forced.
13
+ The default branch is automatically included in this list.
14
+
15
+ parameters:
16
+ --help, -h Show this message.
17
+ --include_prefix Include the folder one level up from the project folder
18
+ when generating paths/repo names.
19
+ Default: true
20
+ --dry-run, -n Do not create an hgrc file, but output the contents
21
+ that would be created.
22
+ DOC
23
+
24
+ class << self
25
+ attr_accessor :params
26
+ end
27
+
28
+ def initialize(params)
29
+ @branches = [:default]
30
+ @include_prefix = true
31
+
32
+ params.each do |param|
33
+ if param.match(/^-/)
34
+ case param.downcase
35
+ when '--no-prefix' then @include_prefix = false
36
+ when '--dry-run', '-n' then @dry_run = true
37
+ when '--help', '-h'
38
+ puts HELP.gsub(/^ {6}/, '')
39
+ exit
40
+ end
41
+ else
42
+ @branches << param
43
+ end
44
+ end
45
+
46
+ self.class.params = self
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,143 @@
1
+ module HgrcGenerator
2
+ class Path
3
+ class << self
4
+ PATHS = {
5
+ 'default' => {
6
+ :path => '~/.source/pull/%s',
7
+ :in => true,
8
+ :out => true,
9
+ :force => false,
10
+ },
11
+ 'default-push' => {
12
+ :path => '~/.source/push/%s',
13
+ :in => false,
14
+ :out => true,
15
+ :force => true,
16
+ },
17
+ 'bb' => {
18
+ :path => 'ssh://hg@bitbucket.org/ToadJamb/%s',
19
+ :in => true,
20
+ :out => true,
21
+ :force => false,
22
+ },
23
+ 'bb-dev' => {
24
+ :path => 'ssh://hg@bitbucket.org/ToadJamb/%s_dev',
25
+ :in => false,
26
+ :out => true,
27
+ :force => true,
28
+ },
29
+ }
30
+
31
+ def paths
32
+ @paths ||=
33
+ PATHS.map do |key, path_hash|
34
+ new key, path_hash
35
+ end
36
+ end
37
+
38
+ def get_local_in
39
+ paths.select(&:in).select(&:local)
40
+ end
41
+
42
+ def get_local_out
43
+ paths.select(&:out).select(&:local)
44
+ end
45
+
46
+ def get_remote_in
47
+ paths.select(&:in).reject(&:local)
48
+ end
49
+
50
+ def get_remote_out
51
+ paths.select(&:out).reject(&:local)
52
+ end
53
+ end
54
+
55
+ attr_reader \
56
+ :key,
57
+ :path,
58
+ :force,
59
+ :out,
60
+ :in,
61
+ :push,
62
+ :pull
63
+
64
+ def initialize(key, hash)
65
+ @key = key
66
+ @path = path_for(hash[:path])
67
+ @force = hash[:force]
68
+ @in = in_command if hash[:in]
69
+ @out = out_command if hash[:out]
70
+ @pull = pull_command if hash[:in]
71
+ @push = push_command if hash[:out]
72
+ end
73
+
74
+ def local
75
+ !path.match(/^ssh:/)
76
+ end
77
+
78
+ private
79
+
80
+ def in_command
81
+ [] << "hg in #{key}"
82
+ end
83
+
84
+ def out_command
85
+ command = []
86
+ command << "hg out #{key}"
87
+ command.concat branches unless force
88
+ command
89
+ end
90
+
91
+ def pull_command
92
+ [] << "hg pull #{key}"
93
+ end
94
+
95
+ def push_command
96
+ command = []
97
+ command << "hg push #{key}"
98
+ if force
99
+ command << '--force'
100
+ else
101
+ command.concat branches
102
+ command << '--new-branch'
103
+ end
104
+ command
105
+ end
106
+
107
+ def branches
108
+ params.branches.map do |branch|
109
+ "--branch #{branch}"
110
+ end
111
+ end
112
+
113
+ def path_for(path)
114
+ if path.match(/^ssh:/)
115
+ path % ssh_path
116
+ else
117
+ path % root_path
118
+ end
119
+ end
120
+
121
+ def ssh_path
122
+ @ssh_path ||= root_path.gsub(%r+\/|\\+, '_')
123
+ end
124
+
125
+ def root_path
126
+ return @root_path if defined?(@root_path)
127
+
128
+ base = File.basename(Dir.pwd)
129
+ prefix = File.basename(File.dirname(Dir.pwd))
130
+
131
+ @root_path =
132
+ if params.include_prefix
133
+ base
134
+ else
135
+ File.join(prefix, base)
136
+ end
137
+ end
138
+
139
+ def params
140
+ Params.params
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,18 @@
1
+ module HgrcGenerator
2
+ module PathsSection
3
+ extend self, Section
4
+
5
+ private
6
+
7
+ def section_content
8
+ Path.paths.map do |path|
9
+ key = path.key.ljust(padding)
10
+ "#{key} = #{path.path}"
11
+ end
12
+ end
13
+
14
+ def padding
15
+ @padding ||= Path.paths.map(&:key).map(&:length).max
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module HgrcGenerator
2
+ module Section
3
+ extend self
4
+
5
+ def content
6
+ content = []
7
+ content << ["[#{name.downcase.gsub(/.*::|section$/, '')}]"]
8
+ content << section_content
9
+ end
10
+
11
+ private
12
+
13
+ def section_content
14
+ []
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,51 @@
1
+ module HgrcGenerator
2
+ module UnixFormatter
3
+ extend self
4
+
5
+ def format(key, command_array, padding)
6
+ return unless command_array && !command_array.empty?
7
+
8
+ @padding = padding
9
+
10
+ commands = command_array.dup
11
+ commands[0][0] = "#{key} = #{command_leader}#{sep}#{cr}#{commands[0][0]}"
12
+
13
+ command = commands.map do |command|
14
+ command.join(params_delimiter)
15
+ end.join(command_delimiter) + command_delimiter
16
+
17
+ command.gsub(/^\s*$/, '')
18
+ end
19
+
20
+ private
21
+
22
+ def sep
23
+ 'hg sep;'
24
+ end
25
+
26
+ def command_leader
27
+ "!"
28
+ end
29
+
30
+ def command_delimiter
31
+ ";#{cr}#{sep}#{cr}"
32
+ end
33
+
34
+ def params_delimiter
35
+ " \\#{cr} "
36
+ end
37
+
38
+ def line_leader
39
+ ' ' * padding
40
+ end
41
+
42
+ def cr
43
+ "\n#{line_leader}"
44
+ end
45
+
46
+ def padding
47
+ @padding + 6
48
+ end
49
+ end
50
+ end
51
+