svn-transform 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,41 @@
1
+ class SvnTransform
2
+ module Transform
3
+ # Convert file extensions
4
+ class Extension
5
+ # Initialize Extension Transform.
6
+ #
7
+ # ==== Parameters
8
+ # file<SvnTransform::File>:: File at a given revision
9
+ # extensions<~each_pair>:: A Hash of old => new extension
10
+ #
11
+ # ==== Example
12
+ #
13
+ # SvnTransform::Transform::Extension.new(@file,
14
+ # {:txt => :markdown, :ruby => :rb}
15
+ # )
16
+ #
17
+ def initialize(file, extensions)
18
+ @file = file
19
+ @extensions = {}
20
+ (extensions || {}).each_pair do |existing, change_to|
21
+ @extensions[".#{existing}"] = ".#{change_to}"
22
+ end
23
+ end
24
+
25
+ # Check if this @file has one of the extensions (matches a key). If so,
26
+ # change its extension.
27
+ #
28
+ # ==== Returns
29
+ # True, False:: indicating whether a change was made.
30
+ def run
31
+ @extensions.each_pair do |existing, change_to|
32
+ if @file.path.extname == existing
33
+ @file.basename = @file.basename.gsub(/#{existing}\Z/, change_to)
34
+ return true
35
+ end
36
+ end
37
+ return false
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,50 @@
1
+ class SvnTransform
2
+ module Transform
3
+ # Transform all newlines to use a given sequence. For example, a repository
4
+ # some files using CRLF and some using LF could be transformed so that all
5
+ # files through all revisions use LF.
6
+ class Newline
7
+ LF = "\n"
8
+ CRLF = "\r\n"
9
+ CR = "\r"
10
+
11
+ # Initialize Newline Transform.
12
+ #
13
+ # ==== Parameters
14
+ # file<SvnTransform::File>:: File at a given revision
15
+ # newline<String>:: What to replace newlines with (defaults to \n)
16
+ def initialize(file, newline = LF)
17
+ @file = file
18
+ @newline = newline
19
+ end
20
+
21
+ # Run the transform. It first converts all newlines (LF, CRLF and CR) to
22
+ # LF, then replaces LF if needed.
23
+ #
24
+ # ==== Returns
25
+ # True, False:: indicating whether a change was made.
26
+ def run
27
+ body = @file.body.dup
28
+ # Replace CR and CRLF
29
+ body = all_to_lf(body)
30
+ # Replace LFs with newline if needed
31
+ body.gsub!(LF, @newline) unless LF == @newline
32
+
33
+ if body != @file.body
34
+ @file.body = body
35
+ return true
36
+ else
37
+ return false
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ # Ensure all newlines are represented by LF
44
+ def all_to_lf(body)
45
+ # Replace CRLF's, then lone CR's
46
+ return body.gsub(CRLF, LF).gsub(CR, LF)
47
+ end
48
+ end # Newline
49
+ end # Transform
50
+ end # SvnTransform
@@ -0,0 +1,28 @@
1
+ class SvnTransform
2
+ module Transform
3
+ # A Transform class that does nothing. Useful for classes to inherit from
4
+ # that don't need any special initialize logic, and as an ultra-simple
5
+ # example.
6
+ class Noop
7
+ # Initialize Noop Transform.
8
+ #
9
+ # ==== Parameters
10
+ # file<SvnTransform::File>:: File at a given revision
11
+ # args<Array>:: Splat of other args, all ignored
12
+ def initialize(file, *args)
13
+ @file = file
14
+ @args = args
15
+ end
16
+
17
+ # Run the transform. In this case it does nothing. In actual transforms,
18
+ # it would alter the @file (#body, #basename, and/or #properties) in
19
+ # some way, probably only if a condition met.
20
+ #
21
+ # ==== Returns
22
+ # False:: indicating that nothing was done.
23
+ def run
24
+ return false
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,134 @@
1
+ require 'yaml'
2
+
3
+ class SvnTransform
4
+ module Transform
5
+ # Move svn properties to YAML Front matter (or, for directories, to a YAML
6
+ # file). This is particularly intended to assist when converting a
7
+ # Subversion repository to another SCM that doesn't have arbitrary
8
+ # per-node properties (or whose conversion tools ignore them).
9
+ #
10
+ # See +new+ (+initialize+) for options
11
+ class PropsToYaml
12
+ # Default filename for yaml file holding directory properties
13
+ DEFAULT_YAML_FILE = 'meta.yml'
14
+
15
+ # Initialize with node, instructions and options
16
+ #
17
+ # ==== Parameters
18
+ # node<SvnTransform::File, SvnTransform::Directory>:
19
+ # The file or directory to be transformed
20
+ # instructions<Array of Array, Symbol>:
21
+ # - :all (default) will move all svn properties to YAML Front Matter
22
+ # (except svn:entry props)
23
+ # - An Array of Arrays. The first element of the inner Array is a Regex
24
+ # or String to be matched against. The second element is an action
25
+ # to take for those properties that match (using === operator).
26
+ # Actions can be:
27
+ # - :move : Just move the property to YAML
28
+ # - :delete : Remove from svn properties, don't add to YAML
29
+ # - String : gsub replacement string
30
+ #
31
+ # ==== Options
32
+ # :yaml_file<String>::
33
+ # Filename for directory YAML properties (defaults to DEFAULT_YAML_FILE)
34
+ def initialize(node, instructions = :all, options = {})
35
+ @node = node
36
+ @instructions = instructions
37
+ @instructions = [[/\A(?!svn:entry)/]] if @instructions == :all
38
+ @yaml_file = options[:yaml_file] || DEFAULT_YAML_FILE
39
+ @dirty = false
40
+ end
41
+
42
+ def run
43
+ if file?
44
+ body_less_yaml = has_yaml_props? ? yaml_split[1] : @node.body
45
+ end
46
+ @yaml_props = yaml_properties
47
+ @svn_props = @node.properties
48
+
49
+ @node.properties.each do |prop_key, prop_val|
50
+ process_property(prop_key, prop_val)
51
+ end
52
+
53
+ if @dirty
54
+ if file?
55
+ @node.body = @yaml_props.empty? ? body_less_yaml :
56
+ (@yaml_props.to_yaml + "---\n\n" + body_less_yaml)
57
+ else
58
+ @node.fixture_dir.file(@yaml_file).body(@yaml_props.to_yaml)
59
+ end
60
+ @node.properties = @svn_props
61
+ return true
62
+ else
63
+ return false
64
+ end
65
+ end
66
+
67
+ def process_property(prop_key, prop_val)
68
+ @instructions.each do |matcher, action|
69
+ action ||= :move
70
+ if matcher === prop_key
71
+ @svn_props.delete(prop_key)
72
+ @dirty = true
73
+ case action
74
+ when :delete
75
+ return true # Do nothing
76
+ when :move
77
+ new_key = prop_key
78
+ else # A string, hopefully
79
+ new_key = prop_key.gsub(matcher, action)
80
+ end
81
+ @yaml_props[new_key] = prop_val
82
+ return true
83
+ end
84
+ end
85
+ return false
86
+ end
87
+
88
+ private
89
+
90
+ # Is the node a File?
91
+ def file?
92
+ @node.kind_of?(SvnTransform::File)
93
+ end
94
+
95
+ # Is the node a Directory?
96
+ def directory?
97
+ !file?
98
+ end
99
+
100
+ # Get YAML properties. For a directory, these are stored in
101
+ # @options[:yaml_file], for a file, they are stored at the beginning of
102
+ # the file: the first line will bec and the YAML will end before a
103
+ # line containing "..." or "---".
104
+ def yaml_properties
105
+ if directory?
106
+ yaml_path = ::File.join(@node.path, @yaml_file)
107
+ @node.repos.stat(yaml_path, @node.rev_num) ?
108
+ YAML.load(@node.repos.file(yaml_path, @node.rev_num)[0]) :
109
+ {}
110
+ else
111
+ has_yaml_props? ?
112
+ YAML.load(yaml_split[0]) :
113
+ {}
114
+ end
115
+ end
116
+
117
+ # Determine if file has yaml properties, by checking if the file starts
118
+ # with three leading dashes
119
+ def has_yaml_props?
120
+ file? && @node.body[0..2] == "---"
121
+ end
122
+
123
+ # Split a file between properties and body at a line with three dots.
124
+ # Left trim the body and there may have been blank lines added for
125
+ # clarity.
126
+ def yaml_split
127
+ body = @node.body.gsub("\r", "")
128
+ ary = body.split(/\n\.\.\.\n|\n---\n/,2)
129
+ ary[1] = ary[1].lstrip
130
+ ary
131
+ end
132
+ end
133
+ end
134
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: svn-transform
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jared Morgan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-28 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: svn-fixture
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - "="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.2.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: spicycode-micronaut
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: |
36
+ Given a Subversion repository, svn-transform creates a new repo that is by
37
+ default identical, but allows changes to files, for example all or some of the
38
+ properties on each file can be moved from the properties to YAML prepended to
39
+ the body of the file. Primarily useful prior to conversions to other repository
40
+ types such as git.
41
+
42
+ email: jmorgan@morgancreative.net
43
+ executables: []
44
+
45
+ extensions: []
46
+
47
+ extra_rdoc_files:
48
+ - LICENSE
49
+ - README.rdoc
50
+ files:
51
+ - .document
52
+ - .gitignore
53
+ - LICENSE
54
+ - README.rdoc
55
+ - Rakefile
56
+ - VERSION
57
+ - examples/example_helper.rb
58
+ - examples/fixtures/dir_props.rb
59
+ - examples/fixtures/original.rb
60
+ - examples/fixtures/result.rb
61
+ - examples/svn-transform/dir_example.rb
62
+ - examples/svn-transform/file_example.rb
63
+ - examples/svn-transform/transform/extension_example.rb
64
+ - examples/svn-transform/transform/newline_example.rb
65
+ - examples/svn-transform/transform/noop_example.rb
66
+ - examples/svn-transform/transform/props_to_yaml_example.rb
67
+ - examples/svn-transform_example.rb
68
+ - lib/svn-transform.rb
69
+ - lib/svn-transform/dir.rb
70
+ - lib/svn-transform/file.rb
71
+ - lib/svn-transform/session.rb
72
+ - lib/svn-transform/transform/extension.rb
73
+ - lib/svn-transform/transform/newline.rb
74
+ - lib/svn-transform/transform/noop.rb
75
+ - lib/svn-transform/transform/props_to_yaml.rb
76
+ has_rdoc: true
77
+ homepage: http://github.com/jm81/svn-props-to-yaml
78
+ licenses: []
79
+
80
+ post_install_message:
81
+ rdoc_options:
82
+ - --charset=UTF-8
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ version:
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: "0"
96
+ version:
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.3.5
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Copy Subversion repository, with the ability to alter files
104
+ test_files:
105
+ - examples/example_helper.rb
106
+ - examples/fixtures/dir_props.rb
107
+ - examples/fixtures/original.rb
108
+ - examples/fixtures/result.rb
109
+ - examples/svn-transform/dir_example.rb
110
+ - examples/svn-transform/file_example.rb
111
+ - examples/svn-transform/transform/extension_example.rb
112
+ - examples/svn-transform/transform/newline_example.rb
113
+ - examples/svn-transform/transform/noop_example.rb
114
+ - examples/svn-transform/transform/props_to_yaml_example.rb
115
+ - examples/svn-transform_example.rb