Class: XCDM::Schema::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/xcdm/schema.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, inpath, outpath, xcode_version = nil) ⇒ Runner

Returns a new instance of Runner.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/xcdm/schema.rb', line 87

def initialize(name, inpath, outpath, xcode_version = nil)
  @inpath = inpath
  @name = name
  @container_path = File.join(outpath, "#{name}.xcdatamodeld")
  if !xcode_version
    begin
      `xcodebuild -version` =~ /(\d+\.\d+\.\d+)/
      xcode_version = $1
    rescue => e
      p e
      puts "XCode not installed?"
    end
  end
  @loader = Loader.new(xcode_version)
end

Instance Method Details

#datamodel_file(version) ⇒ Object



103
104
105
106
107
# File 'lib/xcdm/schema.rb', line 103

def datamodel_file(version)
  dir = File.join(@container_path, "#{version}.xcdatamodel")
  FileUtils.mkdir_p(dir)
  File.join(dir, 'contents')
end

#load_all(&block) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/xcdm/schema.rb', line 109

def load_all(&block)
  Dir["#{@inpath}/*.rb"].each do |file|
    if File.file?(file)
      schema = @loader.load_file(file)
      block.call(schema, file) if block_given?
    end
  end
end

#write_all(&block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/xcdm/schema.rb', line 118

def write_all(&block)
  @loader.schemas.each do |schema|
    filename = datamodel_file(schema.version)
    block.call(schema, filename) if block_given?
    File.open(filename, "w+") do |f|
      f.write(schema.to_xml)
    end
  end

  max = @loader.schemas.map(&:version).max
  File.open(File.join(@container_path, ".xccurrentversion"), "w+") do |f|
    f.write({ "_XCCurrentVersionName" => "#{max}.xcdatamodel" }.to_plist)
  end

end