Class: SvnTransform::Transform::PropsToYaml

Inherits:
Object
  • Object
show all
Defined in:
lib/svn-transform/transform/props_to_yaml.rb

Overview

Move svn properties to YAML Front matter (or, for directories, to a YAML file). This is particularly intended to assist when converting a Subversion repository to another SCM that doesn’t have arbitrary per-node properties (or whose conversion tools ignore them).

See new (initialize) for options

Constant Summary collapse

DEFAULT_YAML_FILE =

Default filename for yaml file holding directory properties

'meta.yml'

Instance Method Summary collapse

Constructor Details

#initialize(node, instructions = :all, options = {}) ⇒ PropsToYaml

Initialize with node, instructions and options

Parameters

node<SvnTransform::File, SvnTransform::Directory>:

The file or directory to be transformed

instructions<Array of Array, Symbol>:

- :all (default) will move all svn properties to YAML Front Matter
  (except svn:entry props)
- An Array of Arrays. The first element of the inner Array is a Regex
  or String to be matched against. The second element is an action
  to take for those properties that match (using === operator).
  Actions can be:
  - :move : Just move the property to YAML
  - :delete : Remove from svn properties, don't add to YAML
  - String : gsub replacement string

Options

:yaml_file<String>

Filename for directory YAML properties (defaults to DEFAULT_YAML_FILE)



34
35
36
37
38
39
40
# File 'lib/svn-transform/transform/props_to_yaml.rb', line 34

def initialize(node, instructions = :all, options = {})
  @node = node
  @instructions = instructions
  @instructions = [[/\A(?!svn:entry)/]] if @instructions == :all
  @yaml_file = options[:yaml_file] || DEFAULT_YAML_FILE
  @dirty = false
end

Instance Method Details

#process_property(prop_key, prop_val) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/svn-transform/transform/props_to_yaml.rb', line 67

def process_property(prop_key, prop_val)
  @instructions.each do |matcher, action|
    action ||= :move
    if matcher === prop_key
      @svn_props.delete(prop_key)
      @dirty = true
      case action
      when :delete
        return true # Do nothing
      when :move
        new_key = prop_key
      else # A string, hopefully
        new_key = prop_key.gsub(matcher, action)
      end
      @yaml_props[new_key] = prop_val
      return true
    end
  end
  return false
end

#runObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/svn-transform/transform/props_to_yaml.rb', line 42

def run
  if file?
    body_less_yaml = has_yaml_props? ? yaml_split[1] : @node.body
  end
  @yaml_props = yaml_properties
  @svn_props = @node.properties
  
  @node.properties.each do |prop_key, prop_val|
    process_property(prop_key, prop_val)
  end
  
  if @dirty
    if file?
      @node.body = @yaml_props.empty? ? body_less_yaml :
        (@yaml_props.to_yaml + "---\n\n" + body_less_yaml)
    else
      @node.fixture_dir.file(@yaml_file).body(@yaml_props.to_yaml)
    end
    @node.properties = @svn_props
    return true
  else
    return false
  end
end